Skip to content

Instantly share code, notes, and snippets.

@raulh82vlc
Last active March 1, 2021 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raulh82vlc/69f3d72c0fcb5502353ce32f6ee18e17 to your computer and use it in GitHub Desktop.
Save raulh82vlc/69f3d72c0fcb5502353ce32f6ee18e17 to your computer and use it in GitHub Desktop.
Search Tweets UseCase from UseCaseFlow<TweetsUIState>
/**
* Search tweets Use case implementation of [UseCaseStateFlow]
*/
@RetainedScope
class SearchTweetUseCase @Inject constructor(
private val repository: TweetsRepository,
@Named("CoroutineScopeTransfer") val coroutineScope: CoroutineScope
) : UseCaseStateFlow<TweetsUIState> {
private val tweetsUIStateFlow = MutableStateFlow<TweetsUIState>(TweetsUIState.InitialIdleUIState)
override fun getStateFlow(): StateFlow<TweetsUIState> = tweetsUIStateFlow
override fun execute(query: String) {
repository.searchTweet(query)
.onStart {
tweetsUIStateFlow.value = TweetsUIState.LoadingUIState
}
.onEach { tweets ->
if (tweets.isEmpty()) {
tweetsUIStateFlow.value = TweetsUIState.EmptyUIState
} else {
tweetsUIStateFlow.value = TweetsUIState.ListResultsUIState(tweets)
}
}.catch { e ->
tweetsUIStateFlow.value = TweetsUIState.ErrorUIState
}.launchIn(coroutineScope)
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment