Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add upgrade banner in the FC47 Primary Course card view #9

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/src/main/java/org/openedx/app/AnalyticsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import org.openedx.auth.presentation.AuthAnalytics
import org.openedx.core.config.Config
import org.openedx.core.presentation.CoreAnalytics
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.dialog.appreview.AppReviewAnalytics
import org.openedx.core.presentation.iap.IAPFlow
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.dashboard.presentation.DashboardAnalytics
import org.openedx.discovery.presentation.DiscoveryAnalytics
Expand Down Expand Up @@ -191,6 +195,25 @@ class AnalyticsManager(
put(Key.TOPIC_NAME.keyName, topicName)
})
}

override fun logIAPEvent(event: IAPAnalyticsEvent, params: MutableMap<String, Any?>, screenName: String) {
logEvent(
event = event.eventName,
params = params.apply {
put(IAPAnalyticsKeys.NAME.key, event.biValue)
put(IAPAnalyticsKeys.SCREEN_NAME.key, screenName)
put(
IAPAnalyticsKeys.IAP_FLOW_TYPE.key,
if (screenName == IAPAnalyticsScreen.PROFILE.screenName) {
IAPFlow.RESTORE.value
} else {
IAPFlow.SILENT.value
}
)
put(IAPAnalyticsKeys.CATEGORY.key, IAPAnalyticsKeys.IN_APP_PURCHASES.key)
}
)
}
}

enum class Event(val eventName: String) {
Expand Down
11 changes: 8 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,7 @@ val screenModule = module {
get(),
get(),
get(),
get()
get(),
)
}

Expand All @@ -164,7 +164,11 @@ val screenModule = module {
get(),
get(),
get(),
windowSize
get(),
get(),
get(),
get(),
windowSize,
)
}
viewModel { AllEnrolledCoursesViewModel(get(), get(), get(), get(), get(), get(), get()) }
Expand Down Expand Up @@ -213,6 +217,7 @@ val screenModule = module {
get(),
get(),
get(),
get(),
get()
)
}
Expand Down Expand Up @@ -441,7 +446,7 @@ val screenModule = module {
}

single { IAPRepository(get()) }
factory { IAPInteractor(get(), get()) }
factory { IAPInteractor(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,47 @@
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.global.AppData
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 iapConfig
get() = preferencesManager.appConfig.iapConfig
private val isIAPEnabled
get() = iapConfig.isEnabled && iapConfig.disableVersions.contains(appData.versionName).not()

fun showFeedbackScreen(context: Context, message: String) {
EmailUtil.showFeedbackScreen(
context = context,
feedbackEmailAddress = config.getFeedbackEmailAddress(),
subject = context.getString(R.string.core_error_upgrading_course_in_app),
feedback = message,
appVersion = appData.versionName
)
}

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

suspend fun detectUnfulfilledPurchase(
onSuccess: () -> Unit,
onFailure: (IAPException) -> Unit,
) {
if (isIAPEnabled) {
preferencesManager.user?.id?.let { userId ->
runCatching {
processUnfulfilledPurchase(userId)
}.onSuccess {
if (it) {
onSuccess()
}
}.onFailure {
if (it is IAPException) {
onFailure(it)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.openedx.core.presentation

interface IAPAnalytics {
fun logEvent(event: String, params: Map<String, Any?>)
fun logIAPEvent(
event: IAPAnalyticsEvent,
params: MutableMap<String, Any?> = mutableMapOf(),
screenName: String,
)
}

enum class IAPAnalyticsEvent(val eventName: String, val biValue: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,24 +339,27 @@ class IAPViewModel(
event: IAPAnalyticsEvent,
params: MutableMap<String, Any?> = mutableMapOf()
) {
analytics.logEvent(event.eventName, 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.formattedPrice?.takeIf { it.isNotBlank() }?.let { formattedPrice ->
put(IAPAnalyticsKeys.PRICE.key, formattedPrice)
}
purchaseFlowData.componentId?.takeIf { it.isNotBlank() }?.let { componentId ->
put(IAPAnalyticsKeys.COMPONENT_ID.key, componentId)
}
put(IAPAnalyticsKeys.SCREEN_NAME.key, purchaseFlowData.screenName)
put(IAPAnalyticsKeys.CATEGORY.key, IAPAnalyticsKeys.IN_APP_PURCHASES.key)
})
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.formattedPrice?.takeIf { it.isNotBlank() }?.let { formattedPrice ->
put(IAPAnalyticsKeys.PRICE.key, formattedPrice)
}
purchaseFlowData.componentId?.takeIf { it.isNotBlank() }?.let { componentId ->
put(IAPAnalyticsKeys.COMPONENT_ID.key, componentId)
}
put(IAPAnalyticsKeys.CATEGORY.key, IAPAnalyticsKeys.IN_APP_PURCHASES.key)
},
screenName = purchaseFlowData.screenName.orEmpty()
)
}

fun clearIAPFLow() {
Expand Down
75 changes: 57 additions & 18 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,54 +37,86 @@ import org.openedx.core.ui.theme.appTypography
fun UpgradeToAccessView(
modifier: Modifier = Modifier,
type: UpgradeToAccessViewType = UpgradeToAccessViewType.DASHBOARD,
iconPadding: PaddingValues = PaddingValues(end = 16.dp),
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 = textColor.copy(0.05f)
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,
modifier = Modifier.padding(iconPadding),
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,
}

@Preview
@Preview(backgroundColor = 0xFFFFFFFF, showBackground = true)
@Composable
private fun UpgradeToAccessViewPreview(
@PreviewParameter(UpgradeToAccessViewTypeParameterProvider::class) type: UpgradeToAccessViewType
Expand All @@ -93,5 +131,6 @@ private class UpgradeToAccessViewTypeParameterProvider :
override val values = sequenceOf(
UpgradeToAccessViewType.DASHBOARD,
UpgradeToAccessViewType.COURSE,
UpgradeToAccessViewType.GALLERY,
)
}
Loading
Loading