From 9ee5918fa86da6c160145498b1a95f4f4a083fdc Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Mon, 22 Feb 2021 14:25:15 +0100 Subject: Sharedpreference now working for the DetailTopMangaActivity.kt --- app/build.gradle | 1 - .../xyz/adjutor/aniki/manga/RestMangaResponse.kt | 4 ++- .../aniki/topmanga/DetailTopMangaActivity.kt | 41 ++++++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1e612ce..d1d101c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,7 +34,6 @@ android { dependencies { - //noinspection GradleDependency implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt index 7a4aff9..00b0dce 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt @@ -2,8 +2,10 @@ package xyz.adjutor.aniki.manga import com.google.gson.annotations.SerializedName -class RestMangaResponse{ //only kept the infos I didnt have and that were interresting to me. +class RestMangaResponse{ //only kept the infos I didn't have and that were interesting to me. + @SerializedName("mal_id") + var mal_id: Int? = null @SerializedName("chapters") var chapters: Int? = null @SerializedName("synopsis") diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt index 1863815..32e3457 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt @@ -1,5 +1,7 @@ package xyz.adjutor.aniki.topmanga +import android.content.Context +import android.content.SharedPreferences import android.os.Bundle import android.widget.ImageView import android.widget.TextView @@ -8,6 +10,7 @@ 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 @@ -16,10 +19,12 @@ import retrofit2.converter.gson.GsonConverterFactory import xyz.adjutor.aniki.R import xyz.adjutor.aniki.manga.MangaApi import xyz.adjutor.aniki.manga.RestMangaResponse +import java.lang.reflect.Type class DetailTopMangaActivity : AppCompatActivity() { private var baseUrl = "https://api.jikan.moe/" + var sharedPreferences: SharedPreferences? = null private val gson = GsonBuilder() .setLenient() .create() @@ -38,6 +43,8 @@ class DetailTopMangaActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail_top_manga) + sharedPreferences = this.getSharedPreferences("app_aniki", Context.MODE_PRIVATE) + val mangaId = intent.getStringExtra(intentMangaId) val mangaTitle = intent.getStringExtra(intentMangaTitle) val mangaRank = intent.getStringExtra(intentMangaRank) @@ -83,8 +90,26 @@ class DetailTopMangaActivity : AppCompatActivity() { fieldIsNull() } - //taking the API's fields I want and displaying them - makeApiCall(baseUrl, mangaId.toString()) + val manga: RestMangaResponse? = 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): RestMangaResponse? { + val jsonManga: String?= sharedPreferences?.getString(mangaId, null) + + if(jsonManga == null) { + return null + } else { + val type: Type = object : TypeToken() {}.type + return gson.fromJson(jsonManga, type) + } } private fun makeApiCall(BASE_URL: String, mangaId: String) { @@ -95,13 +120,14 @@ class DetailTopMangaActivity : AppCompatActivity() { .build() val service = retrofit.create(MangaApi::class.java) - val call = service.getMangaData(mangaId) + 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 RestMangaResponse fields + saveList(manga) showDetail(manga!!) } else { @@ -144,4 +170,13 @@ class DetailTopMangaActivity : AppCompatActivity() { fun fieldIsNull(): String{ return "Unknown" } + + fun saveList(manga: RestMangaResponse?) { + 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