Nav: First iteration integrating Appyx
This commit is contained in:
parent
4c88d8e3c2
commit
2de26a30d5
28 changed files with 566 additions and 280 deletions
|
|
@ -10,4 +10,5 @@ dependencies {
|
|||
api(libs.mavericks.compose)
|
||||
api(libs.dagger)
|
||||
api(libs.androidx.fragment)
|
||||
api(libs.appyx.core)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package io.element.android.x.core.di
|
|||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.bumble.appyx.core.node.Node
|
||||
|
||||
/**
|
||||
* Use this to get the Dagger "Bindings" for your module. Bindings are used if you need to directly interact with a dagger component such as:
|
||||
|
|
@ -20,6 +21,7 @@ import androidx.fragment.app.Fragment
|
|||
* 2) Contribute your interface to the correct component via `@ContributesTo(AppScope::class)`.
|
||||
* 3) Call bindings<YourModuleBindings>().inject(this).
|
||||
*/
|
||||
|
||||
inline fun <reified T : Any> Context.bindings() = bindings(T::class.java)
|
||||
|
||||
/**
|
||||
|
|
@ -27,6 +29,8 @@ inline fun <reified T : Any> Context.bindings() = bindings(T::class.java)
|
|||
*/
|
||||
inline fun <reified T : Any> Fragment.bindings() = bindings(T::class.java)
|
||||
|
||||
inline fun <reified T : Any> Node.bindings() = bindings(T::class.java)
|
||||
|
||||
/** Use no-arg extension function instead: [Context.bindings] */
|
||||
fun <T : Any> Context.bindings(klass: Class<T>): T {
|
||||
// search dagger components in the context hierarchy
|
||||
|
|
@ -50,4 +54,16 @@ fun <T : Any> Fragment.bindings(klass: Class<T>): T {
|
|||
.filterIsInstance(klass)
|
||||
.firstOrNull()
|
||||
?: requireActivity().bindings(klass)
|
||||
}
|
||||
}
|
||||
|
||||
/** Use no-arg extension function instead: [Node.bindings] */
|
||||
fun <T : Any> Node.bindings(klass: Class<T>): T {
|
||||
// search dagger components in node hierarchy
|
||||
return generateSequence(this, Node::parent)
|
||||
.filterIsInstance<DaggerComponentOwner>()
|
||||
.map { it.daggerComponent }
|
||||
.flatMap { if (it is Collection<*>) it else listOf(it) }
|
||||
.filterIsInstance(klass)
|
||||
.firstOrNull()
|
||||
?: error("Unable to find bindings for ${klass.name}")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,121 @@
|
|||
package io.element.android.x.core.di
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.lifecycle.DEFAULT_ARGS_KEY
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.HasDefaultViewModelProviderFactory
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.SAVED_STATE_REGISTRY_OWNER_KEY
|
||||
import androidx.lifecycle.SavedStateViewModelFactory
|
||||
import androidx.lifecycle.VIEW_MODEL_STORE_OWNER_KEY
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelStore
|
||||
import androidx.lifecycle.ViewModelStoreOwner
|
||||
import androidx.lifecycle.enableSavedStateHandles
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.lifecycle.viewmodel.MutableCreationExtras
|
||||
import androidx.savedstate.SavedStateRegistry
|
||||
import androidx.savedstate.SavedStateRegistryController
|
||||
import androidx.savedstate.SavedStateRegistryOwner
|
||||
import com.bumble.appyx.core.modality.BuildContext
|
||||
import com.bumble.appyx.core.node.Node
|
||||
import com.bumble.appyx.core.plugin.Plugin
|
||||
|
||||
fun viewModelSupportNode(buildContext: BuildContext, plugins: List<Plugin> = emptyList(), composable: @Composable (Modifier) -> Unit): Node =
|
||||
ViewModelSupportNode(buildContext, plugins, composable)
|
||||
|
||||
class ViewModelSupportNode(
|
||||
buildContext: BuildContext,
|
||||
plugins: List<Plugin> = emptyList(),
|
||||
private val composable: @Composable (Modifier) -> Unit,
|
||||
) : Node(
|
||||
buildContext, plugins = plugins
|
||||
), ViewModelStoreOwner, SavedStateRegistryOwner {
|
||||
|
||||
private val viewModelSupport = ViewModelSupport(
|
||||
lifecycle,
|
||||
buildContext.savedStateMap?.get("SAVED_STATE_REGISTRY") as Bundle?,
|
||||
)
|
||||
|
||||
override fun getViewModelStore(): ViewModelStore {
|
||||
return viewModelSupport.viewModelStore
|
||||
}
|
||||
|
||||
override val savedStateRegistry: SavedStateRegistry
|
||||
get() = viewModelSupport.savedStateRegistry
|
||||
|
||||
@Composable
|
||||
override fun View(modifier: Modifier) {
|
||||
composable(modifier)
|
||||
}
|
||||
}
|
||||
|
||||
private class ViewModelSupport(
|
||||
private val lifecycle: Lifecycle,
|
||||
private val initialSavedState: Bundle?,
|
||||
val defaultArgs: Bundle? = null,
|
||||
) : ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner {
|
||||
|
||||
private val viewModelStore = ViewModelStore()
|
||||
private val savedStateRegistryController: SavedStateRegistryController =
|
||||
SavedStateRegistryController.create(this)
|
||||
|
||||
//Don't replace the initial saved state until we have at least started
|
||||
private var canSaveState: Boolean = false
|
||||
|
||||
init {
|
||||
savedStateRegistryController.performAttach()
|
||||
|
||||
// We copy the bundle because the `savedStateRegistryController` will modify it.
|
||||
// We don't want to modify `initialSavedState` since we may need to return that as our
|
||||
// state in `saveState`.
|
||||
savedStateRegistryController.performRestore(initialSavedState?.let { Bundle(it) })
|
||||
enableSavedStateHandles()
|
||||
|
||||
lifecycle.addObserver(object : DefaultLifecycleObserver {
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
canSaveState = true
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
viewModelStore.clear()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun getViewModelStore(): ViewModelStore {
|
||||
return viewModelStore
|
||||
}
|
||||
|
||||
override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory {
|
||||
return SavedStateViewModelFactory(null, this, defaultArgs)
|
||||
}
|
||||
|
||||
override fun getDefaultViewModelCreationExtras(): CreationExtras {
|
||||
val extras = MutableCreationExtras()
|
||||
extras[SAVED_STATE_REGISTRY_OWNER_KEY] = this
|
||||
extras[VIEW_MODEL_STORE_OWNER_KEY] = this
|
||||
defaultArgs?.let { args ->
|
||||
extras[DEFAULT_ARGS_KEY] = args
|
||||
}
|
||||
return extras
|
||||
}
|
||||
|
||||
override val savedStateRegistry: SavedStateRegistry
|
||||
get() = savedStateRegistryController.savedStateRegistry
|
||||
|
||||
override fun getLifecycle(): Lifecycle {
|
||||
return lifecycle
|
||||
}
|
||||
|
||||
fun saveState(): Bundle? {
|
||||
return if (canSaveState) {
|
||||
Bundle().also(savedStateRegistryController::performSave)
|
||||
} else {
|
||||
initialSavedState
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,9 +6,11 @@ import io.element.android.x.core.coroutine.CoroutineDispatchers
|
|||
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.core.SessionId
|
||||
import io.element.android.x.matrix.media.MediaFetcher
|
||||
import io.element.android.x.matrix.media.MediaKeyer
|
||||
import io.element.android.x.matrix.session.SessionStore
|
||||
import io.element.android.x.matrix.session.sessionId
|
||||
import io.element.android.x.matrix.util.logError
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
|
@ -80,7 +82,7 @@ class Matrix @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun login(username: String, password: String): MatrixClient =
|
||||
suspend fun login(username: String, password: String): SessionId =
|
||||
withContext(coroutineDispatchers.io) {
|
||||
val client = try {
|
||||
authService.login(username, password, "ElementX Android", null)
|
||||
|
|
@ -88,8 +90,9 @@ class Matrix @Inject constructor(
|
|||
Timber.e(failure, "Fail login")
|
||||
throw failure
|
||||
}
|
||||
sessionStore.storeData(client.session())
|
||||
createMatrixClient(client)
|
||||
val session = client.session()
|
||||
sessionStore.storeData(session)
|
||||
session.sessionId()
|
||||
}
|
||||
|
||||
private fun createMatrixClient(client: Client): MatrixClient {
|
||||
|
|
|
|||
|
|
@ -1,7 +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.SessionId
|
||||
import io.element.android.x.matrix.core.UserId
|
||||
import io.element.android.x.matrix.media.MediaResolver
|
||||
import io.element.android.x.matrix.media.RustMediaResolver
|
||||
|
|
@ -9,10 +9,8 @@ import io.element.android.x.matrix.room.MatrixRoom
|
|||
import io.element.android.x.matrix.room.RoomSummaryDataSource
|
||||
import io.element.android.x.matrix.room.RustRoomSummaryDataSource
|
||||
import io.element.android.x.matrix.session.SessionStore
|
||||
import io.element.android.x.matrix.session.sessionId
|
||||
import io.element.android.x.matrix.sync.SlidingSyncObserverProxy
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.matrix.rustcomponents.sdk.Client
|
||||
|
|
@ -23,6 +21,9 @@ import org.matrix.rustcomponents.sdk.SlidingSyncMode
|
|||
import org.matrix.rustcomponents.sdk.SlidingSyncViewBuilder
|
||||
import org.matrix.rustcomponents.sdk.StoppableSpawn
|
||||
import timber.log.Timber
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
class MatrixClient internal constructor(
|
||||
private val client: Client,
|
||||
|
|
@ -32,8 +33,7 @@ class MatrixClient internal constructor(
|
|||
private val baseDirectory: File,
|
||||
) : Closeable {
|
||||
|
||||
val sessionId: String
|
||||
get() = "${client.session().userId}_${client.session().deviceId}"
|
||||
val sessionId: SessionId = client.session().sessionId()
|
||||
|
||||
private val clientDelegate = object : ClientDelegate {
|
||||
override fun didReceiveAuthError(isSoftLogout: Boolean) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package io.element.android.x.matrix.core
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
@JvmInline
|
||||
value class SessionId(val value: String) : Serializable
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package io.element.android.x.matrix.session
|
||||
|
||||
import io.element.android.x.matrix.core.SessionId
|
||||
import org.matrix.rustcomponents.sdk.Session
|
||||
|
||||
fun Session.sessionId() = SessionId("${userId}_${deviceId}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue