From 5a8c22508e08b00a31c8ebb4df13d9e108d29e1f Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Tue, 13 Apr 2021 13:22:04 +0200 Subject: Restructuring and README update. --- .../aniki/presentation/view/manga/TopMangaPage.kt | 178 +++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt (limited to 'app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt') diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt new file mode 100644 index 0000000..edb5fea --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/manga/TopMangaPage.kt @@ -0,0 +1,178 @@ +package xyz.adjutor.aniki.presentation.view.manga + +import android.content.Context +import android.content.SharedPreferences +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import com.google.android.material.snackbar.Snackbar +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.R +import xyz.adjutor.aniki.data.manga.TopMangaApi +import xyz.adjutor.aniki.presentation.model.manga.TopManga +import xyz.adjutor.aniki.presentation.model.manga.TopMangaResponse +import java.lang.reflect.Type + +//view +class TopMangaPage : Fragment() { + + private lateinit var sharedPreferences: SharedPreferences + val gson: Gson = GsonBuilder() + .setLenient() + .create() + private var baseUrl = "https://api.jikan.moe/" //the api's base url + var page: Int = 1 + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val view = inflater.inflate(R.layout.top_manga_page, container, false) + + sharedPreferences = view.context.getSharedPreferences("sp_manga", Context.MODE_PRIVATE) + + val mangaList: List? = getDataFromCache() + if (mangaList != null) { + showList(view, mangaList) + } else { + makeApiCall(view, baseUrl, 1) + } + + return view + + } + + private fun getDataFromCache(): List? { + //the value of the mangaList json, if nothing is found, return null + val jsonManga: String? = sharedPreferences.getString("jsonMangaList", null) + + //if it's null, well, return null + return if (jsonManga == null) { + null + } else { //else deserialize the list and return it + val listType: Type = object : TypeToken>() {}.type + gson.fromJson(jsonManga, listType) + } + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + //button to return to the home page + view.findViewById