aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2021-03-23 10:38:03 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2021-03-23 10:38:03 +0100
commitd283df0f70241bb6b54bee66e8fb9564b0051376 (patch)
tree374a9a97fc1ad7caa3766b4bac45f517631444cd /app/src/main/java/xyz/adjutor
parent436a01b64a3c2500aa8801a72f95dfc40b0b33b7 (diff)
Feature added to mangas
Display other pages by touching the bottom or by clicking buttons
Diffstat (limited to 'app/src/main/java/xyz/adjutor')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaApi.kt5
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt48
2 files changed, 47 insertions, 6 deletions
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<TopMangaResponse>
+ @GET("v3/top/manga/{page}")
+ fun getTopMangaData(@Path("page") page: Int): Call<TopMangaResponse>
} \ 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<TopManga>? {
@@ -71,16 +92,35 @@ class TopMangaPage : Fragment() {
view.findViewById<Button>(R.id.button_home).setOnClickListener {
findNavController().navigate(R.id.action_TopMangaPage_to_HomePage)
}
+ view.findViewById<Button>(R.id.button_prev).setOnClickListener {
+ if (page > 1) {
+ page -= 1
+ makeApiCall(view, baseUrl, page)
+ Snackbar.make(requireView(), "Page $page has been loaded.", Snackbar.LENGTH_LONG)
+ .setAction("Action", null).show()
+ } else {
+ Snackbar.make(requireView(), "You're already page 1.", Snackbar.LENGTH_LONG)
+ .setAction("Action", null).show()
+ }
+ }
+ view.findViewById<Button>(R.id.button_next).setOnClickListener {
+ page += 1
+ makeApiCall(view, baseUrl, page)
+ Snackbar.make(requireView(), "Page $page has been loaded.", Snackbar.LENGTH_LONG)
+ .setAction("Action", null).show()
+ }
fun updateList() {
- makeApiCall(view, baseUrl)
+ makeApiCall(view, baseUrl, 1)
Snackbar.make(requireView(), "Data refreshed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
+ //refresh when swiping down at the top of the page
val swipeRefresh: SwipeRefreshLayout = view.findViewById(R.id.swiperefresh)
swipeRefresh.setOnRefreshListener {
updateList()
+ page = 1
swipeRefresh.isRefreshing = false
}
@@ -95,7 +135,7 @@ class TopMangaPage : Fragment() {
(recyclerView.adapter as TopMangaAdapter).notifyDataSetChanged()
}
- private fun makeApiCall(view: View, BASE_URL: String) {
+ private fun makeApiCall(view: View, BASE_URL: String, page: Int) {
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
@@ -103,7 +143,7 @@ class TopMangaPage : Fragment() {
.build()
val service = retrofit.create(TopMangaApi::class.java)
- val call = service.getTopMangaData()
+ val call = service.getTopMangaData(page)
call.enqueue(object : Callback<TopMangaResponse> {
override fun onResponse(