Dagger: start setup

This commit is contained in:
ganfra 2022-12-09 19:27:04 +01:00
parent 32dbaaf836
commit cb92ff5d3b
22 changed files with 290 additions and 4 deletions

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2021 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.MavericksState
import com.airbnb.mvrx.MavericksViewModel
interface AssistedViewModelFactory<VM : MavericksViewModel<S>, S : MavericksState> {
fun create(initialState: S): VM
}

View file

@ -0,0 +1,53 @@
package io.element.android.x.core.di
import android.content.Context
import android.content.ContextWrapper
import androidx.fragment.app.Fragment
/**
* 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 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
* 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)
/**
* @see bindings
*/
inline fun <reified T : Any> Fragment.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: [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)
}

View file

@ -0,0 +1,10 @@
package io.element.android.x.core.di
/**
* A [DaggerComponentOwner] is anything that "owns" a Dagger Component.
*
*/
interface DaggerComponentOwner {
/** This is either a component, or a list of components. */
val daggerComponent: Any
}

View file

@ -0,0 +1,68 @@
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, S>(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 = when (viewModelContext) {
is FragmentViewModelContext -> viewModelContext.fragment.bindings()
else -> 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
}
}
/**
* These Anvil/Dagger bindings are used by [DaggerMavericksViewModelFactory]. The factory will find the nearest [DaggerComponentOwner]
* that implements these bindings. It will then attempt to retrieve the [AssistedViewModelFactory] for the given ViewModel class.
*
* In this example, this bindings class is implemented by [com.airbnb.mvrx.sample.anvil.feature.ExampleFeatureComponent] because
* it provides the [com.airbnb.mvrx.sample.anvil.feature.ExampleFeatureViewModel]. Any component that will generate ViewModels should
* either implement this directly or have this added via `@ContributesTo(YourScope::class)`.
*/
interface DaggerMavericksBindings {
fun viewModelFactories(): Map<Class<out MavericksViewModel<*>>, AssistedViewModelFactory<*, *>>
}

View file

@ -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.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<*>>)