From 63499bd72c1a5cfad938ccee3663adab8e61417e Mon Sep 17 00:00:00 2001 From: Hepolise Date: Mon, 21 Apr 2025 23:04:15 +0300 Subject: [PATCH] Settings: Add animations --- .../ui/component/AppFilterSetting.kt | 41 +++- .../ui/component/VibrationEffectSetting.kt | 214 ++++++++++-------- 2 files changed, 151 insertions(+), 104 deletions(-) diff --git a/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/AppFilterSetting.kt b/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/AppFilterSetting.kt index 6e5853d..b427fe4 100644 --- a/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/AppFilterSetting.kt +++ b/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/AppFilterSetting.kt @@ -1,6 +1,14 @@ package ru.hepolise.volumekeytrackcontrol.ui.component import android.content.SharedPreferences +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.expandVertically +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.shrinkVertically +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.material3.Button import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ExperimentalMaterial3Api @@ -14,20 +22,21 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.sp import androidx.core.content.edit -import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.APP_FILTER_TYPE +import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.AppFilterType import ru.hepolise.volumekeytrackcontrolmodule.R @OptIn(ExperimentalMaterial3Api::class) @Composable fun AppFilterSetting( - value: SharedPreferencesUtil.AppFilterType, + value: AppFilterType, sharedPreferences: SharedPreferences, - onValueChange: (SharedPreferencesUtil.AppFilterType) -> Unit, + onValueChange: (AppFilterType) -> Unit, onNavigateToAppFilter: () -> Unit ) { Text(text = stringResource(R.string.app_filter), fontSize = 20.sp) @@ -51,7 +60,7 @@ fun AppFilterSetting( ExposedDropdownMenu( expanded = expanded, onDismissRequest = { expanded = false }) { - SharedPreferencesUtil.AppFilterType.values.forEach { type -> + AppFilterType.values.forEach { type -> DropdownMenuItem( text = { Text(stringResource(type.resourceId)) }, onClick = { @@ -66,11 +75,27 @@ fun AppFilterSetting( } } - if (value == SharedPreferencesUtil.AppFilterType.WhiteList || value == SharedPreferencesUtil.AppFilterType.BlackList) { - Button( - onClick = onNavigateToAppFilter, + Box { + AnimatedVisibility( + visible = value == AppFilterType.WhiteList || value == AppFilterType.BlackList, + enter = fadeIn() + expandVertically(expandFrom = Alignment.Top), + exit = fadeOut() + shrinkVertically(shrinkTowards = Alignment.Top), + modifier = Modifier.fillMaxWidth() ) { - Text(text = stringResource(R.string.manage_apps, stringResource(value.resourceId))) + Column( + horizontalAlignment = Alignment.CenterHorizontally, + ) { + Button( + onClick = onNavigateToAppFilter, + ) { + Text( + text = stringResource( + R.string.manage_apps, + stringResource(value.resourceId) + ) + ) + } + } } } } diff --git a/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/VibrationEffectSetting.kt b/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/VibrationEffectSetting.kt index 0e04144..a9adc33 100644 --- a/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/VibrationEffectSetting.kt +++ b/app/src/main/java/ru/hepolise/volumekeytrackcontrol/ui/component/VibrationEffectSetting.kt @@ -3,8 +3,16 @@ package ru.hepolise.volumekeytrackcontrol.ui.component import android.content.SharedPreferences import android.os.Build import android.os.Vibrator +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.expandVertically +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.shrinkVertically import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.widthIn import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Edit @@ -107,110 +115,124 @@ fun VibrationEffectSetting( } } - if (vibrationType == VibrationType.Manual) { - Slider( - value = vibrationLength.toFloat(), - onValueChange = { - onValueChange(value.copy(vibrationLength = it.toInt())) - }, - valueRange = 10f..500f, - onValueChangeFinished = { - sharedPreferences.edit { - putInt(VIBRATION_LENGTH, vibrationLength) - } - }, - modifier = Modifier.widthIn(max = 300.dp) - ) - - var showManualVibrationLengthDialog by remember { mutableStateOf(false) } - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - text = stringResource(R.string.vibration_length, vibrationLength), - modifier = Modifier.clickable { showManualVibrationLengthDialog = true } - ) - IconButton( - onClick = { - showManualVibrationLengthDialog = true - } + Box { + AnimatedVisibility( + visible = vibrationType != VibrationType.Disabled, + enter = fadeIn() + expandVertically(expandFrom = Alignment.Top), + exit = fadeOut() + shrinkVertically(shrinkTowards = Alignment.Top), + modifier = Modifier.fillMaxWidth() + ) { + Column( + horizontalAlignment = Alignment.CenterHorizontally, ) { - Icon( - imageVector = Icons.Default.Edit, - contentDescription = stringResource(R.string.edit) - ) - } - } + AnimatedVisibility( + visible = vibrationType == VibrationType.Manual, + enter = fadeIn() + expandVertically(expandFrom = Alignment.Top), + exit = fadeOut() + shrinkVertically(shrinkTowards = Alignment.Top) + ) { + Column(horizontalAlignment = Alignment.CenterHorizontally) { + Slider( + value = vibrationLength.toFloat(), + onValueChange = { + onValueChange(value.copy(vibrationLength = it.toInt())) + }, + valueRange = 10f..500f, + onValueChangeFinished = { + sharedPreferences.edit { + putInt(VIBRATION_LENGTH, vibrationLength) + } + }, + modifier = Modifier.widthIn(max = 300.dp) + ) - if (showManualVibrationLengthDialog) { - NumberAlertDialog( - title = stringResource(R.string.vibration_length_dialog_title), - defaultValue = vibrationLength, - minValue = 10, - maxValue = 500, - onDismissRequest = { showManualVibrationLengthDialog = false }, - onConfirm = { - onValueChange(value.copy(vibrationLength = it)) - sharedPreferences.edit { putInt(VIBRATION_LENGTH, it) } - showManualVibrationLengthDialog = false - } - ) - } + var showManualVibrationLengthDialog by remember { mutableStateOf(false) } + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + text = stringResource(R.string.vibration_length, vibrationLength), + modifier = Modifier.clickable { + showManualVibrationLengthDialog = true + } + ) + IconButton(onClick = { showManualVibrationLengthDialog = true }) { + Icon( + imageVector = Icons.Default.Edit, + contentDescription = stringResource(R.string.edit) + ) + } + } - Slider( - value = vibrationAmplitude.toFloat(), - onValueChange = { - onValueChange(value.copy(vibrationAmplitude = it.toInt())) - }, - valueRange = 1f..255f, - onValueChangeFinished = { - sharedPreferences.edit { - putInt(VIBRATION_AMPLITUDE, vibrationAmplitude) - } - }, - modifier = Modifier.widthIn(max = 300.dp) - ) + if (showManualVibrationLengthDialog) { + NumberAlertDialog( + title = stringResource(R.string.vibration_length_dialog_title), + defaultValue = vibrationLength, + minValue = 10, + maxValue = 500, + onDismissRequest = { showManualVibrationLengthDialog = false }, + onConfirm = { + onValueChange(value.copy(vibrationLength = it)) + sharedPreferences.edit { putInt(VIBRATION_LENGTH, it) } + showManualVibrationLengthDialog = false + } + ) + } - var showVibrationAmplitudeDialog by remember { mutableStateOf(false) } - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - text = stringResource(R.string.vibration_amplitude, vibrationAmplitude), - modifier = Modifier.clickable { showVibrationAmplitudeDialog = true } - ) - IconButton( - onClick = { - showVibrationAmplitudeDialog = true - } - ) { - Icon( - imageVector = Icons.Default.Edit, - contentDescription = stringResource(R.string.edit) - ) - } - } + Slider( + value = vibrationAmplitude.toFloat(), + onValueChange = { + onValueChange(value.copy(vibrationAmplitude = it.toInt())) + }, + valueRange = 1f..255f, + onValueChangeFinished = { + sharedPreferences.edit { + putInt(VIBRATION_AMPLITUDE, vibrationAmplitude) + } + }, + modifier = Modifier.widthIn(max = 300.dp) + ) + + var showVibrationAmplitudeDialog by remember { mutableStateOf(false) } + Row(verticalAlignment = Alignment.CenterVertically) { + Text( + text = stringResource( + R.string.vibration_amplitude, + vibrationAmplitude + ), + modifier = Modifier.clickable { + showVibrationAmplitudeDialog = true + } + ) + IconButton(onClick = { showVibrationAmplitudeDialog = true }) { + Icon( + imageVector = Icons.Default.Edit, + contentDescription = stringResource(R.string.edit) + ) + } + } - if (showVibrationAmplitudeDialog) { - NumberAlertDialog( - title = stringResource(R.string.vibration_amplitude_dialog_title), - defaultValue = vibrationAmplitude, - minValue = 1, - maxValue = 255, - onDismissRequest = { showVibrationAmplitudeDialog = false }, - onConfirm = { - onValueChange(value.copy(vibrationAmplitude = it)) - sharedPreferences.edit { - putInt(VIBRATION_AMPLITUDE, it) + if (showVibrationAmplitudeDialog) { + NumberAlertDialog( + title = stringResource(R.string.vibration_amplitude_dialog_title), + defaultValue = vibrationAmplitude, + minValue = 1, + maxValue = 255, + onDismissRequest = { showVibrationAmplitudeDialog = false }, + onConfirm = { + onValueChange(value.copy(vibrationAmplitude = it)) + sharedPreferences.edit { + putInt(VIBRATION_AMPLITUDE, it) + } + showVibrationAmplitudeDialog = false + } + ) + } } - showVibrationAmplitudeDialog = false } - ) - } - - } - if (vibrationType != VibrationType.Disabled) { - Button(onClick = { - vibrator?.triggerVibration(sharedPreferences) - }) { - Text(stringResource(R.string.test_vibration)) + Button(onClick = { vibrator?.triggerVibration(sharedPreferences) }) { + Text(stringResource(R.string.test_vibration)) + } + } } } + } \ No newline at end of file