-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,12 @@ import androidx.lifecycle.SavedStateHandle | |
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.savedstate.SavedStateRegistryOwner | ||
import com.stripe.android.core.BuildConfig | ||
import com.stripe.android.core.Logger | ||
import com.stripe.android.core.injection.IOContext | ||
import com.stripe.android.core.injection.Injectable | ||
import com.stripe.android.core.injection.InjectorKey | ||
import com.stripe.android.core.injection.injectWithFallback | ||
import com.stripe.android.core.injection.WeakMapInjectorRegistry | ||
import com.stripe.android.link.LinkPaymentDetails | ||
import com.stripe.android.link.LinkPaymentLauncher | ||
import com.stripe.android.link.LinkPaymentLauncher.Companion.LINK_ENABLED | ||
|
@@ -24,6 +25,7 @@ import com.stripe.android.model.PaymentMethod | |
import com.stripe.android.model.StripeIntent | ||
import com.stripe.android.payments.paymentlauncher.PaymentResult | ||
import com.stripe.android.paymentsheet.analytics.EventReporter | ||
import com.stripe.android.paymentsheet.forms.FormViewModel | ||
import com.stripe.android.paymentsheet.injection.DaggerPaymentOptionsViewModelFactoryComponent | ||
import com.stripe.android.paymentsheet.injection.PaymentOptionsViewModelSubcomponent | ||
import com.stripe.android.paymentsheet.model.FragmentConfig | ||
|
@@ -35,6 +37,7 @@ import com.stripe.android.paymentsheet.viewmodels.BaseSheetViewModel | |
import com.stripe.android.ui.core.address.AddressRepository | ||
import com.stripe.android.ui.core.forms.resources.LpmRepository | ||
import com.stripe.android.ui.core.forms.resources.ResourceRepository | ||
import com.stripe.android.ui.core.injection.NonFallbackInjector | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
@@ -317,17 +320,12 @@ internal class PaymentOptionsViewModel @Inject constructor( | |
val productUsage: Set<String> | ||
) | ||
|
||
override fun fallbackInitialize(arg: FallbackInitializeParam) { | ||
DaggerPaymentOptionsViewModelFactoryComponent.builder() | ||
.context(arg.application) | ||
.productUsage(arg.productUsage) | ||
.build().inject(this) | ||
} | ||
|
||
@Inject | ||
lateinit var subComponentBuilderProvider: | ||
Provider<PaymentOptionsViewModelSubcomponent.Builder> | ||
|
||
private lateinit var injector: NonFallbackInjector | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create( | ||
key: String, | ||
|
@@ -336,15 +334,52 @@ internal class PaymentOptionsViewModel @Inject constructor( | |
): T { | ||
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) | ||
Comment on lines
335
to
+382
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is now replicated in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
|
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.