Build Robust Android ViewModels with MVVM
Android ViewModel Agent for MVVM, lifecycle-aware components, and state management using Jetpack libraries. Simplifies modern Android development.
Why it matters
Implement and test Android ViewModels following MVVM patterns, ensuring lifecycle awareness, efficient state management, and adherence to modern Jetpack practices for resilient application architecture.
Outcomes
What it gets done
Implement lifecycle-aware ViewModels that survive configuration changes.
Manage UI state effectively using StateFlow and sealed classes.
Integrate ViewModels with dependency injection frameworks like Hilt.
Write unit tests for ViewModels using Coroutines and Mockito.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-android-viewmodel | bash Overview
Android ViewModel Agent
This agent specializes in Android ViewModel architecture, adhering to MVVM patterns and leveraging Jetpack libraries. It provides expertise in lifecycle-aware components, robust state management using StateFlow, and dependency injection patterns like Hilt and ViewModelProvider.Factory. It also demonstrates advanced techniques for one-time event handling and utilizing SavedStateHandle for process death resilience. Use this agent when developing modern Android applications that require clean separation of concerns, lifecycle-aware data handling, and efficient state management. It is particularly useful for projects employing Jetpack Compose or Kotlin Coroutines, and when implementing complex UI states or handling configuration changes gracefully.
What it does
Big Job: Build robust and maintainable Android applications following modern architectural patterns. Small Job: Implement lifecycle-aware state management and business logic separation in Android apps using Jetpack ViewModel.
class UserProfileViewModel(
private val userRepository: UserRepository,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
private val _uiState = MutableStateFlow(UserProfileUiState())
val uiState: StateFlow<UserProfileUiState> = _uiState.asStateFlow()
private val _events = Channel<UserProfileEvent>()
val events = _events.receiveAsFlow()
init {
loadUserProfile()
}
private fun loadUserProfile() {
viewModelScope.launch {
_uiState.value = _uiState.value.copy(isLoading = true)
try {
val user = userRepository.getCurrentUser()
_uiState.value = _uiState.value.copy(
user = user,
isLoading = false
)
} catch (e: Exception) {
_uiState.value = _uiState.value.copy(
isLoading = false,
error = e.message
)
}
}
}
}
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.