Skip to content

Commit

Permalink
Use updated LocalDataManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
MykytaPimonovTD committed Nov 20, 2024
1 parent 0172b01 commit 57e913d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ internal class LocalDataManager {
.vBuild()

/**
* Indicates that the current user is logged into the app.
* Specifies that the current user is logged into the app.
*/
internal fun confirmLogin() {
isNewUnlogged = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ public abstract class LoginStage<T> {
* @property client Enables interaction with the Pingh server.
* @property local Manages the local data for users of the application.
* @property establishSession Updates the application state when a session is established.
* @property confirmLogin Specifies that the user is logged into the app.
*/
public class LoginFlow internal constructor(
private val client: DesktopClient,
private val local: LocalDataManager,
private val establishSession: (SessionId) -> Unit
private val establishSession: (SessionId) -> Unit,
private val confirmLogin: () -> Unit
) {
/**
* Current stage of the GitHub login process.
Expand Down Expand Up @@ -101,7 +103,7 @@ public class LoginFlow internal constructor(
"of the `EnterUsername` stage."
}
stage.value = VerifyLogin(
client, local, ::moveToNextStage,
client, local, ::moveToNextStage, confirmLogin,
stage.value.result as UserCodeReceived
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class MentionsFlow internal constructor(
* Throws an `IllegalStateException` exception if the user is not logged in.
*/
private fun ensureLoggedIn() {
check(!local.isGuest()) { "The user is not logged in." }
check(local.isLoggedIn) { "The user is not logged in." }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public class PinghApplication private constructor(
private set

init {
client = if (local.isGuest()) {
DesktopClient(channel)
} else {
client = if (local.isLoggedIn) {
DesktopClient(channel, local.session.asUserId())
} else {
DesktopClient(channel)
}
}

Expand All @@ -107,7 +107,7 @@ public class PinghApplication private constructor(
private val notificationsFlow = NotificationsFlow(notificationSender, local)

init {
if (!local.isGuest()) {
if (local.isLoggedIn) {
notificationsFlow.enableNotifications(client, local.user)
}
}
Expand All @@ -125,6 +125,13 @@ public class PinghApplication private constructor(
notificationsFlow.enableNotifications(client, id.username)
}

/**
* Specifies that the user is logged into the app.
*/
private fun confirmLogin() {
local.confirmLogin()
}

/**
* Updates the application state when a session is closed:
*
Expand All @@ -142,14 +149,14 @@ public class PinghApplication private constructor(
/**
* Returns `true` if the user is logged in to the application.
*/
public fun isLoggedIn(): Boolean = !local.isGuest()
public fun isLoggedIn(): Boolean = local.isLoggedIn

/**
* Initiates the login flow and terminates any previous flow, if it exists.
*/
public fun startLoginFlow(): LoginFlow {
loginFlow?.close()
loginFlow = LoginFlow(client, local, ::establishSession)
loginFlow = LoginFlow(client, local, ::establishSession, ::confirmLogin)
return loginFlow!!
}

Expand Down Expand Up @@ -183,6 +190,7 @@ public class PinghApplication private constructor(
public fun close() {
loginFlow?.close()
settingsFlow?.saveSettings()
local.clear()
client.close()
channel.shutdown()
.awaitTermination(defaultShutdownTimeout, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ import kotlinx.coroutines.launch
* @property client Enables interaction with the Pingh server.
* @property local Manages the local data for users of the application.
* @property moveToNextStage Switches the current stage to the [LoginFailed].
* @property confirmLogin Specifies that the user is logged into the app.
* @param event The event received after the user enters their name.
*/
@Suppress("MemberVisibilityCanBePrivate" /* Accessed from `desktop` module. */)
public class VerifyLogin internal constructor(
private val client: DesktopClient,
private val local: LocalDataManager,
private val moveToNextStage: () -> Unit,
private val confirmLogin: () -> Unit,
event: UserCodeReceived
) : LoginStage<String>() {

Expand Down Expand Up @@ -164,6 +166,7 @@ public class VerifyLogin internal constructor(
client.observeEither(
EventObserver(command.id, UserLoggedIn::class) {
codeExpirationJob.cancel()
confirmLogin()
future.complete(ActionOutcome.Success)
},
EventObserver(command.id, UserIsNotLoggedIntoGitHub::class) {
Expand Down

0 comments on commit 57e913d

Please sign in to comment.