From 8ede9457fd21ac5e272e73f5ece1d4bc9c23faab Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Wed, 19 May 2021 10:07:23 +0200 Subject: Big update Restructuring the packages and adding a Constants.kt for the api URL. Also adding gson in Singletons.kt --- .../controller/DetailSearchAnimeController.kt | 48 ++++++++ .../controller/DetailSearchMangaController.kt | 48 ++++++++ .../controller/DetailTopAnimeController.kt | 83 ++++++++++++++ .../controller/DetailTopMangaController.kt | 83 ++++++++++++++ .../controller/SearchAnimeController.kt | 57 ++++++++++ .../controller/SearchMangaController.kt | 57 ++++++++++ .../presentation/controller/TopAnimeController.kt | 117 +++++++++++++++++++ .../presentation/controller/TopMangaController.kt | 117 +++++++++++++++++++ .../anime/DetailSearchAnimeController.kt | 56 ---------- .../controller/anime/DetailTopAnimeController.kt | 90 --------------- .../controller/anime/SearchAnimeController.kt | 65 ----------- .../controller/anime/TopAnimeController.kt | 124 --------------------- .../manga/DetailSearchMangaController.kt | 56 ---------- .../controller/manga/DetailTopMangaController.kt | 90 --------------- .../controller/manga/SearchMangaController.kt | 65 ----------- .../controller/manga/TopMangaController.kt | 124 --------------------- 16 files changed, 610 insertions(+), 670 deletions(-) create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchAnimeController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchMangaController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailTopAnimeController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailTopMangaController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchMangaController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopAnimeController.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopMangaController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailTopAnimeController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailTopMangaController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt (limited to 'app/src/main/java/xyz/adjutor/aniki/presentation/controller') diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchAnimeController.kt new file mode 100644 index 0000000..95aabec --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchAnimeController.kt @@ -0,0 +1,48 @@ +package xyz.adjutor.aniki.presentation.controller + +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.model.response.AnimeResponse +import xyz.adjutor.aniki.presentation.view.activity.DetailSearchAnimeActivity + +class DetailSearchAnimeController { + + lateinit var view: DetailSearchAnimeActivity + + fun onStart(DetailSearchAnimeActivity: DetailSearchAnimeActivity, animeId: String) { + + view = DetailSearchAnimeActivity + + makeApiCall(animeId) + } + + private fun makeApiCall(animeId: String) { + + Singletons + .animeApi + .getAnimeData(animeId) //based on the id + .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 anime = response.body() //getting the AnimeResponse fields + view.showDetail(anime!!) + + } else { + view.showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call, 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/DetailSearchMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchMangaController.kt new file mode 100644 index 0000000..d2f718a --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailSearchMangaController.kt @@ -0,0 +1,48 @@ +package xyz.adjutor.aniki.presentation.controller + +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.model.response.MangaResponse +import xyz.adjutor.aniki.presentation.view.activity.DetailSearchMangaActivity + +class DetailSearchMangaController { + + lateinit var view: DetailSearchMangaActivity + + fun onStart(DetailSearchMangaActivity: DetailSearchMangaActivity, mangaId: String) { + + view = DetailSearchMangaActivity + + makeApiCall(mangaId) + } + + private fun makeApiCall(mangaId: String) { + + Singletons + .mangaApi + .getMangaData(mangaId) //based on the id + .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 manga = response.body() //getting the MangaResponse fields + view.showDetail(manga!!) + + } else { + view.showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call, 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/DetailTopAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailTopAnimeController.kt new file mode 100644 index 0000000..46e7edd --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailTopAnimeController.kt @@ -0,0 +1,83 @@ +package xyz.adjutor.aniki.presentation.controller + +import android.content.Context +import android.content.SharedPreferences +import com.google.gson.reflect.TypeToken +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.Singletons.Companion.gson +import xyz.adjutor.aniki.presentation.model.response.AnimeResponse +import xyz.adjutor.aniki.presentation.view.activity.DetailTopAnimeActivity +import java.lang.reflect.Type + +class DetailTopAnimeController { + + private lateinit var sharedPreferences: SharedPreferences + lateinit var view: DetailTopAnimeActivity + + fun onStart(DetailTopAnimeActivity: DetailTopAnimeActivity, animeId: String) { + + view = DetailTopAnimeActivity + 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(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() {}.type + gson.fromJson(jsonAnime, type) + } + } + + private fun makeApiCall(animeId: String) { + + Singletons + .animeApi + .getAnimeData(animeId) //based on the id + .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 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, 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/DetailTopMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailTopMangaController.kt new file mode 100644 index 0000000..f9aaa07 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/DetailTopMangaController.kt @@ -0,0 +1,83 @@ +package xyz.adjutor.aniki.presentation.controller + +import android.content.Context +import android.content.SharedPreferences +import com.google.gson.reflect.TypeToken +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.Singletons.Companion.gson +import xyz.adjutor.aniki.presentation.model.response.MangaResponse +import xyz.adjutor.aniki.presentation.view.activity.DetailTopMangaActivity +import java.lang.reflect.Type + +class DetailTopMangaController { + + private lateinit var sharedPreferences: SharedPreferences + lateinit var view: DetailTopMangaActivity + + fun onStart(DetailTopMangaActivity: DetailTopMangaActivity, mangaId: String) { + + view = DetailTopMangaActivity + 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(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() {}.type + gson.fromJson(jsonManga, type) + } + } + + private fun makeApiCall(mangaId: String) { + + Singletons + .mangaApi + .getMangaData(mangaId) //based on the id + .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 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, 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/SearchAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt new file mode 100644 index 0000000..3ee6047 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchAnimeController.kt @@ -0,0 +1,57 @@ +package xyz.adjutor.aniki.presentation.controller + +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.model.SearchAnime +import xyz.adjutor.aniki.presentation.model.response.SearchAnimeResponse +import xyz.adjutor.aniki.presentation.view.fragment.SearchAnimeFragment + +class SearchAnimeController { + + lateinit var view: SearchAnimeFragment + + fun onStart(searchAnimeFragment: SearchAnimeFragment) { + + view = searchAnimeFragment + } + + //call the API and show the list + private fun makeApiCall(view: SearchAnimeFragment, query: String) { + + Singletons + .searchAnimeApi + .getSearchAnimeData(q = query) + .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, 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 new file mode 100644 index 0000000..7d5f92b --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/SearchMangaController.kt @@ -0,0 +1,57 @@ +package xyz.adjutor.aniki.presentation.controller + +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.model.SearchManga +import xyz.adjutor.aniki.presentation.model.response.SearchMangaResponse +import xyz.adjutor.aniki.presentation.view.fragment.SearchMangaFragment + +class SearchMangaController { + + lateinit var view: SearchMangaFragment + + fun onStart(searchMangaFragment: SearchMangaFragment) { + + view = searchMangaFragment + } + + //call the API and show the list + private fun makeApiCall(view: SearchMangaFragment, query: String) { + + Singletons + .searchMangaApi + .getSearchMangaData(q = query) + .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, 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 new file mode 100644 index 0000000..446f4e6 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopAnimeController.kt @@ -0,0 +1,117 @@ +package xyz.adjutor.aniki.presentation.controller + +import android.content.Context +import android.content.SharedPreferences +import android.view.View +import com.google.gson.reflect.TypeToken +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.Singletons.Companion.gson +import xyz.adjutor.aniki.presentation.model.TopAnime +import xyz.adjutor.aniki.presentation.model.response.TopAnimeResponse +import xyz.adjutor.aniki.presentation.view.fragment.TopAnimeFragment +import java.lang.reflect.Type +import kotlin.properties.Delegates + +class TopAnimeController { + + private lateinit var sharedPreferences: SharedPreferences + private var page by Delegates.notNull() + lateinit var view: TopAnimeFragment + + fun onStart(topAnimeFragment: TopAnimeFragment, viewTopAnimePage: View) { + + view = topAnimeFragment + page = 1 + sharedPreferences = + viewTopAnimePage.context.getSharedPreferences("sp_anime", Context.MODE_PRIVATE) + + + val animeList: List? = getDataFromCache() + if (animeList != null) { + view.showList(viewTopAnimePage, animeList) + } else { + makeApiCall(view, 1) + } + } + + private fun makeApiCall(view: TopAnimeFragment, page: Int) { + + Singletons + .topAnimeApi + .getTopAnimeData(page) + .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, 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, page) + view.showText("Page $page has been loaded.") + } + + fun updateList() { + makeApiCall(view, 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 new file mode 100644 index 0000000..afdbac7 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/TopMangaController.kt @@ -0,0 +1,117 @@ +package xyz.adjutor.aniki.presentation.controller + +import android.content.Context +import android.content.SharedPreferences +import android.view.View +import com.google.gson.reflect.TypeToken +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import xyz.adjutor.aniki.presentation.Singletons +import xyz.adjutor.aniki.presentation.Singletons.Companion.gson +import xyz.adjutor.aniki.presentation.model.TopManga +import xyz.adjutor.aniki.presentation.model.response.TopMangaResponse +import xyz.adjutor.aniki.presentation.view.fragment.TopMangaFragment +import java.lang.reflect.Type +import kotlin.properties.Delegates + +class TopMangaController { + + private lateinit var sharedPreferences: SharedPreferences + private var page by Delegates.notNull() + lateinit var view: TopMangaFragment + + fun onStart(topMangaPage: TopMangaFragment, viewTopMangaPage: View) { + + view = topMangaPage + page = 1 + sharedPreferences = + viewTopMangaPage.context.getSharedPreferences("sp_manga", Context.MODE_PRIVATE) + + + val mangaList: List? = getDataFromCache() + if (mangaList != null) { + view.showList(viewTopMangaPage, mangaList) + } else { + makeApiCall(view, 1) + } + } + + private fun makeApiCall(view: TopMangaFragment, page: Int) { + + Singletons + .topMangaApi + .getTopMangaData(page) + .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, 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, page) + view.showText("Page $page has been loaded.") + } + + fun updateList() { + makeApiCall(view, 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/DetailSearchAnimeController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt deleted file mode 100644 index e12de3a..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailSearchAnimeController.kt +++ /dev/null @@ -1,56 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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) { - - Singletons - .animeApi - .getAnimeData(animeId) //based on the id - .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 anime = response.body() //getting the AnimeResponse fields - view.showDetail(anime!!) - - } else { - view.showError("API ERROR : is not successful") - } - } - - override fun onFailure(call: Call, 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 deleted file mode 100644 index 3fb13b4..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/DetailTopAnimeController.kt +++ /dev/null @@ -1,90 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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() {}.type - gson.fromJson(jsonAnime, type) - } - } - - private fun makeApiCall(BASE_URL: String, animeId: String) { - - Singletons - .animeApi - .getAnimeData(animeId) //based on the id - .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 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, 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 deleted file mode 100644 index 5951f1b..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/SearchAnimeController.kt +++ /dev/null @@ -1,65 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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) { - - Singletons - .searchAnimeApi - .getSearchAnimeData(q = query) - .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 deleted file mode 100644 index 57dc10b..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/anime/TopAnimeController.kt +++ /dev/null @@ -1,124 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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) { - - Singletons - .topAnimeApi - .getTopAnimeData(page) - .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/DetailSearchMangaController.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt deleted file mode 100644 index 080e4ed..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt +++ /dev/null @@ -1,56 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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) { - - Singletons - .mangaApi - .getMangaData(mangaId) //based on the id - .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 manga = response.body() //getting the MangaResponse fields - view.showDetail(manga!!) - - } else { - view.showError("API ERROR : is not successful") - } - } - - override fun onFailure(call: Call, 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 deleted file mode 100644 index 9789d0c..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailTopMangaController.kt +++ /dev/null @@ -1,90 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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() {}.type - gson.fromJson(jsonManga, type) - } - } - - private fun makeApiCall(BASE_URL: String, mangaId: String) { - - Singletons - .mangaApi - .getMangaData(mangaId) //based on the id - .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 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, 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 deleted file mode 100644 index de1edad..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/SearchMangaController.kt +++ /dev/null @@ -1,65 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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) { - - Singletons - .searchMangaApi - .getSearchMangaData(q = query) - .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 deleted file mode 100644 index ea631fd..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/TopMangaController.kt +++ /dev/null @@ -1,124 +0,0 @@ -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 xyz.adjutor.aniki.presentation.Singletons -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) { - - Singletons - .topMangaApi - .getTopMangaData(page) - .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 -- cgit v1.2.3