Complete the code to declare a ViewModel class in Kotlin.
class MainViewModel : [1]() { // ViewModel logic here }
The ViewModel class helps keep UI data separate from UI controllers, which is key for scalable architecture.
Complete the code to observe LiveData in an Activity.
viewModel.data.observe([1]) { value -> // update UI with value }
In an Activity, you pass 'this' as the LifecycleOwner to observe LiveData.
Fix the error in the repository pattern code by completing the interface declaration.
interface [1] {
fun getUser(id: String): User
}The repository interface is commonly named UserRepository to abstract data operations.
Fill both blanks to complete the ViewModel factory code for dependency injection.
class [1](private val repository: [2]) : ViewModel() { // ViewModel code }
The ViewModel class is named MainViewModel and it takes UserRepository as a dependency.
Fill all three blanks to complete the Kotlin code for a simple MVVM data flow.
class [1](private val [2]: [3]) : ViewModel() { val data = MutableLiveData<String>() }
The ViewModel is UserViewModel, it has a repository property of type UserRepository injected.