Skip to content

Commit

Permalink
refactor: Optimize code blocks
Browse files Browse the repository at this point in the history
Fixes: LEARNER-9730
  • Loading branch information
HamzaIsrar12 committed Jan 24, 2024
1 parent 6e4ed99 commit 0198e8d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,46 @@ data class CourseDatesBannerInfo(
) {
val bannerType by lazy { getCourseBannerType() }

val headerResId = when (bannerType) {
RESET_DATES -> R.string.core_dashboard_banner_reset_date_header
else -> 0
}

val bodyResId = when (bannerType) {
INFO_BANNER -> R.string.core_dates_info_banner
UPGRADE_TO_GRADED -> R.string.core_dates_banner_upgrade_to_graded
UPGRADE_TO_RESET -> R.string.core_dates_banner_upgrade_to_reset
RESET_DATES -> R.string.core_dashboard_banner_reset_date_body
else -> 0
}

val buttonResId = when (bannerType) {
RESET_DATES -> R.string.core_dates_banner_reset_date_button
else -> 0
}

fun isBannerAvailableForUserType(isSelfPaced: Boolean): Boolean {
if (hasEnded) return false

val bannerType = getCourseBannerType()
val selfPacedAvailable = isSelfPaced && bannerType != BLANK
val instructorPacedAvailable = !isSelfPaced && bannerType == UPGRADE_TO_GRADED

return selfPacedAvailable || instructorPacedAvailable
}

private fun getCourseBannerType(): CourseBannerType = when {
isUpgradeToGradedBanner() -> UPGRADE_TO_GRADED
isUpgradeToResetBanner() -> UPGRADE_TO_RESET
isResetDatesBanner() -> RESET_DATES
isInfoBanner() -> INFO_BANNER
canUpgradeToGraded() -> UPGRADE_TO_GRADED
canUpgradeToReset() -> UPGRADE_TO_RESET
canResetDates() -> RESET_DATES
infoBanner() -> INFO_BANNER
else -> BLANK
}

private fun isInfoBanner(): Boolean = !missedDeadlines
private fun infoBanner(): Boolean = !missedDeadlines

private fun isUpgradeToGradedBanner(): Boolean = contentTypeGatingEnabled && !missedDeadlines
private fun canUpgradeToGraded(): Boolean = contentTypeGatingEnabled && !missedDeadlines

private fun isUpgradeToResetBanner(): Boolean =
!isUpgradeToGradedBanner() && missedDeadlines && missedGatedContent
private fun canUpgradeToReset(): Boolean =
!canUpgradeToGraded() && missedDeadlines && missedGatedContent

private fun isResetDatesBanner(): Boolean =
!isUpgradeToGradedBanner() && missedDeadlines && !missedGatedContent
private fun canResetDates(): Boolean =
!canUpgradeToGraded() && missedDeadlines && !missedGatedContent
}

enum class CourseBannerType {
BLANK, INFO_BANNER, UPGRADE_TO_GRADED, UPGRADE_TO_RESET, RESET_DATES;
enum class CourseBannerType(
val headerResId: Int = 0,
val bodyResId: Int = 0,
val buttonResId: Int = 0
) {
BLANK,
INFO_BANNER(bodyResId = R.string.core_dates_info_banner_body),
UPGRADE_TO_GRADED(bodyResId = R.string.core_dates_upgrade_to_graded_banner_body),
UPGRADE_TO_RESET(bodyResId = R.string.core_dates_upgrade_to_reset_banner_body),
RESET_DATES(
headerResId = R.string.core_dates_reset_dates_banner_header,
bodyResId = R.string.core_dates_reset_dates_banner_body,
buttonResId = R.string.core_dates_reset_dates_banner_button
);
}
5 changes: 5 additions & 0 deletions core/src/main/java/org/openedx/core/extension/IntExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.openedx.core.extension

fun Int.nonZero(): Int? {
return if (this != 0) this else null
}
2 changes: 0 additions & 2 deletions core/src/main/java/org/openedx/core/ui/theme/Shape.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ data class AppShapes(
val screenBackgroundShapeFull: CornerBasedShape,
val courseImageShape: CornerBasedShape,
val dialogShape: CornerBasedShape,
val datesBannerShape: CornerBasedShape,
)

internal val LocalShapes = staticCompositionLocalOf {
Expand All @@ -38,7 +37,6 @@ internal val LocalShapes = staticCompositionLocalOf {
screenBackgroundShapeFull = RoundedCornerShape(24.dp),
courseImageShape = RoundedCornerShape(8.dp),
dialogShape = RoundedCornerShape(24.dp),
datesBannerShape = RoundedCornerShape(8.dp),
)
}

Expand Down
21 changes: 7 additions & 14 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,13 @@
<item quantity="other">%d Items Hidden</item>
</plurals>

<!-- Label Course Dates Banner Reset Date Button -->
<string name="core_dates_banner_reset_date_button" tools:ignore="MissingTranslation">Shift due dates</string>
<!-- Message Course Dashboard Banner Reset Date Header -->
<string name="core_dashboard_banner_reset_date_header" tools:ignore="MissingTranslation">Missed some deadlines?</string>
<!-- Message Course Dashboard Banner Reset Date Body -->
<string name="core_dashboard_banner_reset_date_body" tools:ignore="MissingTranslation">Don\'t worry - shift our suggested schedule to complete past due assignments without losing any progress.</string>
<!-- Message Course Dates Info Banner -->
<string name="core_dates_info_banner" tools:ignore="MissingTranslation">We built a suggested schedule to help you stay on track. But don’t worry – it’s flexible so you can learn at your own pace.
If you happen to fall behind, you’ll be able to adjust the dates to keep yourself on track.</string>
<!-- Message Course Dates Banner Upgrade to Complete Graded -->
<string name="core_dates_banner_upgrade_to_graded" tools:ignore="MissingTranslation">To complete graded assignments as part of this course, you can upgrade today.</string>
<!-- Message Course Dates Banner Upgrade to Reset -->
<string name="core_dates_banner_upgrade_to_reset" tools:ignore="MissingTranslation">You are auditing this course,
which means that you are unable to participate in graded assignments. It looks like you missed some important deadlines based on our suggested schedule. To complete graded assignments as part of this course and shift the past due assignments into the future, you can upgrade today.</string>
<!-- Course Dates Banner -->
<string name="core_dates_reset_dates_banner_button" tools:ignore="MissingTranslation">Shift due dates</string>
<string name="core_dates_reset_dates_banner_header" tools:ignore="MissingTranslation">Missed some deadlines?</string>
<string name="core_dates_reset_dates_banner_body" tools:ignore="MissingTranslation">Don\'t worry - shift our suggested schedule to complete past due assignments without losing any progress.</string>
<string name="core_dates_info_banner_body" tools:ignore="MissingTranslation">We built a suggested schedule to help you stay on track. But don’t worry – it’s flexible so you can learn at your own pace. If you happen to fall behind, you’ll be able to adjust the dates to keep yourself on track.</string>
<string name="core_dates_upgrade_to_graded_banner_body" tools:ignore="MissingTranslation">To complete graded assignments as part of this course, you can upgrade today.</string>
<string name="core_dates_upgrade_to_reset_banner_body" tools:ignore="MissingTranslation">You are auditing this course, which means that you are unable to participate in graded assignments. It looks like you missed some important deadlines based on our suggested schedule. To complete graded assignments as part of this course and shift the past due assignments into the future, you can upgrade today.</string>

<string name="core_register">Register</string>
<string name="core_sign_in">Sign in</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ import org.openedx.core.BlockType
import org.openedx.core.domain.model.Block
import org.openedx.core.domain.model.BlockCounts
import org.openedx.core.domain.model.Certificate
import org.openedx.core.domain.model.CourseBannerType
import org.openedx.core.domain.model.CourseDatesBannerInfo
import org.openedx.core.domain.model.CourseSharingUtmParameters
import org.openedx.core.domain.model.CoursewareAccess
import org.openedx.core.domain.model.EnrolledCourse
import org.openedx.core.domain.model.EnrolledCourseData
import org.openedx.core.extension.isLinkValid
import org.openedx.core.extension.nonZero
import org.openedx.core.module.db.DownloadedState
import org.openedx.core.ui.BackBtn
import org.openedx.core.ui.IconText
Expand Down Expand Up @@ -975,7 +975,7 @@ fun CourseDatesBanner(
val cardModifier = modifier
.background(
MaterialTheme.appColors.cardViewBackground,
MaterialTheme.appShapes.datesBannerShape
MaterialTheme.appShapes.material.medium
)
.border(
1.dp,
Expand All @@ -985,24 +985,24 @@ fun CourseDatesBanner(
.padding(16.dp)

Column(modifier = cardModifier) {
if (banner.bannerType == CourseBannerType.RESET_DATES) {
banner.bannerType.headerResId.nonZero()?.let {
Text(
modifier = Modifier.padding(bottom = 8.dp),
text = stringResource(id = banner.headerResId),
text = stringResource(id = it),
style = MaterialTheme.appTypography.titleMedium,
color = MaterialTheme.appColors.textDark
)
}

if (banner.bannerType != CourseBannerType.BLANK) {
banner.bannerType.bodyResId.nonZero()?.let {
Text(
text = stringResource(id = banner.bodyResId),
text = stringResource(id = it),
style = MaterialTheme.appTypography.bodyMedium,
color = MaterialTheme.appColors.textDark
)
}

if (banner.bannerType == CourseBannerType.RESET_DATES) {
banner.bannerType.buttonResId.nonZero()?.let {
Button(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -1015,7 +1015,7 @@ fun CourseDatesBanner(
),
) {
Text(
text = stringResource(id = banner.buttonResId),
text = stringResource(id = it),
color = MaterialTheme.appColors.buttonText,
style = MaterialTheme.appTypography.labelLarge
)
Expand Down

0 comments on commit 0198e8d

Please sign in to comment.