Skip to content

Instantly share code, notes, and snippets.

@jhonatansabadi
Created March 17, 2022 15:58
Show Gist options
  • Save jhonatansabadi/3677b974b7bb0ae048e62f2872c62726 to your computer and use it in GitHub Desktop.
Save jhonatansabadi/3677b974b7bb0ae048e62f2872c62726 to your computer and use it in GitHub Desktop.
class GetUserByIdUseCase(
private val dispatcher: CoroutineDispatcher = Dispatcher.IO,
private val userRepository: UserRepository
) {
suspend fun invoke(id: String): User {
return withContext(dispatcher) {
userRepository.getUserById(id)
}
}
}
class MyViewModel(
private val getUserByIdUseCase: GetUserByIdUseCase
): Viewmodel {
private val _user: MutableLiveData<User>
val _user: LiveData<User>
fun setUser(id: String) {
viewModelScope.launch {
_user.value = getUserByIdUseCase(id)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment