|
@@ -1,5 +1,6 @@
|
1
|
1
|
package fr.forum_thalie.tsumugi.ui.news
|
2
|
2
|
|
|
3
|
+import android.content.Context
|
3
|
4
|
import android.os.Build
|
4
|
5
|
import android.util.Log
|
5
|
6
|
import android.view.View
|
|
@@ -8,6 +9,7 @@ import androidx.lifecycle.ViewModel
|
8
|
9
|
import androidx.recyclerview.widget.RecyclerView
|
9
|
10
|
import com.prof.rssparser.Parser
|
10
|
11
|
import fr.forum_thalie.tsumugi.Async
|
|
12
|
+import fr.forum_thalie.tsumugi.R
|
11
|
13
|
import fr.forum_thalie.tsumugi.tag
|
12
|
14
|
import kotlinx.coroutines.CoroutineScope
|
13
|
15
|
import kotlinx.coroutines.Dispatchers
|
|
@@ -31,35 +33,12 @@ class NewsViewModel : ViewModel() {
|
31
|
33
|
val newsArray : ArrayList<News> = ArrayList()
|
32
|
34
|
var isWebViewLoaded = false
|
33
|
35
|
|
34
|
|
- private val urlToScrape = "https://tsumugi.forum-thalie.fr/?feed=rss2"
|
35
|
|
-
|
36
|
|
- private val scrape : (Any?) -> Unit =
|
37
|
|
- {
|
38
|
|
- val t = URL(urlToScrape).readText()
|
39
|
|
- val result = JSONArray(t)
|
40
|
|
- newsArray.clear()
|
41
|
|
- for (n in 0 until result.length())
|
42
|
|
- {
|
43
|
|
- val news = News()
|
44
|
|
- news.title = (result[n] as JSONObject).getString("title")
|
45
|
|
- news.author = (result[n] as JSONObject).getJSONObject("author").getString("user")
|
46
|
|
- news.text = (result[n] as JSONObject).getString("text")
|
47
|
|
- news.header = (result[n] as JSONObject).getString("header")
|
48
|
|
-
|
49
|
|
- val formatter6 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
50
|
|
-
|
51
|
|
- news.date = formatter6.parse((result[n] as JSONObject).getString("updated_at")) ?: Date()
|
52
|
|
-
|
53
|
|
- Log.d(tag, "$news")
|
54
|
|
- newsArray.add(news)
|
55
|
|
- }
|
56
|
|
- }
|
57
|
|
-
|
58
|
36
|
private val viewModelJob = Job()
|
59
|
37
|
private val coroutineScope = CoroutineScope(Dispatchers.Main + viewModelJob)
|
60
|
38
|
|
61
|
|
- fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null)
|
|
39
|
+ fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null, c: Context)
|
62
|
40
|
{
|
|
41
|
+ val urlToScrape = c.getString(R.string.rss_url)
|
63
|
42
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
|
64
|
43
|
return // the RSS Parser does not support API20- because of no TLS v1.2
|
65
|
44
|
|
|
@@ -90,7 +69,7 @@ class NewsViewModel : ViewModel() {
|
90
|
69
|
}
|
91
|
70
|
// The list contains all article's data. For example you can use it for your adapter.
|
92
|
71
|
root?.isRefreshing = false
|
93
|
|
-
|
|
72
|
+ viewAdapter?.notifyDataSetChanged()
|
94
|
73
|
}
|
95
|
74
|
}
|
96
|
75
|
}
|