mirror of
https://github.com/Myzel394/Alibi.git
synced 2025-06-19 07:15:25 +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(
|
fun initialize(
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
|
showOngoing: Boolean = true,
|
||||||
|
icon: Int = R.drawable.launcher_monochrome_noopacity,
|
||||||
) {
|
) {
|
||||||
if (_hasBeenInitialized) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_title.value = title
|
_title.value = title
|
||||||
_description.value = description
|
_description.value = description
|
||||||
|
this.showOngoing = showOngoing
|
||||||
|
this.icon = icon
|
||||||
_hasBeenInitialized = true
|
_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.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import app.myzel394.alibi.R
|
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.db.NotificationSettings
|
||||||
import app.myzel394.alibi.ui.components.CustomRecordingNotificationsScreen.models.NotificationViewModel
|
import app.myzel394.alibi.ui.components.CustomRecordingNotificationsScreen.models.NotificationViewModel
|
||||||
import app.myzel394.alibi.ui.components.CustomRecordingNotificationsScreen.molecules.EditNotificationInput
|
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;
|
val HORIZONTAL_PADDING = 16.dp;
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun NotificationEditor(
|
fun NotificationEditor(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
notificationModel: NotificationViewModel = viewModel(),
|
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 dataStore = LocalContext.current.dataStore
|
||||||
val defaultDescription = stringResource(R.string.ui_audioRecorder_state_recording_description)
|
val settings = dataStore
|
||||||
|
.data
|
||||||
|
.collectAsState(initial = AppSettings.getDefaultInstance())
|
||||||
|
.value
|
||||||
|
|
||||||
LaunchedEffect(defaultTitle, defaultDescription) {
|
if (settings.notificationSettings != null) {
|
||||||
notificationModel.initialize(
|
val title = settings.notificationSettings.let {
|
||||||
defaultTitle,
|
if (it.preset != null)
|
||||||
defaultDescription,
|
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(
|
Column(
|
||||||
@ -128,11 +170,7 @@ fun NotificationEditor(
|
|||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
onNotificationChange(
|
onNotificationChange(
|
||||||
notificationModel.title,
|
notificationModel.asNotificationSettings()
|
||||||
notificationModel.description,
|
|
||||||
notificationModel.icon,
|
|
||||||
notificationModel.showOngoing,
|
|
||||||
notificationModel.notificationPreset,
|
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
@ -99,23 +99,13 @@ fun CustomRecordingNotificationsScreen(
|
|||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(padding)
|
.padding(padding)
|
||||||
.padding(vertical = 16.dp),
|
.padding(vertical = 16.dp),
|
||||||
onNotificationChange = { title, description, icon, showOngoing, preset ->
|
onNotificationChange = { notificationSettings ->
|
||||||
scope.launch {
|
scope.launch {
|
||||||
dataStore.updateData { settings ->
|
dataStore.updateData { settings ->
|
||||||
settings.setNotificationSettings(
|
settings.setNotificationSettings(notificationSettings)
|
||||||
if (preset == null) {
|
|
||||||
NotificationSettings(
|
|
||||||
title = title,
|
|
||||||
message = description,
|
|
||||||
iconID = icon,
|
|
||||||
showOngoing = showOngoing,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
NotificationSettings.fromPreset(preset)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
navController.popBackStack()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user