Introduce SessionComponent
This commit is contained in:
parent
eba807e37a
commit
0a06ca0539
18 changed files with 176 additions and 104 deletions
|
|
@ -3,8 +3,8 @@ package io.element.android.x.matrix
|
|||
import android.content.Context
|
||||
import coil.ComponentRegistry
|
||||
import io.element.android.x.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.x.di.ApplicationContext
|
||||
import io.element.android.x.di.AppScope
|
||||
import io.element.android.x.di.ApplicationContext
|
||||
import io.element.android.x.di.SingleIn
|
||||
import io.element.android.x.matrix.media.MediaFetcher
|
||||
import io.element.android.x.matrix.media.MediaKeyer
|
||||
|
|
@ -37,35 +37,18 @@ class Matrix @Inject constructor(
|
|||
)
|
||||
private val baseDirectory = File(context.filesDir, "sessions")
|
||||
private val sessionStore = SessionStore(context)
|
||||
private val matrixClient = MutableStateFlow<Optional<MatrixClient>>(Optional.empty())
|
||||
private val authService = AuthenticationService(baseDirectory.absolutePath)
|
||||
|
||||
init {
|
||||
sessionStore.isLoggedIn()
|
||||
.distinctUntilChanged()
|
||||
.onEach { isLoggedIn ->
|
||||
if (!isLoggedIn) {
|
||||
matrixClient.value = Optional.empty()
|
||||
}
|
||||
}
|
||||
.launchIn(coroutineScope)
|
||||
}
|
||||
|
||||
fun isLoggedIn(): Flow<Boolean> {
|
||||
return sessionStore.isLoggedIn()
|
||||
}
|
||||
|
||||
fun client(): Flow<Optional<MatrixClient>> {
|
||||
return matrixClient
|
||||
}
|
||||
|
||||
fun activeClient(): MatrixClient {
|
||||
return matrixClient.value.get()
|
||||
}
|
||||
|
||||
fun registerCoilComponents(builder: ComponentRegistry.Builder) {
|
||||
fun registerCoilComponents(
|
||||
builder: ComponentRegistry.Builder,
|
||||
activeClientProvider: () -> MatrixClient?
|
||||
) {
|
||||
builder.add(MediaKeyer())
|
||||
builder.add(MediaFetcher.Factory(this))
|
||||
builder.add(MediaFetcher.Factory(activeClientProvider))
|
||||
}
|
||||
|
||||
suspend fun restoreSession() = withContext(coroutineDispatchers.io) {
|
||||
|
|
@ -116,8 +99,6 @@ class Matrix @Inject constructor(
|
|||
coroutineScope = coroutineScope,
|
||||
dispatchers = coroutineDispatchers,
|
||||
baseDirectory = baseDirectory,
|
||||
).also {
|
||||
matrixClient.value = Optional.of(it)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package io.element.android.x.matrix
|
||||
|
||||
import io.element.android.x.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.x.di.SingleIn
|
||||
import io.element.android.x.matrix.core.UserId
|
||||
import io.element.android.x.matrix.media.MediaResolver
|
||||
import io.element.android.x.matrix.media.RustMediaResolver
|
||||
|
|
@ -25,6 +26,9 @@ class MatrixClient internal constructor(
|
|||
private val baseDirectory: File,
|
||||
) : Closeable {
|
||||
|
||||
val sessionId: String
|
||||
get() = "${client.session().userId}_${client.session().deviceId}"
|
||||
|
||||
private val clientDelegate = object : ClientDelegate {
|
||||
override fun didReceiveAuthError(isSoftLogout: Boolean) {
|
||||
Timber.v("didReceiveAuthError()")
|
||||
|
|
@ -78,7 +82,7 @@ class MatrixClient internal constructor(
|
|||
client.setDelegate(clientDelegate)
|
||||
}
|
||||
|
||||
private fun onRestartSync(){
|
||||
private fun onRestartSync() {
|
||||
slidingSyncObserverToken = slidingSync.sync()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
package io.element.android.x.matrix
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Application
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
|
||||
object MatrixInstance {
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private lateinit var instance: Matrix
|
||||
|
||||
fun init(context: Application, coroutineScope: CoroutineScope) {
|
||||
instance = Matrix(coroutineScope, context)
|
||||
}
|
||||
|
||||
fun getInstance(): Matrix {
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
|
@ -4,31 +4,32 @@ import coil.ImageLoader
|
|||
import coil.fetch.FetchResult
|
||||
import coil.fetch.Fetcher
|
||||
import coil.request.Options
|
||||
import io.element.android.x.matrix.Matrix
|
||||
import io.element.android.x.matrix.MatrixClient
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
internal class MediaFetcher(
|
||||
private val mediaResolver: MediaResolver,
|
||||
private val mediaResolver: MediaResolver?,
|
||||
private val meta: MediaResolver.Meta,
|
||||
private val options: Options,
|
||||
private val imageLoader: ImageLoader
|
||||
) : Fetcher {
|
||||
|
||||
override suspend fun fetch(): FetchResult? {
|
||||
val byteArray = mediaResolver.resolve(meta) ?: return null
|
||||
val byteArray = mediaResolver?.resolve(meta) ?: return null
|
||||
val byteBuffer = ByteBuffer.wrap(byteArray)
|
||||
return imageLoader.components.newFetcher(byteBuffer, options, imageLoader)?.first?.fetch()
|
||||
}
|
||||
|
||||
class Factory(private val matrix: Matrix) : Fetcher.Factory<MediaResolver.Meta> {
|
||||
class Factory(private val activeClientProvider: () -> MatrixClient?) :
|
||||
Fetcher.Factory<MediaResolver.Meta> {
|
||||
override fun create(
|
||||
data: MediaResolver.Meta,
|
||||
options: Options,
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
val activeClient = matrix.activeClient()
|
||||
val activeClient = activeClientProvider()
|
||||
return MediaFetcher(
|
||||
mediaResolver = activeClient.mediaResolver(),
|
||||
mediaResolver = activeClient?.mediaResolver(),
|
||||
meta = data,
|
||||
options = options,
|
||||
imageLoader = imageLoader
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue