From eaef2672c25baaf370c72158d5236f40f6fea01f Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Thu, 18 Feb 2021 21:55:37 +0100 Subject: Preparing Preparing the implementation of the API datas to our recycerview in a fragment. --- .../main/java/xyz/adjutor/aniki/MangaAdapter.kt | 12 +++++---- .../java/xyz/adjutor/aniki/RestTopMangaResponse.kt | 4 +++ .../main/java/xyz/adjutor/aniki/TopMangaPage.kt | 29 +++++++++++----------- 3 files changed, 25 insertions(+), 20 deletions(-) (limited to 'app/src/main/java/xyz/adjutor') diff --git a/app/src/main/java/xyz/adjutor/aniki/MangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/MangaAdapter.kt index 8e1c8f2..9f7a373 100644 --- a/app/src/main/java/xyz/adjutor/aniki/MangaAdapter.kt +++ b/app/src/main/java/xyz/adjutor/aniki/MangaAdapter.kt @@ -3,6 +3,7 @@ package xyz.adjutor.aniki import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import java.util.ArrayList @@ -12,11 +13,10 @@ class MangaAdapter(val mangaList: Array) : // Describes an item view and its place within the RecyclerView class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - private val mangaTextView: TextView = itemView.findViewById(R.id.tv_title) + val mangaTitle: TextView = itemView.findViewById(R.id.tv_title) + val mangaRank: TextView = itemView.findViewById(R.id.tv_rank) + val mangaImage: ImageView = itemView.findViewById(R.id.iv_image) - fun bind(word: String) { - mangaTextView.text = word - } } // Returns a new ViewHolder @@ -34,7 +34,9 @@ class MangaAdapter(val mangaList: Array) : // Displays data at a certain position override fun onBindViewHolder(holder: MangaViewHolder, position: Int) { - holder.bind(mangaList[position]) + holder.mangaTitle.text = mangaList[position] //use specific json data + holder.mangaRank.text = position.toString() //replace by the actual rank + //holder.mangaImage.setImageResource(mangaList[position]) //replace by the manga image } } diff --git a/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt index 8cd5f2b..46b3bd3 100644 --- a/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt @@ -12,4 +12,8 @@ class RestTopMangaResponse { var request_cached_expiry: Int? = null @SerializedName("top") var top = ArrayList() + + fun getResults(): ArrayList { + return top + } } \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt index 9496053..729ff6f 100644 --- a/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt @@ -26,17 +26,8 @@ class TopMangaPage : Fragment() { // Inflate the layout for this fragment val view = inflater.inflate(R.layout.top_manga_page, container, false) - // Retrieves data from mangasource - val mangaList = MangaSource(view.context).getMangaList() + makeApiCall(view, base_url) - - makeApiCall(base_url) - - - val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) - recyclerView.setHasFixedSize(true) - recyclerView.layoutManager = LinearLayoutManager(view.context) - recyclerView.adapter = MangaAdapter(mangaList) return view } @@ -49,7 +40,14 @@ class TopMangaPage : Fragment() { } - fun makeApiCall(BASE_URL: String) { + fun showList(view: View, mangaList: Array ){ + val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) + recyclerView.setHasFixedSize(true) + recyclerView.layoutManager = LinearLayoutManager(view.context) + recyclerView.adapter = MangaAdapter(mangaList) + } + + fun makeApiCall(view: View, BASE_URL: String) { val gson = GsonBuilder() .setLenient() .create() @@ -62,9 +60,12 @@ class TopMangaPage : Fragment() { call.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if(response.isSuccessful && response.body() != null){ - val topMangaResponse = response.body() + val topMangaResponse = response.body()!!.getResults() Snackbar.make(requireView(), "HA? YOU THOUGHT IT WAS AN ERROR !? BUT IT WAS I, API !", Snackbar.LENGTH_LONG) .setAction("Action", null).show() + // Retrieves data from mangasource + val mangaList = MangaSource(view.context).getMangaList() + showList(view, mangaList) } else { showError() } @@ -74,9 +75,7 @@ class TopMangaPage : Fragment() { showError() } - - } - ) + }) } private fun showError() { -- cgit v1.2.3