Merge pull request #1956 from Hafizzle/Fiz/insufficientGemsDialog-fix

Use InsufficientGemsDialogEntryPoint
This commit is contained in:
Phillip Thelen 2023-05-02 12:59:14 +02:00 committed by GitHub
commit 9b750306ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 104 additions and 31 deletions

View file

@ -0,0 +1,25 @@
package com.habitrpg.android.habitica.interactors
import android.app.Activity
import com.habitrpg.android.habitica.helpers.PurchaseHandler
import com.habitrpg.android.habitica.helpers.PurchaseTypes
import com.habitrpg.android.habitica.ui.activities.MainActivity
import javax.inject.Inject
class InsufficientGemsUseCase @Inject constructor(
private val purchaseHandler: PurchaseHandler
) : UseCase<InsufficientGemsUseCase.RequestValues, Unit>() {
override suspend fun run(requestValues: RequestValues) {
val activity = requestValues.activity as? MainActivity ?: return
val gemSku = if (requestValues.gemPrice > 4) {
PurchaseTypes.Purchase21Gems
} else {
PurchaseTypes.Purchase4Gems
}
val sku = purchaseHandler.getInAppPurchaseSKU(gemSku) ?: return
purchaseHandler.purchase(activity, sku)
}
class RequestValues(val gemPrice: Int, val activity: Activity) : UseCase.RequestValues
}

View file

@ -17,9 +17,9 @@ import com.habitrpg.android.habitica.models.shops.ShopCategory
import com.habitrpg.android.habitica.models.shops.ShopItem
import com.habitrpg.android.habitica.models.user.OwnedItem
import com.habitrpg.android.habitica.models.user.User
import com.habitrpg.android.habitica.ui.activities.MainActivity
import com.habitrpg.android.habitica.ui.viewHolders.SectionViewHolder
import com.habitrpg.android.habitica.ui.viewHolders.ShopItemViewHolder
import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaAlertDialog
import com.habitrpg.android.habitica.ui.views.getTranslatedClassName
import com.habitrpg.android.habitica.ui.views.insufficientCurrency.InsufficientGemsDialog
import com.habitrpg.common.habitica.extensions.fromHtml
@ -46,6 +46,7 @@ class ShopRecyclerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Vi
}
}
var context: Context? = null
var mainActivity: MainActivity? = null
var user: User? = null
set(value) {
field = value
@ -145,8 +146,10 @@ class ShopRecyclerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<Vi
if ((user?.gemCount ?: 0) >= 3) {
changeClassEvents?.invoke(selectedGearCategory)
} else {
val dialog = InsufficientGemsDialog(context, 3)
dialog.show()
mainActivity?.let { activity ->
val dialog = InsufficientGemsDialog(activity, 3)
dialog.show()
}
}
}
sectionHolder.switchClassButton?.visibility = View.VISIBLE

View file

@ -117,7 +117,10 @@ open class ShopFragment : BaseMainFragment<FragmentRefreshRecyclerviewBinding>()
}
dialog.show()
}
adapter?.context = context
adapter?.mainActivity = mainActivity
binding?.recyclerView?.adapter = adapter
binding?.recyclerView?.itemAnimator = SafeDefaultItemAnimator()
adapter?.changeClassEvents = {

View file

@ -160,8 +160,10 @@ class PreferencesFragment : BasePreferencesFragment(),
dialog.enqueue()
}
} else {
val dialog = context?.let { InsufficientGemsDialog(it, 3) }
dialog?.show()
activity?.let { activity ->
val dialog = InsufficientGemsDialog(activity, 3)
dialog.show()
}
}
} else {
classSelectionResult.launch(intent)

View file

@ -1,6 +1,6 @@
package com.habitrpg.android.habitica.ui.views.insufficientCurrency
import android.content.Context
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.ProgressBar
@ -14,9 +14,16 @@ import com.habitrpg.android.habitica.helpers.AppConfigManager
import com.habitrpg.android.habitica.helpers.MainNavigationController
import com.habitrpg.android.habitica.helpers.PurchaseHandler
import com.habitrpg.android.habitica.helpers.PurchaseTypes
import com.habitrpg.android.habitica.interactors.InsufficientGemsUseCase
import com.habitrpg.common.habitica.helpers.AnalyticsManager
import com.habitrpg.common.habitica.helpers.launchCatching
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@ -25,7 +32,7 @@ import javax.inject.Inject
* Created by phillip on 27.09.17.
*/
class InsufficientGemsDialog(context: Context, var gemPrice: Int) : InsufficientCurrencyDialog(context) {
class InsufficientGemsDialog(val parentActivity: Activity, var gemPrice: Int) : InsufficientCurrencyDialog(parentActivity) {
@Inject
lateinit var configManager: AppConfigManager
@ -34,6 +41,24 @@ class InsufficientGemsDialog(context: Context, var gemPrice: Int) : Insufficient
@Inject
lateinit var purchaseHandler: PurchaseHandler
@EntryPoint
@InstallIn(SingletonComponent::class)
interface InsufficientGemsDialogEntryPoint {
fun configManager(): AppConfigManager
fun analyticsManager(): AnalyticsManager
fun purchaseHandler(): PurchaseHandler
fun insufficientGemsUseCase(): InsufficientGemsUseCase
}
var insufficientGemsUseCase: InsufficientGemsUseCase
init {
val hiltEntryPoint = EntryPointAccessors.fromApplication(parentActivity, InsufficientGemsDialogEntryPoint::class.java)
insufficientGemsUseCase = hiltEntryPoint.insufficientGemsUseCase()
configManager = hiltEntryPoint.configManager()
analyticsManager = hiltEntryPoint.analyticsManager()
purchaseHandler = hiltEntryPoint.purchaseHandler()
}
override fun getLayoutID(): Int {
return R.layout.dialog_insufficient_gems
}
@ -48,34 +73,45 @@ class InsufficientGemsDialog(context: Context, var gemPrice: Int) : Insufficient
override fun onAttachedToWindow() {
super.onAttachedToWindow()
getActivity()?.let { activity ->
val purchaseTextView = contentView.findViewById<TextView>(R.id.purchase_textview)
val purchaseButton = contentView.findViewById<Button>(R.id.purchase_button)
purchaseHandler.startListening()
val gemSku = if (gemPrice > 4) {
purchaseTextView.text = "21"
PurchaseTypes.Purchase21Gems
} else {
purchaseTextView.text = "4"
PurchaseTypes.Purchase4Gems
}
CoroutineScope(Dispatchers.IO).launch {
val sku = purchaseHandler.getInAppPurchaseSKU(gemSku)
?: return@launch
withContext(Dispatchers.Main) {
purchaseButton?.text = sku.oneTimePurchaseOfferDetails?.formattedPrice
contentView.findViewById<ProgressBar>(R.id.loading_indicator).isVisible = false
purchaseButton.isVisible = true
val purchaseTextView = contentView.findViewById<TextView>(R.id.purchase_textview)
val purchaseButton = contentView.findViewById<Button>(R.id.purchase_button)
purchaseHandler.startListening()
val gemSku = if (gemPrice > 4) {
purchaseTextView.text = "21"
PurchaseTypes.Purchase21Gems
} else {
purchaseTextView.text = "4"
PurchaseTypes.Purchase4Gems
}
CoroutineScope(Dispatchers.IO).launch {
val sku = purchaseHandler.getInAppPurchaseSKU(gemSku)
?: return@launch
withContext(Dispatchers.Main) {
purchaseButton?.text = sku.oneTimePurchaseOfferDetails?.formattedPrice
contentView.findViewById<ProgressBar>(R.id.loading_indicator).isVisible = false
purchaseButton.isVisible = true
purchaseButton?.setOnClickListener {
FirebaseAnalytics.getInstance(context).logEvent(
"purchased_gems_from_insufficient",
bundleOf(Pair("gemPrice", gemPrice), Pair("sku", ""))
purchaseButton?.setOnClickListener {
FirebaseAnalytics.getInstance(context).logEvent(
"purchased_gems_from_insufficient",
bundleOf(Pair("gemPrice", gemPrice), Pair("sku", ""))
)
MainScope().launchCatching {
insufficientGemsUseCase.callInteractor(
InsufficientGemsUseCase.RequestValues(
gemPrice,
parentActivity
)
)
purchaseHandler.purchase(activity, sku)
}
}
}
}
}
override fun onDetachedFromWindow() {
purchaseHandler.stopListening()
super.onDetachedFromWindow()
}
}

View file

@ -331,7 +331,11 @@ class PurchaseDialog(context: Context, private val userRepository : UserReposito
}
}
"gold" == shopItem.currency -> InsufficientGoldDialog(context)
"gems" == shopItem.currency -> InsufficientGemsDialog(context, shopItem.value)
"gems" == shopItem.currency -> {
val application = ownerActivity?.application as? HabiticaBaseApplication
val activity = (application?.currentActivity?.get() ?: getActivity() ?: ownerActivity)
activity?.let { InsufficientGemsDialog(activity, shopItem.value) }
}
"hourglasses" == shopItem.currency -> InsufficientHourglassesDialog(context)
else -> null
}?.show()