-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ViewModel Scope #35
ViewModel Scope #35
Conversation
viewModelScope is added
val backgroundJob = scope.async { run(params) } | ||
scope.launch { onResult(backgroundJob.await()) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to use async{} here? Wouldn't
scope.launch {
val result = run(params)
onResult(result)
}
suffice?
_loginInProgress.value = true | ||
|
||
val username = username.value ?: return | ||
val password = password.value ?: return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 31 should be below line 33 & 34 since one of them could return from the function and _loginInProgress.value would hang with true
nowPlayingMoviesResult.either(::handleFailure, ::postNowPlayingMovieList) | ||
} | ||
fetchNowPlayingMoviesUseCase.invoke(viewModelScope, UseCase.None) { | ||
it.either(::handleFailure, ::postNowPlayingMovieList) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postNowPlayingMovieList() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postNowPlayingMovieList()
popularMoviesResult.either(::handleFailure, ::postPopularMovieList) | ||
} | ||
fetchPopularMoviesUseCase.invoke(viewModelScope, UseCase.None) { | ||
it.either(::handleFailure, ::postPopularMovieList) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postPopularMovieList() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postPopularMovieList()
movieDetailResult.either(::handleFailure, ::postMovieDetail) | ||
} | ||
fetchMovieDetailUseCase.invoke(viewModelScope, FetchMovieDetailUseCase.Params(id)) { | ||
it.either(::handleFailure, ::postMovieDetail) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postMovieDetail() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postMovieDetail()
movieCreditsResult.either(::handleFailure, ::postMovieCredits) | ||
} | ||
fetchMovieCreditsUseCase.invoke(viewModelScope, FetchMovieCreditsUseCase.Params(id)) { | ||
it.either(::handleFailure, ::postMovieCredits) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postMovieCredits() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postMovieCredits()
viewModelScope, | ||
FetchPersonDetailsUseCase.Params(personId) | ||
) { | ||
it.either(::handleFailure, ::postPersonDetails) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postPersonDetails() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postPersonDetails()
loginStateResult.either(::handleFailure, ::handleLoginStateSuccess) | ||
} | ||
getLoginStateUseCase.invoke(viewModelScope, UseCase.None) { | ||
it.either(::handleFailure, ::handleLoginStateSuccess) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleLoginStateSuccess() calls postLoginState() inside. postLoginState() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postLoginState(). Also, handleLoginStateSuccess() calls updateUserDetails() which is a network operation and switches context with Dispatchers.IO. Using postValue() at postLoginState() would better suit here.
userDetailsResult.either(::handleFailure, ::postUserDetails) | ||
} | ||
fetchUserDetailsUseCase.invoke(viewModelScope, UseCase.None) { | ||
it.either(::handleFailure, ::postUserDetails) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postUserDetails() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postUserDetails()
viewModelScope, | ||
MultiSearchUseCase.Params(query) | ||
) { | ||
it.either(::handleSearchFailure, ::postMultiSearchResult) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postMultiSearchResult() uses LiveData's setValue() functions which must be called from main thread. Either we need to use postValue() on both LiveDatas we wan't their values to change, or switch context when calling postMultiSearchResult()
topRatedTvShowsResult.either(::handleFailure, ::postTopRatedTvShows) | ||
} | ||
fetchTopRatedTvShowsUseCase.invoke(viewModelScope, UseCase.None) { | ||
it.either(::handleFailure, ::postTopRatedTvShows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postTopRatedTvShows() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postTopRatedTvShows()
nowPlayingTvShowsResult.either(::handleFailure, ::postNowPlayingTvShows) | ||
} | ||
fetchNowPlayingTvShowsUseCase.invoke(viewModelScope, UseCase.None) { | ||
it.either(::handleFailure, ::postNowPlayingTvShows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postNowPlayingTvShows() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postNowPlayingTvShows()
tvShowDetailResult.either(::handleFailure, ::postTvShowDetail) | ||
} | ||
fetchTvShowDetailUseCase.invoke(viewModelScope, FetchTvShowDetailUseCase.Params(id)) { | ||
it.either(::handleFailure, ::postTvShowDetail) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postTvShowDetail() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postTvShowDetail()
tvShowCreditResult.either(::handleFailure, ::postTvShowCredits) | ||
} | ||
fetchTvShowCreditsUseCase.invoke(viewModelScope, FetchTvShowCreditsUseCase.Params(id)) { | ||
it.either(::handleFailure, ::postTvShowCredits) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postTvShowCredits() uses LiveData's setValue() function which must be called from main thread. Either we need to use postValue(), or switch context when calling postTvShowCredits()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work. After this PR merges, I'm planning to migrate the project from LiveData to StateFlow. Then we will be able to set value inside the coroutine.
viewModelScope is added
bgScope and uiScope removed