Skip to content

Commit

Permalink
rewrite dialog stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Metaroll committed Dec 11, 2024
1 parent ca26863 commit 510d714
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 47 deletions.
95 changes: 52 additions & 43 deletions app/src/main/java/com/mensinator/app/settings/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -454,62 +502,23 @@ 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
) {
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
Expand Down
29 changes: 25 additions & 4 deletions app/src/main/java/com/mensinator/app/settings/SettingsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
}
}

enum class StringSetting(val stringResId: Int, val settingDbKey: String) {
PERIOD_NOTIFICATION_MESSAGE(R.string.period_notification_message, "period_notification_message"),
}

0 comments on commit 510d714

Please sign in to comment.