From b81416463f5740cd3de6f37d31423d5ac4a44dd4 Mon Sep 17 00:00:00 2001
From: Taras Smakula <tarassmakula@gmail.com>
Date: Wed, 13 Dec 2023 14:32:40 +0200
Subject: [PATCH] Bind subscription info

---
 .../ManageSubscriptionDialogFragment.kt       | 58 ++++++++++++++++++-
 .../ManageSubscriptionViewModel.kt            |  1 +
 .../auth/model/ActiveSubscriptionInfo.kt      |  1 +
 3 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionDialogFragment.kt b/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionDialogFragment.kt
index e841124b0..d5d5b4133 100644
--- a/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionDialogFragment.kt
+++ b/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionDialogFragment.kt
@@ -1,13 +1,23 @@
 package org.futo.circles.auth.feature.manage_subscription
 
+import android.content.ActivityNotFoundException
+import android.content.Intent
+import android.net.Uri
 import android.os.Bundle
+import android.text.format.DateFormat
 import android.view.View
-import androidx.fragment.app.Fragment
 import androidx.fragment.app.viewModels
 import dagger.hilt.android.AndroidEntryPoint
+import org.futo.circles.auth.R
 import org.futo.circles.auth.databinding.DialogFragmentManageSubscriptionBinding
+import org.futo.circles.auth.model.ActiveSubscriptionInfo
 import org.futo.circles.auth.subscriptions.SubscriptionProvider
 import org.futo.circles.core.base.fragment.BaseFullscreenDialogFragment
+import org.futo.circles.core.extensions.gone
+import org.futo.circles.core.extensions.observeResponse
+import org.futo.circles.core.extensions.showError
+import org.futo.circles.core.extensions.visible
+import java.util.Date
 import javax.inject.Inject
 
 @AndroidEntryPoint
@@ -33,7 +43,6 @@ class ManageSubscriptionDialogFragment :
         viewModel.getSubscriptionInfo(subscriptionManager)
     }
 
-
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
         setupViews()
@@ -45,6 +54,51 @@ class ManageSubscriptionDialogFragment :
     }
 
     private fun setupObservers() {
+        viewModel.subscriptionInfoLiveData.observeResponse(
+            this,
+            onRequestInvoked = { binding.vLoading.gone() },
+            success = {
+                binding.lSubscriptionInfo.visible()
+                bindSubscriptionInfo(it)
+            },
+            error = {
+                binding.tvEmptyMessage.visible()
+            })
+    }
+
+    private fun bindSubscriptionInfo(subscriptionInfo: ActiveSubscriptionInfo) {
+        with(binding) {
+            tvName.text = subscriptionInfo.name
+            tvDescription.text = subscriptionInfo.description
+            tvPrice.text = getString(R.string.price_format, subscriptionInfo.price)
+            tvDuration.text = getString(R.string.duration_format, subscriptionInfo.duration)
+            tvProductId.text = getString(R.string.product_id_format, subscriptionInfo.productId)
+            val purchaseDate =
+                DateFormat.format("MMM dd yyyy, h:mm a", Date(subscriptionInfo.purchaseTime))
+            tvPurchasedAt.text = getString(R.string.purchase_time_format, purchaseDate)
+            tvAutoRenewMessage.text = getString(
+                if (subscriptionInfo.isAutoRenewing) R.string.is_auto_renew_message
+                else R.string.is_not_auto_renew_message
+            )
+            btnManageGp.setOnClickListener {
+                openPlayStoreSubscriptionInfo(
+                    subscriptionInfo.productId,
+                    subscriptionInfo.packageName
+                )
+            }
+        }
+    }
 
+    private fun openPlayStoreSubscriptionInfo(sku: String, packageName: String) {
+        try {
+            startActivity(
+                Intent(
+                    Intent.ACTION_VIEW,
+                    Uri.parse("https://play.google.com/store/account/subscriptions?sku=$sku&package=$packageName")
+                )
+            )
+        } catch (e: ActivityNotFoundException) {
+            showError(getString(R.string.can_not_open_google_play))
+        }
     }
 }
\ No newline at end of file
diff --git a/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionViewModel.kt b/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionViewModel.kt
index 67b4e086d..715e3b4b3 100644
--- a/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionViewModel.kt
+++ b/auth/src/main/java/org/futo/circles/auth/feature/manage_subscription/ManageSubscriptionViewModel.kt
@@ -36,6 +36,7 @@ class ManageSubscriptionViewModel @Inject constructor(
             subscriptionInfoLiveData.postValue(
                 Response.Success(
                     ActiveSubscriptionInfo(
+                        packageName = activeReceipt.packageName,
                         productId = activeReceipt.productId,
                         purchaseTime = activeReceipt.purchaseTime,
                         isAutoRenewing = activeReceipt.isAutoRenewing,
diff --git a/auth/src/main/java/org/futo/circles/auth/model/ActiveSubscriptionInfo.kt b/auth/src/main/java/org/futo/circles/auth/model/ActiveSubscriptionInfo.kt
index 9de531451..88ddce7fc 100644
--- a/auth/src/main/java/org/futo/circles/auth/model/ActiveSubscriptionInfo.kt
+++ b/auth/src/main/java/org/futo/circles/auth/model/ActiveSubscriptionInfo.kt
@@ -1,6 +1,7 @@
 package org.futo.circles.auth.model
 
 data class ActiveSubscriptionInfo(
+    val packageName: String,
     val productId: String,
     val purchaseTime: Long,
     val isAutoRenewing: Boolean,
-- 
GitLab