SignedOut mode - WIP
This commit is contained in:
parent
fea1fe0f3c
commit
0b3fcecbed
13 changed files with 105 additions and 41 deletions
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.sessionstorage.api
|
||||
|
||||
sealed interface LoggedInState {
|
||||
data object NotLoggedIn : LoggedInState
|
||||
data class LoggedIn(val isTokenValid: Boolean) : LoggedInState
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ import kotlinx.coroutines.flow.Flow
|
|||
import kotlinx.coroutines.flow.map
|
||||
|
||||
interface SessionStore {
|
||||
fun isLoggedIn(): Flow<Boolean>
|
||||
fun isLoggedIn(): Flow<LoggedInState>
|
||||
fun sessionsFlow(): Flow<List<SessionData>>
|
||||
suspend fun storeData(sessionData: SessionData)
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package io.element.android.libraries.sessionstorage.impl.memory
|
||||
|
||||
import io.element.android.libraries.sessionstorage.api.LoggedInState
|
||||
import io.element.android.libraries.sessionstorage.api.SessionData
|
||||
import io.element.android.libraries.sessionstorage.api.SessionStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -26,8 +27,14 @@ class InMemorySessionStore : SessionStore {
|
|||
|
||||
private var sessionDataFlow = MutableStateFlow<SessionData?>(null)
|
||||
|
||||
override fun isLoggedIn(): Flow<Boolean> {
|
||||
return sessionDataFlow.map { it != null }
|
||||
override fun isLoggedIn(): Flow<LoggedInState> {
|
||||
return sessionDataFlow.map {
|
||||
if (it == null) {
|
||||
LoggedInState.NotLoggedIn
|
||||
} else {
|
||||
LoggedInState.LoggedIn(it.isTokenValid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun sessionsFlow(): Flow<List<SessionData>> {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import com.squareup.sqldelight.runtime.coroutines.mapToList
|
|||
import com.squareup.sqldelight.runtime.coroutines.mapToOneOrNull
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import io.element.android.libraries.sessionstorage.api.LoggedInState
|
||||
import io.element.android.libraries.sessionstorage.api.SessionData
|
||||
import io.element.android.libraries.sessionstorage.api.SessionStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -35,11 +36,17 @@ class DatabaseSessionStore @Inject constructor(
|
|||
private val database: SessionDatabase,
|
||||
) : SessionStore {
|
||||
|
||||
override fun isLoggedIn(): Flow<Boolean> {
|
||||
override fun isLoggedIn(): Flow<LoggedInState> {
|
||||
return database.sessionDataQueries.selectFirst()
|
||||
.asFlow()
|
||||
.mapToOneOrNull()
|
||||
.map { it != null }
|
||||
.map {
|
||||
if (it == null) {
|
||||
LoggedInState.NotLoggedIn
|
||||
} else {
|
||||
LoggedInState.LoggedIn((it.isTokenValid ?: 1) == 1L)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun storeData(sessionData: SessionData) {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import app.cash.turbine.test
|
|||
import com.google.common.truth.Truth.assertThat
|
||||
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
|
||||
import io.element.android.libraries.matrix.session.SessionData
|
||||
import io.element.android.libraries.sessionstorage.api.LoggedInState
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
|
@ -65,11 +66,11 @@ class DatabaseSessionStoreTests {
|
|||
@Test
|
||||
fun `isLoggedIn emits true while there are sessions in the DB`() = runTest {
|
||||
databaseSessionStore.isLoggedIn().test {
|
||||
assertThat(awaitItem()).isFalse()
|
||||
assertThat(awaitItem()).isEqualTo(LoggedInState.NotLoggedIn)
|
||||
database.sessionDataQueries.insertSessionData(aSessionData)
|
||||
assertThat(awaitItem()).isTrue()
|
||||
assertThat(awaitItem()).isEqualTo(LoggedInState.LoggedIn(true))
|
||||
database.sessionDataQueries.removeSession(aSessionData.userId)
|
||||
assertThat(awaitItem()).isFalse()
|
||||
assertThat(awaitItem()).isEqualTo(LoggedInState.NotLoggedIn)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue