瀏覽代碼

tweaked NewsAdapter to avoid image blink at first fragment entry

yattoz 4 年之前
父節點
當前提交
9f36fb865e

+ 2 - 1
app/src/main/java/fr/forum_thalie/tsumugi/Values.kt 查看文件

@@ -8,7 +8,8 @@ const val tag = "fr.forum_thalie.tsumugi"
8 8
 const val noConnectionValue = "—"
9 9
 const val streamDownValue = "Tsumugi est HS !" // we don't want this value to be displaed in the "last played" screen.
10 10
 val weekdaysArray : Array<String> = arrayOf( "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche")
11
-
11
+const val newsDateTimePattern = "EEE, d MMM yyyy HH:mm:ss Z"
12
+const val newsDisplayDatePattern = "dd MMM yyyy"
12 13
 // Below this line is only automatically programmed values. Unless your week does not start with Monday, you don't need to change this.
13 14
 
14 15
 val weekdays = ArrayList<String>().apply { weekdaysArray.forEach { add(it) } }

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

@@ -21,6 +21,7 @@ import androidx.core.text.HtmlCompat
21 21
 import androidx.core.widget.TextViewCompat
22 22
 import androidx.recyclerview.widget.RecyclerView
23 23
 import fr.forum_thalie.tsumugi.R
24
+import fr.forum_thalie.tsumugi.newsDisplayDatePattern
24 25
 import java.io.IOException
25 26
 import java.io.InputStream
26 27
 import java.net.URL
@@ -84,7 +85,7 @@ class ImageGetterAsyncTask(
84 85
 
85 86
 }
86 87
 
87
-class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context
88
+class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context, private val vm: NewsViewModel
88 89
     /*,
89 90
     context: Context,
90 91
     resource: Int,
@@ -125,23 +126,34 @@ class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context
125 126
 
126 127
         header.text = HtmlCompat.fromHtml(dataSet[position].header, HtmlCompat.FROM_HTML_MODE_LEGACY).replace(Regex("\n"), " ")
127 128
         author.text = "| ${dataSet[position].author}"
128
-        val sdf = SimpleDateFormat("dd MMM yyyy", Locale.getDefault())
129
+        val sdf = SimpleDateFormat(newsDisplayDatePattern, Locale.getDefault())
129 130
         date.text = sdf.format(dataSet[position].date)
130 131
         TextViewCompat.setAutoSizeTextTypeWithDefaults(author, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
131 132
 
132
-        val spanned = HtmlCompat.fromHtml(
133
-            dataSet[position].text,
134
-            HtmlCompat.FROM_HTML_MODE_LEGACY,
135
-            ImageGetter { source ->
136
-                val d = LevelListDrawable()
137
-                val empty: Drawable? = ContextCompat.getDrawable(c, R.drawable.exo_icon_play)
138
-
139
-                d.addLevel(0, 0, empty!!)
140
-                d.setBounds(0, 0, empty.intrinsicWidth, empty.intrinsicHeight)
141
-                ImageGetterAsyncTask(c, source, d).execute(text)
142
-                d
143
-            }, null
144
-        )
133
+        val spanned = // the trick is to avoid loading images when the adapter is called with preloading.
134
+            if (vm.isPreLoadingNews) {
135
+                HtmlCompat.fromHtml(
136
+                    dataSet[position].text,
137
+                    HtmlCompat.FROM_HTML_MODE_LEGACY)
138
+            } else {
139
+                HtmlCompat.fromHtml(
140
+                    dataSet[position].text,
141
+                    HtmlCompat.FROM_HTML_MODE_LEGACY,
142
+                    ImageGetter { source ->
143
+                        val d = LevelListDrawable()
144
+                        /*
145
+                        val empty: Drawable? = ContextCompat.getDrawable(c, R.drawable.exo_icon_play)
146
+                        d.addLevel(0, 0, empty!!)
147
+                        d.setBounds(0, 0, empty.intrinsicWidth, empty.intrinsicHeight)
148
+                         */
149
+                        ImageGetterAsyncTask(c, source, d).execute(text)
150
+                        d
151
+                    }, null
152
+                )
153
+            }
154
+
155
+
156
+
145 157
         text.text = spanned
146 158
         text.movementMethod = LinkMovementMethod.getInstance()
147 159
     }

+ 2 - 2
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsFragment.kt 查看文件

@@ -58,7 +58,7 @@ class NewsFragment : Fragment() {
58 58
         newsViewModel.root = inflater.inflate(R.layout.fragment_news, container, false) as SwipeRefreshLayout
59 59
 
60 60
         viewManager = LinearLayoutManager(context)
61
-        viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!)
61
+        viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!, newsViewModel)
62 62
         recyclerView = newsViewModel.root.findViewById<RecyclerView>(R.id.news_recycler).apply {
63 63
             // use this setting to improve performance if you know that changes
64 64
             // in content do not change the layout size of the RecyclerView
@@ -100,7 +100,7 @@ class NewsFragment : Fragment() {
100 100
         newsViewModel =
101 101
             ViewModelProviders.of(this).get(NewsViewModel::class.java)
102 102
 
103
-        newsViewModel.fetch(c = context!!)
103
+        newsViewModel.fetch(c = context!!, isPreloading = true)
104 104
         Log.d(tag, "news fetched onCreate")
105 105
         super.onCreate(savedInstanceState)
106 106
     }

+ 6 - 7
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsViewModel.kt 查看文件

@@ -8,16 +8,13 @@ import android.webkit.WebView
8 8
 import androidx.lifecycle.ViewModel
9 9
 import androidx.recyclerview.widget.RecyclerView
10 10
 import com.prof.rssparser.Parser
11
-import fr.forum_thalie.tsumugi.Async
12 11
 import fr.forum_thalie.tsumugi.R
12
+import fr.forum_thalie.tsumugi.newsDateTimePattern
13 13
 import fr.forum_thalie.tsumugi.tag
14 14
 import kotlinx.coroutines.CoroutineScope
15 15
 import kotlinx.coroutines.Dispatchers
16 16
 import kotlinx.coroutines.Job
17 17
 import kotlinx.coroutines.launch
18
-import org.json.JSONArray
19
-import org.json.JSONObject
20
-import java.net.URL
21 18
 import java.text.SimpleDateFormat
22 19
 import java.util.*
23 20
 import kotlin.collections.ArrayList
@@ -30,6 +27,7 @@ class NewsViewModel : ViewModel() {
30 27
     lateinit var root: View
31 28
     var webView: WebView? = null
32 29
     var webViewNews: WebViewNews? = null
30
+    var isPreLoadingNews = false
33 31
 
34 32
     val newsArray : ArrayList<News> = ArrayList()
35 33
     var isWebViewLoaded = false
@@ -37,7 +35,7 @@ class NewsViewModel : ViewModel() {
37 35
     private val viewModelJob = Job()
38 36
     private val coroutineScope = CoroutineScope(Dispatchers.Main + viewModelJob)
39 37
 
40
-    fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null, c: Context)
38
+    fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null, c: Context, isPreloading: Boolean = false)
41 39
     {
42 40
         val urlToScrape = c.getString(R.string.rss_url)
43 41
         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
@@ -60,9 +58,9 @@ class NewsViewModel : ViewModel() {
60 58
                     news.text = item.content ?: ""
61 59
                     news.header = item.description ?: ""
62 60
 
63
-                    val formatter6 = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH)
61
+                    val formatter6 = SimpleDateFormat(newsDateTimePattern, Locale.ENGLISH)
64 62
                     val dateString = item.pubDate.toString()
65
-                    Log.d(tag, "$news --- ${dateString}")
63
+                    Log.d(tag, "$news --- $dateString")
66 64
 
67 65
                     news.date = formatter6.parse(dateString) ?: Date(0)
68 66
 
@@ -70,6 +68,7 @@ class NewsViewModel : ViewModel() {
70 68
                 }
71 69
                 // The list contains all article's data. For example you can use it for your adapter.
72 70
                 root?.isRefreshing = false
71
+                isPreLoadingNews = isPreloading
73 72
                 viewAdapter?.notifyDataSetChanged()
74 73
             }catch (e: Exception)
75 74
             {