aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt50
1 files changed, 29 insertions, 21 deletions
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<TopAnime>? = 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<List<TopAnime>>() {}.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<TopAnime> ){
+ fun showList(view: View, animeList: List<TopAnime>) {
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<RestTopAnimeResponse> {
- override fun onResponse(call: Call<RestTopAnimeResponse>, response: Response<RestTopAnimeResponse>) {
- if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty
-
- val animeList: List<TopAnime> = response.body()!!.getResults() //getting the "top" field containing our list of TopAnimes
+ override fun onResponse(
+ call: Call<RestTopAnimeResponse>,
+ response: Response<RestTopAnimeResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val animeList: List<TopAnime> = 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