Last active
March 1, 2021 23:47
-
-
Save raulh82vlc/69f3d72c0fcb5502353ce32f6ee18e17 to your computer and use it in GitHub Desktop.
Search Tweets UseCase from UseCaseFlow<TweetsUIState>
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
/** | |
* 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