-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite
UserSession
for proto files.
- Loading branch information
1 parent
65817df
commit 5e38eba
Showing
2 changed files
with
63 additions
and
107 deletions.
There are no files selected for viewing
107 changes: 0 additions & 107 deletions
107
client/src/main/kotlin/io/spine/examples/pingh/client/UserSession.kt
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
client/src/main/kotlin/io/spine/examples/pingh/client/UserSessionExts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2024, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.examples.pingh.client | ||
|
||
import io.spine.examples.pingh.github.Username | ||
import io.spine.examples.pingh.github.of | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* The username to which the current session belongs. | ||
*/ | ||
internal val UserSession.username: Username | ||
get() = this.id?.username ?: Guest.username | ||
|
||
/** | ||
* Returns `true` if the session is authenticated, and `false` if it is a guest session. | ||
*/ | ||
internal fun UserSession.isAuthenticated() = this != Guest.session | ||
|
||
/** | ||
* Saves the user session data to a file in the user's data directory. | ||
*/ | ||
internal fun UserSession.save() { | ||
FileStorage.save(FileLocation.Session, this) | ||
} | ||
|
||
/** | ||
* Loads the user session data from a file in the user's data directory. | ||
* | ||
* Returns a guest session if the file is empty. | ||
*/ | ||
@Suppress("UnusedReceiverParameter" /* Associated with the class but doesn't use its data. */) | ||
internal fun KClass<UserSession>.loadOrDefault(): UserSession = | ||
FileStorage.loadOrDefault(FileLocation.Session) { Guest.session } | ||
|
||
internal object Guest { | ||
internal val session = UserSession.getDefaultInstance()!! | ||
internal val username = Username::class.of("unauthenticated-guest") | ||
} |