diff options
author | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-05-13 13:06:19 +0200 |
---|---|---|
committer | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-05-13 13:06:19 +0200 |
commit | ed22cdefdc8db6e09c801369aaf3bd5ef9950c60 (patch) | |
tree | eb3240a35ab2afe3c45ecf1673f4d94beeff857d /app/src/main/java/xyz/adjutor/aniki/presentation/view | |
parent | 55ae44495fb566050e20649452ce6bbe6978a100 (diff) |
DetailTopMangaController.kt and the activity are working fine.
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/presentation/view')
-rw-r--r-- | app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/DetailTopMangaActivity.kt | 109 |
1 files changed, 19 insertions, 90 deletions
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<MangaResponse>() {}.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<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 + 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<MangaResponse>, 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 |