yattoz пре 4 година
родитељ
комит
f5e0034302

+ 2 - 1
app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt Прегледај датотеку

74
         // Handle item selection
74
         // Handle item selection
75
         return when (item.itemId) {
75
         return when (item.itemId) {
76
             /*
76
             /*
77
+            // You can add more actions. This one is used in R/a/dio app Radio2.
77
             R.id.action_refresh -> {
78
             R.id.action_refresh -> {
78
                 PlayerStore.instance.queue.clear()
79
                 PlayerStore.instance.queue.clear()
79
                 PlayerStore.instance.lp.clear()
80
                 PlayerStore.instance.lp.clear()
89
             }
90
             }
90
             R.id.action_sleep -> {
91
             R.id.action_sleep -> {
91
                 val i = Intent(this, ParametersActivity::class.java)
92
                 val i = Intent(this, ParametersActivity::class.java)
92
-                i.putExtra("action", ActionOpenParam.SLEEP.name) // TODO change value with Actions.something
93
+                i.putExtra("action", ActionOpenParam.SLEEP.name)
93
                 startActivity(i)
94
                 startActivity(i)
94
                 true
95
                 true
95
             }
96
             }

+ 3 - 3
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsFragment.kt Прегледај датотеку

36
                     newsViewModel.root = inflater.inflate(R.layout.fragment_news, container, false)
36
                     newsViewModel.root = inflater.inflate(R.layout.fragment_news, container, false)
37
                     newsViewModel.webView = newsViewModel.root.findViewById(R.id.news_webview)
37
                     newsViewModel.webView = newsViewModel.root.findViewById(R.id.news_webview)
38
                     newsViewModel.webViewNews = WebViewNews(newsViewModel.webView as WebView)
38
                     newsViewModel.webViewNews = WebViewNews(newsViewModel.webView as WebView)
39
-                    newsViewModel.webViewNews!!.start()
39
+                    newsViewModel.webViewNews!!.start(getString(R.string.website_url))
40
                 } catch (e: Exception) {
40
                 } catch (e: Exception) {
41
                     newsViewModel.root = inflater.inflate(R.layout.fragment_error_webview, container, false)
41
                     newsViewModel.root = inflater.inflate(R.layout.fragment_error_webview, container, false)
42
                 }
42
                 }
71
 
71
 
72
         root.setOnRefreshListener {
72
         root.setOnRefreshListener {
73
 
73
 
74
-            newsViewModel.fetch(root, viewAdapter)
74
+            newsViewModel.fetch(root, viewAdapter, context!!)
75
 
75
 
76
         }
76
         }
77
 
77
 
82
         newsViewModel =
82
         newsViewModel =
83
             ViewModelProviders.of(this).get(NewsViewModel::class.java)
83
             ViewModelProviders.of(this).get(NewsViewModel::class.java)
84
 
84
 
85
-        newsViewModel.fetch()
85
+        newsViewModel.fetch(c = context!!)
86
         Log.d(tag, "news fetched onCreate")
86
         Log.d(tag, "news fetched onCreate")
87
         super.onCreate(savedInstanceState)
87
         super.onCreate(savedInstanceState)
88
     }
88
     }

+ 5 - 26
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsViewModel.kt Прегледај датотеку

1
 package fr.forum_thalie.tsumugi.ui.news
1
 package fr.forum_thalie.tsumugi.ui.news
2
 
2
 
3
+import android.content.Context
3
 import android.os.Build
4
 import android.os.Build
4
 import android.util.Log
5
 import android.util.Log
5
 import android.view.View
6
 import android.view.View
8
 import androidx.recyclerview.widget.RecyclerView
9
 import androidx.recyclerview.widget.RecyclerView
9
 import com.prof.rssparser.Parser
10
 import com.prof.rssparser.Parser
10
 import fr.forum_thalie.tsumugi.Async
11
 import fr.forum_thalie.tsumugi.Async
12
+import fr.forum_thalie.tsumugi.R
11
 import fr.forum_thalie.tsumugi.tag
13
 import fr.forum_thalie.tsumugi.tag
12
 import kotlinx.coroutines.CoroutineScope
14
 import kotlinx.coroutines.CoroutineScope
13
 import kotlinx.coroutines.Dispatchers
15
 import kotlinx.coroutines.Dispatchers
31
     val newsArray : ArrayList<News> = ArrayList()
33
     val newsArray : ArrayList<News> = ArrayList()
32
     var isWebViewLoaded = false
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
     private val viewModelJob = Job()
36
     private val viewModelJob = Job()
59
     private val coroutineScope = CoroutineScope(Dispatchers.Main + viewModelJob)
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
         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
42
         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
64
             return // the RSS Parser does not support API20- because of no TLS v1.2
43
             return // the RSS Parser does not support API20- because of no TLS v1.2
65
 
44
 
90
                 }
69
                 }
91
                 // The list contains all article's data. For example you can use it for your adapter.
70
                 // The list contains all article's data. For example you can use it for your adapter.
92
                 root?.isRefreshing = false
71
                 root?.isRefreshing = false
93
-
72
+                viewAdapter?.notifyDataSetChanged()
94
         }
73
         }
95
     }
74
     }
96
 }
75
 }

+ 2 - 2
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/WebViewNews.kt Прегледај датотеку

9
 class WebViewNews(private val webView: WebView) {
9
 class WebViewNews(private val webView: WebView) {
10
 
10
 
11
     @SuppressLint("SetJavaScriptEnabled")
11
     @SuppressLint("SetJavaScriptEnabled")
12
-    fun start() {
12
+    fun start(url: String) {
13
 
13
 
14
         val webSetting = this.webView.settings
14
         val webSetting = this.webView.settings
15
         webSetting.javaScriptEnabled = true
15
         webSetting.javaScriptEnabled = true
36
             }
36
             }
37
         }
37
         }
38
 
38
 
39
-        webView.loadUrl("https://tsumugi.forum-thalie.fr/")
39
+        webView.loadUrl(url)
40
     }
40
     }
41
 
41
 
42
 }
42
 }

+ 2 - 0
app/src/main/res/values/strings.xml Прегледај датотеку

41
     <string name="action_alarm">Alarm</string>
41
     <string name="action_alarm">Alarm</string>
42
     <string name="sleep_fade_out_text">One minute before the end of the timer, the volume will gradually decrease.</string>
42
     <string name="sleep_fade_out_text">One minute before the end of the timer, the volume will gradually decrease.</string>
43
     <string name="disable">Disable</string>
43
     <string name="disable">Disable</string>
44
+    <string name="website_url">https://tsumugi.forum-thalie.fr/</string>
45
+    <string name="rss_url">https://tsumugi.forum-thalie.fr/?feed=rss2</string>
44
 
46
 
45
 </resources>
47
 </resources>