Skip to content

Instantly share code, notes, and snippets.

@pranay1494
Created May 2, 2020 17:22
Show Gist options
  • Save pranay1494/0938c9c8fb92c9e77a3440b81a0ae5e7 to your computer and use it in GitHub Desktop.
Save pranay1494/0938c9c8fb92c9e77a3440b81a0ae5e7 to your computer and use it in GitHub Desktop.
open class UserViewModel @Inject constructor(private val repository: UserRepository, @Named("default") private val defaultDispatcher: CoroutineDispatcher) : BaseViewModel() {
private val userData = MutableLiveData<DisplayData>()
fun getData(): LiveData<DisplayData> = userData
fun fetchUserData(username: String) = viewModelScope.launch(defaultDispatcher) {
viewStatus.postValue(ViewStatus.LOADING)
try {
val user = repository.fetchRepository("pranay1494")
userData.postValue(getUserDisplayData(user))
} catch (e: Exception) {
handleError(e)
} finally {
viewStatus.postValue(ViewStatus.SUCCESS)
}
}
private fun getUserDisplayData(user: User): DisplayData = DisplayData(name = user.name?:"", avatarUrl = user.avatarUrl?:"")
data class DisplayData(val name :String = "",val avatarUrl:String = "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment