Skip to content

Commit

Permalink
fix: Add upgrade banner in the FC47 Primary Course card view
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill committed Jul 17, 2024
1 parent 44e39d8 commit 3b3cca2
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 167 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ val screenModule = module {
get(),
get(),
get(),
get()
)
}

Expand All @@ -164,7 +163,10 @@ val screenModule = module {
get(),
get(),
get(),
windowSize
get(),
get(),
get(),
windowSize,
)
}
viewModel { AllEnrolledCoursesViewModel(get(), get(), get(), get(), get(), get(), get()) }
Expand Down Expand Up @@ -441,7 +443,7 @@ val screenModule = module {
}

single { IAPRepository(get()) }
factory { IAPInteractor(get(), get()) }
factory { IAPInteractor(get(), get(), get(), get(), get(), get()) }
viewModel { (iapFlow: IAPFlow, purchaseFlowData: PurchaseFlowData) ->
IAPViewModel(
iapFlow = iapFlow,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ data class CourseEnrollments(
enrollments.results.forEach { courseData ->
courseData.setStoreSku(appConfig.iapConfig.productPrefix)
}
primaryCourse?.setStoreSku(appConfig.iapConfig.productPrefix)
}

return CourseEnrollments(enrollments, appConfig, primaryCourse)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,73 @@
package org.openedx.core.domain.interactor

import android.content.Context
import androidx.fragment.app.FragmentActivity
import com.android.billingclient.api.BillingClient.BillingResponseCode
import com.android.billingclient.api.ProductDetails
import com.android.billingclient.api.Purchase
import org.openedx.core.ApiConstants
import org.openedx.core.R
import org.openedx.core.config.Config
import org.openedx.core.data.repository.iap.IAPRepository
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.domain.model.iap.ProductInfo
import org.openedx.core.exception.iap.IAPException
import org.openedx.core.extension.decodeToLong
import org.openedx.core.module.billing.BillingProcessor
import org.openedx.core.module.billing.getCourseSku
import org.openedx.core.module.billing.getPriceAmount
import org.openedx.core.presentation.IAPAnalytics
import org.openedx.core.presentation.IAPAnalyticsEvent
import org.openedx.core.presentation.IAPAnalyticsKeys
import org.openedx.core.presentation.IAPAnalyticsScreen
import org.openedx.core.presentation.global.AppData
import org.openedx.core.presentation.iap.IAPAction
import org.openedx.core.presentation.iap.IAPFlow
import org.openedx.core.presentation.iap.IAPRequestType
import org.openedx.core.utils.EmailUtil

class IAPInteractor(
private val appData: AppData,
private val billingProcessor: BillingProcessor,
private val config: Config,
private val repository: IAPRepository,
private val preferencesManager: CorePreferences,
private val iapAnalytics: IAPAnalytics,
) {
private val iapConfig
get() = preferencesManager.appConfig.iapConfig
private val isIAPEnabled
get() = iapConfig.isEnabled &&
iapConfig.disableVersions.contains(appData.versionName).not()

fun logIAPCancelEvent(screen: IAPAnalyticsScreen) {
logIAPEvent(
IAPAnalyticsEvent.IAP_ERROR_ALERT_ACTION, buildMap {
put(IAPAnalyticsKeys.ERROR_ALERT_TYPE.key, IAPAction.ACTION_UNFULFILLED.action)
put(IAPAnalyticsKeys.ERROR_ACTION.key, IAPAction.ACTION_CLOSE.action)
}.toMutableMap(),
screen = screen,
)
}

fun showFeedbackScreen(context: Context, message: String, screen: IAPAnalyticsScreen) {
EmailUtil.showFeedbackScreen(
context = context,
feedbackEmailAddress = config.getFeedbackEmailAddress(),
subject = context.getString(R.string.core_error_upgrading_course_in_app),
feedback = message,
appVersion = appData.versionName
)
logIAPEvent(
IAPAnalyticsEvent.IAP_ERROR_ALERT_ACTION,
buildMap {
put(IAPAnalyticsKeys.ERROR_ALERT_TYPE.key, IAPAction.ACTION_UNFULFILLED.action)
put(IAPAnalyticsKeys.ERROR_ACTION.key, IAPAction.ACTION_GET_HELP.action)
}.toMutableMap(),
screen = screen,
)
}

suspend fun loadPrice(productId: String): ProductDetails.OneTimePurchaseOfferDetails {
val response = billingProcessor.querySyncDetails(productId)
val productDetails = response.productDetailsList?.firstOrNull()?.oneTimePurchaseOfferDetails
Expand Down Expand Up @@ -122,4 +172,47 @@ class IAPInteractor(
}
}
}

suspend fun detectUnfulfilledPurchase(
screen: IAPAnalyticsScreen,
onSuccess: () -> Unit,
onFailure: (IAPException) -> Unit,
) {
if (isIAPEnabled) {
preferencesManager.user?.id?.let { userId ->
runCatching {
processUnfulfilledPurchase(userId)
}.onSuccess {
if (it) {
onSuccess()
unfulfilledPurchaseInitiatedEvent(screen)
}
}.onFailure {
if (it is IAPException) {
onFailure(it)
}
}
}
}
}

private fun unfulfilledPurchaseInitiatedEvent(screen: IAPAnalyticsScreen) {
logIAPEvent(
IAPAnalyticsEvent.IAP_UNFULFILLED_PURCHASE_INITIATED,
screen = screen
)
}

private fun logIAPEvent(
event: IAPAnalyticsEvent,
params: MutableMap<String, Any?> = mutableMapOf(),
screen: IAPAnalyticsScreen,
) {
iapAnalytics.logEvent(event.eventName, params.apply {
put(IAPAnalyticsKeys.NAME.key, event.biValue)
put(IAPAnalyticsKeys.SCREEN_NAME.key, screen.screenName)
put(IAPAnalyticsKeys.IAP_FLOW_TYPE.key, IAPFlow.SILENT.value)
put(IAPAnalyticsKeys.CATEGORY.key, IAPAnalyticsKeys.IN_APP_PURCHASES.key)
})
}
}
70 changes: 54 additions & 16 deletions core/src/main/java/org/openedx/core/ui/UpgradeToAccessView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package org.openedx.core.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowForwardIos
import androidx.compose.material.icons.filled.EmojiEvents
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Lock
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
Expand All @@ -31,49 +37,80 @@ import org.openedx.core.ui.theme.appTypography
fun UpgradeToAccessView(
modifier: Modifier = Modifier,
type: UpgradeToAccessViewType = UpgradeToAccessViewType.DASHBOARD,
padding: PaddingValues = PaddingValues(vertical = 8.dp, horizontal = 16.dp),
onClick: () -> Unit,
) {
val shape = when (type) {
UpgradeToAccessViewType.DASHBOARD -> RoundedCornerShape(
bottomStart = 16.dp,
bottomEnd = 16.dp
val shape: Shape
var primaryIcon = Icons.Filled.Lock
var textColor = MaterialTheme.appColors.primaryButtonText
var backgroundColor = MaterialTheme.appColors.primaryButtonBackground
var secondaryIcon: @Composable () -> Unit = {
Icon(
modifier = Modifier
.padding(start = 16.dp),
imageVector = Icons.Filled.Info,
contentDescription = null,
tint = textColor
)
}
when (type) {
UpgradeToAccessViewType.DASHBOARD -> {
shape = RoundedCornerShape(
bottomStart = 16.dp,
bottomEnd = 16.dp
)
}

UpgradeToAccessViewType.COURSE -> {
shape = MaterialTheme.appShapes.buttonShape
}

UpgradeToAccessViewType.COURSE -> MaterialTheme.appShapes.buttonShape
UpgradeToAccessViewType.GALLERY -> {
primaryIcon = Icons.Filled.EmojiEvents
textColor = MaterialTheme.appColors.textDark
shape = RectangleShape
backgroundColor = MaterialTheme.appColors.textFieldBackground
secondaryIcon = {
Icon(
modifier = Modifier
.padding(start = 16.dp)
.size(16.dp),
imageVector = Icons.AutoMirrored.Filled.ArrowForwardIos,
contentDescription = null,
tint = textColor
)
}
}
}
Row(
modifier = modifier
.clip(shape = shape)
.fillMaxWidth()
.background(color = MaterialTheme.appColors.primaryButtonBackground)
.background(color = backgroundColor)
.clickable {
onClick()
}
.padding(vertical = 8.dp, horizontal = 16.dp),
.padding(padding),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
modifier = Modifier.padding(end = 16.dp),
imageVector = Icons.Filled.Lock,
imageVector = primaryIcon,
contentDescription = null,
tint = MaterialTheme.appColors.primaryButtonText
tint = textColor
)
Text(
modifier = Modifier.weight(1f),
text = stringResource(id = R.string.iap_upgrade_access_course),
color = MaterialTheme.appColors.primaryButtonText,
color = textColor,
style = MaterialTheme.appTypography.labelLarge
)
Icon(
modifier = Modifier.padding(start = 16.dp),
imageVector = Icons.Filled.Info,
contentDescription = null,
tint = MaterialTheme.appColors.primaryButtonText
)
secondaryIcon()
}
}

enum class UpgradeToAccessViewType {
GALLERY,
DASHBOARD,
COURSE,
}
Expand All @@ -93,5 +130,6 @@ private class UpgradeToAccessViewTypeParameterProvider :
override val values = sequenceOf(
UpgradeToAccessViewType.DASHBOARD,
UpgradeToAccessViewType.COURSE,
UpgradeToAccessViewType.GALLERY,
)
}
Loading

0 comments on commit 3b3cca2

Please sign in to comment.