From ed22cdefdc8db6e09c801369aaf3bd5ef9950c60 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Thu, 13 May 2021 13:06:19 +0200 Subject: DetailTopMangaController.kt and the activity are working fine. --- .../controller/manga/DetailTopMangaController.kt | 98 +++++++++++++++++- .../view/manga/DetailTopMangaActivity.kt | 109 ++++----------------- 2 files changed, 116 insertions(+), 91 deletions(-) (limited to 'app/src/main') 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 e0f83b9..2d99a45 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 @@ -1,3 +1,99 @@ package xyz.adjutor.aniki.presentation.controller.manga -class DetailTopMangaController \ No newline at end of file +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 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.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) { + + 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) + } + + fun launch(mangaId: String) { + 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) { + + 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!!) + + } 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/view/manga/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailTopMangaActivity.kt index 2bf2f14..74f7595 100644 --- a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailTopMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailTopMangaActivity.kt @@ -1,7 +1,5 @@ package xyz.adjutor.aniki.presentation.view.manga -import android.content.Context -import android.content.SharedPreferences import android.os.Bundle import android.widget.ImageView import android.widget.TextView @@ -9,44 +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 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.R -import xyz.adjutor.aniki.data.manga.MangaApi +import xyz.adjutor.aniki.presentation.controller.manga.DetailTopMangaController import xyz.adjutor.aniki.presentation.model.manga.MangaResponse -import java.lang.reflect.Type class DetailTopMangaActivity : AppCompatActivity() { - private var baseUrl = "https://api.jikan.moe/" - private lateinit var sharedPreferences: SharedPreferences - private val gson = GsonBuilder() - .setLenient() - .create() - - //used in the list - private val intentMangaTitle = "themangatitle" - private val intentMangaRank = "themangarank" - private val intentMangaScore = "themangascore" - private val intentMangaImageUrl = "themangaimageurl" - - //only used for the detail - private val intentMangaId = "themangaid" - private val intentMangaVolumes = "themangavolumes" - private val intentMangaStartDate = "themangastartdate" - private val intentMangaEndDate = "themangaenddate" - private val intentMangaUrl = "themangaurl" + lateinit var controller: DetailTopMangaController override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail_top_manga) - sharedPreferences = this.getSharedPreferences("sp_manga", Context.MODE_PRIVATE) + controller = DetailTopMangaController() + controller.onStart(this) + + //used in the list + val intentMangaTitle = "themangatitle" + val intentMangaRank = "themangarank" + val intentMangaScore = "themangascore" + val intentMangaImageUrl = "themangaimageurl" + + //only used for the detail + val intentMangaId = "themangaid" + val intentMangaVolumes = "themangavolumes" + val intentMangaStartDate = "themangastartdate" + val intentMangaEndDate = "themangaenddate" + val intentMangaUrl = "themangaurl" val mangaTitle = intent.getStringExtra(intentMangaTitle) val mangaRank = intent.getStringExtra(intentMangaRank) @@ -98,61 +85,11 @@ class DetailTopMangaActivity : AppCompatActivity() { tvUrl.text = mangaUrl - val manga: MangaResponse? = getDataFromCache(mangaId.toString()) - if (manga != null) { - showDetail(manga) - } else { - //taking the API's fields I want and displaying them - makeApiCall(baseUrl, mangaId.toString()) - } - - } - - 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) { - - 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 + controller.launch(mangaId.toString()) - val manga = response.body() //getting the MangaResponse fields - saveList(manga) - 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 tvChapters: TextView = findViewById(R.id.tv_chapters) val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) @@ -182,12 +119,4 @@ class DetailTopMangaActivity : AppCompatActivity() { return "Unknown" } - 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 -- cgit v1.2.3