summaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/presentation/controller
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2021-05-13 13:42:16 +0200
committerClyhtsuriva <aimeric@adjutor.xyz>2021-05-13 13:42:16 +0200
commita4fbe1308bb4cf61358d8169dd32505e2783a9af (patch)
tree4f8f67ba83a64dd33c310f1eee659dfdd5d32b85 /app/src/main/java/xyz/adjutor/aniki/presentation/controller
parenta11219745fd6ddf8796682f7ab433c1a1d0402f5 (diff)
parentbe1df4b40a41ae1d4c727637a326ad32eb729ed9 (diff)
Merge branch 'release/3.0'v3.0.0
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/presentation/controller')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt63
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailTopAnimeController.kt97
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt73
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt131
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt63
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailTopMangaController.kt97
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt73
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt131
8 files changed, 728 insertions, 0 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt
new file mode 100644
index 0000000..3114d01
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt
@@ -0,0 +1,63 @@
+package xyz.adjutor.aniki.presentation.controller.anime
+
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.anime.AnimeApi
+import xyz.adjutor.aniki.presentation.model.anime.AnimeResponse
+import xyz.adjutor.aniki.presentation.view.anime.DetailSearchAnimeActivity
+
+class DetailSearchAnimeController {
+
+ lateinit var gson: Gson
+ private lateinit var baseUrl: String //the api's base url
+ lateinit var view: DetailSearchAnimeActivity
+
+ fun onStart(DetailSearchAnimeActivity: DetailSearchAnimeActivity, animeId: String) {
+
+ view = DetailSearchAnimeActivity
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+
+ makeApiCall(baseUrl, animeId)
+ }
+
+ private fun makeApiCall(BASE_URL: String, animeId: String) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(AnimeApi::class.java)
+ val call = service.getAnimeData(animeId) //based on the id
+
+ call.enqueue(object : Callback<AnimeResponse> {
+ override fun onResponse(
+ call: Call<AnimeResponse>,
+ response: Response<AnimeResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val anime = response.body() //getting the AnimeResponse fields
+ view.showDetail(anime!!)
+
+ } else {
+ view.showError("API ERROR : is not successful")
+ }
+ }
+
+ override fun onFailure(call: Call<AnimeResponse>, t: Throwable) {
+ view.showError("API ERROR : onFailure")
+ }
+
+ })
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailTopAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailTopAnimeController.kt
new file mode 100644
index 0000000..6f3cedb
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailTopAnimeController.kt
@@ -0,0 +1,97 @@
+package xyz.adjutor.aniki.presentation.controller.anime
+
+import android.content.Context
+import android.content.SharedPreferences
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import com.google.gson.reflect.TypeToken
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.anime.AnimeApi
+import xyz.adjutor.aniki.presentation.model.anime.AnimeResponse
+import xyz.adjutor.aniki.presentation.view.anime.DetailTopAnimeActivity
+import java.lang.reflect.Type
+
+class DetailTopAnimeController {
+
+ private lateinit var sharedPreferences: SharedPreferences
+ lateinit var gson: Gson
+ private lateinit var baseUrl: String //the api's base url
+ lateinit var view: DetailTopAnimeActivity
+
+ fun onStart(DetailTopAnimeActivity: DetailTopAnimeActivity, animeId: String) {
+
+ view = DetailTopAnimeActivity
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+ sharedPreferences =
+ view.applicationContext.getSharedPreferences("sp_anime", Context.MODE_PRIVATE)
+
+ val anime: AnimeResponse? = getDataFromCache(animeId)
+ if (anime != null) {
+ view.showDetail(anime)
+ } else {
+ //taking the API's fields I want and displaying them
+ makeApiCall(baseUrl, animeId)
+ }
+ }
+
+ private fun getDataFromCache(animeId: String): AnimeResponse? {
+ val jsonAnime: String? = sharedPreferences.getString(animeId, null)
+
+ return if (jsonAnime == null) {
+ null
+ } else {
+ val type: Type = object : TypeToken<AnimeResponse>() {}.type
+ gson.fromJson(jsonAnime, type)
+ }
+ }
+
+ private fun makeApiCall(BASE_URL: String, animeId: String) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(AnimeApi::class.java)
+ val call = service.getAnimeData(animeId) //based on the id
+
+ call.enqueue(object : Callback<AnimeResponse> {
+ override fun onResponse(
+ call: Call<AnimeResponse>,
+ response: Response<AnimeResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val anime = response.body() //getting the AnimeResponse fields
+ saveList(anime)
+ view.showDetail(anime!!)
+
+ } else {
+ view.showError("API ERROR : is not successful")
+ }
+ }
+
+ override fun onFailure(call: Call<AnimeResponse>, t: Throwable) {
+ view.showError("API ERROR : onFailure")
+ }
+
+ })
+ }
+
+ fun saveList(anime: AnimeResponse?) {
+ val jsonString: String = gson.toJson(anime)
+
+ sharedPreferences
+ .edit()
+ .putString(anime?.mal_id.toString(), jsonString)
+ .apply()
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt
new file mode 100644
index 0000000..aa8487e
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt
@@ -0,0 +1,73 @@
+package xyz.adjutor.aniki.presentation.controller.anime
+
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.anime.SearchAnimeApi
+import xyz.adjutor.aniki.presentation.model.anime.SearchAnime
+import xyz.adjutor.aniki.presentation.model.anime.SearchAnimeResponse
+import xyz.adjutor.aniki.presentation.view.anime.SearchAnimePage
+
+class SearchAnimeController {
+
+ lateinit var gson: Gson
+ lateinit var baseUrl: String //the api's base url
+ lateinit var view: SearchAnimePage
+
+ fun onStart(searchAnimePage: SearchAnimePage) {
+
+ view = searchAnimePage
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+ }
+
+ //call the API and show the list
+ private fun makeApiCall(view: SearchAnimePage, BASE_URL: String, query: String) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(SearchAnimeApi::class.java)
+ val call =
+ service.getSearchAnimeData(q = query) //fate is an exemple, we'll have to replace it by the user input.
+
+ call.enqueue(object : Callback<SearchAnimeResponse> {
+ override fun onResponse(
+ call: Call<SearchAnimeResponse>,
+ response: Response<SearchAnimeResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val animeList: List<SearchAnime> = response.body()!!
+ .getResults() //getting the "search" field containing our list of SearchAnimes
+
+ view.showList(
+ view.requireView(),
+ animeList
+ ) //calling the method in charge of displaying on the recyclerview
+
+ } else {
+ view.showError() //a snackbar
+ }
+ }
+
+ override fun onFailure(call: Call<SearchAnimeResponse>, t: Throwable) {
+ view.showError()
+ }
+
+ })
+ }
+
+ fun updateList(userInput: String) {
+ makeApiCall(view, baseUrl, userInput)
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt
new file mode 100644
index 0000000..7105af5
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt
@@ -0,0 +1,131 @@
+package xyz.adjutor.aniki.presentation.controller.anime
+
+import android.content.Context
+import android.content.SharedPreferences
+import android.view.View
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import com.google.gson.reflect.TypeToken
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.anime.TopAnimeApi
+import xyz.adjutor.aniki.presentation.model.anime.TopAnime
+import xyz.adjutor.aniki.presentation.model.anime.TopAnimeResponse
+import xyz.adjutor.aniki.presentation.view.anime.TopAnimePage
+import java.lang.reflect.Type
+import kotlin.properties.Delegates
+
+class TopAnimeController {
+
+ lateinit var sharedPreferences: SharedPreferences
+ lateinit var gson: Gson
+ lateinit var baseUrl: String //the api's base url
+ var page by Delegates.notNull<Int>()
+ lateinit var view: TopAnimePage
+
+ fun onStart(topAnimePage: TopAnimePage, viewTopAnimePage: View) {
+
+ view = topAnimePage
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ page = 1
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+ sharedPreferences =
+ viewTopAnimePage.context.getSharedPreferences("sp_anime", Context.MODE_PRIVATE)
+
+
+ val animeList: List<TopAnime>? = getDataFromCache()
+ if (animeList != null) {
+ view.showList(viewTopAnimePage, animeList)
+ } else {
+ makeApiCall(view, baseUrl, 1)
+ }
+ }
+
+ fun makeApiCall(view: TopAnimePage, BASE_URL: String, page: Int) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(TopAnimeApi::class.java)
+ val call = service.getTopAnimeData(page)
+
+ call.enqueue(object : Callback<TopAnimeResponse> {
+ override fun onResponse(
+ call: Call<TopAnimeResponse>,
+ response: Response<TopAnimeResponse>
+ ) {
+ 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)
+ view.showList(
+ view.requireView(),
+ animeList
+ ) //calling the method in charge of displaying on the recyclerview
+
+ } else {
+ view.showError() //a snackbar
+ }
+ }
+
+ override fun onFailure(call: Call<TopAnimeResponse>, t: Throwable) {
+ view.showError()
+ }
+
+ })
+ }
+
+ private fun saveList(animeList: List<TopAnime>) {
+ val jsonString: String = gson.toJson(animeList)
+
+ sharedPreferences
+ .edit()
+ .putString("jsonAnimeList", jsonString)
+ .apply()
+ }
+
+ private fun getDataFromCache(): List<TopAnime>? {
+ //the value of the animeList json, if nothing is found, return null
+ val jsonAnime: String? = sharedPreferences.getString("jsonAnimeList", null)
+
+ //if it's null, well, return null
+ return if (jsonAnime == null) {
+ null
+ } else { //else deserialize the list and return it
+ val listType: Type = object : TypeToken<List<TopAnime>>() {}.type
+ gson.fromJson(jsonAnime, listType)
+ }
+ }
+
+
+ fun onButtonPrevClick() {
+ if (page > 1) { // if we're not on the first page, because we can't go back if we're on the first one.
+ page -= 1
+ makeApiCall(view, baseUrl, page)
+ view.showText("Page $page has been loaded.")
+ } else {
+ view.showText("You're already on page 1. (If you're actually not, refresh the list.)")
+ }
+ }
+
+ fun onButtonNextClick() {
+ page += 1
+ makeApiCall(view, baseUrl, page)
+ view.showText("Page $page has been loaded.")
+ }
+
+ fun updateList() {
+ makeApiCall(view, baseUrl, 1)
+ view.showText("Data refreshed")
+ page = 1
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt
new file mode 100644
index 0000000..a874976
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt
@@ -0,0 +1,63 @@
+package xyz.adjutor.aniki.presentation.controller.manga
+
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.manga.MangaApi
+import xyz.adjutor.aniki.presentation.model.manga.MangaResponse
+import xyz.adjutor.aniki.presentation.view.manga.DetailSearchMangaActivity
+
+class DetailSearchMangaController {
+
+ lateinit var gson: Gson
+ private lateinit var baseUrl: String //the api's base url
+ lateinit var view: DetailSearchMangaActivity
+
+ fun onStart(DetailSearchMangaActivity: DetailSearchMangaActivity, mangaId: String) {
+
+ view = DetailSearchMangaActivity
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+
+ makeApiCall(baseUrl, mangaId)
+ }
+
+ private fun makeApiCall(BASE_URL: String, mangaId: String) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(MangaApi::class.java)
+ val call = service.getMangaData(mangaId) //based on the id
+
+ call.enqueue(object : Callback<MangaResponse> {
+ override fun onResponse(
+ call: Call<MangaResponse>,
+ response: Response<MangaResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val manga = response.body() //getting the MangaResponse fields
+ view.showDetail(manga!!)
+
+ } else {
+ view.showError("API ERROR : is not successful")
+ }
+ }
+
+ override fun onFailure(call: Call<MangaResponse>, t: Throwable) {
+ view.showError("API ERROR : onFailure")
+ }
+
+ })
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailTopMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailTopMangaController.kt
new file mode 100644
index 0000000..807421c
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailTopMangaController.kt
@@ -0,0 +1,97 @@
+package xyz.adjutor.aniki.presentation.controller.manga
+
+import android.content.Context
+import android.content.SharedPreferences
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import com.google.gson.reflect.TypeToken
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.manga.MangaApi
+import xyz.adjutor.aniki.presentation.model.manga.MangaResponse
+import xyz.adjutor.aniki.presentation.view.manga.DetailTopMangaActivity
+import java.lang.reflect.Type
+
+class DetailTopMangaController {
+
+ private lateinit var sharedPreferences: SharedPreferences
+ lateinit var gson: Gson
+ private lateinit var baseUrl: String //the api's base url
+ lateinit var view: DetailTopMangaActivity
+
+ fun onStart(DetailTopMangaActivity: DetailTopMangaActivity, mangaId: String) {
+
+ view = DetailTopMangaActivity
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+ sharedPreferences =
+ view.applicationContext.getSharedPreferences("sp_manga", Context.MODE_PRIVATE)
+
+ val manga: MangaResponse? = getDataFromCache(mangaId)
+ if (manga != null) {
+ view.showDetail(manga)
+ } else {
+ //taking the API's fields I want and displaying them
+ makeApiCall(baseUrl, mangaId)
+ }
+ }
+
+ private fun getDataFromCache(mangaId: String): MangaResponse? {
+ val jsonManga: String? = sharedPreferences.getString(mangaId, null)
+
+ return if (jsonManga == null) {
+ null
+ } else {
+ val type: Type = object : TypeToken<MangaResponse>() {}.type
+ gson.fromJson(jsonManga, type)
+ }
+ }
+
+ private fun makeApiCall(BASE_URL: String, mangaId: String) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(MangaApi::class.java)
+ val call = service.getMangaData(mangaId) //based on the id
+
+ call.enqueue(object : Callback<MangaResponse> {
+ override fun onResponse(
+ call: Call<MangaResponse>,
+ response: Response<MangaResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val manga = response.body() //getting the MangaResponse fields
+ saveList(manga)
+ view.showDetail(manga!!)
+
+ } else {
+ view.showError("API ERROR : is not successful")
+ }
+ }
+
+ override fun onFailure(call: Call<MangaResponse>, t: Throwable) {
+ view.showError("API ERROR : onFailure")
+ }
+
+ })
+ }
+
+ fun saveList(manga: MangaResponse?) {
+ val jsonString: String = gson.toJson(manga)
+
+ sharedPreferences
+ .edit()
+ .putString(manga?.mal_id.toString(), jsonString)
+ .apply()
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt
new file mode 100644
index 0000000..32e46eb
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt
@@ -0,0 +1,73 @@
+package xyz.adjutor.aniki.presentation.controller.manga
+
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.manga.SearchMangaApi
+import xyz.adjutor.aniki.presentation.model.manga.SearchManga
+import xyz.adjutor.aniki.presentation.model.manga.SearchMangaResponse
+import xyz.adjutor.aniki.presentation.view.manga.SearchMangaPage
+
+class SearchMangaController {
+
+ lateinit var gson: Gson
+ lateinit var baseUrl: String //the api's base url
+ lateinit var view: SearchMangaPage
+
+ fun onStart(searchMangaPage: SearchMangaPage) {
+
+ view = searchMangaPage
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+ }
+
+ //call the API and show the list
+ private fun makeApiCall(view: SearchMangaPage, BASE_URL: String, query: String) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(SearchMangaApi::class.java)
+ val call =
+ service.getSearchMangaData(q = query) //fate is an exemple, we'll have to replace it by the user input.
+
+ call.enqueue(object : Callback<SearchMangaResponse> {
+ override fun onResponse(
+ call: Call<SearchMangaResponse>,
+ response: Response<SearchMangaResponse>
+ ) {
+ if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty
+
+ val mangaList: List<SearchManga> = response.body()!!
+ .getResults() //getting the "search" field containing our list of SearchMangas
+
+ view.showList(
+ view.requireView(),
+ mangaList
+ ) //calling the method in charge of displaying on the recyclerview
+
+ } else {
+ view.showError() //a snackbar
+ }
+ }
+
+ override fun onFailure(call: Call<SearchMangaResponse>, t: Throwable) {
+ view.showError()
+ }
+
+ })
+ }
+
+ fun updateList(userInput: String) {
+ makeApiCall(view, baseUrl, userInput)
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt
new file mode 100644
index 0000000..845e750
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt
@@ -0,0 +1,131 @@
+package xyz.adjutor.aniki.presentation.controller.manga
+
+import android.content.Context
+import android.content.SharedPreferences
+import android.view.View
+import com.google.gson.Gson
+import com.google.gson.GsonBuilder
+import com.google.gson.reflect.TypeToken
+import retrofit2.Call
+import retrofit2.Callback
+import retrofit2.Response
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import xyz.adjutor.aniki.data.manga.TopMangaApi
+import xyz.adjutor.aniki.presentation.model.manga.TopManga
+import xyz.adjutor.aniki.presentation.model.manga.TopMangaResponse
+import xyz.adjutor.aniki.presentation.view.manga.TopMangaPage
+import java.lang.reflect.Type
+import kotlin.properties.Delegates
+
+class TopMangaController {
+
+ lateinit var sharedPreferences: SharedPreferences
+ lateinit var gson: Gson
+ lateinit var baseUrl: String //the api's base url
+ var page by Delegates.notNull<Int>()
+ lateinit var view: TopMangaPage
+
+ fun onStart(topMangaPage: TopMangaPage, viewTopMangaPage: View) {
+
+ view = topMangaPage
+ baseUrl = "https://api.jikan.moe/" //the api's base url
+ page = 1
+ gson = GsonBuilder()
+ .setLenient()
+ .create()
+ sharedPreferences =
+ viewTopMangaPage.context.getSharedPreferences("sp_manga", Context.MODE_PRIVATE)
+
+
+ val mangaList: List<TopManga>? = getDataFromCache()
+ if (mangaList != null) {
+ view.showList(viewTopMangaPage, mangaList)
+ } else {
+ makeApiCall(view, baseUrl, 1)
+ }
+ }
+
+ private fun makeApiCall(view: TopMangaPage, BASE_URL: String, page: Int) {
+
+ val retrofit = Retrofit.Builder()
+ .baseUrl(BASE_URL)
+ .addConverterFactory(GsonConverterFactory.create(gson))
+ .build()
+
+ val service = retrofit.create(TopMangaApi::class.java)
+ val call = service.getTopMangaData(page)
+
+ call.enqueue(object : Callback<TopMangaResponse> {
+ override fun onResponse(
+ call: Call<TopMangaResponse>,
+ response: Response<TopMangaResponse>
+ ) {
+ 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)
+ view.showList(
+ view.requireView(),
+ mangaList
+ ) //calling the method in charge of displaying on the recyclerview
+
+ } else {
+ view.showError() //a snackbar
+ }
+ }
+
+ override fun onFailure(call: Call<TopMangaResponse>, t: Throwable) {
+ view.showError()
+ }
+
+ })
+ }
+
+ private fun saveList(mangaList: List<TopManga>) {
+ val jsonString: String = gson.toJson(mangaList)
+
+ sharedPreferences
+ .edit()
+ .putString("jsonMangaList", jsonString)
+ .apply()
+ }
+
+ private fun getDataFromCache(): List<TopManga>? {
+ //the value of the mangaList json, if nothing is found, return null
+ val jsonManga: String? = sharedPreferences.getString("jsonMangaList", null)
+
+ //if it's null, well, return null
+ return if (jsonManga == null) {
+ null
+ } else { //else deserialize the list and return it
+ val listType: Type = object : TypeToken<List<TopManga>>() {}.type
+ gson.fromJson(jsonManga, listType)
+ }
+ }
+
+
+ fun onButtonPrevClick() {
+ if (page > 1) { // if we're not on the first page, because we can't go back if we're on the first one.
+ page -= 1
+ makeApiCall(view, baseUrl, page)
+ view.showText("Page $page has been loaded.")
+ } else {
+ view.showText("You're already on page 1. (If you're actually not, refresh the list.)")
+ }
+ }
+
+ fun onButtonNextClick() {
+ page += 1
+ makeApiCall(view, baseUrl, page)
+ view.showText("Page $page has been loaded.")
+ }
+
+ fun updateList() {
+ makeApiCall(view, baseUrl, 1)
+ view.showText("Data refreshed")
+ page = 1
+ }
+
+} \ No newline at end of file