Created
May 2, 2020 17:22
-
-
Save pranay1494/0938c9c8fb92c9e77a3440b81a0ae5e7 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
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