Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to toggle primary custom button in settings #148

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ import live.mehiz.mpvkt.ui.theme.spacing
@Composable
fun CustomButtonsScreen(
buttons: List<CustomButtonEntity>,
primaryId: Int,
onClickAdd: () -> Unit,
onClickRename: (CustomButtonEntity) -> Unit,
onClickDelete: (CustomButtonEntity) -> Unit,
onClickMoveUp: (CustomButtonEntity) -> Unit,
onClickMoveDown: (CustomButtonEntity) -> Unit,
onTogglePrimary: (CustomButtonEntity) -> Unit,
onNavigateBack: () -> Unit,
) {
val lazyListState = rememberLazyListState()
Expand Down Expand Up @@ -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(),
Expand All @@ -100,6 +103,7 @@ fun CustomButtonsScreen(
),
onClickRename = onClickRename,
onClickDelete = onClickDelete,
onTogglePrimary = onTogglePrimary,
onMoveUp = onClickMoveUp,
onMoveDown = onClickMoveDown,
)
Expand All @@ -109,10 +113,12 @@ fun CustomButtonsScreen(
@Composable
private fun CustomButtonsContent(
customButtons: List<CustomButtonEntity>,
primaryId: Int,
lazyListState: LazyListState,
paddingValues: PaddingValues,
onClickRename: (CustomButtonEntity) -> Unit,
onClickDelete: (CustomButtonEntity) -> Unit,
onTogglePrimary: (CustomButtonEntity) -> Unit,
onMoveUp: (CustomButtonEntity) -> Unit,
onMoveDown: (CustomButtonEntity) -> Unit,
) {
Expand All @@ -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) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@ 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() {
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val viewModel = koinViewModel<CustomButtonsScreenViewModel>()
val playerPreferences = koinInject<PlayerPreferences>()

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class CustomButtonsScreenViewModel(
private val _dialog = MutableStateFlow<CustomButtonDialog>(CustomButtonDialog.None)
val dialog = _dialog.asStateFlow()

private val primaryCustomButtonId = MutableStateFlow(playerPreferences.primaryCustomButtonId.get())
val customButtons: StateFlow<List<CustomButtonEntity>> = customButtonsRepository.getCustomButtons()
.stateIn(
scope = viewModelScope,
Expand Down Expand Up @@ -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 }
}
Expand Down