Browse Source

put day for ProgrammeDayFragment in a ViewModel - should fix crash

yattoz 5 years ago
parent
commit
81f53966f0

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

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

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

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
+}