MVVM on Android

Sep 1 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 1: MVVM on Android

06. Build the ViewModel

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Explore the View Next episode: 07. Test the ViewModel

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 06. Build the ViewModel

The student materials have been reviewed and are updated as of July 2022.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

With our Model and View in place, we’ll now turn to creating a ViewModel for the Add Creature screen named CreatureViewModel.

class CreatureViewModel : ViewModel() {
 
  

}
class CreatureViewModel(private val generator: CreatureGenerator = CreatureGenerator()) : ViewModel() {
 
  

}
  private val creatureLiveData = MutableLiveData<Creature>()
  
  fun getCreatureLiveData(): LiveData<Creature> = creatureLiveData
  var name = ""
  var intelligence = 0
  var strength = 0
  var endurance = 0
  var drawable = 0
lateinit var creature: Creature
  fun updateCreature() {
    val attributes = CreatureAttributes(intelligence, strength, endurance)
    creature = generator.generateCreature(attributes, name, drawable)
    creatureLiveData.postValue(creature)
  }
  fun attributeSelected(attributeType: AttributeType, position: Int) {
    when (attributeType) {
      AttributeType.INTELLIGENCE ->
      AttributeType.STRENGTH ->
      AttributeType.ENDURANCE ->
    }
    updateCreature()
  }
  fun drawableSelected(drawable: Int) {
    this.drawable = drawable
    updateCreature()
  }
private lateinit var viewModel: CreatureViewModel
viewModel = ViewModelProviders.of(this).get(CreatureViewModel::class.java)
viewModel.attributeSelected(AttributeType.INTELLIGENCE, position)
...
viewModel.attributeSelected(AttributeType.STRENGTH, position)
...
viewModel.attributeSelected(AttributeType.ENDURANCE, position)
viewModel.name = s.toString()
viewModel.drawableSelected(avatar.drawable)
if (viewModel.drawable != 0) hideTapLabel()
  private fun configureLiveDataObservers() {
    viewModel.getCreatureLiveData().observe(this, Observer { creature ->

    })
  }
    
      hitPoints.text = creature.hitPoints.toString()
        avatarImageView.setImageResource(creature.drawable)
        nameEditText.setText(creature.name)
     
creature?.let {
    ...
}
configureLiveDataObservers()