Tangle binds Android components for Dagger with Anvil.
Fragments
@ContributesFragment(AppScope::class)
class MyFragment @FragmentInject constructor(
val repository: MyRepository
) : Fragment() {
val name: String by arg("name")
@FragmentInjectFactory
interface Factory {
fun create(
@TangleParam("name") name: String
): MyFragment
}
}
Use constructor injection in Fragment
s, with optional AssistedInject-like factories for type-safe Bundle
arguments. Bindings are created automatically.
ViewModels
class MyViewModel @VMInject constructor(
val repository: MyRepository,
@TangleParam("userId")
val userId: String
) : ViewModel()
@Composable
fun MyComposable(
navController: NavController,
viewModel: MyViewModel = tangleViewModel()
) { /* ... */ }
class MyFragment : Fragment() {
val viewModel: MyViewModel by tangleViewModel()
}
Inject ViewModel
s, including scoped SavedStateHandle
arguments. Use the TangleParam
annotation to automatically extract navigation/Bundle
arguments and inject them explicitly.
Member Injection
@TangleScope(UserScope::class)
class UserActivity : Activity() {
@Inject lateinit var logger: MyLogger
override fun onCreate(savedInstanceState: Bundle?) {
// inject MyLogger
TangleGraph.inject(this)
super.onCreate(savedInstanceState)
logger.log("started UserActivity")
}
}
Inject scoped dependencies into "entry point" Android components using@TangleScope
and any desired scope.
WorkManager
@TangleWorker
class MyWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted params: WorkerParameters
) : CoroutineWorker(context,params) {
override suspend fun doWork(): Result {
/* ... */
}
}
Use Dagger's @AssistedInject
and @Assisted
annotations and@TangleWorker
to inject any ListenableWorker
.