Browse Source

tweaked NewsAdapter to avoid image blink at first fragment entry

yattoz 5 years ago
parent
commit
9f36fb865e

+ 2 - 1
app/src/main/java/fr/forum_thalie/tsumugi/Values.kt View File

8
 const val noConnectionValue = "—"
8
 const val noConnectionValue = "—"
9
 const val streamDownValue = "Tsumugi est HS !" // we don't want this value to be displaed in the "last played" screen.
9
 const val streamDownValue = "Tsumugi est HS !" // we don't want this value to be displaed in the "last played" screen.
10
 val weekdaysArray : Array<String> = arrayOf( "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche")
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
 // Below this line is only automatically programmed values. Unless your week does not start with Monday, you don't need to change this.
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
 val weekdays = ArrayList<String>().apply { weekdaysArray.forEach { add(it) } }
15
 val weekdays = ArrayList<String>().apply { weekdaysArray.forEach { add(it) } }

+ 27 - 15
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsAdapter.kt View File

21
 import androidx.core.widget.TextViewCompat
21
 import androidx.core.widget.TextViewCompat
22
 import androidx.recyclerview.widget.RecyclerView
22
 import androidx.recyclerview.widget.RecyclerView
23
 import fr.forum_thalie.tsumugi.R
23
 import fr.forum_thalie.tsumugi.R
24
+import fr.forum_thalie.tsumugi.newsDisplayDatePattern
24
 import java.io.IOException
25
 import java.io.IOException
25
 import java.io.InputStream
26
 import java.io.InputStream
26
 import java.net.URL
27
 import java.net.URL
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
     context: Context,
90
     context: Context,
90
     resource: Int,
91
     resource: Int,
125
 
126
 
126
         header.text = HtmlCompat.fromHtml(dataSet[position].header, HtmlCompat.FROM_HTML_MODE_LEGACY).replace(Regex("\n"), " ")
127
         header.text = HtmlCompat.fromHtml(dataSet[position].header, HtmlCompat.FROM_HTML_MODE_LEGACY).replace(Regex("\n"), " ")
127
         author.text = "| ${dataSet[position].author}"
128
         author.text = "| ${dataSet[position].author}"
128
-        val sdf = SimpleDateFormat("dd MMM yyyy", Locale.getDefault())
129
+        val sdf = SimpleDateFormat(newsDisplayDatePattern, Locale.getDefault())
129
         date.text = sdf.format(dataSet[position].date)
130
         date.text = sdf.format(dataSet[position].date)
130
         TextViewCompat.setAutoSizeTextTypeWithDefaults(author, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
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
         text.text = spanned
157
         text.text = spanned
146
         text.movementMethod = LinkMovementMethod.getInstance()
158
         text.movementMethod = LinkMovementMethod.getInstance()
147
     }
159
     }

+ 2 - 2
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsFragment.kt View File

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

+ 6 - 7
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsViewModel.kt View File

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