@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.combine
2222import kotlinx.coroutines.flow.flowOn
2323import kotlinx.coroutines.flow.map
2424import kotlinx.coroutines.flow.shareIn
25+ import timber.log.Timber
2526
2627/* *
2728 * Helps fetch current purchases and available products from Play billing provider.
@@ -39,6 +40,18 @@ class BillingViewModel(application: Application) : AndroidViewModel(application)
3940
4041 val augmentedUnlockState: Flow <AugmentedUnlockState >
4142
43+ enum class ProductsProvider {
44+ PlayBilling ,
45+ GalaxyBilling
46+ }
47+
48+ private val productsProvider = when (application.getSgAppContainer().installedBy) {
49+ PackageTools .InstalledByGalaxyStore -> ProductsProvider .GalaxyBilling
50+ // Default to Play, it's most likely to be available when installing manually or from other
51+ // sources.
52+ else -> ProductsProvider .PlayBilling
53+ }.also { Timber .i(" Using %s products provider" , it) }
54+
4255 /* *
4356 * A list of supported products filtered to only contain those
4457 * that are available on Google Play.
@@ -72,30 +85,52 @@ class BillingViewModel(application: Application) : AndroidViewModel(application)
7285 // no StateFlow as UI doesn't need initial value, it will display a wait indicator.
7386 .shareIn(viewModelScope, SharingStarted .Lazily , replay = 1 )
7487
75- availableProducts = playBilling.productDetails
76- .map { products ->
77- products.mapNotNull { product ->
78- if (product.productDetails != null ) {
79- val subscriptionOfferDetails =
80- product.productDetails.subscriptionOfferDetails
81- if (subscriptionOfferDetails != null ) {
82- return @mapNotNull SafeAugmentedProductDetails (
83- product.productId,
84- product.canPurchase,
85- product.productDetails,
86- subscriptionOfferDetails.flatMap { it.pricingPhases.pricingPhaseList }
87- )
88+ when (productsProvider) {
89+ ProductsProvider .PlayBilling -> {
90+ availableProducts = playBilling.productDetails
91+ .map { products ->
92+ products.mapNotNull { product ->
93+ if (product.productDetails != null ) {
94+ val subscriptionOfferDetails =
95+ product.productDetails.subscriptionOfferDetails
96+ if (subscriptionOfferDetails != null ) {
97+ return @mapNotNull SafeAugmentedProductDetails (
98+ product.productId,
99+ product.canPurchase,
100+ product.productDetails,
101+ subscriptionOfferDetails.flatMap { it.pricingPhases.pricingPhaseList }
102+ )
103+ }
104+ }
105+ return @mapNotNull null
88106 }
89107 }
90- return @mapNotNull null
91- }
108+ .flowOn(Dispatchers .IO )
109+ errorEvent = playBilling.errorEvent
110+ }
111+
112+ ProductsProvider .GalaxyBilling -> {
113+ // TODO
114+ availableProducts = galaxyBilling.productDetails
115+ errorEvent = playBilling.errorEvent
92116 }
93- .flowOn(Dispatchers .IO )
94- errorEvent = playBilling.errorEvent
117+ }
95118 }
96119
97120 fun makePurchase (activity : Activity , productDetails : SafeAugmentedProductDetails ) {
98- playBilling.launchBillingFlow(activity, productDetails)
121+ when (productsProvider) {
122+ ProductsProvider .PlayBilling -> {
123+ playBilling.launchBillingFlow(activity, productDetails)
124+ }
125+
126+ ProductsProvider .GalaxyBilling -> {
127+ // TODO
128+ // galaxyBilling.purchaseItem(
129+ // productDetails.productId,
130+ // repository.galaxyPurchaseListener
131+ // )
132+ }
133+ }
99134 }
100135
101136}
0 commit comments