aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2021-02-19 14:17:15 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2021-02-19 14:17:15 +0100
commitcd37b807397b50b8e9c45389fc04679998e628db (patch)
tree1d4e58436f7d88c82b588c86fe99fce74ea09de3 /app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt
parent41bb476749fa84164726b728676b144531009446 (diff)
parentfa849021bf137fc4b245a8c0e7a2a8fc46d574df (diff)
Merge branch 'release/1.0' into develop
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt69
1 files changed, 69 insertions, 0 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt
new file mode 100644
index 0000000..1478111
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/TopMangaAdapter.kt
@@ -0,0 +1,69 @@
+@file:Suppress("DEPRECATION")
+
+package xyz.adjutor.aniki
+
+import android.annotation.SuppressLint
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.os.AsyncTask
+import android.util.Log
+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
+
+class TopMangaAdapter(val mangaList: List<TopManga>) :
+ RecyclerView.Adapter<TopMangaAdapter.MangaViewHolder>() {
+
+ // Describes an item view and its place within the RecyclerView
+ class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
+ 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)
+
+ }
+
+ // Returns a new ViewHolder
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MangaViewHolder {
+ val view = LayoutInflater.from(parent.context)
+ .inflate(R.layout.item_layout, parent, false)
+
+ return MangaViewHolder(view)
+ }
+
+ // Returns size of data list
+ override fun getItemCount(): Int {
+ return mangaList.size
+ }
+
+ // Displays data at a certain position
+ override fun onBindViewHolder(holder: MangaViewHolder, position: Int) {
+ val currentManga: TopManga = mangaList[position]
+ holder.mangaTitle.text = currentManga.title
+ holder.mangaRank.text = currentManga.rank.toString()
+ DownloadImageFromInternet(holder.mangaImage).execute(currentManga.image_url)
+ }
+
+ @SuppressLint("StaticFieldLeak")
+ private inner class DownloadImageFromInternet(var imageView: ImageView) : AsyncTask<String, Void, Bitmap?>() {
+ override fun doInBackground(vararg urls: String): Bitmap? {
+ val imageURL = urls[0]
+ var image: Bitmap? = null
+ try {
+ val `in` = java.net.URL(imageURL).openStream()
+ image = BitmapFactory.decodeStream(`in`)
+ }
+ catch (e: Exception) {
+ Log.e("Error Message", e.message.toString())
+ e.printStackTrace()
+ }
+ return image
+ }
+ override fun onPostExecute(result: Bitmap?) {
+ imageView.setImageBitmap(result)
+ }
+ }
+}
+