|
@@ -3,6 +3,7 @@ package com.vpn.fastestvpnservice.viewmodels
|
|
|
import android.app.Activity
|
|
|
import android.content.Context
|
|
|
import android.util.Log
|
|
|
+import androidx.compose.runtime.livedata.observeAsState
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.core.app.ComponentActivity
|
|
|
import androidx.lifecycle.LiveData
|
|
@@ -21,6 +22,7 @@ import com.google.gson.reflect.TypeToken
|
|
|
import com.vpn.fastestvpnservice.application.App
|
|
|
import com.vpn.fastestvpnservice.beans.DataResponse
|
|
|
import com.vpn.fastestvpnservice.beans.ProductFeatures
|
|
|
+import com.vpn.fastestvpnservice.beans.SubscriptionPackageList
|
|
|
import com.vpn.fastestvpnservice.customItems.getSelectedPosition
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
|
|
|
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
|
|
@@ -42,10 +44,7 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
private var context: Context
|
|
|
private var activity: ComponentActivity
|
|
|
|
|
|
- private var selectedPosition: Int = 0
|
|
|
-
|
|
|
var mutableLiveDataNumber = MutableLiveData<Int>(0)
|
|
|
- var liveDataNumber: LiveData<Int> = mutableLiveDataNumber
|
|
|
|
|
|
val tag = "SubscriptionViewModel"
|
|
|
val prefHelper = BasePreferenceHelper(context)
|
|
@@ -53,6 +52,7 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
|
|
|
var featuresList = MutableLiveData<MutableList<ProductFeatures>>()
|
|
|
var featuresListSorted = MutableLiveData<MutableList<ProductFeatures>>()
|
|
|
+ var selectPriceList = MutableLiveData<MutableList<SubscriptionPackageList>>()
|
|
|
|
|
|
|
|
|
private val billingClient: BillingClient by lazy {
|
|
@@ -62,13 +62,14 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
.build()
|
|
|
}
|
|
|
|
|
|
+ companion object {
|
|
|
+ var selectedPosition: Int = 0
|
|
|
+ }
|
|
|
init {
|
|
|
this.context = context
|
|
|
this.activity = activity
|
|
|
preferenceHelper = BasePreferenceHelper(context)
|
|
|
|
|
|
- startConnection(getSelectedPosition())
|
|
|
-
|
|
|
}
|
|
|
|
|
|
fun setSelectedPosition(num: Int) {
|
|
@@ -101,6 +102,9 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
mutableLiveDataProducts.value = data
|
|
|
mutableLiveDataProductsErrorStatus.value = false
|
|
|
|
|
|
+ startConnection(getSelectedPosition())
|
|
|
+
|
|
|
+
|
|
|
} catch (ex: Exception) {
|
|
|
Log.d("test_api_response", "Products catch:")
|
|
|
mutableLiveDataProductsErrorStatus.value = true
|
|
@@ -122,7 +126,7 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
}
|
|
|
|
|
|
fun startConnection(selectedPosition: Int) {
|
|
|
- this.selectedPosition = selectedPosition
|
|
|
+ SubscriptionViewModel.selectedPosition = selectedPosition
|
|
|
Log.d(tag, "startConnection Billing")
|
|
|
|
|
|
billingClient.startConnection(object : BillingClientStateListener {
|
|
@@ -185,7 +189,7 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
mutableLiveDataSkuDetails.postValue(skuDetailsListsSubs)
|
|
|
// subscriptionProduct(selectedPosition, skuDetailsListsSubs, activity)
|
|
|
|
|
|
-// initProductList(skuDetailsListsSubs)
|
|
|
+ initProductList(skuDetailsListsSubs)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -200,93 +204,101 @@ class SubscriptionViewModel constructor(context: Context, activity: ComponentAct
|
|
|
) {
|
|
|
Log.d(tag, "subscriptionProduct Billing $skuDetailsListsSubs")
|
|
|
|
|
|
- if (skuDetailsListsSubs != null && skuDetailsListsSubs!!.isNotEmpty())
|
|
|
+ if (skuDetailsListsSubs != null && skuDetailsListsSubs.isNotEmpty())
|
|
|
{
|
|
|
Log.d(tag, "subscriptionProduct IN Billing")
|
|
|
|
|
|
val billingFlowParams = BillingFlowParams.newBuilder().setSkuDetails(
|
|
|
- skuDetailsListsSubs!!.get(selectedPosition)).build()
|
|
|
+ skuDetailsListsSubs.get(selectedPosition)).build()
|
|
|
billingClient.launchBillingFlow(activity, billingFlowParams)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private fun initProductList(skuDetailsList: List<SkuDetails>) {
|
|
|
-
|
|
|
+ Log.d("test_subscription_list", "skuDetailsList size = ${skuDetailsList.size}")
|
|
|
this.skuDetailsList = skuDetailsList
|
|
|
if (this.skuDetailsList?.size!! > 0) {
|
|
|
+ Log.d("test_subscription_list", "skuDetailsList[2] size = ${skuDetailsList.size}")
|
|
|
|
|
|
var boolValue: Boolean
|
|
|
var freePeriod: String
|
|
|
var title: String
|
|
|
var selectFeatureListSorted = ArrayList<ProductFeatures>()
|
|
|
|
|
|
+ val featuresListSortedData = ArrayList<ProductFeatures>()
|
|
|
+ val selectPriceListData = ArrayList<SubscriptionPackageList>()
|
|
|
+
|
|
|
+
|
|
|
featuresList.value?.let {
|
|
|
- for ((index, value) in skuDetailsList.withIndex()){
|
|
|
- for ((indexed, values) in it.withIndex()){
|
|
|
- if (value.sku == values.identifier)
|
|
|
- {
|
|
|
- Log.d("value_of_sku", "Equal => $index + $indexed")
|
|
|
- featuresListSorted.value?.add(it.get(indexed))
|
|
|
+ for ((index, value) in skuDetailsList.withIndex()) {
|
|
|
+ for ((indexed, values) in it.withIndex()) {
|
|
|
+ if (value.sku == values.identifier) {
|
|
|
+ Log.d("test_subscription_list", "Equal => $index + $indexed")
|
|
|
+ featuresListSortedData.add(it.get(indexed))
|
|
|
+ Log.d(
|
|
|
+ "test_subscription_list",
|
|
|
+ "featuresList: ${it.get(indexed).title} ${
|
|
|
+ featuresListSortedData?.get(indexed)?.title
|
|
|
+ }"
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ featuresListSorted.postValue(featuresListSortedData)
|
|
|
|
|
|
|
|
|
- featuresListSorted.value?.forEachIndexed { index, productFeatures ->
|
|
|
- Log.d("featuresListSorted", "${productFeatures.title} + ${productFeatures.identifier} + ${productFeatures.price}")
|
|
|
- }
|
|
|
+ val list = featuresListSorted.value
|
|
|
+ Log.d("test_subscription_list", "featuresListSorted size = ${list?.size}")
|
|
|
|
|
|
-// for ((index, value) in skuDetailsList.withIndex()){
|
|
|
-//
|
|
|
-// Log.d("value of 1", value.title + " ," + value.freeTrialPeriod + "=>" + featuresList.get(index).identifier)
|
|
|
-//
|
|
|
-// if (skuDetailsList.size > 1)
|
|
|
-// {
|
|
|
-// boolValue = index == 1
|
|
|
-// }
|
|
|
-// else{
|
|
|
-// boolValue = index == 0
|
|
|
-// }
|
|
|
-//
|
|
|
-// if (value.freeTrialPeriod == "P3D")
|
|
|
-// {
|
|
|
-// freePeriod = "Free for 3 days"
|
|
|
-// } else if (value.freeTrialPeriod == "P1W")
|
|
|
-// {
|
|
|
-// freePeriod = "Free for 7 days"
|
|
|
-// }
|
|
|
-// else
|
|
|
-// {
|
|
|
-// freePeriod = ""
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// selectPriceList.add(
|
|
|
-// UpgradePriceList(value.title, value.description, value.price, freePeriod,
|
|
|
-// boolValue, index)
|
|
|
-// )
|
|
|
-// }
|
|
|
-//
|
|
|
-// selectPriceList.forEachIndexed { index, upgradePriceList ->
|
|
|
-// if (upgradePriceList.isEnabled) {
|
|
|
-// selectedPosition = index
|
|
|
-// }
|
|
|
+// featuresListSortedData.forEachIndexed { index, productFeatures ->
|
|
|
+// Log.d("test_subscription_list", "${productFeatures.title} + ${productFeatures.identifier} + ${productFeatures.price}")
|
|
|
// }
|
|
|
-//
|
|
|
-// upgradeListAdapter.data.clear()
|
|
|
-// if (featuresList.size > 1)
|
|
|
-// {
|
|
|
-// upgradeListAdapter.setNewData(featuresListSorted.get(1).features)
|
|
|
-// }
|
|
|
-// else if (featuresList.size > 0)
|
|
|
-// {
|
|
|
-// upgradeListAdapter.setNewData(featuresListSorted.get(0).features)
|
|
|
-// }
|
|
|
+
|
|
|
+ for ((index, value) in skuDetailsList.withIndex()) {
|
|
|
+
|
|
|
+// Log.d("test_subscription_list", value.title + " ," + value.freeTrialPeriod + "=>" + featuresList.value?.get(index)?.identifier)
|
|
|
+
|
|
|
+ if (skuDetailsList.size > 1) {
|
|
|
+ boolValue = index == 1
|
|
|
+ } else {
|
|
|
+ boolValue = index == 0
|
|
|
+ }
|
|
|
+
|
|
|
+ freePeriod = if (value.freeTrialPeriod == "P3D") {
|
|
|
+ "Free for 3 days"
|
|
|
+ } else if (value.freeTrialPeriod == "P1W") {
|
|
|
+ "Free for 7 days"
|
|
|
+ } else {
|
|
|
+ ""
|
|
|
+ }
|
|
|
+
|
|
|
+ val planTitle = featuresListSortedData.get(index).title
|
|
|
+ selectPriceListData.add(
|
|
|
+ SubscriptionPackageList(
|
|
|
+ planTitle ?: value.title, "Auto-Renewable", value.description, value.price,
|
|
|
+ mutableStateOf(boolValue)
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ selectPriceListData.forEachIndexed { index, upgradePriceList ->
|
|
|
+ Log.d(
|
|
|
+ "test_subscription_list",
|
|
|
+ "${upgradePriceList.packageDuration} ${upgradePriceList.planType} ${upgradePriceList.desc} ${upgradePriceList.price} ${upgradePriceList.isSelected.value}"
|
|
|
+ )
|
|
|
+
|
|
|
+ if (upgradePriceList.isSelected.value) {
|
|
|
+ selectedPosition = index
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ selectPriceList.postValue(selectPriceListData)
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
override fun onPurchasesUpdated(p0: BillingResult, p1: MutableList<Purchase>?) {
|
|
|
}
|
|
|
|