|
@@ -3,21 +3,28 @@ package fr.forum_thalie.tsumugi.ui.news
|
3
|
3
|
import android.util.Log
|
4
|
4
|
import androidx.lifecycle.ViewModel
|
5
|
5
|
import androidx.recyclerview.widget.RecyclerView
|
|
6
|
+import com.prof.rssparser.Parser
|
6
|
7
|
import fr.forum_thalie.tsumugi.Async
|
7
|
8
|
import fr.forum_thalie.tsumugi.tag
|
|
9
|
+import kotlinx.coroutines.CoroutineScope
|
|
10
|
+import kotlinx.coroutines.Dispatchers
|
|
11
|
+import kotlinx.coroutines.Job
|
|
12
|
+import kotlinx.coroutines.launch
|
8
|
13
|
import org.json.JSONArray
|
9
|
14
|
import org.json.JSONObject
|
10
|
15
|
import java.net.URL
|
11
|
16
|
import java.text.SimpleDateFormat
|
12
|
17
|
import java.util.*
|
13
|
18
|
import kotlin.collections.ArrayList
|
|
19
|
+import kotlin.math.max
|
|
20
|
+import kotlin.math.min
|
14
|
21
|
|
15
|
22
|
|
16
|
23
|
class NewsViewModel : ViewModel() {
|
17
|
24
|
|
18
|
25
|
val newsArray : ArrayList<News> = ArrayList()
|
19
|
26
|
|
20
|
|
- private val urlToScrape = "https://r-a-d.io/api/news"
|
|
27
|
+ private val urlToScrape = "https://tsumugi.forum-thalie.fr/?feed=rss2"
|
21
|
28
|
|
22
|
29
|
private val scrape : (Any?) -> Unit =
|
23
|
30
|
{
|
|
@@ -41,12 +48,42 @@ class NewsViewModel : ViewModel() {
|
41
|
48
|
}
|
42
|
49
|
}
|
43
|
50
|
|
|
51
|
+ private val viewModelJob = Job()
|
|
52
|
+ private val coroutineScope = CoroutineScope(Dispatchers.Main + viewModelJob)
|
|
53
|
+
|
44
|
54
|
fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null)
|
45
|
55
|
{
|
46
|
|
- val post : (parameter: Any?) -> Unit = {
|
47
|
|
- root?.isRefreshing = false
|
48
|
|
- viewAdapter?.notifyDataSetChanged()
|
|
56
|
+ val maxNumberOfArticles = 5
|
|
57
|
+ coroutineScope.launch(Dispatchers.Main) {
|
|
58
|
+ Log.d(tag, "launching coroutine")
|
|
59
|
+ try {
|
|
60
|
+ val parser = Parser()
|
|
61
|
+ val articleList = parser.getArticles(urlToScrape)
|
|
62
|
+ newsArray.clear()
|
|
63
|
+ for (i in 0 until min(articleList.size, maxNumberOfArticles))
|
|
64
|
+ {
|
|
65
|
+ val item = articleList[i]
|
|
66
|
+ Log.d(tag, "i = $i / ${articleList.size}")
|
|
67
|
+ val news = News()
|
|
68
|
+ news.title = item.title ?: ""
|
|
69
|
+ news.link = item.link ?: urlToScrape
|
|
70
|
+ news.author = item.author ?: ""
|
|
71
|
+ news.text = item.content ?: ""
|
|
72
|
+ news.header = item.description ?: ""
|
|
73
|
+
|
|
74
|
+ //val formatter6 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
|
75
|
+
|
|
76
|
+ //news.date = formatter6.parse(item.pubDate ?: Date().toString()) ?: Date()
|
|
77
|
+
|
|
78
|
+ Log.d(tag, "$news - ${item.pubDate}")
|
|
79
|
+ newsArray.add(news)
|
|
80
|
+ }
|
|
81
|
+ // The list contains all article's data. For example you can use it for your adapter.
|
|
82
|
+ root?.isRefreshing = false
|
|
83
|
+ viewAdapter?.notifyDataSetChanged()
|
|
84
|
+ } catch (e: Exception) {
|
|
85
|
+ // Handle the exception
|
|
86
|
+ }
|
49
|
87
|
}
|
50
|
|
- Async(scrape, post)
|
51
|
88
|
}
|
52
|
89
|
}
|