-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PM-13943 : PT1 Custom snackbar UI (#4135)
- Loading branch information
1 parent
c5a266d
commit a23fc31
Showing
8 changed files
with
243 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
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
111 changes: 111 additions & 0 deletions
111
app/src/main/java/com/x8bit/bitwarden/ui/platform/components/snackbar/BitwardenSnackbar.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,111 @@ | ||
package com.x8bit.bitwarden.ui.platform.components.snackbar | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.x8bit.bitwarden.R | ||
import com.x8bit.bitwarden.ui.platform.base.util.asText | ||
import com.x8bit.bitwarden.ui.platform.components.button.BitwardenOutlinedButton | ||
import com.x8bit.bitwarden.ui.platform.components.button.color.bitwardenOutlinedButtonColors | ||
import com.x8bit.bitwarden.ui.platform.components.util.rememberVectorPainter | ||
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme | ||
|
||
/** | ||
* Custom snackbar for Bitwarden. | ||
* Shows a message with an optional actions and title. | ||
*/ | ||
@Composable | ||
fun BitwardenSnackbar( | ||
bitwardenSnackbarData: BitwardenSnackbarData, | ||
modifier: Modifier = Modifier, | ||
onDismiss: () -> Unit = {}, | ||
onActionClick: () -> Unit = {}, | ||
) { | ||
Box( | ||
modifier = modifier.padding(12.dp), | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.background( | ||
color = BitwardenTheme.colorScheme.background.alert, | ||
shape = BitwardenTheme.shapes.snackbar, | ||
) | ||
.padding(16.dp), | ||
) { | ||
Column { | ||
bitwardenSnackbarData.messageHeader?.let { | ||
Text( | ||
text = it(), | ||
color = BitwardenTheme.colorScheme.text.reversed, | ||
style = BitwardenTheme.typography.titleSmall, | ||
) | ||
Spacer(Modifier.height(4.dp)) | ||
} | ||
Text( | ||
text = bitwardenSnackbarData.message(), | ||
color = BitwardenTheme.colorScheme.text.reversed, | ||
style = BitwardenTheme.typography.bodyMedium, | ||
) | ||
bitwardenSnackbarData.actionLabel?.let { | ||
Spacer(Modifier.height(12.dp)) | ||
BitwardenOutlinedButton( | ||
label = it(), | ||
onClick = onActionClick, | ||
colors = bitwardenOutlinedButtonColors( | ||
contentColor = BitwardenTheme.colorScheme.text.reversed, | ||
outlineColor = BitwardenTheme | ||
.colorScheme | ||
.outlineButton | ||
.borderReversed, | ||
), | ||
) | ||
} | ||
} | ||
if (bitwardenSnackbarData.withDismissAction) { | ||
Spacer(Modifier.weight(1f)) | ||
IconButton( | ||
onClick = onDismiss, | ||
content = { | ||
Icon( | ||
rememberVectorPainter(R.drawable.ic_close), | ||
contentDescription = stringResource(R.string.close), | ||
tint = BitwardenTheme.colorScheme.icon.reversed, | ||
) | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun BitwardenCustomSnackbar_preview() { | ||
BitwardenTheme { | ||
Surface { | ||
BitwardenSnackbar( | ||
BitwardenSnackbarData( | ||
messageHeader = "Header".asText(), | ||
message = "Message".asText(), | ||
actionLabel = "Action".asText(), | ||
withDismissAction = true, | ||
), | ||
) | ||
} | ||
} | ||
} |
31 changes: 14 additions & 17 deletions
31
...rc/main/java/com/x8bit/bitwarden/ui/platform/components/snackbar/BitwardenSnackbarHost.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,35 +1,32 @@ | ||
package com.x8bit.bitwarden.ui.platform.components.snackbar | ||
|
||
import androidx.compose.material3.Snackbar | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme | ||
|
||
/** | ||
* A custom Bitwarden-themed snackbar. | ||
* | ||
* @param hostState The state of this snackbar. | ||
* @param modifier The [Modifier] to be applied to this radio button. | ||
* @param bitwardenHostState The state of this snackbar. | ||
* @param modifier The [Modifier] to be applied to the [SnackbarHost]. | ||
*/ | ||
@Composable | ||
fun BitwardenSnackbarHost( | ||
hostState: SnackbarHostState, | ||
bitwardenHostState: BitwardenSnackbarHostState, | ||
modifier: Modifier = Modifier, | ||
) { | ||
SnackbarHost( | ||
hostState = hostState, | ||
hostState = bitwardenHostState.snackbarHostState, | ||
modifier = modifier, | ||
) { | ||
Snackbar( | ||
snackbarData = it, | ||
shape = BitwardenTheme.shapes.snackbar, | ||
containerColor = BitwardenTheme.colorScheme.background.alert, | ||
contentColor = BitwardenTheme.colorScheme.text.reversed, | ||
actionColor = BitwardenTheme.colorScheme.background.alert, | ||
actionContentColor = BitwardenTheme.colorScheme.icon.reversed, | ||
dismissActionContentColor = BitwardenTheme.colorScheme.icon.reversed, | ||
) | ||
) { snackbarData -> | ||
val message = snackbarData.visuals.message | ||
val currentCustomSnackbarData = bitwardenHostState.currentSnackbarData | ||
if (currentCustomSnackbarData?.key == message) { | ||
BitwardenSnackbar( | ||
bitwardenSnackbarData = currentCustomSnackbarData, | ||
onDismiss = snackbarData::dismiss, | ||
onActionClick = snackbarData::performAction, | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.