Move some code to 'architecture' module

This commit is contained in:
ganfra 2023-01-04 21:09:59 +01:00
parent 3ffbba954e
commit eeebb99292
38 changed files with 48 additions and 67 deletions

View file

@ -1,9 +0,0 @@
package io.element.android.x.core.di
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
interface AssistedNodeFactory<NODE : Node> {
fun create(buildContext: BuildContext, plugins: List<Plugin>): NODE
}

View file

@ -1,8 +0,0 @@
package io.element.android.x.core.di
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
interface AssistedViewModelFactory<VM : MavericksViewModel<S>, S : MavericksState> {
fun create(initialState: S): VM
}

View file

@ -1,52 +0,0 @@
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:
* * an inject function: `inject(node: MyNode)`
* * an explicit getter: `fun myClass(): MyClass`
*
* Anvil will make your Dagger component implement these bindings so that you can call any of these functions on an instance of your component.
*
* [bindings] will walk up the Node/Activity hierarchy and check for [DaggerComponentOwner] to see if any of its components implement the
* specified bindings. Most of the time this will "just work" and you don't have to think about it.
*
* For example, if your class has @Inject properties:
* 1) Create an bindings interface such as `YourModuleBindings`
* 1) Add an inject function like `fun inject(yourClass: YourClass)`
* 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)
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
return generateSequence(this) { (it as? ContextWrapper)?.baseContext }
.plus(applicationContext)
.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}")
}
/** 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}")
}

View file

@ -1,57 +0,0 @@
package io.element.android.x.core.di
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.ViewModelContext
/**
* To connect Mavericks ViewModel creation with Anvil's dependency injection, add the following to your MavericksViewModel.
*
* Example:
*
* @ContributesViewModel(YourScope::class)
* class MyViewModel @AssistedInject constructor(
* @Assisted initialState: MyState,
* ,
* ): MavericksViewModel<MyState>(...) {
*
*
* companion object : MavericksViewModelFactory<MyViewModel, MyState> by daggerMavericksViewModelFactory()
* }
*/
inline fun <reified VM : MavericksViewModel<S>, S : MavericksState> daggerMavericksViewModelFactory() = DaggerMavericksViewModelFactory(VM::class.java)
/**
* A [MavericksViewModelFactory] makes it easy to create instances of a ViewModel
* using its AssistedInject Factory. This class should be implemented by the companion object
* of every ViewModel which uses AssistedInject via [daggerMavericksViewModelFactory].
*
* @param viewModelClass The [Class] of the ViewModel being requested for creation
*
* This class accesses the map of ViewModel class to [AssistedViewModelFactory]s from the nearest [DaggerComponentOwner] and
* uses it to retrieve the requested ViewModel's factory class. It then creates an instance of this ViewModel
* using the retrieved factory and returns it.
* @see daggerMavericksViewModelFactory
*/
class DaggerMavericksViewModelFactory<VM : MavericksViewModel<S>, S : MavericksState>(
private val viewModelClass: Class<VM>
) : MavericksViewModelFactory<VM, S> {
override fun create(viewModelContext: ViewModelContext, state: S): VM {
val bindings: DaggerMavericksBindings = viewModelContext.activity.bindings()
val viewModelFactoryMap = bindings.viewModelFactories()
val viewModelFactory = viewModelFactoryMap[viewModelClass] ?: error("Cannot find ViewModelFactory for ${viewModelClass.name}.")
@Suppress("UNCHECKED_CAST")
val castedViewModelFactory = viewModelFactory as? AssistedViewModelFactory<VM, S>
val viewModel = castedViewModelFactory?.create(state)
return viewModel as VM
}
}
interface DaggerMavericksBindings {
fun viewModelFactories(): Map<Class<out MavericksViewModel<*>>, AssistedViewModelFactory<*, *>>
}

View file

@ -1,21 +0,0 @@
package io.element.android.x.core.di
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
inline fun <reified NODE : Node> Node.createNode(context: BuildContext, plugins: List<Plugin> = emptyList()): NODE {
val nodeClass = NODE::class.java
val bindings: NodeFactoriesBindings = bindings()
val nodeFactoryMap = bindings.nodeFactories()
val nodeFactory = nodeFactoryMap[nodeClass] ?: error("Cannot find NodeFactory for ${nodeClass.name}.")
@Suppress("UNCHECKED_CAST")
val castedNodeFactory = nodeFactory as? AssistedNodeFactory<NODE>
val node = castedNodeFactory?.create(context, plugins)
return node as NODE
}
interface NodeFactoriesBindings {
fun nodeFactories(): Map<Class<out Node>, AssistedNodeFactory<*>>
}

View file

@ -1,26 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.core.di
import com.bumble.appyx.core.node.Node
import dagger.MapKey
import kotlin.reflect.KClass
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@MapKey
annotation class NodeKey(val value: KClass<out Node>)

View file

@ -1,26 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.core.di
import com.airbnb.mvrx.MavericksViewModel
import dagger.MapKey
import kotlin.reflect.KClass
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
@MapKey
annotation class ViewModelKey(val value: KClass<out MavericksViewModel<*>>)

View file

@ -1,121 +0,0 @@
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
}
}
}