|
@@ -1,29 +1,52 @@
|
|
|
package com.vpn.fastestvpnservice.viewmodels
|
|
|
|
|
|
+import android.app.Activity
|
|
|
import android.content.Context
|
|
|
import android.util.Log
|
|
|
+import androidx.compose.runtime.mutableStateOf
|
|
|
+import androidx.core.app.ComponentActivity
|
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
+import com.android.billingclient.api.BillingClient
|
|
|
+import com.android.billingclient.api.BillingClientStateListener
|
|
|
+import com.android.billingclient.api.BillingFlowParams
|
|
|
+import com.android.billingclient.api.BillingResult
|
|
|
+import com.android.billingclient.api.Purchase
|
|
|
+import com.android.billingclient.api.PurchasesUpdatedListener
|
|
|
+import com.android.billingclient.api.SkuDetails
|
|
|
+import com.android.billingclient.api.SkuDetailsParams
|
|
|
import com.google.gson.Gson
|
|
|
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.helpers.BasePreferenceHelper
|
|
|
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
|
|
|
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
|
|
|
+import com.vpn.fastestvpnservice.screens.accountScreensAll.SubscriptionScreen
|
|
|
import retrofit2.Call
|
|
|
|
|
|
-class SubscriptionViewModel constructor(context: Context): ViewModel() {
|
|
|
+class SubscriptionViewModel constructor(context: Context, activity: ComponentActivity): ViewModel(), PurchasesUpdatedListener {
|
|
|
|
|
|
var mutableLiveDataProducts = MutableLiveData<DataResponse<ArrayList<ProductFeatures>>>()
|
|
|
var liveDataProducts: LiveData<DataResponse<ArrayList<ProductFeatures>>> = mutableLiveDataProducts
|
|
|
|
|
|
+
|
|
|
+ var mutableLiveDataSkuDetails = MutableLiveData<MutableList<SkuDetails>>()
|
|
|
+ var liveDataSkuDetails: LiveData<MutableList<SkuDetails>> = mutableLiveDataSkuDetails
|
|
|
+
|
|
|
var mutableLiveDataProductsErrorStatus = MutableLiveData<Boolean>()
|
|
|
|
|
|
var preferenceHelper: BasePreferenceHelper
|
|
|
+ private var context: Context
|
|
|
+ private var activity: ComponentActivity
|
|
|
+
|
|
|
+ private var selectedPosition: Int = 0
|
|
|
|
|
|
init {
|
|
|
+ this.context = context
|
|
|
+ this.activity = activity
|
|
|
preferenceHelper = BasePreferenceHelper(context)
|
|
|
}
|
|
|
|
|
@@ -70,4 +93,201 @@ class SubscriptionViewModel constructor(context: Context): ViewModel() {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ val tag = "SubscriptionViewModel"
|
|
|
+ val prefHelper = BasePreferenceHelper(context)
|
|
|
+ private var skuDetailsList: List<SkuDetails>? = null
|
|
|
+
|
|
|
+ private val billingClient: BillingClient by lazy {
|
|
|
+ BillingClient.newBuilder(context)
|
|
|
+ .setListener(this)
|
|
|
+ .enablePendingPurchases()
|
|
|
+ .build()
|
|
|
+ }
|
|
|
+
|
|
|
+ init {
|
|
|
+ Log.d(tag, "init Billing")
|
|
|
+// startConnection()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun startConnection(selectedPosition: Int) {
|
|
|
+ this.selectedPosition = selectedPosition
|
|
|
+ Log.d(tag, "startConnection Billing")
|
|
|
+
|
|
|
+ billingClient.startConnection(object : BillingClientStateListener {
|
|
|
+
|
|
|
+ override fun onBillingSetupFinished(billingResult: BillingResult) {
|
|
|
+ if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
|
|
+ Log.d(tag, "onBillingSetupFinished")
|
|
|
+ Log.d(tag, "BILLING | startConnection | RESULT OK")
|
|
|
+ onLoadProductsClicked()
|
|
|
+ } else {
|
|
|
+ Log.d(tag, "BILLING | startConnection | RESULT: ${billingResult.responseCode}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onBillingServiceDisconnected() {
|
|
|
+ Log.d(tag, "BILLING | onBillingServiceDisconnected | DISCONNECTED")
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun onLoadProductsClicked() {
|
|
|
+ Log.d(tag, "onLoadProductsClicked Billing")
|
|
|
+
|
|
|
+// val skuList = listOf("com.fastestvpn.vpn.one.month", "com.fastestvpn.vpn.one.year")
|
|
|
+ val skuList: ArrayList<String> = ArrayList<String>()
|
|
|
+
|
|
|
+ val identifierProduct = prefHelper.getFeaturesData()
|
|
|
+
|
|
|
+ identifierProduct.forEachIndexed { index, productFeatures ->
|
|
|
+ productFeatures.identifier?.let { skuList.add(it) }
|
|
|
+ }
|
|
|
+
|
|
|
+ skuList.forEachIndexed { index, s ->
|
|
|
+ Log.d("skulist identidiers", s)
|
|
|
+ }
|
|
|
+
|
|
|
+ val skuDetailsListsSubs: MutableList<SkuDetails> = ArrayList<SkuDetails>()
|
|
|
+ val params = SkuDetailsParams.newBuilder()
|
|
|
+ params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)
|
|
|
+
|
|
|
+ billingClient.querySkuDetailsAsync(params.build())
|
|
|
+ { billingResult, skuDetailsList ->
|
|
|
+ if (billingResult.responseCode == BillingClient.BillingResponseCode.OK
|
|
|
+ && skuDetailsList != null) {
|
|
|
+ for (skuDetails in skuDetailsList) {
|
|
|
+ Log.d(tag, "[1st] ${skuDetails.title} Billing")
|
|
|
+ skuDetailsListsSubs.add(skuDetails)
|
|
|
+ }
|
|
|
+
|
|
|
+ params.setType(BillingClient.SkuType.SUBS)
|
|
|
+ billingClient.querySkuDetailsAsync(params.build())
|
|
|
+ { billingResults, skuDetailsLists ->
|
|
|
+ if (billingResults.responseCode == BillingClient.BillingResponseCode.OK
|
|
|
+ && skuDetailsLists != null) {
|
|
|
+ for (skuDetails in skuDetailsLists) {
|
|
|
+ Log.d(tag, "[2nd] ${skuDetails.title} Billing")
|
|
|
+ skuDetailsListsSubs.add(skuDetails)
|
|
|
+ }
|
|
|
+ subscriptionProduct(selectedPosition, skuDetailsListsSubs, activity)
|
|
|
+ mutableLiveDataSkuDetails.value = skuDetailsListsSubs
|
|
|
+
|
|
|
+
|
|
|
+// initProductList(skuDetailsListsSubs)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun subscriptionProduct(
|
|
|
+ selectedPosition: Int,
|
|
|
+ skuDetailsListsSubs: MutableList<SkuDetails>,
|
|
|
+ activity: ComponentActivity
|
|
|
+ ) {
|
|
|
+ Log.d(tag, "subscriptionProduct Billing $skuDetailsListsSubs")
|
|
|
+
|
|
|
+ if (skuDetailsListsSubs != null && skuDetailsListsSubs!!.isNotEmpty())
|
|
|
+ {
|
|
|
+ Log.d(tag, "subscriptionProduct IN Billing")
|
|
|
+
|
|
|
+ val billingFlowParams = BillingFlowParams.newBuilder().setSkuDetails(
|
|
|
+ skuDetailsListsSubs!!.get(selectedPosition)).build()
|
|
|
+ billingClient.launchBillingFlow(activity, billingFlowParams)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// private fun initProductList(skuDetailsList: List<SkuDetails>) {
|
|
|
+//
|
|
|
+// this.skuDetailsList = skuDetailsList
|
|
|
+// if (this.skuDetailsList?.size!! > 0) {
|
|
|
+//
|
|
|
+// var boolValue: Boolean
|
|
|
+// var freePeriod: String
|
|
|
+// var title: String
|
|
|
+// var selectFeatureListSorted = ArrayList<ProductFeatures>()
|
|
|
+//
|
|
|
+// for ((index, value) in skuDetailsList.withIndex()){
|
|
|
+// for ((indexed, values) in featuresList.withIndex()){
|
|
|
+//
|
|
|
+// if (value.sku == values.identifier)
|
|
|
+// {
|
|
|
+// Log.d("value of sku", "Equal => $index + $indexed")
|
|
|
+// featuresListSorted.add(featuresList.get(indexed))
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// featuresListSorted.forEachIndexed { index, productFeatures ->
|
|
|
+// Log.d("featuresListSorted", "${productFeatures.title} + ${productFeatures.identifier} + ${productFeatures.price}")
|
|
|
+// }
|
|
|
+//
|
|
|
+// 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
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// requireActivity().runOnUiThread(Runnable {
|
|
|
+// if (isVisible) mainActivity.hideLoading()
|
|
|
+//
|
|
|
+// upgradeListAdapter.data.clear()
|
|
|
+// if (featuresList.size > 1)
|
|
|
+// {
|
|
|
+// upgradeListAdapter.setNewData(featuresListSorted.get(1).features)
|
|
|
+// }
|
|
|
+// else if (featuresList.size > 0)
|
|
|
+// {
|
|
|
+// upgradeListAdapter.setNewData(featuresListSorted.get(0).features)
|
|
|
+// }
|
|
|
+//
|
|
|
+// selectPriceListAdapter.data.clear()
|
|
|
+// selectPriceListAdapter.setNewData(selectPriceList)
|
|
|
+// selectPriceListAdapter.notifyDataSetChanged()
|
|
|
+// mDialogBoxes?.hideProgress()
|
|
|
+//
|
|
|
+// })
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ override fun onPurchasesUpdated(p0: BillingResult, p1: MutableList<Purchase>?) {
|
|
|
+ }
|
|
|
+
|
|
|
}
|