ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Passive Views: keep your UI code simple and stupid

Rygel Louv
ProAndroidDev
Published in
7 min readApr 29, 2022

Minimal image by Sarah Dorweiler on Unsplash

Passive View

UI State representation

class MyBadViewModel: ViewModel() {
val isLoading: MutableLiveData<Boolean> = MutableLiveData(true)
val isPremium: MutableLiveData<Boolean> = MutableLiveData(false)
val isError: MutableLiveData<Boolean> = MutableLiveData(false)
val userMessages: MutableLiveData<List<Message>> = MutableLiveData(listOf())
...
}
data class NewsUiState(
val isLoading: Boolean = true,
val isPremium: Boolean = false,
val isError: Boolean = false,
val userMessages: List<Message> = listOf()
)
class MyGoodViewModel: ViewModel() {
private val _state: MutableStateFlow<NewsUiState> = MutableStateFlow(NewsUiState())
val state: StateFlow<QuestionsUIState> = _state.asStateFlow()
...
}

UI models: avoid “Backend driven development”

This might not be very representative of the reality
binding.amount.setTextColor(state.transaction.color)
Text(text= state.transaction.title, color = state.transaction.color)

Make it Parcelable if needed

Use UI mappers to handle the transformations

UI Mappers: how to build them?

Back to the UI state

data class TransactionsUiState(
val isLoading: Boolean = true,
val isError: Boolean = false,
val transactions: List<TransactionUI>? = null
)

Conclusion

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Rygel Louv

Software Engineer | Kotlin, Android, Python

Responses (1)

Write a response