Skip to content

Instantly share code, notes, and snippets.

View raulh82vlc's full-sized avatar

Raul Hernandez raulh82vlc

View GitHub Profile
@raulh82vlc
raulh82vlc / SearchViewDelegate.kt
Created February 28, 2021 12:44
SearchViewDelegate's StateFlow propagation
@ActivityScope
class SearchViewDelegate @Inject constructor(
private val presenter: SearchTweetPresenter,
@Named("CoroutineUIScope") private val scope: CoroutineScope
) {
// ...
fun getStateFlow(): StateFlow<TweetsUIState> = presenter.stateFlow
}
@raulh82vlc
raulh82vlc / SearchTweetPresenter.java
Last active February 28, 2021 12:49
SearchTweetPresenter's StateFlow propagation
@ActivityScope
public class SearchTweetPresenter {
@Inject
public SearchTweetPresenter(@NotNull SearchTweetUseCase tweetSearchUseCase) {
this.tweetSearchUseCase = tweetSearchUseCase;
}
// ...
@NotNull
public StateFlow<TweetsUIState> getStateFlow() {
return tweetSearchUseCase.getStateFlow();
@raulh82vlc
raulh82vlc / UseCaseStateFlow.kt
Last active February 28, 2021 12:25
UseCaseStateFlow contract
/**
* Use case for State Flow defines a unit of execution
*/
interface UseCaseStateFlow<T> {
fun execute(param: String)
fun cancel()
fun getStateFlow(): StateFlow<T>
}
@raulh82vlc
raulh82vlc / SearchTweetUseCase.kt
Last active March 1, 2021 23:47
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)
/**
* 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()
}
@raulh82vlc
raulh82vlc / SearchTweetUseCaseTest.kt
Last active December 27, 2021 01:24
SearchTweetUseCaseTest test cases for StateFlow resolution - only latest state is captured
/*
* 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
@raulh82vlc
raulh82vlc / StateFlowHandlerTest.kt
Last active October 31, 2020 15:38
StateFlowHandlerTest with a couple of tests cases that pass correctly for StateFlow
/*
* 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
@raulh82vlc
raulh82vlc / TweetsListUI.java
Created October 14, 2020 19:12
TweetsListUI java destroying any resources
public class TweetsListUI extends ViewUI {
@Inject
SearchViewDelegate viewDelegate;
@Inject
StateFlowHandler stateFlowHandler;
// ...
@Override
public void onDestroy() {
@raulh82vlc
raulh82vlc / StateFlowHandler.kt
Created October 14, 2020 19:08
StateFlowHandler cancels scope
@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
@raulh82vlc
raulh82vlc / TweetsListUIUtils.kt
Last active October 28, 2020 08:57
TweetsListUIUtils.kt extension function toolkit
fun TweetsListUI.handleStates(stateFlow: TweetsUIState?) {
when (stateFlow) {
is TweetsUIState.LoadingUIState -> {
showLoading()
}
is TweetsUIState.ListResultsUIState -> {
showResults(stateFlow)
}
is TweetsUIState.EmptyUIState -> {
showEmptyState()