mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-18 23:05:26 +02:00
feat: Properly update notification settings
This commit is contained in:
parent
f1296c32ec
commit
6ad7ff12e6
@ -55,13 +55,26 @@ class NotificationViewModel : ViewModel() {
|
||||
fun initialize(
|
||||
title: String,
|
||||
description: String,
|
||||
showOngoing: Boolean = true,
|
||||
icon: Int = R.drawable.launcher_monochrome_noopacity,
|
||||
) {
|
||||
if (_hasBeenInitialized) {
|
||||
return
|
||||
}
|
||||
|
||||
_title.value = title
|
||||
_description.value = description
|
||||
this.showOngoing = showOngoing
|
||||
this.icon = icon
|
||||
_hasBeenInitialized = true
|
||||
}
|
||||
|
||||
fun asNotificationSettings(): NotificationSettings {
|
||||
return if (!_presetChanged && notificationPreset != null) {
|
||||
NotificationSettings.fromPreset(notificationPreset!!)
|
||||
} else {
|
||||
NotificationSettings(
|
||||
title = title,
|
||||
message = description,
|
||||
iconID = icon,
|
||||
showOngoing = showOngoing,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,15 +25,19 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import app.myzel394.alibi.R
|
||||
import app.myzel394.alibi.dataStore
|
||||
import app.myzel394.alibi.db.AppSettings
|
||||
import app.myzel394.alibi.db.NotificationSettings
|
||||
import app.myzel394.alibi.ui.components.CustomRecordingNotificationsScreen.models.NotificationViewModel
|
||||
import app.myzel394.alibi.ui.components.CustomRecordingNotificationsScreen.molecules.EditNotificationInput
|
||||
@ -41,21 +45,59 @@ import app.myzel394.alibi.ui.components.CustomRecordingNotificationsScreen.molec
|
||||
|
||||
val HORIZONTAL_PADDING = 16.dp;
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun NotificationEditor(
|
||||
modifier: Modifier = Modifier,
|
||||
notificationModel: NotificationViewModel = viewModel(),
|
||||
onNotificationChange: (String, String, Int, Boolean, NotificationSettings.Preset?) -> Unit,
|
||||
onNotificationChange: (NotificationSettings) -> Unit,
|
||||
) {
|
||||
val defaultTitle = stringResource(R.string.ui_audioRecorder_state_recording_title)
|
||||
val defaultDescription = stringResource(R.string.ui_audioRecorder_state_recording_description)
|
||||
val dataStore = LocalContext.current.dataStore
|
||||
val settings = dataStore
|
||||
.data
|
||||
.collectAsState(initial = AppSettings.getDefaultInstance())
|
||||
.value
|
||||
|
||||
LaunchedEffect(defaultTitle, defaultDescription) {
|
||||
notificationModel.initialize(
|
||||
defaultTitle,
|
||||
defaultDescription,
|
||||
)
|
||||
if (settings.notificationSettings != null) {
|
||||
val title = settings.notificationSettings.let {
|
||||
if (it.preset != null)
|
||||
stringResource(it.preset.titleID)
|
||||
else
|
||||
it.title
|
||||
}
|
||||
val description = settings.notificationSettings.let {
|
||||
if (it.preset != null)
|
||||
stringResource(it.preset.messageID)
|
||||
else
|
||||
it.message
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
notificationModel.initialize(
|
||||
title,
|
||||
description,
|
||||
settings.notificationSettings.showOngoing,
|
||||
settings.notificationSettings.iconID,
|
||||
)
|
||||
|
||||
if (settings.notificationSettings.preset != null) {
|
||||
notificationModel.setPreset(
|
||||
title,
|
||||
description,
|
||||
settings.notificationSettings.preset
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val defaultTitle = stringResource(R.string.ui_audioRecorder_state_recording_title)
|
||||
val defaultDescription =
|
||||
stringResource(R.string.ui_audioRecorder_state_recording_description)
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
notificationModel.initialize(
|
||||
defaultTitle,
|
||||
defaultDescription,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
@ -128,11 +170,7 @@ fun NotificationEditor(
|
||||
Button(
|
||||
onClick = {
|
||||
onNotificationChange(
|
||||
notificationModel.title,
|
||||
notificationModel.description,
|
||||
notificationModel.icon,
|
||||
notificationModel.showOngoing,
|
||||
notificationModel.notificationPreset,
|
||||
notificationModel.asNotificationSettings()
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
|
@ -99,23 +99,13 @@ fun CustomRecordingNotificationsScreen(
|
||||
modifier = Modifier
|
||||
.padding(padding)
|
||||
.padding(vertical = 16.dp),
|
||||
onNotificationChange = { title, description, icon, showOngoing, preset ->
|
||||
onNotificationChange = { notificationSettings ->
|
||||
scope.launch {
|
||||
dataStore.updateData { settings ->
|
||||
settings.setNotificationSettings(
|
||||
if (preset == null) {
|
||||
NotificationSettings(
|
||||
title = title,
|
||||
message = description,
|
||||
iconID = icon,
|
||||
showOngoing = showOngoing,
|
||||
)
|
||||
} else {
|
||||
NotificationSettings.fromPreset(preset)
|
||||
}
|
||||
)
|
||||
settings.setNotificationSettings(notificationSettings)
|
||||
}
|
||||
}
|
||||
navController.popBackStack()
|
||||
}
|
||||
)
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user