У меня есть интерфейс FlowController:
Код: Выделить всё
interface FlowController {
enum class Type { SINGLE, MULTIPLE, OTHER }
}
Код: Выделить всё
SingleFlowControllerImpl(savedStateHandle: SavedStateHandle): FlowControllerКод: Выделить всё
MultipleFlowControllerImpl(savedStateHandle: SavedStateHandle): FlowControllerКод: Выделить всё
OtherFlowControllerImpl(savedStateHandle: SavedStateHandle): FlowController
Код: Выделить всё
@ViewModelScoped
@Component
abstract class ViewModelComponent(@Component val parent: ApplicationComponent) {
@Provides
fun providesFlowController(
@Assisted savedStateHandle: SavedStateHandle,
controllers: Set
): FlowController = controllers.first {
it.flowType == checkNotNull(savedStateHandle[Keys.FLOW_TYPE])
}
@ViewModelScoped
@IntoSet
@Provides
fun SingleFlowControllerImpl.bindIntoSet(): FlowController = this
[rest of the bindings]
}
Код: Выделить всё
@Inject
class MyFragment(
createViewModel: (SavedInstanceState) -> MyViewModel,
) : BaseFragment(R.layout.fragment_my) { {
private val viewModel by viewModel(createViewModel)
}
/**
* [viewModels] helper that allows you to pass a single factory function using a [SavedStateHandle].
*/
inline fun Fragment.viewModel(crossinline factory: (SavedStateHandle) -> VM): Lazy =
viewModels {
viewModelFactory { addInitializer(VM::class) { factory(createSavedStateHandle()) } }
}
Код: Выделить всё
@Inject
class MyViewModel(
private val flowController: FlowController,
@Assisted private val savedInstanceState: SavedInstanceState,
) { }
Подробнее здесь: https://stackoverflow.com/questions/792 ... -parameter
Мобильная версия