Skip to content

Instantly share code, notes, and snippets.

@gpeal
Last active January 12, 2023 18:19
Show Gist options
  • Save gpeal/b463928f601c47ad9df8b3483bf2ffc8 to your computer and use it in GitHub Desktop.
Save gpeal/b463928f601c47ad9df8b3483bf2ffc8 to your computer and use it in GitHub Desktop.
inline fun <reified T : Any> Fragment.fragmentComponent(crossinline factory: (CoroutineScope, Application) -> T) = lazy {
ViewModelProvider(this)[DaggerComponentHolderViewModel::class.java].get<T>(factory)
}
class DaggerComponentHolderViewModel(app: Application) : AndroidViewModel(app) {
val map = ConcurrentHashMap<Class<*>, Any>()
inline fun <reified T> get(factory: (CoroutineScope, Application) -> T): T {
return map.getOrPut(T::class.java) { factory(viewModelScope, getApplication()) } as T
}
}
class GuidedWorkoutFragment : TonalFragment(R.layout.guided_workout_fragment) {
override val daggerComponent: GuidedWorkoutMergeComponent by fragmentComponent { scope, app ->
app.bindings<GuidedWorkoutMergeComponent.Parent>().guidedWorkoutComponentBuilder()
.coroutineScope(WorkoutCoroutineScope(scope))
.args(arg)
.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment