From d283df0f70241bb6b54bee66e8fb9564b0051376 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Tue, 23 Mar 2021 10:38:03 +0100 Subject: Feature added to mangas Display other pages by touching the bottom or by clicking buttons --- README.md | 4 +- .../adjutor/aniki/manga/topmanga/TopMangaApi.kt | 5 ++- .../adjutor/aniki/manga/topmanga/TopMangaPage.kt | 48 ++++++++++++++++++++-- app/src/main/res/layout/top_manga_page.xml | 16 ++++++++ app/src/main/res/values/strings.xml | 2 + 5 files changed, 68 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ad56981..24ea18c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ List of elements used in top manga and top anime with a recycler view -SwipeRefresh used to refresh the list of data. +SwipeRefresh used to refresh the list of data of the frist page. +Thanks to the scroll listener, touch the bottom of the page to load another page, replacing the first one. +Use the button (prev and next) to navigate with ease through the pages. The "ScrollListener" was added as a practice, it's not the best feature to navigate. Details of a chosen element from the recycler view with an intent object Title, synopsys and background clickable. diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaApi.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaApi.kt index 629fe02..2688fab 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaApi.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaApi.kt @@ -2,10 +2,11 @@ package xyz.adjutor.aniki.manga.topmanga import retrofit2.Call import retrofit2.http.GET +import retrofit2.http.Path interface TopMangaApi { - @GET("v3/top/manga") - fun getTopMangaData(): Call + @GET("v3/top/manga/{page}") + fun getTopMangaData(@Path("page") page: Int): Call } \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt index b35bcdd..ea240b9 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt @@ -24,6 +24,7 @@ import retrofit2.converter.gson.GsonConverterFactory import xyz.adjutor.aniki.R import java.lang.reflect.Type + class TopMangaPage : Fragment() { private lateinit var sharedPreferences: SharedPreferences @@ -31,6 +32,7 @@ class TopMangaPage : Fragment() { .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?, @@ -45,10 +47,29 @@ class TopMangaPage : Fragment() { if (mangaList != null) { showList(view, mangaList) } else { - makeApiCall(view, baseUrl) + makeApiCall(view, baseUrl, 1) } + // add other pages when we touch the bottom + val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) + recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) + if (!recyclerView.canScrollVertically(1)) { //direction integers: -1 for up, 1 for down, 0 will always return false. + page += 1 + makeApiCall(view, baseUrl, page) + Snackbar.make( + requireView(), + "Page $page has been loaded.", + Snackbar.LENGTH_LONG + ) + .setAction("Action", null).show() + } + } + }) + return view + } private fun getDataFromCache(): List? { @@ -71,16 +92,35 @@ class TopMangaPage : Fragment() { view.findViewById