Skip to content

Commit

Permalink
Remove default values from NotificationSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmb13 committed Feb 11, 2025
1 parent 36d8a65 commit 0895106
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,14 @@ class ConferenceService(
fun requestNotificationPermissions() {
scope.launch {
storage.setNotificationsAllowed(true)
storage.setNotificationSettings(NotificationSettings()) // Set default values (all true)
notificationManager.requestPermission()
}
}

fun getNotificationSettings(): Flow<NotificationSettings> = storage.getNotificationSettings()

fun setNotificationSettings(settings: NotificationSettings) {
scope.launch {
storage.setNotificationSettings(settings)
}
suspend fun setNotificationSettings(settings: NotificationSettings) {
storage.setNotificationSettings(settings)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class NewsItem(

@Serializable
data class NotificationSettings(
val scheduleUpdates: Boolean = true,
val kotlinConfNews: Boolean = true,
val jetbrainsNews: Boolean = true,
val scheduleUpdates: Boolean,
val kotlinConfNews: Boolean,
val jetbrainsNews: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ private fun SettingsScreenImpl(

Divider(thickness = 1.dp, color = KotlinConfTheme.colors.strokePale)

val notificationSettings by viewModel.notificationSettings.collectAsStateWithLifecycle()
NotificationSettings(
notificationSettings = notificationSettings,
onChangeSettings = { newSettings ->
viewModel.setNotificationSettings(newSettings)
onNotificationSettingsChange(newSettings)
}
)
val notificationSettings = viewModel.notificationSettings.collectAsStateWithLifecycle().value
if (notificationSettings != null)
NotificationSettings(
notificationSettings = notificationSettings,
onChangeSettings = { newSettings ->
viewModel.setNotificationSettings(newSettings)
onNotificationSettingsChange(newSettings)
}
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.jetbrains.kotlinconf.ConferenceService
import org.jetbrains.kotlinconf.NotificationSettings
import org.jetbrains.kotlinconf.Theme
Expand All @@ -15,14 +16,16 @@ class SettingsViewModel(
val theme: StateFlow<Theme> = service.getTheme()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), Theme.SYSTEM)

val notificationSettings: StateFlow<NotificationSettings> = service.getNotificationSettings()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), NotificationSettings())
val notificationSettings: StateFlow<NotificationSettings?> = service.getNotificationSettings()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)

fun setTheme(theme: Theme) {
service.setTheme(theme)
}

fun setNotificationSettings(settings: NotificationSettings) {
service.setNotificationSettings(settings)
viewModelScope.launch {
service.setNotificationSettings(settings)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.jetbrains.kotlinconf.ConferenceService
import org.jetbrains.kotlinconf.NotificationSettings

class StartNotificationsViewModel(
private val service: ConferenceService,
) : ViewModel() {
val notificationSettings: StateFlow<NotificationSettings> = service.getNotificationSettings()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), NotificationSettings())
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), NotificationSettings(true, true, true))

fun setNotificationSettings(settings: NotificationSettings) {
service.setNotificationSettings(settings)
viewModelScope.launch {
service.setNotificationSettings(settings)
}
}

fun requestNotificationPermissions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class MultiplatformSettingsStorage(context: ApplicationContext) : ApplicationSto

override fun getNotificationSettings(): Flow<NotificationSettings> =
settings.getStringOrNullFlow(Keys.NOTIFICATION_SETTINGS)
.map { it?.let { Json.decodeFromString<NotificationSettings>(it) } ?: NotificationSettings() }

.map { it?.let { Json.decodeFromString<NotificationSettings>(it) } ?: NotificationSettings(true, true, true) }
override suspend fun setNotificationSettings(value: NotificationSettings) = settings.set(
Keys.NOTIFICATION_SETTINGS,
Json.encodeToString(value)
Expand Down

0 comments on commit 0895106

Please sign in to comment.