Browse Source

put day for ProgrammeDayFragment in a ViewModel - should fix crash

yattoz 4 years ago
parent
commit
81f53966f0

+ 8 - 4
app/src/main/java/fr/forum_thalie/tsumugi/ui/programme/ProgrammeDayFragment.kt View File

@@ -6,6 +6,7 @@ import android.view.View
6 6
 import android.view.ViewGroup
7 7
 import androidx.fragment.app.Fragment
8 8
 import androidx.lifecycle.Observer
9
+import androidx.lifecycle.ViewModelProviders
9 10
 import androidx.recyclerview.widget.LinearLayoutManager
10 11
 import androidx.recyclerview.widget.RecyclerView
11 12
 import fr.forum_thalie.tsumugi.R
@@ -20,21 +21,24 @@ class ProgrammeDayFragment : Fragment() {
20 21
     private lateinit var viewAdapter: RecyclerView.Adapter<*>
21 22
     private lateinit var viewManager: RecyclerView.LayoutManager
22 23
     private val programmeOfTheDay: ArrayList<Programme> = ArrayList()
23
-    private val day = arguments?.getString("day") ?: weekdays.first()
24
+
24 25
 
25 26
     override fun onCreateView(
26 27
         inflater: LayoutInflater, container: ViewGroup?,
27 28
         savedInstanceState: Bundle?
28 29
     ): View? {
29 30
         // Inflate the layout for this fragment
31
+        val viewModel: ProgrammeDayViewModel = ViewModelProviders.of(this).get(ProgrammeDayViewModel::class.java)
32
+        viewModel.day = arguments?.getString("day") ?: ""
33
+
30 34
         val root = inflater.inflate(R.layout.fragment_programme_day, container, false)
31 35
         Planning.instance.programmes.forEach {
32
-            if (it.isThisDay(day = weekdays.indexOf(day)))
36
+            if (it.isThisDay(day = weekdays.indexOf(viewModel.day)))
33 37
                 programmeOfTheDay.add(it)
34 38
         }
35 39
         viewManager = LinearLayoutManager(context)
36 40
         viewAdapter =
37
-            ProgrammeAdapter(programmeOfTheDay, day)
41
+            ProgrammeAdapter(programmeOfTheDay, viewModel.day)
38 42
         recyclerView = root.findViewById<RecyclerView>(R.id.programme_recycler).apply {
39 43
             // use this setting to improve performance if you know that changes
40 44
             // in content do not change the layout size of the RecyclerView
@@ -45,7 +49,7 @@ class ProgrammeDayFragment : Fragment() {
45 49
         Planning.instance.currentProgramme.observe(viewLifecycleOwner,  Observer<String> {
46 50
             programmeOfTheDay.clear()
47 51
             Planning.instance.programmes.forEach {
48
-                if (it.isThisDay(day = weekdays.indexOf(day)))
52
+                if (it.isThisDay(day = weekdays.indexOf(viewModel.day)))
49 53
                     programmeOfTheDay.add(it)
50 54
             }
51 55
             viewAdapter.notifyDataSetChanged()

+ 8 - 0
app/src/main/java/fr/forum_thalie/tsumugi/ui/programme/ProgrammeDayViewModel.kt View File

@@ -0,0 +1,8 @@
1
+package fr.forum_thalie.tsumugi.ui.programme
2
+
3
+import androidx.lifecycle.ViewModel
4
+
5
+class ProgrammeDayViewModel: ViewModel() {
6
+    var day: String = ""
7
+
8
+}