diff --git a/app/src/main/java/com/mensinator/app/settings/SettingsScreen.kt b/app/src/main/java/com/mensinator/app/settings/SettingsScreen.kt index e1edd86..42a8b0b 100644 --- a/app/src/main/java/com/mensinator/app/settings/SettingsScreen.kt +++ b/app/src/main/java/com/mensinator/app/settings/SettingsScreen.kt @@ -132,9 +132,18 @@ fun SettingsScreen( ) SettingText( text = stringResource(R.string.period_notification_message), - dbKey = "period_notification_message" + onClick = { viewModel.showPeriodNotificationDialog(true) } ) + if (viewState.showPeriodNotificationDialog) { + NotificationDialog( + title = stringResource(R.string.period_notification_message), + messageText = viewState.periodNotificationMessage, + onSave = { viewModel.updateStringSetting(StringSetting.PERIOD_NOTIFICATION_MESSAGE, it) }, + onDismissRequest = { viewModel.showPeriodNotificationDialog(false) }, + ) + } + Spacer(Modifier.height(16.dp)) SettingSectionHeader(text = stringResource(R.string.other_settings)) @@ -216,6 +225,45 @@ fun SettingsScreen( } } +@Composable +fun NotificationDialog( + title: String, + messageText: String, + onDismissRequest: () -> Unit, + onSave: (String) -> Unit +) { + var newMessageText by remember { mutableStateOf(messageText) } + + AlertDialog( + title = { Text(title) }, + text = { + TextField( + value = newMessageText, + onValueChange = { newMessageText = it }, + singleLine = false + ) + }, + confirmButton = { + Button(onClick = { + onSave(newMessageText) + onDismissRequest() + }) { + Text(text = stringResource(id = R.string.save_button)) + } + }, + onDismissRequest = onDismissRequest, + dismissButton = { + Button( + onClick = { + onDismissRequest() + } + ) { + Text(text = stringResource(id = R.string.cancel_button)) + } + } + ) +} + @Composable @OptIn(ExperimentalLayoutApi::class) private fun AboutSection( @@ -454,16 +502,10 @@ private fun ColorPickerPreview() { @Composable private fun SettingText( text: String, - modifier: Modifier = Modifier, - dbKey: String, + onClick: () -> Unit ) { - val dbHelper: IPeriodDatabaseHelper = koinInject() - val message = dbHelper.getSettingByKey(dbKey)?.value.toString() - var showDialog by remember { mutableStateOf(false) } - var newMessage by remember { mutableStateOf(message) } - Row( - modifier = modifier + modifier = Modifier .fillMaxWidth() .padding(vertical = 16.dp), verticalAlignment = Alignment.CenterVertically @@ -471,45 +513,12 @@ private fun SettingText( Text(text = text, modifier = Modifier.weight(1f), maxLines = 1) Spacer(Modifier.width(4.dp)) TextButton( - onClick = { showDialog = true }, + onClick = onClick, colors = ButtonDefaults.filledTonalButtonColors() ) { Text(text = stringResource(id = R.string.change_text)) } } - - if (showDialog) { - AlertDialog( - onDismissRequest = { - newMessage = message - showDialog = false - }, - title = { Text(text = text) }, - text = { - TextField( - value = newMessage, - onValueChange = { newMessage = it }, - singleLine = false - ) - }, - confirmButton = { - Button(onClick = { - dbHelper.updateSetting(key = dbKey, value = newMessage) - showDialog = false - }) { - Text(text = stringResource(id = R.string.save_button)) - } - }, - dismissButton = { - Button(onClick = { - newMessage = message - showDialog = false - }) { - Text(text = stringResource(id = R.string.cancel_button)) - } - } - ) - } } @Composable diff --git a/app/src/main/java/com/mensinator/app/settings/SettingsViewModel.kt b/app/src/main/java/com/mensinator/app/settings/SettingsViewModel.kt index abb18cb..1b090c9 100644 --- a/app/src/main/java/com/mensinator/app/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/mensinator/app/settings/SettingsViewModel.kt @@ -36,6 +36,8 @@ class SettingsViewModel( openColorPickerForSetting = null, daysBeforeReminder = -1, + periodNotificationMessage = "Your period is about to start", + showPeriodNotificationDialog = false, daysForPeriodHistory = -1, daysForOvulationHistory = -1, openIntPickerForSetting = null, @@ -68,6 +70,8 @@ class SettingsViewModel( val openColorPickerForSetting: ColorSetting? = null, val daysBeforeReminder: Int, + val periodNotificationMessage: String, + val showPeriodNotificationDialog: Boolean, val daysForPeriodHistory: Int, val daysForOvulationHistory: Int, val openIntPickerForSetting: IntSetting? = null, @@ -107,6 +111,7 @@ class SettingsViewModel( expectedOvulationColor = getColor(isDarkMode, EXPECTED_OVULATION.settingDbKey), daysBeforeReminder = getInt(IntSetting.REMINDER_DAYS.settingDbKey), + periodNotificationMessage = getString(StringSetting.PERIOD_NOTIFICATION_MESSAGE.settingDbKey), daysForPeriodHistory = getInt(IntSetting.PERIOD_HISTORY.settingDbKey), daysForOvulationHistory = getInt(IntSetting.OVULATION_HISTORY.settingDbKey), @@ -135,16 +140,23 @@ class SettingsViewModel( refreshData() } + fun updateStringSetting(stringSetting: StringSetting, newString: String) { + periodDatabaseHelper.updateSetting(stringSetting.settingDbKey, newString) + refreshData() + } + fun showColorPicker(colorSetting: ColorSetting?) { - _viewState.update { - it.copy(openColorPickerForSetting = colorSetting) - } + _viewState.update { it.copy(openColorPickerForSetting = colorSetting) } } fun showIntPicker(intSetting: IntSetting?) { _viewState.update { it.copy(openIntPickerForSetting = intSetting) } } + fun showPeriodNotificationDialog(show: Boolean) { + _viewState.update { it.copy(showPeriodNotificationDialog = show) } + } + fun showFaqDialog(show: Boolean) { _viewState.update { it.copy(showFaqDialog = show) } } @@ -210,6 +222,11 @@ class SettingsViewModel( return value } + private fun getString(settingKey: String): String { + val string = periodDatabaseHelper.getSettingByKey(settingKey)?.value.toString() + return string + } + private fun getAppVersion(context: Context): String { return try { val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0) @@ -240,4 +257,8 @@ enum class BooleanSetting(val stringResId: Int, val settingDbKey: String) { LUTEAL_PHASE_CALCULATION(R.string.luteal_phase_calculation, "luteal_period_calculation"), SHOW_CYCLE_NUMBERS(R.string.cycle_numbers_show, "cycle_numbers_show"), PREVENT_SCREENSHOTS(R.string.screen_protection, "screen_protection"), -} \ No newline at end of file +} + +enum class StringSetting(val stringResId: Int, val settingDbKey: String) { + PERIOD_NOTIFICATION_MESSAGE(R.string.period_notification_message, "period_notification_message"), +}