From be1df4b40a41ae1d4c727637a326ad32eb729ed9 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Thu, 13 May 2021 13:35:46 +0200 Subject: DetailSearchAnimeController.kt and activity OK --- .../anime/DetailSearchAnimeController.kt | 62 ++++++++++++++++- .../view/anime/DetailSearchAnimeActivity.kt | 78 +++++----------------- 2 files changed, 79 insertions(+), 61 deletions(-) (limited to 'app/src') 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 5dc0a81..3114d01 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 @@ -1,3 +1,63 @@ package xyz.adjutor.aniki.presentation.controller.anime -class DetailSearchAnimeController \ 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.anime.AnimeApi +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) { + + 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 + 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/view/anime/DetailSearchAnimeActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/DetailSearchAnimeActivity.kt index f9d1325..5acf1c3 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/DetailSearchAnimeActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/anime/DetailSearchAnimeActivity.kt @@ -7,39 +7,32 @@ 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.anime.AnimeApi +import xyz.adjutor.aniki.presentation.controller.anime.DetailSearchAnimeController import xyz.adjutor.aniki.presentation.model.anime.AnimeResponse class DetailSearchAnimeActivity : AppCompatActivity() { - private var baseUrl = "https://api.jikan.moe/" - private val gson = GsonBuilder() - .setLenient() - .create() - - //used in the list - private val intentAnimeImageUrl = "theanimeimageurl" - private val intentAnimeTitle = "theanimetitle" - private val intentAnimeScore = "theanimescore" - - //only used for the detail - private val intentAnimeId = "theanimeid" - private val intentAnimeUrl = "theanimeurl" - private val intentAnimeEpisodes = "theanimeepisodes" - private val intentAnimeStartDate = "theanimestartdate" - private val intentAnimeEndDate = "theanimeenddate" + lateinit var controller: DetailSearchAnimeController override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail_search_anime) + controller = DetailSearchAnimeController() + + //used in the list + val intentAnimeImageUrl = "theanimeimageurl" + val intentAnimeTitle = "theanimetitle" + val intentAnimeScore = "theanimescore" + + //only used for the detail + val intentAnimeId = "theanimeid" + val intentAnimeUrl = "theanimeurl" + val intentAnimeEpisodes = "theanimeepisodes" + val intentAnimeStartDate = "theanimestartdate" + val intentAnimeEndDate = "theanimeenddate" + val animeImageUrl = intent.getStringExtra(intentAnimeImageUrl) val animeTitle = intent.getStringExtra(intentAnimeTitle) val animeScore = intent.getStringExtra(intentAnimeScore) @@ -88,7 +81,7 @@ class DetailSearchAnimeActivity : AppCompatActivity() { fieldIsNull() } - makeApiCall(baseUrl, animeId.toString()) + controller.onStart(this, animeId.toString()) } @@ -100,42 +93,7 @@ class DetailSearchAnimeActivity : AppCompatActivity() { } - private fun makeApiCall( - BASE_URL: String, - animeId: String - ) { //we take the rest of the data that we need from the internet - - 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 - showDetail(anime!!) - - } else { - showError("API ERROR : is not successful") - } - } - - override fun onFailure(call: Call, t: Throwable) { - showError("API ERROR : onFailure") - } - - }) - } - - private fun showDetail(anime: AnimeResponse) { + fun showDetail(anime: AnimeResponse) { //elements from AnimeResponse val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) val tvRank: TextView = findViewById(R.id.tv_detail_rank) -- cgit v1.2.3