Day76 of #100DaysOfCode

Kushagra Kesav
2 min readApr 23, 2022

--

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 the ViewModel instance scoped to the current fragment. This will be different for different fragments.
  • activityViewModels() gives us the ViewModel 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 by activityViewModels() Kotlin property delegate from the fragment-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 the orderCupcake() method, call the setQuantity()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 the StartFragment class in a later step.
fun hasNoFlavorSet(): Boolean {
return _flavor.value.isNullOrEmpty()
}
  • In StartFragment class, inside orderCupcake() 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!

--

--

Kushagra Kesav
Kushagra Kesav

Written by Kushagra Kesav

Developer Relations | Communities | Software Engineering | https://www.linkedin.com/in/kushagrakesav

No responses yet