瀏覽代碼

Merge branch 'rss-images' into devel

yattoz 4 年之前
父節點
當前提交
8df3d45139
共有 1 個文件被更改,包括 94 次插入11 次删除
  1. 94 11
      app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsAdapter.kt

+ 94 - 11
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsAdapter.kt 查看文件

@@ -1,20 +1,89 @@
1 1
 package fr.forum_thalie.tsumugi.ui.news
2 2
 
3 3
 import android.annotation.SuppressLint
4
+import android.app.Activity
4 5
 import android.content.Context
5
-import android.content.Intent
6
-import android.net.Uri
7
-import android.widget.TextView
6
+import android.graphics.Bitmap
7
+import android.graphics.BitmapFactory
8
+import android.graphics.Point
9
+import android.graphics.drawable.BitmapDrawable
10
+import android.graphics.drawable.Drawable
11
+import android.graphics.drawable.LevelListDrawable
12
+import android.os.AsyncTask
13
+import android.text.Html.ImageGetter
14
+import android.text.method.LinkMovementMethod
8 15
 import android.view.LayoutInflater
9 16
 import android.view.ViewGroup
17
+import android.widget.TextView
10 18
 import androidx.constraintlayout.widget.ConstraintLayout
19
+import androidx.core.content.ContextCompat
11 20
 import androidx.core.text.HtmlCompat
12 21
 import androidx.core.widget.TextViewCompat
13 22
 import androidx.recyclerview.widget.RecyclerView
14 23
 import fr.forum_thalie.tsumugi.R
24
+import java.io.IOException
25
+import java.io.InputStream
26
+import java.net.URL
15 27
 import java.text.SimpleDateFormat
16 28
 import java.util.*
17
-import kotlin.collections.ArrayList
29
+
30
+// Using solution found here: https://stackoverflow.com/questions/3758535/display-images-on-android-using-textview-and-html-imagegetter-asynchronously
31
+// but without Picasso (just fetching image by myself.)
32
+class ImageGetterAsyncTask(
33
+    private val context: Context,
34
+    private val source: String,
35
+    private val levelListDrawable: LevelListDrawable
36
+) :
37
+    AsyncTask<TextView?, Void?, Bitmap?>() {
38
+    private var t: TextView? = null
39
+    override fun doInBackground(vararg params: TextView?): Bitmap? {
40
+        t = params[0]
41
+        return try {
42
+            //Log.d(LOG_CAT, "Downloading the image from: $source")
43
+            var k: InputStream? = null
44
+            var pic: Bitmap? = null
45
+            try {
46
+                k = URL(source).content as InputStream
47
+                val options = BitmapFactory.Options()
48
+                options.inSampleSize = 1/4
49
+                // this makes 1/2 of origin image size from width and height.
50
+                // it alleviates the memory for API16-API19 especially
51
+                pic = BitmapFactory.decodeStream(k, null, options)
52
+                k.close()
53
+            } catch (e: IOException) {
54
+                e.printStackTrace()
55
+            } finally {
56
+                k?.close()
57
+            }
58
+            pic
59
+        } catch (e: Exception) {
60
+            null
61
+        }
62
+    }
63
+
64
+    override fun onPostExecute(bitmap: Bitmap?) {
65
+        try {
66
+            val d: Drawable = BitmapDrawable(context.resources, bitmap)
67
+            val size = Point()
68
+            (context as Activity).windowManager.defaultDisplay.getSize(size)
69
+            // Lets calculate the ratio according to the screen width in px
70
+            val multiplier: Int = size.x / bitmap!!.width
71
+            //Log.d(LOG_CAT, "multiplier: $multiplier")
72
+            levelListDrawable.addLevel(1, 1, d)
73
+            // Set bounds width  and height according to the bitmap resized size
74
+            levelListDrawable.setBounds(
75
+                0,
76
+                0,
77
+                bitmap.width * multiplier,
78
+                bitmap.height * multiplier
79
+            )
80
+            levelListDrawable.level = 1
81
+            t!!.text = t!!.text // invalidate() doesn't work correctly...
82
+        } catch (e: Exception) { /* Like a null bitmap, etc. */
83
+        }
84
+    }
85
+
86
+}
18 87
 
19 88
 class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context
20 89
     /*,
@@ -50,18 +119,32 @@ class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context
50 119
         val author = holder.itemView.findViewById<TextView>(R.id.news_author)
51 120
         val header = holder.itemView.findViewById<TextView>(R.id.news_header)
52 121
         val date = holder.itemView.findViewById<TextView>(R.id.news_date)
53
-        title.text = dataSet[position].title
54
-        title.setOnClickListener {
55
-            val i = Intent(Intent.ACTION_VIEW)
56
-            i.data = Uri.parse(dataSet[position].link)
57
-            c.startActivity(i)
58
-        }
59
-        text.text = HtmlCompat.fromHtml(dataSet[position].text, HtmlCompat.FROM_HTML_MODE_LEGACY)
122
+
123
+        val titleLink = "<a href=\"${dataSet[position].link}\">${dataSet[position].title}</>"
124
+        title.text = HtmlCompat.fromHtml(titleLink, HtmlCompat.FROM_HTML_MODE_LEGACY)
125
+        title.movementMethod = LinkMovementMethod.getInstance()
126
+
60 127
         header.text = HtmlCompat.fromHtml(dataSet[position].header, HtmlCompat.FROM_HTML_MODE_LEGACY).replace(Regex("\n"), " ")
61 128
         author.text = "| ${dataSet[position].author}"
62 129
         val sdf = SimpleDateFormat("dd MMM yyyy", Locale.getDefault())
63 130
         date.text = sdf.format(dataSet[position].date)
64 131
         TextViewCompat.setAutoSizeTextTypeWithDefaults(author, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
132
+
133
+        val spanned = HtmlCompat.fromHtml(
134
+            dataSet[position].text,
135
+            HtmlCompat.FROM_HTML_MODE_LEGACY,
136
+            ImageGetter { source ->
137
+                val d = LevelListDrawable()
138
+                val empty: Drawable? = ContextCompat.getDrawable(c, R.drawable.exo_icon_circular_play)
139
+
140
+                d.addLevel(0, 0, empty!!)
141
+                d.setBounds(0, 0, empty.intrinsicWidth, empty.intrinsicHeight)
142
+                ImageGetterAsyncTask(c, source, d).execute(text)
143
+                d
144
+            }, null
145
+        )
146
+        text.text = spanned
147
+        text.movementMethod = LinkMovementMethod.getInstance()
65 148
     }
66 149
 
67 150
     // Return the size of your dataset (invoked by the layout manager)