Last active
May 19, 2019 14:47
-
-
Save cesarferreira/84961855d44aeb6bbbede4a614d620a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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