Nav: First iteration integrating Appyx
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user