-
Notifications
You must be signed in to change notification settings - Fork 658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass injector directly into FormViewModel
#5738
Conversation
val application = applicationSupplier() | ||
val starterArgs = starterArgsSupplier() | ||
injectWithFallback( | ||
starterArgsSupplier().injectorKey, | ||
FallbackInitializeParam(application, starterArgs.productUsage) | ||
) | ||
return subComponentBuilderProvider.get() | ||
val logger = Logger.getInstance(BuildConfig.DEBUG) | ||
|
||
WeakMapInjectorRegistry.retrieve(starterArgs.injectorKey)?.let { | ||
it as? NonFallbackInjector | ||
}?.let { | ||
logger.info( | ||
"Injector available, " + | ||
"injecting dependencies into ${this::class.java.canonicalName}" | ||
) | ||
injector = it | ||
it.inject(this) | ||
} ?: run { | ||
logger.info( | ||
"Injector unavailable, " + | ||
"initializing dependencies of ${this::class.java.canonicalName}" | ||
) | ||
fallbackInitialize(FallbackInitializeParam(application, starterArgs.productUsage)) | ||
} | ||
|
||
val subcomponent = subComponentBuilderProvider.get() | ||
.application(application) | ||
.args(starterArgs) | ||
.savedStateHandle(savedStateHandle) | ||
.build().viewModel as T | ||
.build() | ||
val viewModel = subcomponent.viewModel | ||
viewModel.injector = injector | ||
return viewModel as T | ||
} | ||
|
||
override fun fallbackInitialize(arg: FallbackInitializeParam) { | ||
val component = DaggerPaymentOptionsViewModelFactoryComponent.builder() | ||
.context(arg.application) | ||
.productUsage(arg.productUsage) | ||
.build() | ||
|
||
injector = object : NonFallbackInjector { | ||
override fun inject(injectable: Injectable<*>) { | ||
when (injectable) { | ||
is FormViewModel.Factory -> component.inject(injectable) | ||
else -> { | ||
throw IllegalArgumentException("invalid Injectable $injectable requested in $this") | ||
} | ||
} | ||
} | ||
} | ||
component.inject(this) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is now replicated in PaymentOptionsViewModel
, PaymentSheetViewModel
and LinkActivityViewModel
. I'll refactor to make it generic and part of the stripe-core
module in the next PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -150,7 +150,7 @@ complexity: | |||
thresholdInObjects: 11 | |||
thresholdInEnums: 11 | |||
ignoreDeprecated: false | |||
ignorePrivate: false | |||
ignorePrivate: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What’s the reason for the changes in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detekt was failing for one of the files for having more than 11 methods, I think ignoring private methods is a good change to make it a little more flexible.
Summary
FormViewModel
's lifecycle is always strictly contained within the PaymentSheet lifecycle. Therefore, it doesn't need to be able to recreate the whole dependency graph.FormViewModel.Factory
now receives the injector as a parameter, which guarantees that it will reuse all already created dependencies. This allows us to deleteFormViewModelComponent
, which was used byFormViewModel
during fallback injection.BaseSheetViewModel
is now responsible for injecting into the ViewModels with smaller lifecycle, similar to howLinkActivityViewModel
holds an injector that is passed to the screen-specific ViewModels.PaymentOptionsViewModel.Factory
andPaymentSheetViewModel.Factory
create theNonFallbackInjector
and set it intoBaseSheetViewModel
.Motivation
Fix #5737
Testing