Day76 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which we will use the ViewModel to update the UI.
Unit 3: Navigation
Pathway 3: Advanced navigation app Examples
https://developer.android.com/courses/android-basics-kotlin/course
Use the ViewModel to update the UI
Today, we will use the shared view model we created to update the app’s UI. The main difference in the implementation of a shared view model is the way we access it from the UI controllers.
We will use the activity instance instead of the fragment instance, which means the view model can be shared across fragments. Each fragment could access the view model to check on some detail of the order or update some data in the view model.
Update StartFragment to use viewModel
To use the shared view model in StartFragment
we will initialize the OrderViewModel
using activityViewModels()
instead of viewModels()
delegate class.
viewModels()
gives us theViewModel
instance scoped to the current fragment. This will be different for different fragments.activityViewModels()
gives us theViewModel
instance scoped to the current activity. Therefore the instance will remain the same across multiple fragments in the same activity.
Use Kotlin property delegate
In Kotlin, each mutable (var
) property has default getter and setter functions automatically generated for it.
A delegate property is defined using the by
clause and a delegate class instance:
// Syntax for property delegation
var <property-name> : <property-type> by <delegate-class>()
- In
StartFragment
class, we will get a reference to the shared view model as a class variable. Use the byactivityViewModels()
Kotlin property delegate from thefragment-ktx
library.
private val sharedViewModel: OrderViewModel by activityViewModels()
- We may need these new imports:
import androidx.fragment.app.activityViewModels
import com.example.cupcake.model.OrderViewModel
- Going back to the
StartFragment
class, we can now use the view model. At the beginning of theorderCupcake()
method, call thesetQuantity()
method in the shared view model to update quantity, before navigating to the flavor fragment.
fun orderCupcake(quantity: Int) {
sharedViewModel.setQuantity(quantity)
findNavController().navigate(R.id.action_startFragment_to_flavorFragment)
}
- Within the
OrderViewModel
class, add the following method to check if the flavor for the order has been set or not. We will use this method in theStartFragment
class in a later step.
fun hasNoFlavorSet(): Boolean {
return _flavor.value.isNullOrEmpty()
}
- In
StartFragment
class, insideorderCupcake()
method, after setting the quantity, we will set the default flavor as Vanilla if no flavor is set, before navigating to the flavor fragment.
fun orderCupcake(quantity: Int) {
sharedViewModel.setQuantity(quantity)
if (sharedViewModel.hasNoFlavorSet()) {
sharedViewModel.setFlavor(getString(R.string.vanilla))
}
findNavController().navigate(R.id.action_startFragment_to_flavorFragment)
}
That is all for Day76 ✅
Thanks for reading, See you tomorrow!