Skip to content

Commit

Permalink
fix: primary course in landscape
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill committed Jul 17, 2024
1 parent 44e39d8 commit 049426b
Showing 1 changed file with 150 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -52,6 +53,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
Expand All @@ -61,13 +63,13 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentManager
import coil.compose.AsyncImage
import coil.request.ImageRequest
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf
import org.openedx.Lock
import org.openedx.core.UIMessage
import org.openedx.core.domain.model.AppConfig
import org.openedx.core.domain.model.Certificate
Expand All @@ -87,6 +89,7 @@ import org.openedx.core.ui.HandleUIMessage
import org.openedx.core.ui.OfflineModeDialog
import org.openedx.core.ui.OpenEdXButton
import org.openedx.core.ui.TextIcon
import org.openedx.core.ui.displayCutoutForLandscape
import org.openedx.core.ui.rememberWindowSize
import org.openedx.core.ui.theme.OpenEdXTheme
import org.openedx.core.ui.theme.appColors
Expand Down Expand Up @@ -188,6 +191,7 @@ private fun DashboardGalleryView(
Surface(
modifier = Modifier
.fillMaxSize()
.displayCutoutForLandscape()
.padding(paddingValues),
color = MaterialTheme.appColors.background
) {
Expand Down Expand Up @@ -513,7 +517,7 @@ private fun PrimaryCourseCard(
resumeBlockId: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
openCourse: (EnrolledCourse) -> Unit,
) {
val context = LocalContext.current
val orientation = LocalConfiguration.current.orientation
Card(
modifier = Modifier
.padding(horizontal = 16.dp)
Expand All @@ -523,29 +527,153 @@ private fun PrimaryCourseCard(
shape = MaterialTheme.appShapes.courseImageShape,
elevation = 4.dp
) {
Column(
modifier = Modifier
.clickable {
openCourse(primaryCourse)
when (orientation) {
Configuration.ORIENTATION_LANDSCAPE -> {
Row(
modifier = Modifier
.clickable {
openCourse(primaryCourse)
}
.height(IntrinsicSize.Min)
) {
PrimaryCourseCaption(
modifier = Modifier.weight(1f),
primaryCourse = primaryCourse,
apiHostUrl = apiHostUrl,
imageHeight = null,
)
PrimaryCourseButtons(
modifier = Modifier.weight(1f),
primaryCourse = primaryCourse,
navigateToDates = navigateToDates,
resumeBlockId = resumeBlockId,
openCourse = openCourse
)
}
) {
AsyncImage(
model = ImageRequest.Builder(context)
.data(apiHostUrl + primaryCourse.course.courseImage)
.error(CoreR.drawable.core_no_image_course)
.placeholder(CoreR.drawable.core_no_image_course)
.build(),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.fillMaxWidth()
.height(140.dp)
}

else -> {
Column(
modifier = Modifier.clickable {
openCourse(primaryCourse)
}
) {
PrimaryCourseCaption(primaryCourse = primaryCourse, apiHostUrl = apiHostUrl)
Divider()
PrimaryCourseButtons(
primaryCourse = primaryCourse,
navigateToDates = navigateToDates,
resumeBlockId = resumeBlockId,
openCourse = openCourse
)
}
}
}
}
}

@Composable
private fun PrimaryCourseButtons(
modifier: Modifier = Modifier,
primaryCourse: EnrolledCourse,
navigateToDates: (EnrolledCourse) -> Unit,
resumeBlockId: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
openCourse: (EnrolledCourse) -> Unit,
) {
val context = LocalContext.current
val pastAssignments = primaryCourse.courseAssignments?.pastAssignments
Column(modifier = modifier) {
if (!pastAssignments.isNullOrEmpty()) {
val nearestAssignment = pastAssignments.maxBy { it.date }
val title = if (pastAssignments.size == 1) nearestAssignment.title else null
AssignmentItem(
modifier = Modifier.clickable {
if (pastAssignments.size == 1) {
resumeBlockId(primaryCourse, nearestAssignment.blockId)
} else {
navigateToDates(primaryCourse)
}
},
painter = rememberVectorPainter(Icons.Default.Warning),
title = title,
info = pluralStringResource(
R.plurals.dashboard_past_due_assignment,
pastAssignments.size,
pastAssignments.size
)
)
val progress: Float = try {
primaryCourse.progress.assignmentsCompleted.toFloat() / primaryCourse.progress.totalAssignmentsCount.toFloat()
} catch (_: ArithmeticException) {
0f
}
val futureAssignments = primaryCourse.courseAssignments?.futureAssignments
if (!futureAssignments.isNullOrEmpty()) {
val nearestAssignment = futureAssignments.minBy { it.date }
val title = if (futureAssignments.size == 1) nearestAssignment.title else null
Divider()
AssignmentItem(
modifier = Modifier.clickable {
if (futureAssignments.size == 1) {
resumeBlockId(primaryCourse, nearestAssignment.blockId)
} else {
navigateToDates(primaryCourse)
}
},
painter = painterResource(id = CoreR.drawable.ic_core_chapter_icon),
title = title,
info = stringResource(
R.string.dashboard_assignment_due,
nearestAssignment.assignmentType ?: "",
TimeUtils.getAssignmentFormattedDate(context, nearestAssignment.date)
)
)
}
ResumeButton(
primaryCourse = primaryCourse,
onClick = {
if (primaryCourse.courseStatus == null) {
openCourse(primaryCourse)
} else {
resumeBlockId(
primaryCourse,
primaryCourse.courseStatus?.lastVisitedBlockId ?: ""
)
}
}
)
}
}

@Composable
private fun PrimaryCourseCaption(
modifier: Modifier = Modifier,
primaryCourse: EnrolledCourse,
imageHeight: Dp? = 140.dp,
apiHostUrl: String,
) {
val context = LocalContext.current
Column(modifier = modifier) {
val imageModifier = imageHeight?.let {
Modifier
.height(it)
.fillMaxWidth()
} ?: Modifier
.height(IntrinsicSize.Max)
.fillMaxWidth()
.weight(1f)
AsyncImage(
model = ImageRequest.Builder(context)
.data(apiHostUrl + primaryCourse.course.courseImage)
.error(CoreR.drawable.core_no_image_course)
.placeholder(CoreR.drawable.core_no_image_course)
.build(),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = imageModifier
)
val progress: Float = try {
primaryCourse.progress.assignmentsCompleted.toFloat() / primaryCourse.progress.totalAssignmentsCount.toFloat()
} catch (_: ArithmeticException) {
0f
}
Column {
LinearProgressIndicator(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -561,63 +689,6 @@ private fun PrimaryCourseCard(
.padding(top = 8.dp, bottom = 16.dp),
primaryCourse = primaryCourse
)
val pastAssignments = primaryCourse.courseAssignments?.pastAssignments
if (!pastAssignments.isNullOrEmpty()) {
val nearestAssignment = pastAssignments.maxBy { it.date }
val title = if (pastAssignments.size == 1) nearestAssignment.title else null
Divider()
AssignmentItem(
modifier = Modifier.clickable {
if (pastAssignments.size == 1) {
resumeBlockId(primaryCourse, nearestAssignment.blockId)
} else {
navigateToDates(primaryCourse)
}
},
painter = rememberVectorPainter(Icons.Default.Warning),
title = title,
info = pluralStringResource(
R.plurals.dashboard_past_due_assignment,
pastAssignments.size,
pastAssignments.size
)
)
}
val futureAssignments = primaryCourse.courseAssignments?.futureAssignments
if (!futureAssignments.isNullOrEmpty()) {
val nearestAssignment = futureAssignments.minBy { it.date }
val title = if (futureAssignments.size == 1) nearestAssignment.title else null
Divider()
AssignmentItem(
modifier = Modifier.clickable {
if (futureAssignments.size == 1) {
resumeBlockId(primaryCourse, nearestAssignment.blockId)
} else {
navigateToDates(primaryCourse)
}
},
painter = painterResource(id = CoreR.drawable.ic_core_chapter_icon),
title = title,
info = stringResource(
R.string.dashboard_assignment_due,
nearestAssignment.assignmentType ?: "",
TimeUtils.getAssignmentFormattedDate(context, nearestAssignment.date)
)
)
}
ResumeButton(
primaryCourse = primaryCourse,
onClick = {
if (primaryCourse.courseStatus == null) {
openCourse(primaryCourse)
} else {
resumeBlockId(
primaryCourse,
primaryCourse.courseStatus?.lastVisitedBlockId ?: ""
)
}
}
)
}
}
}
Expand Down

0 comments on commit 049426b

Please sign in to comment.