Skip to content

Commit 0ca7094

Browse files
feat: open block logic
1 parent be47123 commit 0ca7094

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

course/src/main/java/org/openedx/course/presentation/container/CourseContainerViewModel.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import org.openedx.core.R as CoreR
5959
class CourseContainerViewModel(
6060
val courseId: String,
6161
var courseName: String,
62-
var openBlock: String,
62+
private var openBlock: String,
6363
private val enrollmentMode: String,
6464
private val config: Config,
6565
private val interactor: CourseInteractor,
@@ -186,7 +186,9 @@ class CourseContainerViewModel(
186186
if (isReady) {
187187
_isNavigationEnabled.value = true
188188
courseNotifier.send(CourseDataReady(courseStructure))
189-
courseNotifier.send(CourseOpenBlock(openBlock))
189+
if (openBlock.isNotEmpty()) {
190+
courseNotifier.send(CourseOpenBlock(openBlock))
191+
}
190192
}
191193
isReady
192194
}

course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineScreen.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.compose.material.Surface
2626
import androidx.compose.material.Text
2727
import androidx.compose.material.rememberScaffoldState
2828
import androidx.compose.runtime.Composable
29+
import androidx.compose.runtime.LaunchedEffect
2930
import androidx.compose.runtime.collectAsState
3031
import androidx.compose.runtime.getValue
3132
import androidx.compose.runtime.mutableStateOf
@@ -80,8 +81,15 @@ fun CourseOutlineScreen(
8081
) {
8182
val uiState by viewModel.uiState.collectAsState()
8283
val uiMessage by viewModel.uiMessage.collectAsState(null)
84+
val openBlock by viewModel.openBlock.collectAsState("")
8385
val context = LocalContext.current
8486

87+
LaunchedEffect(openBlock) {
88+
if (openBlock.isNotEmpty()) {
89+
viewModel.openBlock(fragmentManager, openBlock)
90+
}
91+
}
92+
8593
CourseOutlineUI(
8694
windowSize = windowSize,
8795
uiState = uiState,

course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineViewModel.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class CourseOutlineViewModel(
7575
val uiMessage: SharedFlow<UIMessage>
7676
get() = _uiMessage.asSharedFlow()
7777

78+
private val _openBlock = MutableSharedFlow<String>()
79+
val openBlock: SharedFlow<String>
80+
get() = _openBlock.asSharedFlow()
81+
7882
private var resumeSectionBlock: Block? = null
7983
private var resumeVerticalBlock: Block? = null
8084

@@ -99,7 +103,7 @@ class CourseOutlineViewModel(
99103
}
100104

101105
is CourseOpenBlock -> {
102-
//TODO
106+
_openBlock.emit(event.blockId)
103107
}
104108
}
105109
}
@@ -288,6 +292,13 @@ class CourseOutlineViewModel(
288292
}
289293

290294
fun openBlock(fragmentManager: FragmentManager, blockId: String) {
295+
val courseStructure = interactor.getCourseStructureFromCache()
296+
val blocks = courseStructure.blockData
297+
getResumeBlock(blocks, blockId)
298+
resumeBlock(fragmentManager, blockId)
299+
}
300+
301+
private fun resumeBlock(fragmentManager: FragmentManager, blockId: String) {
291302
resumeSectionBlock?.let { subSection ->
292303
resumeCourseTappedEvent(subSection.id)
293304
resumeVerticalBlock?.let { unit ->

dashboard/src/main/java/org/openedx/courses/presentation/UserCoursesScreen.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fun UsersCourseScreen(
137137
)
138138
}
139139

140-
is UserCoursesScreenAction.ResumeCourse -> {
140+
is UserCoursesScreenAction.OpenBlock -> {
141141
viewModel.dashboardRouter.navigateToCourseOutline(
142142
fm = fragmentManager,
143143
courseId = action.enrolledCourse.course.id,
@@ -211,8 +211,8 @@ private fun UsersCourseScreen(
211211
navigateToDates = {
212212
onAction(UserCoursesScreenAction.NavigateToDates(it))
213213
},
214-
resume = { course, blockId ->
215-
onAction(UserCoursesScreenAction.ResumeCourse(course, blockId))
214+
openBlock = { course, blockId ->
215+
onAction(UserCoursesScreenAction.OpenBlock(course, blockId))
216216
}
217217
)
218218
}
@@ -257,7 +257,7 @@ private fun UserCourses(
257257
openCourse: (EnrolledCourse) -> Unit,
258258
navigateToDates: (EnrolledCourse) -> Unit,
259259
onViewAllClick: () -> Unit,
260-
resume: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
260+
openBlock: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
261261
) {
262262
Column(
263263
modifier = modifier
@@ -268,7 +268,7 @@ private fun UserCourses(
268268
primaryCourse = userCourses.primary,
269269
apiHostUrl = apiHostUrl,
270270
navigateToDates = navigateToDates,
271-
resume = resume,
271+
openBlock = openBlock,
272272
openCourse = openCourse
273273
)
274274
}
@@ -428,7 +428,7 @@ private fun PrimaryCourseCard(
428428
primaryCourse: EnrolledCourse,
429429
apiHostUrl: String,
430430
navigateToDates: (EnrolledCourse) -> Unit,
431-
resume: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
431+
openBlock: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
432432
openCourse: (EnrolledCourse) -> Unit,
433433
) {
434434
val context = LocalContext.current
@@ -454,7 +454,11 @@ private fun PrimaryCourseCard(
454454
.fillMaxWidth()
455455
.height(140.dp)
456456
)
457-
val progress = (primaryCourse.progress.numPointsEarned / primaryCourse.progress.numPointsPossible).toFloat()
457+
val progress: Float = try {
458+
(primaryCourse.progress.numPointsEarned / primaryCourse.progress.numPointsPossible).toFloat()
459+
} catch (_: ArithmeticException) {
460+
0f
461+
}
458462
LinearProgressIndicator(
459463
modifier = Modifier
460464
.fillMaxWidth()
@@ -478,7 +482,7 @@ private fun PrimaryCourseCard(
478482
AssignmentItem(
479483
modifier = Modifier.clickable {
480484
if (pastAssignments.size == 1) {
481-
resume(primaryCourse, nearestAssignment.blockId)
485+
openBlock(primaryCourse, nearestAssignment.blockId)
482486
} else {
483487
navigateToDates(primaryCourse)
484488
}
@@ -496,7 +500,7 @@ private fun PrimaryCourseCard(
496500
AssignmentItem(
497501
modifier = Modifier.clickable {
498502
if (futureAssignments.size == 1) {
499-
resume(primaryCourse, nearestAssignment.blockId)
503+
openBlock(primaryCourse, nearestAssignment.blockId)
500504
} else {
501505
navigateToDates(primaryCourse)
502506
}
@@ -516,7 +520,7 @@ private fun PrimaryCourseCard(
516520
if (primaryCourse.courseStatus == null) {
517521
openCourse(primaryCourse)
518522
} else {
519-
resume(primaryCourse, primaryCourse.courseStatus?.lastVisitedBlockId ?: "")
523+
openBlock(primaryCourse, primaryCourse.courseStatus?.lastVisitedBlockId ?: "")
520524
}
521525
}
522526
)

dashboard/src/main/java/org/openedx/courses/presentation/UserCoursesViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ interface UserCoursesScreenAction {
105105
object SwipeRefresh : UserCoursesScreenAction
106106
object ViewAll : UserCoursesScreenAction
107107
object Reload : UserCoursesScreenAction
108-
data class ResumeCourse(val enrolledCourse: EnrolledCourse, val blockId: String) : UserCoursesScreenAction
108+
data class OpenBlock(val enrolledCourse: EnrolledCourse, val blockId: String) : UserCoursesScreenAction
109109
data class OpenCourse(val enrolledCourse: EnrolledCourse) : UserCoursesScreenAction
110110
data class NavigateToDates(val enrolledCourse: EnrolledCourse) : UserCoursesScreenAction
111111
}

0 commit comments

Comments
 (0)