-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add snackbar and refactor auth animation
- Loading branch information
Showing
13 changed files
with
387 additions
and
97 deletions.
There are no files selected for viewing
212 changes: 168 additions & 44 deletions
212
core/ui/src/commonMain/kotlin/com/stslex/core/ui/components/AppSnackbar.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,207 @@ | ||
package com.stslex.core.ui.components | ||
|
||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.foundation.layout.BoxWithConstraintsScope | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.offset | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.Snackbar | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Info | ||
import androidx.compose.material.rememberSwipeableState | ||
import androidx.compose.material.swipeable | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.snapshotFlow | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.IntOffset | ||
import com.stslex.core.ui.theme.AppDimension | ||
import com.stslex.core.ui.theme.toPx | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.filter | ||
import kotlin.math.roundToInt | ||
|
||
@Composable | ||
fun BoxWithConstraintsScope.AppSnackbarHost( | ||
snackbarHostState: SnackbarHostState, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val width = maxWidth | ||
AppSnackbarHost( | ||
snackbarHostState = snackbarHostState, | ||
width = width, | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Composable | ||
fun BoxScope.AppSnackbarHost( | ||
snackbarHostState: SnackbarHostState, | ||
width: Dp, | ||
modifier: Modifier = Modifier, | ||
) { | ||
SnackbarHost( | ||
modifier = modifier | ||
.align(Alignment.BottomCenter), | ||
hostState = snackbarHostState | ||
) { snackbarData -> | ||
val actionLabel = snackbarData.visuals.actionLabel ?: return@SnackbarHost | ||
val action = SnackbarType.getByAction(actionLabel) ?: return@SnackbarHost | ||
when (action) { | ||
SnackbarType.ERROR -> ErrorSnackbar( | ||
snackbarHostState = snackbarHostState, | ||
message = snackbarData.visuals.message, | ||
width = width, | ||
) | ||
|
||
SnackbarType.SUCCESS -> SuccessSnackbar( | ||
snackbarHostState = snackbarHostState, | ||
snackbarData.visuals.message, | ||
width = width, | ||
) | ||
|
||
SnackbarType.INFO -> InfoSnackbar( | ||
snackbarHostState = snackbarHostState, | ||
snackbarData.visuals.message, | ||
width = width, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun SuccessSnackbar( | ||
snackbarHostState: SnackbarHostState, | ||
message: String, | ||
width: Dp, | ||
modifier: Modifier = Modifier, | ||
) { | ||
AppSnackbar( | ||
type = SnackbarType.SUCCESS, | ||
message = message, | ||
width = width, | ||
snackbarHostState = snackbarHostState, | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Composable | ||
fun InfoSnackbar( | ||
snackbarHostState: SnackbarHostState, | ||
message: String, | ||
width: Dp, | ||
modifier: Modifier = Modifier, | ||
) { | ||
AppSnackbar( | ||
type = SnackbarType.INFO, | ||
message = message, | ||
width = width, | ||
snackbarHostState = snackbarHostState, | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Composable | ||
fun ErrorSnackbar( | ||
snackbarHostState: SnackbarHostState, | ||
message: String, | ||
width: Dp, | ||
modifier: Modifier = Modifier, | ||
) { | ||
AppSnackbar( | ||
type = SnackbarType.ERROR, | ||
message = message, | ||
width = width, | ||
snackbarHostState = snackbarHostState, | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun AppSnackbar( | ||
type: SnackbarType, | ||
message: String, | ||
width: Dp, | ||
snackbarHostState: SnackbarHostState, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val swipeableState = rememberSwipeableState(SnackbarSwipeState.NONE) | ||
var width by remember { mutableStateOf(0f) } | ||
val swipeableState = rememberSwipeableState(SnackbarSwipeState.CENTER) | ||
val widthPx = width.toPx | ||
|
||
LaunchedEffect(swipeableState) { | ||
snapshotFlow { | ||
swipeableState.offset.value | ||
} | ||
.filter { value -> | ||
value == widthPx || value == -widthPx | ||
} | ||
.distinctUntilChanged() | ||
.collect { | ||
snackbarHostState.currentSnackbarData?.dismiss() | ||
} | ||
} | ||
|
||
Snackbar( | ||
modifier = modifier | ||
.swipeable( | ||
state = swipeableState, | ||
orientation = Orientation.Horizontal, | ||
anchors = mapOf( | ||
0f to SnackbarSwipeState.LEFT, | ||
width * 0.5f to SnackbarSwipeState.RIGHT, | ||
width to SnackbarSwipeState.RIGHT | ||
-widthPx to SnackbarSwipeState.LEFT, | ||
0f to SnackbarSwipeState.CENTER, | ||
widthPx to SnackbarSwipeState.RIGHT | ||
), | ||
) | ||
.onGloballyPositioned { | ||
width = it.size.width.toFloat() | ||
}, | ||
.offset { | ||
IntOffset( | ||
x = swipeableState.offset.value.roundToInt(), | ||
y = 0 | ||
) | ||
} | ||
.padding(horizontal = AppDimension.Padding.medium), | ||
action = { | ||
Text(text = "OK") | ||
TextButton( | ||
onClick = { | ||
snackbarHostState.currentSnackbarData?.performAction() | ||
} | ||
) { | ||
// TODO repeat for errors??? | ||
Text(text = "OK") | ||
} | ||
} | ||
) { | ||
Row { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(AppDimension.Padding.small), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Icon( | ||
imageVector = Icons.Default.Info, | ||
contentDescription = "info" | ||
imageVector = type.imageVector, | ||
contentDescription = type.contentDescription | ||
) | ||
Spacer(modifier = Modifier.padding(AppDimension.Padding.medium)) | ||
Text( | ||
modifier = Modifier, | ||
text = type.message, | ||
color = MaterialTheme.colorScheme.onBackground | ||
text = message, | ||
color = MaterialTheme.colorScheme.onBackground, | ||
style = MaterialTheme.typography.titleMedium, | ||
textAlign = TextAlign.Start, | ||
overflow = TextOverflow.Ellipsis, | ||
maxLines = 2 | ||
) | ||
} | ||
} | ||
} | ||
|
||
enum class SnackbarSwipeState { | ||
LEFT, | ||
RIGHT, | ||
NONE | ||
} | ||
|
||
sealed class SnackbarType( | ||
val message: String | ||
) { | ||
data class Error( | ||
private val mes: String | ||
) : SnackbarType(mes) | ||
|
||
data class Success( | ||
private val mes: String | ||
) : SnackbarType(mes) | ||
|
||
data class Info( | ||
private val mes: String | ||
) : SnackbarType(mes) | ||
} |
7 changes: 7 additions & 0 deletions
7
core/ui/src/commonMain/kotlin/com/stslex/core/ui/components/SnackbarSwipeState.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.stslex.core.ui.components | ||
|
||
internal enum class SnackbarSwipeState { | ||
LEFT, | ||
CENTER, | ||
RIGHT, | ||
} |
38 changes: 38 additions & 0 deletions
38
core/ui/src/commonMain/kotlin/com/stslex/core/ui/components/SnackbarType.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.stslex.core.ui.components | ||
|
||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Done | ||
import androidx.compose.material.icons.filled.Info | ||
import androidx.compose.material.icons.filled.Warning | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
|
||
enum class SnackbarType( | ||
val label: String, | ||
val imageVector: ImageVector, | ||
val contentDescription: String | ||
) { | ||
ERROR( | ||
label = "error", | ||
imageVector = Icons.Default.Warning, | ||
contentDescription = "Error" | ||
), | ||
SUCCESS( | ||
label = "success", | ||
imageVector = Icons.Default.Done, | ||
contentDescription = "Success" | ||
), | ||
INFO( | ||
label = "info", | ||
imageVector = Icons.Default.Info, | ||
contentDescription = "Info" | ||
); | ||
|
||
companion object { | ||
|
||
fun getByAction( | ||
actionLabel: String? | ||
): SnackbarType? = entries.firstOrNull { type -> | ||
type.label == actionLabel | ||
} | ||
} | ||
} |
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
52 changes: 51 additions & 1 deletion
52
core/ui/src/commonMain/kotlin/com/stslex/core/ui/mvi/Store.kt
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
Oops, something went wrong.