Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 97bc0faf authored by 62160052's avatar 62160052
Browse files

Update StartFragment to use view model

parent 265f23b8
No related branches found
No related tags found
No related merge requests found
...@@ -15,12 +15,14 @@ ...@@ -15,12 +15,14 @@
*/ */
package com.example.cupcake package com.example.cupcake
import OrderViewModel
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import com.example.cupcake.databinding.FragmentStartBinding import com.example.cupcake.databinding.FragmentStartBinding
...@@ -33,6 +35,7 @@ class StartFragment : Fragment() { ...@@ -33,6 +35,7 @@ class StartFragment : Fragment() {
// This property is non-null between the onCreateView() and onDestroyView() lifecycle callbacks, // This property is non-null between the onCreateView() and onDestroyView() lifecycle callbacks,
// when the view hierarchy is attached to the fragment. // when the view hierarchy is attached to the fragment.
private var binding: FragmentStartBinding? = null private var binding: FragmentStartBinding? = null
private val sharedViewModel: OrderViewModel by activityViewModels()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
...@@ -58,9 +61,14 @@ class StartFragment : Fragment() { ...@@ -58,9 +61,14 @@ class StartFragment : Fragment() {
* Start an order with the desired quantity of cupcakes and navigate to the next screen. * Start an order with the desired quantity of cupcakes and navigate to the next screen.
*/ */
fun orderCupcake(quantity: Int) { fun orderCupcake(quantity: Int) {
sharedViewModel.setQuantity(quantity)
if (sharedViewModel.hasNoFlavorSet()) {
sharedViewModel.setFlavor(getString(R.string.vanilla))
}
findNavController().navigate(R.id.action_startFragment_to_flavorFragment) findNavController().navigate(R.id.action_startFragment_to_flavorFragment)
} }
/** /**
* This fragment lifecycle method is called when the view hierarchy associated with the fragment * This fragment lifecycle method is called when the view hierarchy associated with the fragment
* is being removed. As a result, clear out the binding object. * is being removed. As a result, clear out the binding object.
......
...@@ -26,4 +26,8 @@ class OrderViewModel : ViewModel() { ...@@ -26,4 +26,8 @@ class OrderViewModel : ViewModel() {
fun setDate(pickupDate: String) { fun setDate(pickupDate: String) {
_date.value = pickupDate _date.value = pickupDate
} }
fun hasNoFlavorSet(): Boolean {
return _flavor.value.isNullOrEmpty()
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment