From dea0bc3e943e46b9bbea14cc759ce1a62ef9f197 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Thu, 13 May 2021 13:30:29 +0200 Subject: DetailSearchMangaController.kt and activity OK --- .../manga/DetailSearchMangaController.kt | 62 ++++++++++++++++- .../view/manga/DetailSearchMangaActivity.kt | 77 ++++++---------------- 2 files changed, 80 insertions(+), 59 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki') 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 { + 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/view/manga/DetailSearchMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailSearchMangaActivity.kt index d04760b..83beb9b 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailSearchMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailSearchMangaActivity.kt @@ -7,40 +7,33 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions -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.R -import xyz.adjutor.aniki.data.manga.MangaApi +import xyz.adjutor.aniki.presentation.controller.manga.DetailSearchMangaController import xyz.adjutor.aniki.presentation.model.manga.MangaResponse class DetailSearchMangaActivity : AppCompatActivity() { - private var baseUrl = "https://api.jikan.moe/" - private val gson = GsonBuilder() - .setLenient() - .create() - - //used in the list - private val intentMangaImageUrl = "themangaimageurl" - private val intentMangaTitle = "themangatitle" - private val intentMangaScore = "themangascore" - - //only used for the detail - private val intentMangaId = "themangaid" - private val intentMangaUrl = "themangaurl" - private val intentMangaChapters = "themangachapters" - private val intentMangaVolumes = "themangavolumes" - private val intentMangaStartDate = "themangastartdate" - private val intentMangaEndDate = "themangaenddate" + lateinit var controller: DetailSearchMangaController override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail_search_manga) + controller = DetailSearchMangaController() + + //used in the list + val intentMangaImageUrl = "themangaimageurl" + val intentMangaTitle = "themangatitle" + val intentMangaScore = "themangascore" + + //only used for the detail + val intentMangaId = "themangaid" + val intentMangaUrl = "themangaurl" + val intentMangaChapters = "themangachapters" + val intentMangaVolumes = "themangavolumes" + val intentMangaStartDate = "themangastartdate" + val intentMangaEndDate = "themangaenddate" + val mangaImageUrl = intent.getStringExtra(intentMangaImageUrl) val mangaTitle = intent.getStringExtra(intentMangaTitle) val mangaScore = intent.getStringExtra(intentMangaScore) @@ -97,7 +90,7 @@ class DetailSearchMangaActivity : AppCompatActivity() { fieldIsNull() } - makeApiCall(baseUrl, mangaId.toString()) + controller.onStart(this, mangaId.toString()) } @@ -109,39 +102,7 @@ class DetailSearchMangaActivity : AppCompatActivity() { } - 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 - showDetail(manga!!) - - } else { - showError("API ERROR : is not successful") - } - } - - override fun onFailure(call: Call, t: Throwable) { - showError("API ERROR : onFailure") - } - - }) - } - - private fun showDetail(manga: MangaResponse) { + fun showDetail(manga: MangaResponse) { //elements from MangaResponse val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) val tvRank: TextView = findViewById(R.id.tv_detail_rank) -- cgit v1.2.3