Skip to content

Commit b16f5b8

Browse files
committed
BillingViewModel: support Galaxy provider, prepare switching if available
1 parent a2a03b2 commit b16f5b8

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

app/src/main/java/com/battlelancer/seriesguide/billing/BillingViewModel.kt

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.combine
2222
import kotlinx.coroutines.flow.flowOn
2323
import kotlinx.coroutines.flow.map
2424
import 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
}

app/src/main/java/com/battlelancer/seriesguide/billing/galaxy/GalaxyBillingHelper.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.battlelancer.seriesguide.billing.galaxy
55

66
import android.content.Context
77
import com.battlelancer.seriesguide.BuildConfig
8+
import com.battlelancer.seriesguide.billing.SafeAugmentedProductDetails
89
import com.battlelancer.seriesguide.util.PackageTools
910
import com.samsung.android.sdk.iap.lib.constants.HelperDefine
1011
import com.samsung.android.sdk.iap.lib.helper.IapHelper
@@ -16,6 +17,8 @@ import com.samsung.android.sdk.iap.lib.vo.ErrorVo
1617
import com.samsung.android.sdk.iap.lib.vo.OwnedProductVo
1718
import com.samsung.android.sdk.iap.lib.vo.ProductVo
1819
import kotlinx.coroutines.CoroutineScope
20+
import kotlinx.coroutines.flow.Flow
21+
import kotlinx.coroutines.flow.MutableStateFlow
1922
import kotlinx.coroutines.launch
2023
import timber.log.Timber
2124
import java.nio.charset.StandardCharsets
@@ -32,6 +35,9 @@ class GalaxyBillingHelper(
3235
private val coroutineScope: CoroutineScope
3336
) {
3437

38+
// TODO
39+
val productDetails: Flow<List<SafeAugmentedProductDetails>> = MutableStateFlow(emptyList())
40+
3541
private val iapHelper by lazy {
3642
IapHelper.getInstance(context)
3743
.apply {

0 commit comments

Comments
 (0)