diff --git a/client/src/main/kotlin/io/spine/examples/pingh/client/LoginFlow.kt b/client/src/main/kotlin/io/spine/examples/pingh/client/LoginFlow.kt index c9efd452..96bc48f3 100644 --- a/client/src/main/kotlin/io/spine/examples/pingh/client/LoginFlow.kt +++ b/client/src/main/kotlin/io/spine/examples/pingh/client/LoginFlow.kt @@ -62,12 +62,12 @@ public abstract class LoginStage { * [confirmed][VerifyLogin.confirm] in the Pingh app. * * @property client Enables interaction with the Pingh server. - * @property session The information about the current user session. + * @property local The local user data * @property establishSession Updates the application state when a session is established. */ public class LoginFlow internal constructor( private val client: DesktopClient, - private val session: UserSession, + private val local: LocalData, private val establishSession: (SessionId) -> Unit ) { /** @@ -101,7 +101,7 @@ public class LoginFlow internal constructor( "of the `EnterUsername` stage." } stage.value = VerifyLogin( - client, session, ::moveToNextStage, + client, local.session, ::moveToNextStage, stage.value.result as UserCodeReceived ) } diff --git a/client/src/main/kotlin/io/spine/examples/pingh/client/MentionsFlow.kt b/client/src/main/kotlin/io/spine/examples/pingh/client/MentionsFlow.kt index 2b3898f0..bef75d75 100644 --- a/client/src/main/kotlin/io/spine/examples/pingh/client/MentionsFlow.kt +++ b/client/src/main/kotlin/io/spine/examples/pingh/client/MentionsFlow.kt @@ -52,13 +52,11 @@ import kotlinx.coroutines.flow.MutableStateFlow * as commands to the server-side. * * @property client Enables interaction with the Pingh server. - * @property session The information about the current user session. - * @property settings The information about the application settings. + * @property local The local user data. */ public class MentionsFlow internal constructor( private val client: DesktopClient, - private val session: UserSession, - private val settings: UserSettings + private val local: LocalData ) { /** * User mentions. @@ -75,7 +73,7 @@ public class MentionsFlow internal constructor( */ private fun subscribeToMentionsUpdates() { ensureLoggedIn() - val id = UserMentionsId::class.of(session.username) + val id = UserMentionsId::class.of(local.session.username) client.observeEntity(id, UserMentions::class) { entity -> mentions.value = entity.mentionList } @@ -91,7 +89,7 @@ public class MentionsFlow internal constructor( public fun updateMentions() { ensureLoggedIn() val command = UpdateMentionsFromGitHub::class.buildBy( - GitHubClientId::class.of(session.username) + GitHubClientId::class.of(local.session.username) ) client.send(command) } @@ -104,7 +102,7 @@ public class MentionsFlow internal constructor( public fun allMentions(): List { ensureLoggedIn() val userMentions = client.readById( - UserMentionsId::class.of(session.username), + UserMentionsId::class.of(local.session.username), UserMentions::class ) return userMentions @@ -122,7 +120,7 @@ public class MentionsFlow internal constructor( * @param id The identifier of the mention to be snoozed. */ public fun snooze(id: MentionId) { - snooze(id, settings.snoozeTime.value) + snooze(id, local.settings.snoozeTime.value) } /** @@ -152,7 +150,7 @@ public class MentionsFlow internal constructor( * Throws an `IllegalStateException` exception if the user is not logged in. */ private fun ensureLoggedIn() { - check(session.isAuthenticated()) { "The user is not logged in." } + check(local.session.isAuthenticated()) { "The user is not logged in." } } } diff --git a/client/src/main/kotlin/io/spine/examples/pingh/client/NotificationsFlow.kt b/client/src/main/kotlin/io/spine/examples/pingh/client/NotificationsFlow.kt index 7e12f2e8..babfe1f7 100644 --- a/client/src/main/kotlin/io/spine/examples/pingh/client/NotificationsFlow.kt +++ b/client/src/main/kotlin/io/spine/examples/pingh/client/NotificationsFlow.kt @@ -66,11 +66,11 @@ public interface NotificationSender { * before initiating notifications for a new `client`. * * @property sender Allows to send notifications. - * @property settings The information about the application settings. + * @property local The local user data. */ internal class NotificationsFlow( private val sender: NotificationSender, - private val settings: UserSettings + private val local: LocalData ) { companion object { /** @@ -115,7 +115,7 @@ internal class NotificationsFlow( notification.onEvent, eq(usernameField(), username) ) { event -> - if (!settings.enabledDndMode) { + if (!local.settings.enabledDndMode) { sender.send(notification.title, notification.content(event)) } } diff --git a/client/src/main/kotlin/io/spine/examples/pingh/client/SettingsFlow.kt b/client/src/main/kotlin/io/spine/examples/pingh/client/SettingsFlow.kt index 258fd543..e3265e08 100644 --- a/client/src/main/kotlin/io/spine/examples/pingh/client/SettingsFlow.kt +++ b/client/src/main/kotlin/io/spine/examples/pingh/client/SettingsFlow.kt @@ -26,13 +26,10 @@ package io.spine.examples.pingh.client -import com.google.protobuf.Duration import io.spine.examples.pingh.github.Username import io.spine.examples.pingh.sessions.withSession import io.spine.examples.pingh.sessions.command.LogUserOut import io.spine.examples.pingh.sessions.event.UserLoggedOut -import io.spine.protobuf.Durations2.hours -import io.spine.protobuf.Durations2.minutes import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -45,26 +42,26 @@ import kotlinx.coroutines.flow.StateFlow * use [saveSettings()][saveSettings] method. * * @property client Enables interaction with the Pingh server. - * @property session The information about the current user session. - * @property userSettings The information about the application settings. + * @property local The local user data. * @property closeSession Updates the application state when a session is closed. */ public class SettingsFlow internal constructor( private val client: DesktopClient, - private val session: UserSession, - private val userSettings: UserSettings, + private val local: LocalData, private val closeSession: () -> Unit ) { + private val mutableSettings = local.settings.toBuilder() + /** * The state of application settings. */ - public val settings: SettingsState = SettingsState(userSettings) + public val settings: SettingsState = SettingsState(mutableSettings) /** * The username to which the current session belongs. */ public val username: Username - get() = session.username + get() = local.session.username /** * Logs the user out, cancels all subscriptions and clears the session ID. @@ -72,7 +69,7 @@ public class SettingsFlow internal constructor( * @param onSuccess Called when the user successfully logs out. */ public fun logOut(onSuccess: (event: UserLoggedOut) -> Unit = {}) { - val command = LogUserOut::class.withSession(session.id) + val command = LogUserOut::class.withSession(local.session.id) client.observeEvent(command.id, UserLoggedOut::class) { event -> closeSession() onSuccess(event) @@ -85,7 +82,7 @@ public class SettingsFlow internal constructor( */ @Suppress("MemberVisibilityCanBePrivate" /* Accessed from `desktop` module. */) public fun saveSettings() { - userSettings.save() + local.settings = mutableSettings.vBuild() } } @@ -93,7 +90,7 @@ public class SettingsFlow internal constructor( * State of application settings. */ public class SettingsState internal constructor( - private val data: UserSettings + private val data: UserSettings.Builder ) { private val _enabledDndMode = MutableStateFlow(data.enabledDndMode) private val _snoozeTime = MutableStateFlow(data.snoozeTime)