aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt
blob: 68a2c7bacbdd53bb3ae1773642e8cf24697a1cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@file:Suppress("DEPRECATION")

package xyz.adjutor.aniki.topanime

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
import xyz.adjutor.aniki.R
import xyz.adjutor.aniki.topanime.TopAnime
import xyz.adjutor.aniki.topanime.TopAnimeAdapter

class TopAnimeAdapter(val animeList: List<TopAnime>) :
        RecyclerView.Adapter<TopAnimeAdapter.AnimeViewHolder>() {

    // Describes an item view and its place within the RecyclerView
    class AnimeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val animeTitle: TextView = itemView.findViewById(R.id.tv_title)
        val animeRank: TextView = itemView.findViewById(R.id.tv_rank)
        val animeScore: TextView = itemView.findViewById(R.id.tv_score)
        val animeImage: ImageView = itemView.findViewById(R.id.iv_image)

    }

    // Returns a new ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnimeViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_layout, parent, false)

        return AnimeViewHolder(view)
    }

    // Returns size of data list
    override fun getItemCount(): Int {
        return animeList.size
    }

    // Displays data at a certain position
    override fun onBindViewHolder(holder: AnimeViewHolder, position: Int) {
        val currentAnime: TopAnime = animeList[position]
        holder.animeTitle.text = currentAnime.title
        holder.animeRank.text = currentAnime.rank.toString()
        holder.animeScore.text = currentAnime.score.toString()
        DownloadImageFromInternet(holder.animeImage).execute(currentAnime.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)
        }
    }
}