From d860cdd577879910792380cf0f227b206159a6f1 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Wed, 12 May 2021 14:49:55 +0200 Subject: creating anime and manga packages for controllers --- .../controller/SearchAnimeController.kt | 73 ------------ .../controller/SearchMangaController.kt | 73 ------------ .../presentation/controller/TopAnimeController.kt | 131 --------------------- .../presentation/controller/TopMangaController.kt | 131 --------------------- .../controller/anime/SearchAnimeController.kt | 73 ++++++++++++ .../controller/anime/TopAnimeController.kt | 131 +++++++++++++++++++++ .../controller/manga/SearchMangaController.kt | 73 ++++++++++++ .../controller/manga/TopMangaController.kt | 131 +++++++++++++++++++++ .../presentation/view/anime/SearchAnimePage.kt | 2 +- .../aniki/presentation/view/anime/TopAnimePage.kt | 2 +- .../presentation/view/manga/SearchMangaPage.kt | 2 +- .../aniki/presentation/view/manga/TopMangaPage.kt | 2 +- 12 files changed, 412 insertions(+), 412 deletions(-) delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchMangaController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopAnimeController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopMangaController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt (limited to 'app') diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt deleted file mode 100644 index d00137d..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt +++ /dev/null @@ -1,73 +0,0 @@ -package xyz.adjutor.aniki.presentation.controller - -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 { - 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 "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, 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/SearchMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchMangaController.kt deleted file mode 100644 index 572c57e..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchMangaController.kt +++ /dev/null @@ -1,73 +0,0 @@ -package xyz.adjutor.aniki.presentation.controller - -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 { - 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 mangaList: List = 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, 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/TopAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopAnimeController.kt deleted file mode 100644 index 36026ec..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopAnimeController.kt +++ /dev/null @@ -1,131 +0,0 @@ -package xyz.adjutor.aniki.presentation.controller - -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() - 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? = 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 { - 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) - 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, t: Throwable) { - view.showError() - } - - }) - } - - private fun saveList(animeList: List) { - val jsonString: String = gson.toJson(animeList) - - sharedPreferences - .edit() - .putString("jsonAnimeList", jsonString) - .apply() - } - - private fun getDataFromCache(): List? { - //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>() {}.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/TopMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopMangaController.kt deleted file mode 100644 index 3fa1c2c..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopMangaController.kt +++ /dev/null @@ -1,131 +0,0 @@ -package xyz.adjutor.aniki.presentation.controller - -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() - 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? = 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 { - 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 mangaList: List = 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, t: Throwable) { - view.showError() - } - - }) - } - - private fun saveList(mangaList: List) { - val jsonString: String = gson.toJson(mangaList) - - sharedPreferences - .edit() - .putString("jsonMangaList", jsonString) - .apply() - } - - private fun getDataFromCache(): List? { - //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>() {}.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 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 { + 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 "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, 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() + 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? = 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 { + 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) + 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, t: Throwable) { + view.showError() + } + + }) + } + + private fun saveList(animeList: List) { + val jsonString: String = gson.toJson(animeList) + + sharedPreferences + .edit() + .putString("jsonAnimeList", jsonString) + .apply() + } + + private fun getDataFromCache(): List? { + //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>() {}.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/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 { + 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 mangaList: List = 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, 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() + 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? = 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 { + 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 mangaList: List = 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, t: Throwable) { + view.showError() + } + + }) + } + + private fun saveList(mangaList: List) { + val jsonString: String = gson.toJson(mangaList) + + sharedPreferences + .edit() + .putString("jsonMangaList", jsonString) + .apply() + } + + private fun getDataFromCache(): List? { + //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>() {}.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 diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/SearchAnimePage.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/SearchAnimePage.kt index 4d01f4f..764601f 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/SearchAnimePage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/SearchAnimePage.kt @@ -16,7 +16,7 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.snackbar.Snackbar import com.google.android.material.textfield.TextInputEditText import xyz.adjutor.aniki.R -import xyz.adjutor.aniki.presentation.controller.SearchAnimeController +import xyz.adjutor.aniki.presentation.controller.anime.SearchAnimeController import xyz.adjutor.aniki.presentation.model.anime.SearchAnime import xyz.adjutor.aniki.presentation.view.MainActivity diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/TopAnimePage.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/TopAnimePage.kt index 40df41c..5453154 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/TopAnimePage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/TopAnimePage.kt @@ -12,7 +12,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.google.android.material.snackbar.Snackbar import xyz.adjutor.aniki.R -import xyz.adjutor.aniki.presentation.controller.TopAnimeController +import xyz.adjutor.aniki.presentation.controller.anime.TopAnimeController import xyz.adjutor.aniki.presentation.model.anime.TopAnime //view diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/SearchMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/SearchMangaPage.kt index 853ee22..b6ae2bb 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/SearchMangaPage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/SearchMangaPage.kt @@ -16,7 +16,7 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.snackbar.Snackbar import com.google.android.material.textfield.TextInputEditText import xyz.adjutor.aniki.R -import xyz.adjutor.aniki.presentation.controller.SearchMangaController +import xyz.adjutor.aniki.presentation.controller.manga.SearchMangaController import xyz.adjutor.aniki.presentation.model.manga.SearchManga import xyz.adjutor.aniki.presentation.view.MainActivity diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt index 32fa1b1..98eb0dd 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt @@ -12,7 +12,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.google.android.material.snackbar.Snackbar import xyz.adjutor.aniki.R -import xyz.adjutor.aniki.presentation.controller.TopMangaController +import xyz.adjutor.aniki.presentation.controller.manga.TopMangaController import xyz.adjutor.aniki.presentation.model.manga.TopManga //view -- cgit v1.2.3