Skip to content

Instantly share code, notes, and snippets.

View raulh82vlc's full-sized avatar

Raul Hernandez raulh82vlc

View GitHub Profile
@raulh82vlc
raulh82vlc / StateFlowHandler.kt
Last active October 28, 2020 08:58
StateFlowHandler with process method
private val scope = CoroutineScope(taskThreading.ui() + SupervisorJob())
fun processStateCollection() {
stateFlow
.filter {
stateFlow -> stateFlow != null
}.onEach { stateFlow ->
tweetsListUI?.handleStates(stateFlow)
}.launchIn(scope)
}
@raulh82vlc
raulh82vlc / StateFlowHandler.kt
Last active October 28, 2020 08:57
StateFlowHandler in kotlin with init method
@ActivityScope
class StateFlowHandler @Inject constructor(taskThreading: TaskThreading) {
private var tweetsListUI: TweetsListUI? = null
private lateinit var stateFlow: StateFlow<TweetsUIState?>
fun init(val stateFlow: StateFlow<TweetsUIState?>, val tweetsListUI: TweetsListUI) {
stateFlow = stateFlow
tweetsListUI = tweetsListUI
@raulh82vlc
raulh82vlc / TweetsListUI.java
Created October 14, 2020 18:43
TweetsListUI view in Java
public class TweetsListUI extends ViewUI {
@Inject
SearchViewDelegate viewDelegate;
@Inject
StateFlowHandler stateFlowHandler;
public void initStateFlow(@NotNull StateFlow<? extends TweetsUIState> stateFlow) {
stateFlowHandler.init(stateFlow, this);
stateFlowHandler.processStateFlowCollection();
@raulh82vlc
raulh82vlc / SearchViewDelegate.kt
Last active October 28, 2020 08:59
SearchViewDelegate in kotlin with initialisation of the view
@ActivityScope
class SearchViewDelegate @Inject constructor(
private val presenter: SearchTweetPresenter,
private val taskThreading: TaskThreading
) {
private fun getStateFlow(): StateFlow<TweetsUIState> = presenter.stateFlow
@FlowPreview
@ExperimentalCoroutinesApi
@raulh82vlc
raulh82vlc / SearchTweetPresenter.java
Created October 14, 2020 18:24
SearchTweetPresenter java with searchTweets method
@ActivityScope
public class SearchTweetPresenter {
// ...
public void searchTweets(@NotNull final String query) {
// TODO Remove callback code:
// if (callback == null && view != null) {
// callback = new SearchCallbackImpl(view);
// }
tweetSearchUseCase.execute(query);
@raulh82vlc
raulh82vlc / SearchTweetPresenter.java
Created October 14, 2020 18:21
SearchTweetPresenter written in Java
@ActivityScope
public class SearchTweetPresenter {
@NotNull
private final SearchTweetUseCase tweetSearchUseCase;
@Inject
public SearchTweetPresenter(@NotNull SearchTweetUseCase tweetSearchUseCase) {
this.tweetSearchUseCase = tweetSearchUseCase;
}
@raulh82vlc
raulh82vlc / SearchTweetUseCase.kt
Last active October 28, 2020 08:59
all states covered into the UseCase
override fun execute(query: String) {
tweetsStateFlow.value = TweetsUIState.LoadingUIState
repository.searchTweet(query)
.onEach { tweets ->
val stateFlow =
if (tweets.isEmpty()) {
TweetsUIState.EmptyUIState
} else {
TweetsUIState.ListResultsUIState(tweets)
@raulh82vlc
raulh82vlc / SearchTweetUseCase.kt
Last active October 28, 2020 09:00
SearchTweetUseCase method substituting the previous callback with the new state flow
override fun execute(query: String) {
// TODO remove this: callback?.onShowLoader()
// add new Loading state
tweetsStateFlow.value = TweetsUIState.LoadingUIState
// ...
}
@raulh82vlc
raulh82vlc / UseCase.kt
Last active October 14, 2020 15:34
Use Case contract for callbacks
// Use Case contract with Callback input parameter
interface UseCase<T> {
fun execute(param: String, callbackInput: T?)
fun cancel()
}
@raulh82vlc
raulh82vlc / TweetsUIState.kt
Created October 13, 2020 20:50
Tweets UI State sealed class with contract of types required for the different states
/**
* UI States defined for StateFlow in the workflow
*/
sealed class TweetsUIState {
// List of results coming from the Data layer
data class ListResultsUIState(val tweets: List<Tweet>): TweetsUIState()
// Error state with error message
data class ErrorUIState(val msg: String): TweetsUIState()
// Empty state with "no results" String
object EmptyUIState: TweetsUIState()