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
@ActivityScope | |
class SearchViewDelegate @Inject constructor( | |
private val presenter: SearchTweetPresenter, | |
@Named("CoroutineUIScope") private val scope: CoroutineScope | |
) { | |
// ... | |
fun getStateFlow(): StateFlow<TweetsUIState> = presenter.stateFlow | |
} |
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
@ActivityScope | |
public class SearchTweetPresenter { | |
@Inject | |
public SearchTweetPresenter(@NotNull SearchTweetUseCase tweetSearchUseCase) { | |
this.tweetSearchUseCase = tweetSearchUseCase; | |
} | |
// ... | |
@NotNull | |
public StateFlow<TweetsUIState> getStateFlow() { | |
return tweetSearchUseCase.getStateFlow(); |
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
/** | |
* Use case for State Flow defines a unit of execution | |
*/ | |
interface UseCaseStateFlow<T> { | |
fun execute(param: String) | |
fun cancel() | |
fun getStateFlow(): StateFlow<T> | |
} |
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) |
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
/** | |
* Output UI States defined for StateFlow & Compose in the workflow | |
*/ | |
sealed class TweetsUIState { | |
data class ListResultsUIState(val tweets: List<Tweet>): TweetsUIState() | |
data class ErrorUIState(val msg: String): TweetsUIState() | |
data class EmptyUIState(val query: String): TweetsUIState() | |
object InitialIdleUIState: TweetsUIState() | |
object LoadingUIState: 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
/* | |
* Copyright (C) 2020 Raul Hernandez Lopez | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
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
/* | |
* Copyright (C) 2020 Raul Hernandez Lopez | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
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
public class TweetsListUI extends ViewUI { | |
@Inject | |
SearchViewDelegate viewDelegate; | |
@Inject | |
StateFlowHandler stateFlowHandler; | |
// ... | |
@Override | |
public void onDestroy() { |
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
@ActivityScope | |
class StateFlowHandler @Inject constructor(taskThreading: TaskThreading) { | |
private val scope = CoroutineScope(taskThreading.ui() + SupervisorJob()) | |
private var tweetsListUI: TweetsListUI? = null | |
// ... | |
fun cancel() { | |
scope.cancel() | |
tweetsListUI = null |
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
fun TweetsListUI.handleStates(stateFlow: TweetsUIState?) { | |
when (stateFlow) { | |
is TweetsUIState.LoadingUIState -> { | |
showLoading() | |
} | |
is TweetsUIState.ListResultsUIState -> { | |
showResults(stateFlow) | |
} | |
is TweetsUIState.EmptyUIState -> { | |
showEmptyState() |