From 789cb4c6c3e2f4b6bea325b21801d098ebf04623 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 14 May 2021 13:26:34 +0200 Subject: Addedd Singletons for TopManga. --- .../xyz/adjutor/aniki/presentation/Singletons.kt | 23 ++++++++++++++ .../controller/manga/TopMangaController.kt | 37 +++++++++------------- 2 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt (limited to 'app/src/main/java') diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt new file mode 100644 index 0000000..430f73b --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt @@ -0,0 +1,23 @@ +package xyz.adjutor.aniki.presentation + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import xyz.adjutor.aniki.data.manga.TopMangaApi + +class Singletons { + + companion object { + val gson: Gson = GsonBuilder() + .setLenient() + .create()!! + private const val BASE_URL = "https://api.jikan.moe/" + + val topMangaApi: TopMangaApi = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(TopMangaApi::class.java) + } +} 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 index 845e750..ea631fd 100644 --- 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 @@ -9,9 +9,7 @@ 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.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 @@ -48,25 +46,20 @@ class TopMangaController { 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( + 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 -- cgit v1.2.3 From 5d4d65293a667f9d4807b3599f13ce36af2d1060 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 14 May 2021 13:39:43 +0200 Subject: Singletons : DetailTopManga : OK --- .../xyz/adjutor/aniki/presentation/Singletons.kt | 35 ++++++++++++++++++++++ .../controller/manga/DetailTopMangaController.kt | 35 +++++++++------------- 2 files changed, 49 insertions(+), 21 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt index 430f73b..827c7e3 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/Singletons.kt @@ -4,6 +4,11 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory +import xyz.adjutor.aniki.data.anime.AnimeApi +import xyz.adjutor.aniki.data.anime.SearchAnimeApi +import xyz.adjutor.aniki.data.anime.TopAnimeApi +import xyz.adjutor.aniki.data.manga.MangaApi +import xyz.adjutor.aniki.data.manga.SearchMangaApi import xyz.adjutor.aniki.data.manga.TopMangaApi class Singletons { @@ -19,5 +24,35 @@ class Singletons { .addConverterFactory(GsonConverterFactory.create(gson)) .build() .create(TopMangaApi::class.java) + + val searchMangaApi: SearchMangaApi = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(SearchMangaApi::class.java) + + val mangaApi: MangaApi = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(MangaApi::class.java) + + val topAnimeApi: TopAnimeApi = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(TopAnimeApi::class.java) + + val searchAnimeApi: SearchAnimeApi = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(SearchAnimeApi::class.java) + + val animeApi: AnimeApi = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + .create(AnimeApi::class.java) } } 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 index 807421c..9789d0c 100644 --- 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 @@ -8,9 +8,7 @@ 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.Singletons import xyz.adjutor.aniki.presentation.model.manga.MangaResponse import xyz.adjutor.aniki.presentation.view.manga.DetailTopMangaActivity import java.lang.reflect.Type @@ -54,24 +52,19 @@ class DetailTopMangaController { 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 { - 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!!) + 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") -- cgit v1.2.3 From 315c8932950c003f373de601eb3f9e678c073663 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 14 May 2021 13:48:39 +0200 Subject: Singletons on all pages : OK --- .../anime/DetailSearchAnimeController.kt | 33 ++++++++----------- .../controller/anime/DetailTopAnimeController.kt | 35 ++++++++------------ .../controller/anime/SearchAnimeController.kt | 34 ++++++++------------ .../controller/anime/TopAnimeController.kt | 37 +++++++++------------- .../manga/DetailSearchMangaController.kt | 33 ++++++++----------- .../controller/manga/SearchMangaController.kt | 34 ++++++++------------ 6 files changed, 81 insertions(+), 125 deletions(-) (limited to 'app/src/main/java') 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 index 3114d01..e12de3a 100644 --- 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 @@ -5,9 +5,7 @@ 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.Singletons import xyz.adjutor.aniki.presentation.model.anime.AnimeResponse import xyz.adjutor.aniki.presentation.view.anime.DetailSearchAnimeActivity @@ -30,25 +28,20 @@ class DetailSearchAnimeController { private fun makeApiCall(BASE_URL: String, animeId: String) { - val retrofit = Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() + 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 service = retrofit.create(AnimeApi::class.java) - val call = service.getAnimeData(animeId) //based on the id + val anime = response.body() //getting the AnimeResponse fields + view.showDetail(anime!!) - 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 anime = response.body() //getting the AnimeResponse fields - view.showDetail(anime!!) - - } else { + } else { view.showError("API ERROR : is not successful") } } 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 index 6f3cedb..3fb13b4 100644 --- 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 @@ -8,9 +8,7 @@ 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.Singletons import xyz.adjutor.aniki.presentation.model.anime.AnimeResponse import xyz.adjutor.aniki.presentation.view.anime.DetailTopAnimeActivity import java.lang.reflect.Type @@ -54,24 +52,19 @@ class DetailTopAnimeController { 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 { - 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!!) + 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") 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 index aa8487e..5951f1b 100644 --- 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 @@ -5,9 +5,7 @@ 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.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 @@ -30,26 +28,20 @@ class SearchAnimeController { //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() + 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 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. + val animeList: List = response.body()!! + .getResults() //getting the "search" field containing our list of SearchAnimes - 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.showList( view.requireView(), animeList ) //calling the method in charge of displaying on the recyclerview 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 index 7105af5..57dc10b 100644 --- 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 @@ -9,9 +9,7 @@ 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.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 @@ -48,25 +46,20 @@ class TopAnimeController { 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( + 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 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 index a874976..080e4ed 100644 --- 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 @@ -5,9 +5,7 @@ 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.Singletons import xyz.adjutor.aniki.presentation.model.manga.MangaResponse import xyz.adjutor.aniki.presentation.view.manga.DetailSearchMangaActivity @@ -30,25 +28,20 @@ class DetailSearchMangaController { private fun makeApiCall(BASE_URL: String, mangaId: String) { - val retrofit = Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() + 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 service = retrofit.create(MangaApi::class.java) - val call = service.getMangaData(mangaId) //based on the id + val manga = response.body() //getting the MangaResponse fields + view.showDetail(manga!!) - 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 manga = response.body() //getting the MangaResponse fields - view.showDetail(manga!!) - - } else { + } else { view.showError("API ERROR : is not successful") } } 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 index 32e46eb..de1edad 100644 --- 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 @@ -5,9 +5,7 @@ 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.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 @@ -30,26 +28,20 @@ class SearchMangaController { //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() + 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 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. + val mangaList: List = response.body()!! + .getResults() //getting the "search" field containing our list of SearchMangas - 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.showList( view.requireView(), mangaList ) //calling the method in charge of displaying on the recyclerview -- cgit v1.2.3