From f9f079eea806454d49d6088089511d72e1c28f86 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Tue, 2 Mar 2021 09:13:54 +0100 Subject: Cleaning up the code. --- .../adjutor/aniki/anime/topanime/TopAnimePage.kt | 50 +++++++++++++--------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt') diff --git a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt index efa45dd..ccae2fa 100644 --- a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt @@ -27,13 +27,13 @@ class TopAnimePage : Fragment() { var sharedPreferences: SharedPreferences? = null val gson = GsonBuilder() - .setLenient() - .create() + .setLenient() + .create() var base_url = "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_anime_page, container, false) @@ -41,7 +41,7 @@ class TopAnimePage : Fragment() { sharedPreferences = view.context.getSharedPreferences("sp_anime", Context.MODE_PRIVATE) val animeList: List? = getDataFromCache() - if(animeList != null ){ + if (animeList != null) { showList(view, animeList) } else { makeApiCall(view, base_url) @@ -55,7 +55,7 @@ class TopAnimePage : Fragment() { val jsonAnime: String? = sharedPreferences?.getString("jsonAnimeList", null) //if it's null, well, return null - if(jsonAnime == null) { + if (jsonAnime == null) { return null } else { //else deserialize the list and return it val listType: Type = object : TypeToken>() {}.type @@ -72,12 +72,13 @@ class TopAnimePage : Fragment() { } fun updateList() { - makeApiCall(view,base_url) + makeApiCall(view, base_url) 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 +86,7 @@ class TopAnimePage : Fragment() { } //display the recyclerview - fun showList(view: View, animeList: List ){ + fun showList(view: View, animeList: List) { val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) recyclerView.setHasFixedSize(true) recyclerView.layoutManager = LinearLayoutManager(view.context) @@ -95,20 +96,27 @@ class TopAnimePage : 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(TopAnimeApi::class.java) val call = service.getTopAnimeData() call.enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty - - val animeList: List = response.body()!!.getResults() //getting the "top" field containing our list of TopAnimes + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty + + val animeList: List = response.body()!! + .getResults() //getting the "top" field containing our list of TopAnimes saveList(animeList) - showList(view, animeList) //calling the method in charge of displaying on the recyclerview + showList( + view, + animeList + ) //calling the method in charge of displaying on the recyclerview } else { showError() //a snackbar @@ -126,14 +134,14 @@ class TopAnimePage : Fragment() { val jsonString: String = gson.toJson(animeList) sharedPreferences - ?.edit() - ?.putString("jsonAnimeList", jsonString) - ?.apply() + ?.edit() + ?.putString("jsonAnimeList", 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 -- cgit v1.2.3