Skip to content

Instantly share code, notes, and snippets.

View mcatta's full-sized avatar
📚
Learning

Marco Cattaneo mcatta

📚
Learning
View GitHub Profile
@mcatta
mcatta / .zshrc
Created January 4, 2024 10:31
Useful gradlew alias
# gradlew alias
alias gw=./gradlew
@mcatta
mcatta / SingleEventEffect.kt
Created December 20, 2023 16:28
A composable function that allows you to handle side-effect just once
/**
* Composable function that aims to help the single event handing.
* Use it only for SideEffect event (one shot) and not for handling UI state changes.
*
* @param sideEffectFlow Flow
* @param lifeCycleState default [Lifecycle.State.STARTED]
* @param collector
*/
@Composable
fun <T : Any> SingleEventEffect(
@mcatta
mcatta / ComposeColoredShadow.kt
Created November 17, 2022 14:09
A Jetpack Compose Modifier which allows to add a colored shadow with a specific X and Y offset
/**
* Change shadow color and style
* @param color shadow color
* @param alpha shadow alpha
* @param borderRadius shadow border radius
* @param shadowRadius shadow size
* @param offsetX X offset
* @param offsetY Y offset
*/
@RequiresApi(Build.VERSION_CODES.P)
@Test
fun `Test confirm upon a failure`() = coroutineTestRule.scope.runTest {
// Given
coEvery { githubRepository.repositoryByOwner(any()) } returns Either.Left(AppError.Network)
// When / Then
stateMachine.state.test {
stateMachine.dispatch(GithubAction.TypeOwner("mcatta"))
stateMachine.dispatch(GithubAction.Confirm)
@Test
fun `Test typing`() = coroutineTestRule.scope.runTest {
// When / Then
stateMachine.state.test {
stateMachine.dispatch(GithubAction.TypeOwner("mcatta"))
assertEquals("", (awaitItem() as GithubState.ContentState).owner)
assertEquals("mcatta", (awaitItem() as GithubState.ContentState).owner)
}
}
@Composable
fun RepoInfoScreen(
githubViewModel: GithubViewModel
) {
val uiState by githubViewModel.rememberState()
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
@HiltViewModel
class GithubViewModel @Inject constructor(
githubStateMachine: GithubStateMachine
): AbsStateViewModel<GithubState, GithubAction>(githubStateMachine)
@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
abstract class AbsStateViewModel<State: Any, Action: Any>(
private val stateMachine: FlowReduxStateMachine<State, Action>
): BaseViewModel() {
@Composable
fun rememberState() = stateMachine.rememberState()
fun dispatch(action: Action) = viewModelScope.launch {
stateMachine.dispatch(action = action)
// Error
inState<GithubState.Error> {
on { _: GithubAction.RetryLoadingAction, state: State<GithubState.Error> ->
state.override { GithubState.Load(owner = state.snapshot.owner) }
}
}
inState<GithubState.Load> {
onEnter { state: State<GithubState.Load> ->
val owner = state.snapshot.owner
githubRepository.repositoryByOwner(owner = owner).fold(
ifLeft = {
GithubState.Error(Throwable("Fail"), owner = owner)
},
ifRight = {
GithubState.ContentState(repositories = it, owner = owner)
}