diff options
author | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-05-13 13:30:29 +0200 |
---|---|---|
committer | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-05-13 13:30:29 +0200 |
commit | dea0bc3e943e46b9bbea14cc759ce1a62ef9f197 (patch) | |
tree | 750ea8f74e4fbca949118fd15d156c1917a0752f /app/src/main/java/xyz/adjutor/aniki/presentation/controller | |
parent | d6c121e3f626d5e012cdeb984feee7cb522b1442 (diff) |
DetailSearchMangaController.kt and activity OK
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/presentation/controller')
-rw-r--r-- | app/src/main/java/xyz/adjutor/aniki/presentation/controller/manga/DetailSearchMangaController.kt | 62 |
1 files changed, 61 insertions, 1 deletions
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 13993d8..a874976 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 @@ -1,3 +1,63 @@ package xyz.adjutor.aniki.presentation.controller.manga -class DetailSearchMangaController
\ No newline at end of file +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import xyz.adjutor.aniki.data.manga.MangaApi +import xyz.adjutor.aniki.presentation.model.manga.MangaResponse +import xyz.adjutor.aniki.presentation.view.manga.DetailSearchMangaActivity + +class DetailSearchMangaController { + + lateinit var gson: Gson + private lateinit var baseUrl: String //the api's base url + lateinit var view: DetailSearchMangaActivity + + fun onStart(DetailSearchMangaActivity: DetailSearchMangaActivity, mangaId: String) { + + view = DetailSearchMangaActivity + baseUrl = "https://api.jikan.moe/" //the api's base url + gson = GsonBuilder() + .setLenient() + .create() + + makeApiCall(baseUrl, mangaId) + } + + private fun makeApiCall(BASE_URL: String, mangaId: String) { + + val retrofit = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + + val service = retrofit.create(MangaApi::class.java) + val call = service.getMangaData(mangaId) //based on the id + + call.enqueue(object : Callback<MangaResponse> { + override fun onResponse( + call: Call<MangaResponse>, + response: Response<MangaResponse> + ) { + if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty + + val manga = response.body() //getting the MangaResponse fields + view.showDetail(manga!!) + + } else { + view.showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call<MangaResponse>, t: Throwable) { + view.showError("API ERROR : onFailure") + } + + }) + } + +}
\ No newline at end of file |