mirror of
https://github.com/bitwarden/android.git
synced 2024-11-24 18:36:32 +03:00
PM-13286: Update segmented control to match the TopAppBar (#4058)
This commit is contained in:
parent
3e9e45ba2f
commit
c3af26d83f
6 changed files with 69 additions and 60 deletions
|
@ -1,14 +1,21 @@
|
||||||
package com.x8bit.bitwarden.ui.platform.components.segment
|
package com.x8bit.bitwarden.ui.platform.components.segment
|
||||||
|
|
||||||
|
import androidx.compose.foundation.BorderStroke
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.SegmentedButton
|
import androidx.compose.material3.SegmentedButton
|
||||||
import androidx.compose.material3.SegmentedButtonDefaults
|
|
||||||
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
|
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.semantics.semantics
|
import androidx.compose.ui.semantics.semantics
|
||||||
import androidx.compose.ui.semantics.testTag
|
import androidx.compose.ui.semantics.testTag
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
import com.x8bit.bitwarden.ui.platform.components.segment.color.bitwardenSegmentedButtonColors
|
import com.x8bit.bitwarden.ui.platform.components.segment.color.bitwardenSegmentedButtonColors
|
||||||
|
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
||||||
import kotlinx.collections.immutable.ImmutableList
|
import kotlinx.collections.immutable.ImmutableList
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,25 +28,44 @@ import kotlinx.collections.immutable.ImmutableList
|
||||||
fun BitwardenSegmentedButton(
|
fun BitwardenSegmentedButton(
|
||||||
options: ImmutableList<SegmentedButtonState>,
|
options: ImmutableList<SegmentedButtonState>,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
) {
|
||||||
|
Box(
|
||||||
|
modifier = modifier
|
||||||
|
.background(color = BitwardenTheme.colorScheme.background.secondary)
|
||||||
|
.padding(top = 4.dp, bottom = 8.dp, start = 16.dp, end = 16.dp),
|
||||||
) {
|
) {
|
||||||
SingleChoiceSegmentedButtonRow(
|
SingleChoiceSegmentedButtonRow(
|
||||||
modifier = modifier,
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.background(
|
||||||
|
color = BitwardenTheme.colorScheme.background.primary,
|
||||||
|
shape = BitwardenTheme.shapes.segmentedControl,
|
||||||
|
)
|
||||||
|
.padding(horizontal = 4.dp),
|
||||||
|
space = 0.dp,
|
||||||
) {
|
) {
|
||||||
options.forEachIndexed { index, option ->
|
options.forEachIndexed { index, option ->
|
||||||
SegmentedButton(
|
SegmentedButton(
|
||||||
selected = option.isChecked,
|
selected = option.isChecked,
|
||||||
onClick = option.onClick,
|
onClick = option.onClick,
|
||||||
colors = bitwardenSegmentedButtonColors(),
|
colors = bitwardenSegmentedButtonColors(),
|
||||||
shape = SegmentedButtonDefaults.itemShape(
|
shape = BitwardenTheme.shapes.segmentedControl,
|
||||||
index = index,
|
border = BorderStroke(width = 0.dp, color = Color.Transparent),
|
||||||
count = options.size,
|
label = {
|
||||||
),
|
Text(
|
||||||
label = { Text(text = option.text) },
|
text = option.text,
|
||||||
|
style = BitwardenTheme.typography.labelLarge,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
icon = {
|
||||||
|
// No icon required
|
||||||
|
},
|
||||||
modifier = Modifier.semantics { option.testTag?.let { testTag = it } },
|
modifier = Modifier.semantics { option.testTag?.let { testTag = it } },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models state for an individual button in a [BitwardenSegmentedButton].
|
* Models state for an individual button in a [BitwardenSegmentedButton].
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.x8bit.bitwarden.ui.platform.components.segment.color
|
||||||
|
|
||||||
import androidx.compose.material3.SegmentedButtonColors
|
import androidx.compose.material3.SegmentedButtonColors
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,14 +12,14 @@ import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
|
||||||
fun bitwardenSegmentedButtonColors(): SegmentedButtonColors = SegmentedButtonColors(
|
fun bitwardenSegmentedButtonColors(): SegmentedButtonColors = SegmentedButtonColors(
|
||||||
activeContainerColor = BitwardenTheme.colorScheme.filledButton.backgroundReversed,
|
activeContainerColor = BitwardenTheme.colorScheme.filledButton.backgroundReversed,
|
||||||
activeContentColor = BitwardenTheme.colorScheme.filledButton.foregroundReversed,
|
activeContentColor = BitwardenTheme.colorScheme.filledButton.foregroundReversed,
|
||||||
activeBorderColor = BitwardenTheme.colorScheme.stroke.divider,
|
activeBorderColor = Color.Transparent,
|
||||||
inactiveContainerColor = BitwardenTheme.colorScheme.background.primary,
|
inactiveContainerColor = BitwardenTheme.colorScheme.background.primary,
|
||||||
inactiveContentColor = BitwardenTheme.colorScheme.text.secondary,
|
inactiveContentColor = BitwardenTheme.colorScheme.text.secondary,
|
||||||
inactiveBorderColor = BitwardenTheme.colorScheme.stroke.divider,
|
inactiveBorderColor = Color.Transparent,
|
||||||
disabledActiveContainerColor = BitwardenTheme.colorScheme.background.primary,
|
disabledActiveContainerColor = BitwardenTheme.colorScheme.background.primary,
|
||||||
disabledActiveContentColor = BitwardenTheme.colorScheme.filledButton.foregroundDisabled,
|
disabledActiveContentColor = BitwardenTheme.colorScheme.filledButton.foregroundDisabled,
|
||||||
disabledActiveBorderColor = BitwardenTheme.colorScheme.stroke.divider,
|
disabledActiveBorderColor = Color.Transparent,
|
||||||
disabledInactiveContainerColor = BitwardenTheme.colorScheme.background.primary,
|
disabledInactiveContainerColor = BitwardenTheme.colorScheme.background.primary,
|
||||||
disabledInactiveContentColor = BitwardenTheme.colorScheme.filledButton.foregroundDisabled,
|
disabledInactiveContentColor = BitwardenTheme.colorScheme.filledButton.foregroundDisabled,
|
||||||
disabledInactiveBorderColor = BitwardenTheme.colorScheme.stroke.divider,
|
disabledInactiveBorderColor = Color.Transparent,
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,5 +16,6 @@ data class BitwardenShapes(
|
||||||
val dialog: CornerBasedShape,
|
val dialog: CornerBasedShape,
|
||||||
val infoCallout: CornerBasedShape,
|
val infoCallout: CornerBasedShape,
|
||||||
val menu: CornerBasedShape,
|
val menu: CornerBasedShape,
|
||||||
|
val segmentedControl: CornerBasedShape,
|
||||||
val snackbar: CornerBasedShape,
|
val snackbar: CornerBasedShape,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.x8bit.bitwarden.ui.platform.theme.shape
|
package com.x8bit.bitwarden.ui.platform.theme.shape
|
||||||
|
|
||||||
|
import androidx.compose.foundation.shape.CircleShape
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
|
@ -15,5 +16,6 @@ val bitwardenShapes: BitwardenShapes = BitwardenShapes(
|
||||||
dialog = RoundedCornerShape(size = 28.dp),
|
dialog = RoundedCornerShape(size = 28.dp),
|
||||||
infoCallout = RoundedCornerShape(size = 8.dp),
|
infoCallout = RoundedCornerShape(size = 8.dp),
|
||||||
menu = RoundedCornerShape(size = 4.dp),
|
menu = RoundedCornerShape(size = 4.dp),
|
||||||
|
segmentedControl = CircleShape,
|
||||||
snackbar = RoundedCornerShape(size = 8.dp),
|
snackbar = RoundedCornerShape(size = 8.dp),
|
||||||
)
|
)
|
||||||
|
|
|
@ -72,6 +72,26 @@ fun AddSendContent(
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
.verticalScroll(rememberScrollState()),
|
.verticalScroll(rememberScrollState()),
|
||||||
) {
|
) {
|
||||||
|
if (isAddMode && !isShared) {
|
||||||
|
BitwardenSegmentedButton(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
options = persistentListOf(
|
||||||
|
SegmentedButtonState(
|
||||||
|
text = stringResource(id = R.string.file),
|
||||||
|
onClick = addSendHandlers.onFileTypeSelect,
|
||||||
|
isChecked = state.isFileType,
|
||||||
|
testTag = "SendFileButton",
|
||||||
|
),
|
||||||
|
SegmentedButtonState(
|
||||||
|
text = stringResource(id = R.string.text),
|
||||||
|
onClick = addSendHandlers.onTextTypeSelect,
|
||||||
|
isChecked = state.isTextType,
|
||||||
|
testTag = "SendTextButton",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (policyDisablesSend) {
|
if (policyDisablesSend) {
|
||||||
BitwardenInfoCalloutCard(
|
BitwardenInfoCalloutCard(
|
||||||
text = stringResource(id = R.string.send_disabled_warning),
|
text = stringResource(id = R.string.send_disabled_warning),
|
||||||
|
@ -106,37 +126,6 @@ fun AddSendContent(
|
||||||
onValueChange = addSendHandlers.onNamChange,
|
onValueChange = addSendHandlers.onNamChange,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (isAddMode && !isShared) {
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
BitwardenListHeaderText(
|
|
||||||
label = stringResource(id = R.string.type),
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 16.dp),
|
|
||||||
)
|
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
BitwardenSegmentedButton(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 16.dp),
|
|
||||||
options = persistentListOf(
|
|
||||||
SegmentedButtonState(
|
|
||||||
text = stringResource(id = R.string.file),
|
|
||||||
onClick = addSendHandlers.onFileTypeSelect,
|
|
||||||
isChecked = state.isFileType,
|
|
||||||
testTag = "SendFileButton",
|
|
||||||
),
|
|
||||||
SegmentedButtonState(
|
|
||||||
text = stringResource(id = R.string.text),
|
|
||||||
onClick = addSendHandlers.onTextTypeSelect,
|
|
||||||
isChecked = state.isTextType,
|
|
||||||
testTag = "SendTextButton",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
when (val type = state.selectedType) {
|
when (val type = state.selectedType) {
|
||||||
is AddSendState.ViewState.Content.SendType.File -> {
|
is AddSendState.ViewState.Content.SendType.File -> {
|
||||||
|
|
|
@ -358,9 +358,6 @@ class AddSendScreenTest : BaseComposeTest() {
|
||||||
@Test
|
@Test
|
||||||
fun `segmented buttons should appear based on state`() {
|
fun `segmented buttons should appear based on state`() {
|
||||||
mutableStateFlow.update { it.copy(isShared = true) }
|
mutableStateFlow.update { it.copy(isShared = true) }
|
||||||
composeTestRule
|
|
||||||
.onNodeWithText("Type")
|
|
||||||
.assertDoesNotExist()
|
|
||||||
composeTestRule
|
composeTestRule
|
||||||
.onAllNodesWithText("File")
|
.onAllNodesWithText("File")
|
||||||
.filterToOne(!isEditableText)
|
.filterToOne(!isEditableText)
|
||||||
|
@ -376,10 +373,6 @@ class AddSendScreenTest : BaseComposeTest() {
|
||||||
addSendType = AddSendType.AddItem,
|
addSendType = AddSendType.AddItem,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
composeTestRule
|
|
||||||
.onNodeWithText("Type")
|
|
||||||
.performScrollTo()
|
|
||||||
.assertIsDisplayed()
|
|
||||||
composeTestRule
|
composeTestRule
|
||||||
.onAllNodesWithText("File")
|
.onAllNodesWithText("File")
|
||||||
.filterToOne(!isEditableText)
|
.filterToOne(!isEditableText)
|
||||||
|
@ -395,9 +388,6 @@ class AddSendScreenTest : BaseComposeTest() {
|
||||||
it.copy(addSendType = AddSendType.EditItem(sendItemId = "sendId"))
|
it.copy(addSendType = AddSendType.EditItem(sendItemId = "sendId"))
|
||||||
}
|
}
|
||||||
|
|
||||||
composeTestRule
|
|
||||||
.onNodeWithText("Type")
|
|
||||||
.assertIsNotDisplayed()
|
|
||||||
composeTestRule
|
composeTestRule
|
||||||
.onAllNodesWithText("File")
|
.onAllNodesWithText("File")
|
||||||
.filterToOne(!isEditableText)
|
.filterToOne(!isEditableText)
|
||||||
|
|
Loading…
Reference in a new issue