Update to latest matrix-rust-sdk and update store session so it compiles
This commit is contained in:
parent
dca9563762
commit
8b48f4a370
4 changed files with 46 additions and 24 deletions
|
|
@ -36,6 +36,7 @@ timber = "5.0.1"
|
||||||
coil = "2.2.1"
|
coil = "2.2.1"
|
||||||
datetime = "0.4.0"
|
datetime = "0.4.0"
|
||||||
wysiwyg = "0.4.0"
|
wysiwyg = "0.4.0"
|
||||||
|
serialization-json = "1.4.1"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
# Project
|
# Project
|
||||||
|
|
@ -45,7 +46,8 @@ kotlin_gradle_plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", v
|
||||||
# AndroidX
|
# AndroidX
|
||||||
androidx_material = { module = "com.google.android.material:material", version.ref = "material" }
|
androidx_material = { module = "com.google.android.material:material", version.ref = "material" }
|
||||||
androidx_corektx = { module = "androidx.core:core-ktx", version.ref = "corektx" }
|
androidx_corektx = { module = "androidx.core:core-ktx", version.ref = "corektx" }
|
||||||
androidx_datastore = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
|
androidx_datastore_preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
|
||||||
|
androidx_datastore_datastore = { module = "androidx.datastore:datastore", version.ref = "datastore" }
|
||||||
androidx_constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
|
androidx_constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
|
||||||
|
|
||||||
androidx_compose_bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose_bom" }
|
androidx_compose_bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose_bom" }
|
||||||
|
|
@ -78,6 +80,7 @@ mavericks_compose = { module = "com.airbnb.android:mavericks-compose", version.r
|
||||||
timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
|
timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
|
||||||
coil_compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
|
coil_compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
|
||||||
datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" }
|
datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" }
|
||||||
|
serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization-json" }
|
||||||
|
|
||||||
# Composer
|
# Composer
|
||||||
wysiwyg = { module = "io.element.android:wysiwyg", version.ref = "wysiwyg" }
|
wysiwyg = { module = "io.element.android:wysiwyg", version.ref = "wysiwyg" }
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
plugins {
|
plugins {
|
||||||
id("io.element.android-library")
|
id("io.element.android-library")
|
||||||
|
kotlin("plugin.serialization") version "1.7.20"
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
|
|
@ -11,6 +12,6 @@ dependencies {
|
||||||
implementation(project(":libraries:core"))
|
implementation(project(":libraries:core"))
|
||||||
implementation(libs.timber)
|
implementation(libs.timber)
|
||||||
implementation("net.java.dev.jna:jna:5.10.0@aar")
|
implementation("net.java.dev.jna:jna:5.10.0@aar")
|
||||||
implementation("androidx.datastore:datastore-core:1.0.0")
|
implementation(libs.androidx.datastore.preferences)
|
||||||
implementation("androidx.datastore:datastore-preferences:1.0.0")
|
implementation(libs.serialization.json)
|
||||||
}
|
}
|
||||||
|
|
@ -53,14 +53,14 @@ class Matrix(
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun restoreSession() = withContext(coroutineDispatchers.io) {
|
suspend fun restoreSession() = withContext(coroutineDispatchers.io) {
|
||||||
sessionStore.getStoredData()
|
sessionStore.getLatestSession()
|
||||||
?.let { sessionData ->
|
?.let { session ->
|
||||||
try {
|
try {
|
||||||
ClientBuilder()
|
ClientBuilder()
|
||||||
.basePath(baseFolder.absolutePath)
|
.basePath(baseFolder.absolutePath)
|
||||||
.username(sessionData.userId)
|
.username(session.userId)
|
||||||
.build().apply {
|
.build().apply {
|
||||||
restoreLogin(sessionData.restoreToken)
|
restoreSession(session)
|
||||||
}
|
}
|
||||||
} catch (throwable: Throwable) {
|
} catch (throwable: Throwable) {
|
||||||
logError(throwable)
|
logError(throwable)
|
||||||
|
|
@ -76,7 +76,7 @@ class Matrix(
|
||||||
val authService = AuthenticationService(baseFolder.absolutePath)
|
val authService = AuthenticationService(baseFolder.absolutePath)
|
||||||
authService.configureHomeserver(homeserver)
|
authService.configureHomeserver(homeserver)
|
||||||
val client = authService.login(username, password, "MatrixRustSDKSample", null)
|
val client = authService.login(username, password, "MatrixRustSDKSample", null)
|
||||||
sessionStore.storeData(SessionStore.SessionData(client.userId(), client.restoreToken()))
|
sessionStore.storeData(client.session())
|
||||||
createMatrixClient(client)
|
createMatrixClient(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,45 +9,63 @@ import androidx.datastore.preferences.preferencesDataStore
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.firstOrNull
|
import kotlinx.coroutines.flow.firstOrNull
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.*
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import org.matrix.rustcomponents.sdk.Session
|
||||||
|
|
||||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "elementx_sessions")
|
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "elementx_sessions")
|
||||||
private val userIdPreference = stringPreferencesKey("userId")
|
|
||||||
|
|
||||||
// TODO It contains the access token, so it has to be stored in a more secured storage.
|
// TODO It contains the access token, so it has to be stored in a more secured storage.
|
||||||
// I would expect the Rust SDK to provide a more obscure token.
|
private val sessionKey = stringPreferencesKey("session")
|
||||||
private val restoreTokenPreference = stringPreferencesKey("restoreToken")
|
|
||||||
|
|
||||||
|
|
||||||
internal class SessionStore(
|
internal class SessionStore(
|
||||||
context: Context
|
context: Context
|
||||||
) {
|
) {
|
||||||
|
@Serializable
|
||||||
data class SessionData(
|
data class SessionData(
|
||||||
val userId: String,
|
val accessToken: String,
|
||||||
val restoreToken: String,
|
val deviceId: String,
|
||||||
|
val homeserverUrl: String,
|
||||||
|
val isSoftLogout: Boolean,
|
||||||
|
val refreshToken: String?,
|
||||||
|
val userId: String
|
||||||
)
|
)
|
||||||
|
|
||||||
private val store = context.dataStore
|
private val store = context.dataStore
|
||||||
|
|
||||||
fun isLoggedIn(): Flow<Boolean> {
|
fun isLoggedIn(): Flow<Boolean> {
|
||||||
return store.data.map { prefs ->
|
return store.data.map { prefs ->
|
||||||
prefs[userIdPreference] != null && prefs[restoreTokenPreference] != null
|
prefs[sessionKey] != null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun storeData(sessionData: SessionData) {
|
suspend fun storeData(session: Session) {
|
||||||
store.edit { prefs ->
|
store.edit { prefs ->
|
||||||
prefs[userIdPreference] = sessionData.userId
|
val sessionData = SessionData(
|
||||||
prefs[restoreTokenPreference] = sessionData.restoreToken
|
accessToken = session.accessToken,
|
||||||
|
deviceId = session.deviceId,
|
||||||
|
homeserverUrl = session.homeserverUrl,
|
||||||
|
isSoftLogout = session.isSoftLogout,
|
||||||
|
refreshToken = session.refreshToken,
|
||||||
|
userId = session.userId
|
||||||
|
)
|
||||||
|
val encodedSession = Json.encodeToString(sessionData)
|
||||||
|
prefs[sessionKey] = encodedSession
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getStoredData(): SessionData? {
|
suspend fun getLatestSession(): Session? {
|
||||||
return store.data.firstOrNull()?.let { prefs ->
|
return store.data.firstOrNull()?.let { prefs ->
|
||||||
val userId = prefs[userIdPreference] ?: return@let null
|
val encodedSession = prefs[sessionKey] ?: return@let null
|
||||||
val restoreToken = prefs[restoreTokenPreference] ?: return@let null
|
val sessionData = Json.decodeFromString<SessionData>(encodedSession)
|
||||||
SessionData(
|
Session(
|
||||||
userId = userId,
|
accessToken = sessionData.accessToken,
|
||||||
restoreToken = restoreToken,
|
deviceId = sessionData.deviceId,
|
||||||
|
homeserverUrl = sessionData.homeserverUrl,
|
||||||
|
isSoftLogout = sessionData.isSoftLogout,
|
||||||
|
refreshToken = sessionData.refreshToken,
|
||||||
|
userId = sessionData.userId
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue