From c1e5a8bda77af8514adaabeb2597c7edebb52246 Mon Sep 17 00:00:00 2001 From: Secozzi <49240133+Secozzi@users.noreply.github.com> Date: Sat, 9 Nov 2024 20:15:03 +0100 Subject: [PATCH] Add button to toggle primary custom button in settings (#148) * Add button to toggle primary custom button in settings * Use "ok" instead of "add" when editing --- .../custombuttons/CustomButtonsScreen.kt | 8 ++++++++ .../components/CustomButtonDialogs.kt | 2 +- .../components/CustomButtonListItem.kt | 15 +++++++++++++++ .../mpvkt/ui/custombuttons/CustomButtonsScreen.kt | 8 ++++++++ .../custombuttons/CustomButtonsScreenViewModel.kt | 11 +++++++++-- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/CustomButtonsScreen.kt b/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/CustomButtonsScreen.kt index e1f833d..0c32b12 100644 --- a/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/CustomButtonsScreen.kt +++ b/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/CustomButtonsScreen.kt @@ -41,11 +41,13 @@ import live.mehiz.mpvkt.ui.theme.spacing @Composable fun CustomButtonsScreen( buttons: List, + primaryId: Int, onClickAdd: () -> Unit, onClickRename: (CustomButtonEntity) -> Unit, onClickDelete: (CustomButtonEntity) -> Unit, onClickMoveUp: (CustomButtonEntity) -> Unit, onClickMoveDown: (CustomButtonEntity) -> Unit, + onTogglePrimary: (CustomButtonEntity) -> Unit, onNavigateBack: () -> Unit, ) { val lazyListState = rememberLazyListState() @@ -91,6 +93,7 @@ fun CustomButtonsScreen( val layoutDirection = LocalLayoutDirection.current CustomButtonsContent( customButtons = buttons, + primaryId = primaryId, lazyListState = lazyListState, paddingValues = PaddingValues( top = MaterialTheme.spacing.small + padding.calculateTopPadding(), @@ -100,6 +103,7 @@ fun CustomButtonsScreen( ), onClickRename = onClickRename, onClickDelete = onClickDelete, + onTogglePrimary = onTogglePrimary, onMoveUp = onClickMoveUp, onMoveDown = onClickMoveDown, ) @@ -109,10 +113,12 @@ fun CustomButtonsScreen( @Composable private fun CustomButtonsContent( customButtons: List, + primaryId: Int, lazyListState: LazyListState, paddingValues: PaddingValues, onClickRename: (CustomButtonEntity) -> Unit, onClickDelete: (CustomButtonEntity) -> Unit, + onTogglePrimary: (CustomButtonEntity) -> Unit, onMoveUp: (CustomButtonEntity) -> Unit, onMoveDown: (CustomButtonEntity) -> Unit, ) { @@ -128,12 +134,14 @@ private fun CustomButtonsContent( CustomButtonListItem( modifier = Modifier.animateItem(), customButton = button, + isPrimary = button.id == primaryId, canMoveUp = index != 0, canMoveDown = index != customButtons.lastIndex, onMoveUp = onMoveUp, onMoveDown = onMoveDown, onRename = { onClickRename(button) }, onDelete = { onClickDelete(button) }, + onTogglePrimary = { onTogglePrimary(button) }, ) } } diff --git a/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonDialogs.kt b/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonDialogs.kt index 960e6c2..e8000a1 100644 --- a/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonDialogs.kt +++ b/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonDialogs.kt @@ -157,7 +157,7 @@ fun CustomButtonEditDialog( onDismissRequest() } ) { - Text(text = stringResource(id = R.string.pref_custom_button_action_add)) + Text(text = stringResource(id = R.string.generic_ok)) } }, dismissButton = { diff --git a/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonListItem.kt b/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonListItem.kt index be49bf7..5befaff 100644 --- a/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonListItem.kt +++ b/app/src/main/java/live/mehiz/mpvkt/presentation/custombuttons/components/CustomButtonListItem.kt @@ -11,6 +11,8 @@ import androidx.compose.material.icons.outlined.ArrowDropUp import androidx.compose.material.icons.outlined.Code import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.Edit +import androidx.compose.material.icons.outlined.Star +import androidx.compose.material.icons.outlined.StarOutline import androidx.compose.material3.ElevatedCard import androidx.compose.material3.Icon import androidx.compose.material3.IconButton @@ -19,6 +21,7 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextOverflow import live.mehiz.mpvkt.R @@ -28,12 +31,14 @@ import live.mehiz.mpvkt.ui.theme.spacing @Composable fun CustomButtonListItem( customButton: CustomButtonEntity, + isPrimary: Boolean, canMoveUp: Boolean, canMoveDown: Boolean, onMoveUp: (CustomButtonEntity) -> Unit, onMoveDown: (CustomButtonEntity) -> Unit, onRename: () -> Unit, onDelete: () -> Unit, + onTogglePrimary: () -> Unit, modifier: Modifier = Modifier, ) { ElevatedCard( @@ -83,6 +88,16 @@ fun CustomButtonListItem( Icon(imageVector = Icons.Outlined.ArrowDropDown, contentDescription = null) } Spacer(modifier = Modifier.weight(1f)) + + val starColor = Color(0xFFFDD835) + val starImage = if (isPrimary) Icons.Outlined.Star else Icons.Outlined.StarOutline + IconButton(onClick = onTogglePrimary) { + Icon( + imageVector = starImage, + tint = starColor, + contentDescription = null, + ) + } IconButton(onClick = onRename) { Icon( imageVector = Icons.Outlined.Edit, diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreen.kt b/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreen.kt index e1477eb..e2525e0 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreen.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreen.kt @@ -7,11 +7,14 @@ import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow import kotlinx.collections.immutable.toImmutableList import live.mehiz.mpvkt.database.entities.CustomButtonEntity +import live.mehiz.mpvkt.preferences.PlayerPreferences +import live.mehiz.mpvkt.preferences.preference.collectAsState import live.mehiz.mpvkt.presentation.Screen import live.mehiz.mpvkt.presentation.custombuttons.CustomButtonsScreen import live.mehiz.mpvkt.presentation.custombuttons.components.CustomButtonAddDialog import live.mehiz.mpvkt.presentation.custombuttons.components.CustomButtonDeleteDialog import live.mehiz.mpvkt.presentation.custombuttons.components.CustomButtonEditDialog +import org.koin.compose.koinInject import org.koin.compose.viewmodel.koinViewModel object CustomButtonsScreen : Screen() { @@ -19,14 +22,19 @@ object CustomButtonsScreen : Screen() { override fun Content() { val navigator = LocalNavigator.currentOrThrow val viewModel = koinViewModel() + val playerPreferences = koinInject() + + val primaryButtonId by playerPreferences.primaryCustomButtonId.collectAsState() val customButtons by viewModel.customButtons.collectAsState() val dialog by viewModel.dialog.collectAsState() CustomButtonsScreen( buttons = customButtons, + primaryId = primaryButtonId, onClickAdd = { viewModel.showDialog(CustomButtonDialog.Create) }, onClickRename = { viewModel.showDialog(CustomButtonDialog.Edit(it)) }, onClickDelete = { viewModel.showDialog(CustomButtonDialog.Delete(it)) }, + onTogglePrimary = viewModel::togglePrimary, onClickMoveUp = viewModel::moveUp, onClickMoveDown = viewModel::moveDown, onNavigateBack = navigator::pop, diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt b/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt index e91a6c2..ece95e9 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt @@ -21,7 +21,6 @@ class CustomButtonsScreenViewModel( private val _dialog = MutableStateFlow(CustomButtonDialog.None) val dialog = _dialog.asStateFlow() - private val primaryCustomButtonId = MutableStateFlow(playerPreferences.primaryCustomButtonId.get()) val customButtons: StateFlow> = customButtonsRepository.getCustomButtons() .stateIn( scope = viewModelScope, @@ -64,11 +63,19 @@ class CustomButtonsScreenViewModel( customButtonsRepository.deleteAndReindex(customButton) } - if (customButton.id == primaryCustomButtonId.value) { + if (customButton.id == playerPreferences.primaryCustomButtonId.get()) { playerPreferences.primaryCustomButtonId.set(0) } } + fun togglePrimary(customButton: CustomButtonEntity) { + if (customButton.id == playerPreferences.primaryCustomButtonId.get()) { + playerPreferences.primaryCustomButtonId.set(0) + } else { + playerPreferences.primaryCustomButtonId.set(customButton.id) + } + } + fun showDialog(dialog: CustomButtonDialog) { _dialog.update { _ -> dialog } }