Skip to content

Instantly share code, notes, and snippets.

@cesarferreira
Last active May 19, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarferreira/84961855d44aeb6bbbede4a614d620a1 to your computer and use it in GitHub Desktop.
Save cesarferreira/84961855d44aeb6bbbede4a614d620a1 to your computer and use it in GitHub Desktop.
class FriendsViewModel @Inject constructor(
private val getFriendsUseCase: GetFriendsUseCase
) : BaseViewModel() {
sealed class FriendsState {
object Loading : FriendsState()
object Empty : FriendsState()
data class Success(val users: List<UserUiModel>) : FriendsState()
}
val state = MutableLiveData<FriendsState>().apply {
this.value = FriendsState.Loading
}
fun loadData() {
val params = GetFriendsUseCase.Params(50)
getFriendsUseCase.invoke(viewModelScope, params) { it.either(::handleFailure, ::handleSuccess) }
}
private fun handleSuccess(list: List<User>) {
when {
list.isEmpty() -> state.value = FriendsState.Empty
list.isNotEmpty() -> state.value = FriendsState.Success(mapToPresentation(list))
}
}
private fun mapToPresentation(friends: List<User>): List<UserUiModel> {
return friends.map { UserUiModel(it.name, it.pictureUrl) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment