From 0abac7c5512c3690662de24a46115028478cbe11 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 18 Jan 2023 21:10:57 +0000 Subject: [PATCH] Create reusable dropdown option --- .../app/dapk/st/design/components/OverflowMenu.kt | 14 ++++++++++---- .../app/dapk/st/messenger/MessengerScreen.kt | 14 ++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/design-library/src/main/kotlin/app/dapk/st/design/components/OverflowMenu.kt b/design-library/src/main/kotlin/app/dapk/st/design/components/OverflowMenu.kt index 31674e0..8918cc4 100644 --- a/design-library/src/main/kotlin/app/dapk/st/design/components/OverflowMenu.kt +++ b/design-library/src/main/kotlin/app/dapk/st/design/components/OverflowMenu.kt @@ -4,10 +4,7 @@ import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.MoreVert -import androidx.compose.material3.DropdownMenu -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.unit.DpOffset @@ -35,4 +32,13 @@ fun OverflowMenu(content: @Composable () -> Unit) { ) } } +} + +@Composable +fun BooleanOption(value: Boolean, trueText: String, falseText: String, onClick: (Boolean) -> Unit) { + val itemTextColor = MaterialTheme.colorScheme.onSecondaryContainer + when (value) { + true -> DropdownMenuItem(text = { Text(trueText, color = itemTextColor) }, onClick = { onClick(true) }) + false -> DropdownMenuItem(text = { Text(falseText, color = itemTextColor) }, onClick = { onClick(false) }) + } } \ No newline at end of file diff --git a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt index c46a71d..fe54173 100644 --- a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt +++ b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt @@ -113,14 +113,12 @@ internal fun MessengerScreen( Toolbar(onNavigate = { navigator.navigate.upToHome() }, roomTitle, actions = { state.roomState.takeIfContent()?.let { OverflowMenu { - when (it.isMuted) { - true -> DropdownMenuItem(text = { Text("Unmute notifications", color = MaterialTheme.colorScheme.onSecondaryContainer) }, onClick = { - viewModel.dispatch(ScreenAction.Notifications.Unmute) - }) - - false -> DropdownMenuItem(text = { Text("Mute notifications", color = MaterialTheme.colorScheme.onSecondaryContainer) }, onClick = { - viewModel.dispatch(ScreenAction.Notifications.Mute) - }) + BooleanOption(value = it.isMuted, trueText = "Unmute notifications", falseText = "Mute notifications") { + val action = when (it) { + true -> ScreenAction.Notifications.Unmute + false -> ScreenAction.Notifications.Unmute + } + viewModel.dispatch(action) } DropdownMenuItem(text = { Text("Leave room", color = MaterialTheme.colorScheme.onSecondaryContainer) }, onClick = { viewModel.dispatch(ScreenAction.LeaveRoom)