Skip to content

Commit

Permalink
Show error when failed to fetch google prices
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-markowicz committed Apr 25, 2024
1 parent ac0e00a commit 67952bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ class PaymentPanelFragment : Fragment() {
state.inProgress
)
}
is CommonUpgradeDialogViewModel.State.LoadError ->
onError(state.error.getUserMessage(resources), state.error)
is CommonUpgradeDialogViewModel.State.LoadError -> {
val message = state.messageRes?.let { resources.getString(it) }
onError(message ?: state.error?.getUserMessage(resources), state.error)
}
is CommonUpgradeDialogViewModel.State.PlansFallback ->
currentViewState.value = ViewState.FallbackFlowReady
CommonUpgradeDialogViewModel.State.Initializing -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.protonvpn.android.ui.planupgrade

import android.app.Activity
import androidx.lifecycle.viewModelScope
import com.protonvpn.android.R
import com.protonvpn.android.auth.usecase.CurrentUser
import com.protonvpn.android.telemetry.UpgradeTelemetry
import com.protonvpn.android.ui.planupgrade.usecase.CycleInfo
Expand Down Expand Up @@ -135,14 +136,18 @@ class UpgradeDialogViewModel(
if (giapPlan != null) {
loadedPlan = giapPlan
val prices = calculatePriceInfos(loadedPlan.cycles, loadedPlan.dynamicPlan)
state.value = State.PurchaseReady(GiapPlanModel(loadedPlan), prices)
selectedCycle.value = giapPlan.preselectedCycle
if (prices.isEmpty())
state.value = State.LoadError(R.string.error_fetching_prices)
else {
state.value = State.PurchaseReady(GiapPlanModel(loadedPlan), prices)
selectedCycle.value = giapPlan.preselectedCycle
}
} else {
state.value = State.PlansFallback
}
}.runCatchingCheckedExceptions { e ->
// loadDefaultGiapPlan throws errors.
state.value = State.LoadError(e)
state.value = State.LoadError(error = e)
}
}

Expand Down Expand Up @@ -180,7 +185,7 @@ class UpgradeDialogViewModel(
}
}
}.catch {
state.value = State.LoadError(it)
state.value = State.LoadError(error = it)
}.onEach {
state.value = it
}.launchIn(viewModelScope)
Expand All @@ -202,6 +207,12 @@ class UpgradeDialogViewModel(
val currencies = dynamicPlan.instances
.flatMap { (_, instance) -> instance.price.map { it.value.currency } }
.toSet()

// Temporary workaround for issue in core returning wrong prices if there was an issue
// fetching prices from google billing library.
if (currencies.size > 1)
return emptyMap()

// The prices coming from Google Play will have a single currency:
val currency = currencies.first()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.protonvpn.android.ui.planupgrade

import androidx.activity.ComponentActivity
import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.protonvpn.android.telemetry.UpgradeSource
Expand Down Expand Up @@ -62,7 +63,10 @@ abstract class CommonUpgradeDialogViewModel(
object Initializing : State()
object UpgradeDisabled : State()
object LoadingPlans : State()
class LoadError(val error: Throwable) : State()
class LoadError(
@StringRes val messageRes: Int? = null,
val error: Throwable? = null
) : State()
data class PurchaseReady(
val plan: PlanModel,
val priceInfo: Map<PlanCycle, PriceInfo>,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1341,4 +1341,5 @@
<string name="p2p_upsell_banner_description">Download files through BitTorrent and other file sharing protocols</string>
<string name="tor_upsell_banner_description">Use the Tor network over your VPN connection for extra privacy</string>
<string name="vpnplus_upsell_banner_description">Upgrade to VPN Plus</string>
<string name="error_fetching_prices">Error fetching prices.</string>
</resources>

0 comments on commit 67952bd

Please sign in to comment.