Menu

Post image 1
Post image 2
Post image 3
Post image 4
1 / 4
0

Stop Fighting Your State. Reduce And Conquer It.

DEV Community·numq·19 days ago
#mPDdEmYR
#kotlin#mvi#mvvm#state#fullscreen#command
Reading 0:00
15s threshold

MVVM seemed simple. Then you added ten MutableStateFlow properties to your ViewModel. MVI promised purity. Then you wrote a middleware for side effects. There’s a better way. The Problem With MVVM A typical ViewModel looks like this: class ProfileViewModel : ViewModel () { val name = MutableStateFlow ( "" ) val email = MutableStateFlow ( "" ) val isLoading = MutableStateFlow ( false ) val error = MutableStateFlow < String ?>( null ) fun updateProfile ( name : String ) { viewModelScope . launch { isLoading . value = true try { profileService . update ( name ) this @ProfileViewModel . name . value = name } catch ( e : Exception ) { error . value = e . message } finally { isLoading . value = false } } } } Enter fullscreen mode Exit fullscreen mode Five mutable properties. Loading state scattered across three places. Error handling duplicated in every method. And the ViewModel doesn’t own its side effects — viewModelScope does.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More