|
@@ -11,6 +11,7 @@ import androidx.fragment.app.Fragment
|
11
|
11
|
import androidx.lifecycle.ViewModelProviders
|
12
|
12
|
import androidx.recyclerview.widget.LinearLayoutManager
|
13
|
13
|
import androidx.recyclerview.widget.RecyclerView
|
|
14
|
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
14
|
15
|
import fr.forum_thalie.tsumugi.R
|
15
|
16
|
|
16
|
17
|
class NewsFragment : Fragment() {
|
|
@@ -47,17 +48,18 @@ class NewsFragment : Fragment() {
|
47
|
48
|
Log.d(tag, "webview already created!?")
|
48
|
49
|
}
|
49
|
50
|
|
|
51
|
+ newsViewModel.root.addOnLayoutChangeListener(orientationLayoutListener)
|
50
|
52
|
return newsViewModel.root
|
51
|
53
|
}
|
52
|
54
|
|
53
|
55
|
newsViewModel =
|
54
|
56
|
ViewModelProviders.of(this).get(NewsViewModel::class.java)
|
55
|
57
|
|
56
|
|
- val root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|
58
|
+ newsViewModel.root = inflater.inflate(R.layout.fragment_news, container, false) as SwipeRefreshLayout
|
57
|
59
|
|
58
|
60
|
viewManager = LinearLayoutManager(context)
|
59
|
61
|
viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!)
|
60
|
|
- recyclerView = root.findViewById<RecyclerView>(R.id.news_recycler).apply {
|
|
62
|
+ recyclerView = newsViewModel.root.findViewById<RecyclerView>(R.id.news_recycler).apply {
|
61
|
63
|
// use this setting to improve performance if you know that changes
|
62
|
64
|
// in content do not change the layout size of the RecyclerView
|
63
|
65
|
setHasFixedSize(true)
|
|
@@ -69,13 +71,28 @@ class NewsFragment : Fragment() {
|
69
|
71
|
adapter = viewAdapter
|
70
|
72
|
}
|
71
|
73
|
|
72
|
|
- root.setOnRefreshListener {
|
|
74
|
+ (newsViewModel.root as SwipeRefreshLayout).setOnRefreshListener {
|
|
75
|
+ newsViewModel.fetch((newsViewModel.root as SwipeRefreshLayout), viewAdapter, context!!)
|
|
76
|
+ }
|
73
|
77
|
|
74
|
|
- newsViewModel.fetch(root, viewAdapter, context!!)
|
|
78
|
+ newsViewModel.root.addOnLayoutChangeListener(orientationLayoutListener)
|
|
79
|
+ return newsViewModel.root
|
|
80
|
+ }
|
75
|
81
|
|
76
|
|
- }
|
|
82
|
+ private val orientationLayoutListener : View.OnLayoutChangeListener = View.OnLayoutChangeListener { _: View, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int, _: Int ->
|
77
|
83
|
|
78
|
|
- return root
|
|
84
|
+ val viewHeight = (newsViewModel.root.rootView?.height ?: 1)
|
|
85
|
+ val viewWidth = (newsViewModel.root.rootView?.width ?: 1)
|
|
86
|
+
|
|
87
|
+ val newRatio = if (viewWidth > 0)
|
|
88
|
+ (viewHeight*100)/viewWidth
|
|
89
|
+ else
|
|
90
|
+ 100
|
|
91
|
+
|
|
92
|
+ if (newsViewModel.screenRatio != newRatio) {
|
|
93
|
+ newsViewModel.fetch((newsViewModel.root as SwipeRefreshLayout), viewAdapter, context!!)
|
|
94
|
+ newsViewModel.screenRatio = newRatio
|
|
95
|
+ }
|
79
|
96
|
}
|
80
|
97
|
|
81
|
98
|
override fun onCreate(savedInstanceState: Bundle?) {
|