Skip to content

Commit 279187f

Browse files
committed
Update database to be able to sort accounts by creation date.
1 parent 914aa6c commit 279187f

File tree

11 files changed

+25
-26
lines changed

11 files changed

+25
-26
lines changed

features/home/impl/src/main/kotlin/io/element/android/features/home/impl/HomePresenter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ class HomePresenter(
116116
}
117117

118118
private fun List<SessionData>.takeCurrentUserWithNeighbors(matrixUser: MatrixUser): List<MatrixUser> {
119-
// Sort by userId to always have the same order (not depending on last account usage)
120-
return sortedBy { it.userId }
119+
// Sort by position to always have the same order (not depending on last account usage)
120+
return sortedBy { it.position }
121121
.map {
122122
if (it.userId == matrixUser.userId.value) {
123123
// Always use the freshest profile for the current user

features/signedout/impl/src/main/kotlin/io/element/android/features/signedout/impl/SignedOutStateProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ private fun aSessionData(
4444
passphrase = null,
4545
sessionPath = "/a/path/to/a/session",
4646
cachePath = "/a/path/to/a/cache",
47+
position = 0,
4748
lastUsageIndex = 0,
48-
lastUsageDate = Date(),
4949
userDisplayName = null,
5050
userAvatarUrl = null,
5151
)

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/mapper/Session.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ internal fun Session.toSessionData(
3434
passphrase = passphrase,
3535
sessionPath = sessionPaths.fileDirectory.absolutePath,
3636
cachePath = sessionPaths.cacheDirectory.absolutePath,
37+
// Note: position and lastUsageIndex will be set by the SessionStore when adding the session
38+
position = 0,
3739
lastUsageIndex = 0,
38-
lastUsageDate = Date(),
3940
userDisplayName = null,
4041
userAvatarUrl = null,
4142
)
@@ -59,8 +60,8 @@ internal fun ExternalSession.toSessionData(
5960
passphrase = passphrase,
6061
sessionPath = sessionPaths.fileDirectory.absolutePath,
6162
cachePath = sessionPaths.cacheDirectory.absolutePath,
63+
position = 0,
6264
lastUsageIndex = 0,
63-
lastUsageDate = Date(),
6465
userDisplayName = null,
6566
userAvatarUrl = null,
6667
)

libraries/session-storage/api/src/main/kotlin/io/element/android/libraries/sessionstorage/api/SessionData.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ data class SessionData(
3939
val sessionPath: String,
4040
/** The path to the cache data stored for the session in the filesystem. */
4141
val cachePath: String,
42+
/** The position, to be able to order account */
43+
val position: Long,
4244
/** The index of the last date of session usage. */
4345
val lastUsageIndex: Long,
44-
/** The last date of session usage. */
45-
val lastUsageDate: Date,
4646
/** The optional display name of the user. */
4747
val userDisplayName: String?,
4848
/** The optional avatar URL of the user. */

libraries/session-storage/impl/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dependencies {
2121
implementation(projects.libraries.androidutils)
2222
implementation(projects.libraries.core)
2323
implementation(projects.libraries.encryptedDb)
24-
implementation(projects.services.toolbox.api)
2524
api(projects.libraries.sessionStorage.api)
2625
implementation(libs.sqldelight.driver.android)
2726
implementation(libs.sqlcipher)
@@ -40,7 +39,7 @@ dependencies {
4039
sqldelight {
4140
databases {
4241
create("SessionDatabase") {
43-
// https://cashapp.github.io/sqldelight/2.0.0/android_sqlite/migrations/
42+
// https://sqldelight.github.io/sqldelight/2.1.0/android_sqlite/migrations/
4443
// To generate a .db file from your latest schema, run this task
4544
// ./gradlew generateDebugSessionDatabaseSchema
4645
// Test migration by running

libraries/session-storage/impl/src/main/kotlin/io/element/android/libraries/sessionstorage/impl/DatabaseSessionStore.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@ import io.element.android.libraries.core.coroutine.CoroutineDispatchers
1818
import io.element.android.libraries.sessionstorage.api.LoggedInState
1919
import io.element.android.libraries.sessionstorage.api.SessionData
2020
import io.element.android.libraries.sessionstorage.api.SessionStore
21-
import io.element.android.services.toolbox.api.systemclock.SystemClock
2221
import kotlinx.coroutines.flow.Flow
2322
import kotlinx.coroutines.flow.map
2423
import kotlinx.coroutines.sync.Mutex
2524
import kotlinx.coroutines.sync.withLock
2625
import timber.log.Timber
27-
import java.util.Date
2826

2927
@SingleIn(AppScope::class)
3028
@ContributesBinding(AppScope::class)
3129
@Inject
3230
class DatabaseSessionStore(
3331
private val database: SessionDatabase,
3432
private val dispatchers: CoroutineDispatchers,
35-
private val systemClock: SystemClock,
3633
) : SessionStore {
3734
private val sessionDataMutex = Mutex()
3835

@@ -58,8 +55,10 @@ class DatabaseSessionStore(
5855
database.sessionDataQueries.insertSessionData(
5956
sessionData
6057
.copy(
58+
// position value does not really matter, so just use lastUsageIndex + 1 to ensure that
59+
// the value is always greater than value of any existing account
60+
position = lastUsageIndex + 1,
6161
lastUsageIndex = lastUsageIndex + 1,
62-
lastUsageDate = Date(systemClock.epochMillis()),
6362
)
6463
.toDbModel()
6564
)
@@ -80,8 +79,8 @@ class DatabaseSessionStore(
8079
database.sessionDataQueries.updateSession(
8180
sessionData.copy(
8281
loginTimestamp = result.loginTimestamp,
82+
position = result.position,
8383
lastUsageIndex = result.lastUsageIndex,
84-
lastUsageDate = result.lastUsageDate,
8584
userDisplayName = result.userDisplayName,
8685
userAvatarUrl = result.userAvatarUrl,
8786
).toDbModel()
@@ -122,11 +121,10 @@ class DatabaseSessionStore(
122121
return
123122
}
124123
sessionDataMutex.withLock {
125-
// Update lastUsageDate and lastSessionIndex of the session
124+
// Update lastUsageIndex of the session
126125
database.sessionDataQueries.updateSession(
127126
result.copy(
128127
lastUsageIndex = lastUsageIndex + 1,
129-
lastUsageDate = Date(systemClock.epochMillis()),
130128
).toDbModel()
131129
)
132130
}

libraries/session-storage/impl/src/main/kotlin/io/element/android/libraries/sessionstorage/impl/SessionDataMapper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ internal fun SessionData.toDbModel(): DbSessionData {
2727
passphrase = passphrase,
2828
sessionPath = sessionPath,
2929
cachePath = cachePath,
30+
position = position,
3031
lastUsageIndex = lastUsageIndex,
31-
lastUsageDate = lastUsageDate.time,
3232
userDisplayName = userDisplayName,
3333
userAvatarUrl = userAvatarUrl,
3434
)
@@ -49,8 +49,8 @@ internal fun DbSessionData.toApiModel(): SessionData {
4949
passphrase = passphrase,
5050
sessionPath = sessionPath,
5151
cachePath = cachePath,
52+
position = position,
5253
lastUsageIndex = lastUsageIndex,
53-
lastUsageDate = Date(lastUsageDate),
5454
userDisplayName = userDisplayName,
5555
userAvatarUrl = userAvatarUrl,
5656
)
12 KB
Binary file not shown.

libraries/session-storage/impl/src/main/sqldelight/io/element/android/libraries/matrix/session/SessionData.sq

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ CREATE TABLE SessionData (
2828
sessionPath TEXT NOT NULL DEFAULT "",
2929
-- added in version 9
3030
cachePath TEXT NOT NULL DEFAULT "",
31+
-- added in version 10
32+
-- position, to be able to sort account by session creation date
33+
position INTEGER NOT NULL DEFAULT 0,
3134
-- index of the last usage session. Each time the current session change, the index of the current
32-
-- session is incremented to the max value + 1 so it become the current session
35+
-- session is incremented to the max value + 1 so it becomes the current session
3336
lastUsageIndex INTEGER NOT NULL DEFAULT 0,
34-
-- informative last usage date
35-
lastUsageDate INTEGER NOT NULL DEFAULT 0,
3637
-- user display name
3738
userDisplayName TEXT,
3839
-- user avatar url
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
-- Migrate DB from version 9
2-
-- Add lastUsageDate and lastUsageIndex so we can restore the last session and switch to another one
2+
-- Add position to be able to sort account by session creation date
3+
-- Add lastUsageIndex so we can restore the last session and switch to another one
34
-- Add display name and avatar url of the user so that we can display a list of accounts.
45

5-
ALTER TABLE SessionData ADD COLUMN lastUsageDate INTEGER NOT NULL DEFAULT 0;
6+
ALTER TABLE SessionData ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
67
ALTER TABLE SessionData ADD COLUMN lastUsageIndex INTEGER NOT NULL DEFAULT 0;
78
ALTER TABLE SessionData ADD COLUMN userDisplayName TEXT;
89
ALTER TABLE SessionData ADD COLUMN userAvatarUrl TEXT;

0 commit comments

Comments
 (0)