diff options
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/topmanga')
-rw-r--r-- | app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt | 41 |
1 files changed, 38 insertions, 3 deletions
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<RestMangaResponse>() {}.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<RestMangaResponse> { override fun onResponse(call: Call<RestMangaResponse>, response: Response<RestMangaResponse>) { 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 |