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)
@mcatta
mcatta / Response.java
Last active November 7, 2022 06:38
Make a android permission for Android M
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_PERMISSIONS:
boolean userAllowed = true;
for (final int result : grantResults) {
userAllowed &= result == PackageManager.PERMISSION_GRANTED;
}
if (userAllowed) {
start();
@ViewModelScoped
class GithubStateMachine @Inject constructor(
private val githubRepository: GithubRepository
) : FlowReduxStateMachine<GithubState, GithubAction>(
initialState = GithubState.ContentState(owner = "", repositories = emptyList())
) {
init {
spec {
// spec
}
@mcatta
mcatta / Screen.kt
Last active October 26, 2022 13:11
Column {
Row {
TextField(
placeholder = { Text(text = "Owner") },
value = contentState.owner, onValueChange = { /* On type */ }
)
Button(
content = { Text(text = "OK") },
onClick = { /* Confirm */ }
)
@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)