Skip to content

Commit

Permalink
feat: open block logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloNetrebchuk committed May 2, 2024
1 parent be47123 commit 0ca7094
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import org.openedx.core.R as CoreR
class CourseContainerViewModel(
val courseId: String,
var courseName: String,
var openBlock: String,
private var openBlock: String,
private val enrollmentMode: String,
private val config: Config,
private val interactor: CourseInteractor,
Expand Down Expand Up @@ -186,7 +186,9 @@ class CourseContainerViewModel(
if (isReady) {
_isNavigationEnabled.value = true
courseNotifier.send(CourseDataReady(courseStructure))
courseNotifier.send(CourseOpenBlock(openBlock))
if (openBlock.isNotEmpty()) {
courseNotifier.send(CourseOpenBlock(openBlock))
}
}
isReady
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -80,8 +81,15 @@ fun CourseOutlineScreen(
) {
val uiState by viewModel.uiState.collectAsState()
val uiMessage by viewModel.uiMessage.collectAsState(null)
val openBlock by viewModel.openBlock.collectAsState("")
val context = LocalContext.current

LaunchedEffect(openBlock) {
if (openBlock.isNotEmpty()) {
viewModel.openBlock(fragmentManager, openBlock)
}
}

CourseOutlineUI(
windowSize = windowSize,
uiState = uiState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class CourseOutlineViewModel(
val uiMessage: SharedFlow<UIMessage>
get() = _uiMessage.asSharedFlow()

private val _openBlock = MutableSharedFlow<String>()
val openBlock: SharedFlow<String>
get() = _openBlock.asSharedFlow()

private var resumeSectionBlock: Block? = null
private var resumeVerticalBlock: Block? = null

Expand All @@ -99,7 +103,7 @@ class CourseOutlineViewModel(
}

is CourseOpenBlock -> {
//TODO
_openBlock.emit(event.blockId)
}
}
}
Expand Down Expand Up @@ -288,6 +292,13 @@ class CourseOutlineViewModel(
}

fun openBlock(fragmentManager: FragmentManager, blockId: String) {
val courseStructure = interactor.getCourseStructureFromCache()
val blocks = courseStructure.blockData
getResumeBlock(blocks, blockId)
resumeBlock(fragmentManager, blockId)
}

private fun resumeBlock(fragmentManager: FragmentManager, blockId: String) {
resumeSectionBlock?.let { subSection ->
resumeCourseTappedEvent(subSection.id)
resumeVerticalBlock?.let { unit ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fun UsersCourseScreen(
)
}

is UserCoursesScreenAction.ResumeCourse -> {
is UserCoursesScreenAction.OpenBlock -> {
viewModel.dashboardRouter.navigateToCourseOutline(
fm = fragmentManager,
courseId = action.enrolledCourse.course.id,
Expand Down Expand Up @@ -211,8 +211,8 @@ private fun UsersCourseScreen(
navigateToDates = {
onAction(UserCoursesScreenAction.NavigateToDates(it))
},
resume = { course, blockId ->
onAction(UserCoursesScreenAction.ResumeCourse(course, blockId))
openBlock = { course, blockId ->
onAction(UserCoursesScreenAction.OpenBlock(course, blockId))
}
)
}
Expand Down Expand Up @@ -257,7 +257,7 @@ private fun UserCourses(
openCourse: (EnrolledCourse) -> Unit,
navigateToDates: (EnrolledCourse) -> Unit,
onViewAllClick: () -> Unit,
resume: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
openBlock: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
) {
Column(
modifier = modifier
Expand All @@ -268,7 +268,7 @@ private fun UserCourses(
primaryCourse = userCourses.primary,
apiHostUrl = apiHostUrl,
navigateToDates = navigateToDates,
resume = resume,
openBlock = openBlock,
openCourse = openCourse
)
}
Expand Down Expand Up @@ -428,7 +428,7 @@ private fun PrimaryCourseCard(
primaryCourse: EnrolledCourse,
apiHostUrl: String,
navigateToDates: (EnrolledCourse) -> Unit,
resume: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
openBlock: (enrolledCourse: EnrolledCourse, blockId: String) -> Unit,
openCourse: (EnrolledCourse) -> Unit,
) {
val context = LocalContext.current
Expand All @@ -454,7 +454,11 @@ private fun PrimaryCourseCard(
.fillMaxWidth()
.height(140.dp)
)
val progress = (primaryCourse.progress.numPointsEarned / primaryCourse.progress.numPointsPossible).toFloat()
val progress: Float = try {
(primaryCourse.progress.numPointsEarned / primaryCourse.progress.numPointsPossible).toFloat()
} catch (_: ArithmeticException) {
0f
}
LinearProgressIndicator(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -478,7 +482,7 @@ private fun PrimaryCourseCard(
AssignmentItem(
modifier = Modifier.clickable {
if (pastAssignments.size == 1) {
resume(primaryCourse, nearestAssignment.blockId)
openBlock(primaryCourse, nearestAssignment.blockId)
} else {
navigateToDates(primaryCourse)
}
Expand All @@ -496,7 +500,7 @@ private fun PrimaryCourseCard(
AssignmentItem(
modifier = Modifier.clickable {
if (futureAssignments.size == 1) {
resume(primaryCourse, nearestAssignment.blockId)
openBlock(primaryCourse, nearestAssignment.blockId)
} else {
navigateToDates(primaryCourse)
}
Expand All @@ -516,7 +520,7 @@ private fun PrimaryCourseCard(
if (primaryCourse.courseStatus == null) {
openCourse(primaryCourse)
} else {
resume(primaryCourse, primaryCourse.courseStatus?.lastVisitedBlockId ?: "")
openBlock(primaryCourse, primaryCourse.courseStatus?.lastVisitedBlockId ?: "")
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ interface UserCoursesScreenAction {
object SwipeRefresh : UserCoursesScreenAction
object ViewAll : UserCoursesScreenAction
object Reload : UserCoursesScreenAction
data class ResumeCourse(val enrolledCourse: EnrolledCourse, val blockId: String) : UserCoursesScreenAction
data class OpenBlock(val enrolledCourse: EnrolledCourse, val blockId: String) : UserCoursesScreenAction
data class OpenCourse(val enrolledCourse: EnrolledCourse) : UserCoursesScreenAction
data class NavigateToDates(val enrolledCourse: EnrolledCourse) : UserCoursesScreenAction
}

0 comments on commit 0ca7094

Please sign in to comment.