SHARE
TWEET

Untitled

a guest Jun 18th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package *
  2.  
  3. import android.content.Intent
  4. import androidx.preference.PreferenceFragmentCompat
  5. import android.os.Bundle
  6. import android.widget.Toast
  7. import androidx.preference.Preference
  8.  
  9. import com.starikov.datecalc.R
  10.  
  11. class SettingsFragment : PreferenceFragmentCompat(), SettingsContract.View {
  12.  
  13.     private lateinit var presenter: SettingsContract.Presenter
  14.  
  15.     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
  16.         setPreferencesFromResource(R.xml.preferences, rootKey)
  17.  
  18.         presenter = SettingsPresenter(activity!!)
  19.         presenter.attachView(this)
  20.  
  21. val makeDonatePref: Preference = findPreference(MAKE_DONATE_KEY)!!
  22.  
  23.         makeDonatePref.setOnPreferenceClickListener { presenter.makeDonateClicked(); true }
  24.  
  25.         presenter.viewIsReady()
  26.     }
  27.  
  28.     override fun showToast(resId: Int) {
  29.         Toast.makeText(activity, resId, Toast.LENGTH_LONG).show()
  30.     }
  31.  
  32.     override fun startActivity(intent: Intent) {
  33.         activity!!.startActivity(intent)
  34.     }
  35.  
  36.     override fun onDestroy() {
  37.         super.onDestroy()
  38.         presenter.detachView()
  39.         if (activity!!.isFinishing) {
  40.             presenter.destroy()
  41.         }
  42.     }
  43.  
  44.     companion object {
  45.         private const val MAKE_DONATE_KEY = "make_donate"
  46.     }
  47. }
  48.      
  49. package *
  50.  
  51. import android.app.Activity
  52. import android.content.ActivityNotFoundException
  53. import android.content.Intent
  54. import android.net.Uri
  55. import com.android.billingclient.api.*
  56.  
  57. import com.starikov.datecalc.common.PresenterBase
  58. import com.android.billingclient.api.BillingClient
  59. import com.android.billingclient.api.SkuDetailsParams
  60. import com.android.billingclient.api.BillingFlowParams
  61. import com.starikov.datecalc.R
  62. import java.util.*
  63.  
  64.  
  65. internal class SettingsPresenter internal constructor(private val activity: Activity)
  66.     : PresenterBase<SettingsContract.View>(), SettingsContract.Presenter {
  67.     private lateinit var billingClient: BillingClient
  68.     private var skuDetailsMap: HashMap<String, SkuDetails> = HashMap()
  69.  
  70.     override fun makeDonateClicked() {
  71.         initBillingProcessor()
  72.         launchBilling(DONATE_PRODUCT_ID)
  73.     }
  74.  
  75.     override fun destroy() {
  76.         billingClient.endConnection()
  77.     }
  78.  
  79.     override fun viewIsReady() {}
  80.  
  81.     private fun initBillingProcessor() {
  82.         billingClient = BillingClient.newBuilder(activity)
  83.                 .enablePendingPurchases()
  84.                 .setListener {
  85.             billingResult, purchases ->
  86.             if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
  87.                 //purchase done
  88.                 view!!.showToast(R.string.thanks_for_donate)
  89.             }
  90.         }.build()
  91.  
  92.         billingClient.startConnection(object : BillingClientStateListener {
  93.             override fun onBillingSetupFinished(billingResult: BillingResult?) {
  94.                 if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK) {
  95.                      // place of logs
  96.                     querySkuDetails()
  97.                 }
  98.             }
  99.  
  100.             override fun onBillingServiceDisconnected() {
  101.                 //if wrong
  102.             }
  103.         })
  104.     }
  105.  
  106.     private fun launchBilling(skuId: String) {
  107.         val billingFlowParams = BillingFlowParams.newBuilder()
  108.                 .setSkuDetails(skuDetailsMap[skuId])
  109.                 .build()
  110.         billingClient.launchBillingFlow(activity, billingFlowParams)
  111.     }
  112.  
  113.     private fun querySkuDetails() {
  114.         val skuDetailsParamsBuilder = SkuDetailsParams.newBuilder()
  115.         val skuList = ArrayList<String>()
  116.         skuList.add(DONATE_PRODUCT_ID)
  117.         skuDetailsParamsBuilder.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)
  118.         billingClient.querySkuDetailsAsync(skuDetailsParamsBuilder.build()) { billingResult, skuDetailsList ->
  119.             if (billingResult?.responseCode ==  BillingClient.BillingResponseCode.OK) {
  120.                 for (skuDetails in skuDetailsList!!) {
  121.                     skuDetailsMap[skuDetails.sku] = skuDetails
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.     companion object {
  128.         private const val DONATE_PRODUCT_ID = "donate"
  129.     }
  130. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top