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 --- .../aniki/topmanga/DetailTopMangaActivity.kt | 41 ++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki/topmanga') 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