aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt61
1 files changed, 35 insertions, 26 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt
index d28161a..ce9b1c6 100644
--- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt
+++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt
@@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.snackbar.Snackbar
+import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import retrofit2.Call
@@ -26,14 +27,14 @@ import java.lang.reflect.Type
class TopMangaPage : Fragment() {
var sharedPreferences: SharedPreferences? = null
- val gson = GsonBuilder()
- .setLenient()
- .create()
- var base_url = "https://api.jikan.moe/" //the api's base url
+ val gson: Gson = GsonBuilder()
+ .setLenient()
+ .create()
+ private var baseUrl = "https://api.jikan.moe/" //the api's base url
override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?
+ inflater: LayoutInflater, container: ViewGroup?,
+ savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.top_manga_page, container, false)
@@ -41,10 +42,10 @@ class TopMangaPage : Fragment() {
sharedPreferences = view.context.getSharedPreferences("sp_manga", Context.MODE_PRIVATE)
val mangaList: List<TopManga>? = getDataFromCache()
- if(mangaList != null ){
+ if (mangaList != null) {
showList(view, mangaList)
} else {
- makeApiCall(view, base_url)
+ makeApiCall(view, baseUrl)
}
return view
@@ -55,11 +56,11 @@ class TopMangaPage : Fragment() {
val jsonManga: String? = sharedPreferences?.getString("jsonMangaList", null)
//if it's null, well, return null
- if(jsonManga == null) {
- return null
+ return if (jsonManga == null) {
+ null
} else { //else deserialize the list and return it
val listType: Type = object : TypeToken<List<TopManga>>() {}.type
- return gson.fromJson(jsonManga, listType)
+ gson.fromJson(jsonManga, listType)
}
}
@@ -72,12 +73,13 @@ class TopMangaPage : Fragment() {
}
fun updateList() {
- makeApiCall(view,base_url)
+ makeApiCall(view, baseUrl)
Snackbar.make(requireView(), "Data refreshed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
+
val swipeRefresh: SwipeRefreshLayout = view.findViewById(R.id.swiperefresh)
- swipeRefresh.setOnRefreshListener{
+ swipeRefresh.setOnRefreshListener {
updateList()
swipeRefresh.isRefreshing = false
}
@@ -85,7 +87,7 @@ class TopMangaPage : Fragment() {
}
//display the recyclerview
- fun showList(view: View, mangaList: List<TopManga> ){
+ fun showList(view: View, mangaList: List<TopManga>) {
val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(view.context)
@@ -95,20 +97,27 @@ class TopMangaPage : Fragment() {
private fun makeApiCall(view: View, BASE_URL: String) {
val retrofit = Retrofit.Builder()
- .baseUrl(BASE_URL)
- .addConverterFactory(GsonConverterFactory.create(gson))
- .build()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
val service = retrofit.create(TopMangaApi::class.java)
val call = service.getTopMangaData()
call.enqueue(object : Callback<RestTopMangaResponse> {
- override fun onResponse(call: Call<RestTopMangaResponse>, response: Response<RestTopMangaResponse>) {
- if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty
-
- val mangaList: List<TopManga> = response.body()!!.getResults() //getting the "top" field containing our list of TopMangas
+ override fun onResponse(
+ call: Call<RestTopMangaResponse>,
+ response: Response<RestTopMangaResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val mangaList: List<TopManga> = response.body()!!
+ .getResults() //getting the "top" field containing our list of TopMangas
saveList(mangaList)
- showList(view, mangaList) //calling the method in charge of displaying on the recyclerview
+ showList(
+ view,
+ mangaList
+ ) //calling the method in charge of displaying on the recyclerview
} else {
showError() //a snackbar
@@ -126,14 +135,14 @@ class TopMangaPage : Fragment() {
val jsonString: String = gson.toJson(mangaList)
sharedPreferences
- ?.edit()
- ?.putString("jsonMangaList", jsonString)
- ?.apply()
+ ?.edit()
+ ?.putString("jsonMangaList", jsonString)
+ ?.apply()
}
private fun showError() {
Snackbar.make(requireView(), "API ERROR", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show()
+ .setAction("Action", null).show()
}
} \ No newline at end of file