Skip to content

Commit

Permalink
Update client flows.
Browse files Browse the repository at this point in the history
  • Loading branch information
MykytaPimonovTD committed Nov 15, 2024
1 parent 64af009 commit e8e2d08
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public abstract class LoginStage<T> {
* [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
) {
/**
Expand Down Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -104,7 +102,7 @@ public class MentionsFlow internal constructor(
public fun allMentions(): List<MentionView> {
ensureLoggedIn()
val userMentions = client.readById(
UserMentionsId::class.of(session.username),
UserMentionsId::class.of(local.session.username),
UserMentions::class
)
return userMentions
Expand All @@ -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)
}

/**
Expand Down Expand Up @@ -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." }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -45,34 +42,34 @@ 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.
*
* @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)
Expand All @@ -85,15 +82,15 @@ public class SettingsFlow internal constructor(
*/
@Suppress("MemberVisibilityCanBePrivate" /* Accessed from `desktop` module. */)
public fun saveSettings() {
userSettings.save()
local.settings = mutableSettings.vBuild()
}
}

/**
* 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)
Expand Down

0 comments on commit e8e2d08

Please sign in to comment.