RoomList: setup dagger for node (remove fragment bindings)
This commit is contained in:
parent
fc14973049
commit
3ffbba954e
15 changed files with 122 additions and 60 deletions
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
}
|
||||
|
|
@ -7,12 +7,12 @@ 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(MyFragment frag)`
|
||||
* * 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 Fragment/Activity hierarchy and check for [DaggerComponentOwner] to see if any of its components implement the
|
||||
* [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:
|
||||
|
|
@ -24,11 +24,6 @@ import com.bumble.appyx.core.node.Node
|
|||
|
||||
inline fun <reified T : Any> Context.bindings() = bindings(T::class.java)
|
||||
|
||||
/**
|
||||
* @see bindings
|
||||
*/
|
||||
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] */
|
||||
|
|
@ -44,18 +39,6 @@ fun <T : Any> Context.bindings(klass: Class<T>): T {
|
|||
?: error("Unable to find bindings for ${klass.name}")
|
||||
}
|
||||
|
||||
/** Use no-arg extension function instead: [Fragment.bindings] */
|
||||
fun <T : Any> Fragment.bindings(klass: Class<T>): T {
|
||||
// search dagger components in fragment hierarchy, then fallback to activity and application
|
||||
return generateSequence(this, Fragment::getParentFragment)
|
||||
.filterIsInstance<DaggerComponentOwner>()
|
||||
.map { it.daggerComponent }
|
||||
.flatMap { if (it is Collection<*>) it else listOf(it) }
|
||||
.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
|
||||
|
|
|
|||
|
|
@ -41,10 +41,7 @@ class DaggerMavericksViewModelFactory<VM : MavericksViewModel<S>, S : MavericksS
|
|||
) : MavericksViewModelFactory<VM, S> {
|
||||
|
||||
override fun create(viewModelContext: ViewModelContext, state: S): VM {
|
||||
val bindings: DaggerMavericksBindings = when (viewModelContext) {
|
||||
is FragmentViewModelContext -> viewModelContext.fragment.bindings()
|
||||
else -> viewModelContext.activity.bindings()
|
||||
}
|
||||
val bindings: DaggerMavericksBindings = viewModelContext.activity.bindings()
|
||||
val viewModelFactoryMap = bindings.viewModelFactories()
|
||||
val viewModelFactory = viewModelFactoryMap[viewModelClass] ?: error("Cannot find ViewModelFactory for ${viewModelClass.name}.")
|
||||
|
||||
|
|
@ -57,4 +54,4 @@ class DaggerMavericksViewModelFactory<VM : MavericksViewModel<S>, S : MavericksS
|
|||
|
||||
interface DaggerMavericksBindings {
|
||||
fun viewModelFactories(): Map<Class<out MavericksViewModel<*>>, AssistedViewModelFactory<*, *>>
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
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<*>>
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* 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>)
|
||||
Loading…
Add table
Add a link
Reference in a new issue