Skip to content

Commit

Permalink
fix: In App Purchases(IAP) Improvements (#17)
Browse files Browse the repository at this point in the history
* chore: add value prop screen event

* fix: remove value_prop_enabled check for IAP

* fix: put restore button behind the iap enable config

* fix: Make IAP error dialog Non-Cancellable

* fix: Improve IAP full screen dialog UI

* fix: hide upgrade button for collapsed toolbar in course dashboard
  • Loading branch information
farhan-arshad-dev authored Jul 25, 2024
1 parent 12056d2 commit ae34c4c
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 131 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ android {

sourceSets {
prod {
res.srcDirs = ["src/$themeDirectory/res"]
res.srcDirs += ["src/$themeDirectory/res"]
}
develop {
res.srcDirs = ["src/$themeDirectory/res"]
res.srcDirs += ["src/$themeDirectory/res"]
}
stage {
res.srcDirs = ["src/$themeDirectory/res"]
res.srcDirs += ["src/$themeDirectory/res"]
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ android {
sourceSets {
prod {
java.srcDirs = ["src/$themeDirectory"]
res.srcDirs = ["src/$themeDirectory/res"]
res.srcDirs += ["src/$themeDirectory/res"]
}
develop {
java.srcDirs = ["src/$themeDirectory"]
res.srcDirs = ["src/$themeDirectory/res"]
res.srcDirs += ["src/$themeDirectory/res"]
}
stage {
java.srcDirs = ["src/$themeDirectory"]
res.srcDirs = ["src/$themeDirectory/res"]
res.srcDirs += ["src/$themeDirectory/res"]
}
main {
assets {
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/org/openedx/core/data/model/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ data class AppConfig(
@SerializedName("course_dates_calendar_sync")
val calendarSyncConfig: CalendarSyncConfig = CalendarSyncConfig(),

@SerializedName("value_prop_enabled")
val isValuePropEnabled: Boolean = false,

@SerializedName("iap_config")
val iapConfig: IAPConfig = IAPConfig(),
) {
fun mapToDomain(): DomainAppConfig {
return DomainAppConfig(
courseDatesCalendarSync = calendarSyncConfig.mapToDomain(),
isValuePropEnabled = isValuePropEnabled,
iapConfig = iapConfig.mapToDomain(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.io.Serializable

data class AppConfig(
val courseDatesCalendarSync: CourseDatesCalendarSync,
val isValuePropEnabled: Boolean = false,
val iapConfig: IAPConfig = IAPConfig(),
) : Serializable

Expand Down
9 changes: 0 additions & 9 deletions core/src/main/java/org/openedx/core/extension/ViewExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ fun DialogFragment.setWidthPercent(percentage: Int) {
dialog?.window?.setLayout(percentWidth.toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
}

fun DialogFragment.setFullScreen(percentage: Int) {
val percent = percentage.toFloat() / 100
val dm = Resources.getSystem().displayMetrics
val rect = dm.run { Rect(0, 0, widthPixels, heightPixels) }
val percentWidth = rect.width() * percent
val percentHeight = rect.height() * percent
dialog?.window?.setLayout(percentWidth.toInt(), percentHeight.toInt())
}

fun Context.toastMessage(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface IAPAnalytics {
params: MutableMap<String, Any?> = mutableMapOf(),
screenName: String,
)

fun logScreenEvent(screenName: String, params: Map<String, Any?>)
}

enum class IAPAnalyticsEvent(val eventName: String, val biValue: String) {
Expand Down Expand Up @@ -45,6 +47,10 @@ enum class IAPAnalyticsEvent(val eventName: String, val biValue: String) {
IAP_RESTORE_PURCHASE_CLICKED(
"Payments: Restore Purchases Clicked",
"edx.bi.app.payments.restore_purchases.clicked"
),
IAP_VALUE_PROP_VIEWED(
"Payments: Value Prop Viewed",
"edx.bi.app.payments.value_prop.viewed"
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.openedx.core.presentation.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
Expand Down Expand Up @@ -38,7 +36,6 @@ import org.openedx.core.domain.model.iap.ProductInfo
import org.openedx.core.domain.model.iap.PurchaseFlowData
import org.openedx.core.extension.parcelable
import org.openedx.core.extension.serializable
import org.openedx.core.extension.setFullScreen
import org.openedx.core.presentation.iap.IAPAction
import org.openedx.core.presentation.iap.IAPFlow
import org.openedx.core.presentation.iap.IAPLoaderType
Expand Down Expand Up @@ -67,7 +64,6 @@ class IAPDialogFragment : DialogFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?,
) = ComposeView(requireContext()).apply {
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
OpenEdXTheme {
Expand All @@ -77,7 +73,7 @@ class IAPDialogFragment : DialogFragment() {

val isFullScreenLoader =
(iapState as? IAPUIState.Loading)?.loaderType == IAPLoaderType.FULL_SCREEN

isCancelable = !isFullScreenLoader
Scaffold(
modifier = Modifier.fillMaxSize(),
backgroundColor = MaterialTheme.appColors.background,
Expand Down Expand Up @@ -228,9 +224,8 @@ class IAPDialogFragment : DialogFragment() {
}
}

override fun onStart() {
super.onStart()
setFullScreen(100)
override fun getTheme(): Int {
return R.style.Theme_OpenEdX_IAPDialog
}

private fun onDismiss() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class IAPViewModel(

when (iapFlow) {
IAPFlow.USER_INITIATED -> {
loadIAPScreenEvent()
loadPrice()
}

Expand Down Expand Up @@ -337,39 +338,58 @@ class IAPViewModel(
}.toMutableMap())
}

private fun getIAPEventParams(): MutableMap<String, Any?> {
return buildMap {
purchaseFlowData.takeIf { it.courseId.isNullOrBlank().not() }?.let {
put(IAPAnalyticsKeys.COURSE_ID.key, purchaseFlowData.courseId)
put(
IAPAnalyticsKeys.PACING.key,
if (purchaseFlowData.isSelfPaced == true) IAPAnalyticsKeys.SELF.key else IAPAnalyticsKeys.INSTRUCTOR.key
)
}
purchaseFlowData.productInfo?.lmsUSDPrice?.nonZero()?.let { lmsUSDPrice ->
put(IAPAnalyticsKeys.LMS_USD_PRICE.key, lmsUSDPrice)
}
purchaseFlowData.price.nonZero()?.let { localizedPrice ->
put(IAPAnalyticsKeys.LOCALIZED_PRICE.key, localizedPrice)
}
purchaseFlowData.currencyCode.takeIfNotEmpty()?.let { currencyCode ->
put(IAPAnalyticsKeys.CURRENCY_CODE.key, currencyCode)
}
purchaseFlowData.componentId?.takeIf { it.isNotBlank() }?.let { componentId ->
put(IAPAnalyticsKeys.COMPONENT_ID.key, componentId)
}
put(IAPAnalyticsKeys.CATEGORY.key, IAPAnalyticsKeys.IN_APP_PURCHASES.key)
}.toMutableMap()
}

private fun logIAPEvent(
event: IAPAnalyticsEvent,
params: MutableMap<String, Any?> = mutableMapOf(),
) {
params.apply {
put(IAPAnalyticsKeys.NAME.key, event.biValue)
putAll(getIAPEventParams())
}
analytics.logIAPEvent(
event = event,
params = params.apply {
put(IAPAnalyticsKeys.NAME.key, event.biValue)
purchaseFlowData.takeIf { it.courseId.isNullOrBlank().not() }?.let {
put(IAPAnalyticsKeys.COURSE_ID.key, purchaseFlowData.courseId)
put(
IAPAnalyticsKeys.PACING.key,
if (purchaseFlowData.isSelfPaced == true) IAPAnalyticsKeys.SELF.key else IAPAnalyticsKeys.INSTRUCTOR.key
)
}
purchaseFlowData.productInfo?.lmsUSDPrice?.nonZero()?.let { lmsUSDPrice ->
put(IAPAnalyticsKeys.LMS_USD_PRICE.key, lmsUSDPrice)
}
purchaseFlowData.price.nonZero()?.let { localizedPrice ->
put(IAPAnalyticsKeys.LOCALIZED_PRICE.key, localizedPrice)
}
purchaseFlowData.currencyCode.takeIfNotEmpty()?.let { currencyCode ->
put(IAPAnalyticsKeys.CURRENCY_CODE.key, currencyCode)
}
purchaseFlowData.componentId?.takeIf { it.isNotBlank() }?.let { componentId ->
put(IAPAnalyticsKeys.COMPONENT_ID.key, componentId)
}
put(IAPAnalyticsKeys.CATEGORY.key, IAPAnalyticsKeys.IN_APP_PURCHASES.key)
},
params = params,
screenName = purchaseFlowData.screenName.orEmpty()
)
}

private fun loadIAPScreenEvent() {
val event = IAPAnalyticsEvent.IAP_VALUE_PROP_VIEWED
val params = buildMap {
put(IAPAnalyticsKeys.NAME.key, event.biValue)
purchaseFlowData.screenName?.takeIfNotEmpty()?.let { screenName ->
put(IAPAnalyticsKeys.SCREEN_NAME.key, screenName)
}
putAll(getIAPEventParams())
}
analytics.logScreenEvent(screenName = event.eventName, params = params)
}

fun clearIAPFLow() {
_uiState.value = IAPUIState.Clear
purchaseFlowData.reset()
Expand Down
19 changes: 14 additions & 5 deletions core/src/main/java/org/openedx/core/ui/IAPUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import org.openedx.core.R
import org.openedx.core.exception.iap.IAPException
import org.openedx.core.presentation.iap.IAPAction
Expand Down Expand Up @@ -183,7 +184,8 @@ fun NoSkuErrorDialog(
onClick = onConfirm
)
},
onDismissRequest = onConfirm
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false),
onDismissRequest = {}
)
}

Expand Down Expand Up @@ -252,6 +254,7 @@ fun CourseAlreadyPurchasedExecuteErrorDialog(
)
}
},
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false),
onDismissRequest = {}
)
}
Expand Down Expand Up @@ -302,13 +305,17 @@ fun UpgradeErrorDialog(
onClick = onDismiss
)
},
onDismissRequest = onConfirm
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false),
onDismissRequest = {}
)
}

@Composable
fun CheckingPurchasesDialog() {
Dialog(onDismissRequest = { }) {
Dialog(
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false),
onDismissRequest = {}
) {
Column(
Modifier
.padding(16.dp)
Expand Down Expand Up @@ -372,7 +379,8 @@ fun FakePurchasesFulfillmentCompleted(onCancel: () -> Unit, onGetHelp: () -> Uni
onClick = onGetHelp
)
},
onDismissRequest = onCancel
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false),
onDismissRequest = {}
)
}

Expand Down Expand Up @@ -415,7 +423,8 @@ fun PurchasesFulfillmentCompletedDialog(onConfirm: () -> Unit, onDismiss: () ->
onClick = onDismiss
)
},
onDismissRequest = onDismiss
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false),
onDismissRequest = {}
)
}

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<resources>

<style name="Theme.OpenEdX.IAPDialog" parent="Theme.OpenEdX.Dialog.FullScreen">
<item name="android:windowLightStatusBar">false</item>
</style>

</resources>
28 changes: 20 additions & 8 deletions core/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<resources>
<!-- Base application theme. -->
<style name="Theme.OpenEdX" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/primary</item>
<!-- <item name="colorPrimaryVariant">@color/primaryVariant</item>-->
<!-- <item name="colorOnPrimary">@color/white</item>-->
<!-- &lt;!&ndash; Secondary brand color. &ndash;&gt;-->
<!-- <item name="colorSecondary">@color/secondary</item>-->
<!-- <item name="colorSecondaryVariant">@color/secondaryVariant</item>-->
<!-- <item name="colorOnSecondary">@color/black</item>-->
<!-- <item name="colorPrimaryVariant">@color/primaryVariant</item>-->
<!-- <item name="colorOnPrimary">@color/white</item>-->
<!-- &lt;!&ndash; Secondary brand color. &ndash;&gt;-->
<!-- <item name="colorSecondary">@color/secondary</item>-->
<!-- <item name="colorSecondaryVariant">@color/secondaryVariant</item>-->
<!-- <item name="colorOnSecondary">@color/black</item>-->
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
Expand All @@ -23,4 +23,16 @@
<style name="Theme.OpenEdX.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="Theme.OpenEdX.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

<style name="Theme.OpenEdX.Dialog.FullScreen" parent="Theme.OpenEdX">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@color/background</item>
</style>

<style name="Theme.OpenEdX.IAPDialog" parent="Theme.OpenEdX.Dialog.FullScreen">
<item name="android:windowLightStatusBar">true</item>
</style>

</resources>
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ae34c4c

Please sign in to comment.