Skip to content

Commit fab3624

Browse files
author
Ahsan Arif
committed
Merge branch 'develop' into refactor/migration-course-flow
2 parents 94b43ea + 7b5846a commit fab3624

8 files changed

Lines changed: 34 additions & 31 deletions

File tree

core/src/main/java/org/openedx/core/data/api/CourseApi.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ interface CourseApi {
6161
)
6262

6363
@GET("/api/course_home/v1/dates/{course_id}")
64-
suspend fun getCourseDates(@Path("course_id") courseId: String): CourseDates
64+
suspend fun getCourseDates(
65+
@Path("course_id") courseId: String,
66+
@Query("allow_not_started_courses") allowNotStartedCourses: Boolean = true
67+
): CourseDates
6568

6669
@POST("/api/course_experience/v1/reset_course_deadlines")
6770
suspend fun resetCourseDates(@Body courseBody: Map<String, String>): ResetCourseDates

core/src/main/java/org/openedx/core/domain/model/Progress.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.openedx.core.domain.model
33
import android.os.Parcelable
44
import kotlinx.parcelize.IgnoredOnParcel
55
import kotlinx.parcelize.Parcelize
6+
import org.openedx.core.extension.safeDivBy
67

78
@Parcelize
89
data class Progress(
@@ -11,11 +12,7 @@ data class Progress(
1112
) : Parcelable {
1213

1314
@IgnoredOnParcel
14-
val value: Float = try {
15-
assignmentsCompleted.toFloat() / totalAssignmentsCount.toFloat()
16-
} catch (_: ArithmeticException) {
17-
0f
18-
}
15+
val value: Float = assignmentsCompleted.toFloat().safeDivBy(totalAssignmentsCount.toFloat())
1916

2017
companion object {
2118
val DEFAULT_PROGRESS = Progress(0, 0)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.openedx.core.extension
2+
3+
/**
4+
* Safely divides this Float by [divisor], returning 0f if:
5+
* - [divisor] is zero,
6+
* - the result is NaN.
7+
*
8+
* Workaround for accessibility issue:
9+
* https://github.com/openedx/openedx-app-android/issues/442
10+
*/
11+
fun Float.safeDivBy(divisor: Float): Float = try {
12+
var result = this / divisor
13+
if (result.isNaN()) {
14+
result = 0f
15+
}
16+
result
17+
} catch (_: ArithmeticException) {
18+
0f
19+
}

course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineViewModel.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.openedx.core.BlockType
1818
import org.openedx.core.data.storage.CorePreferences
1919
import org.openedx.core.domain.model.Block
2020
import org.openedx.core.extension.collectLatestNotNull
21+
import org.openedx.core.extension.safeDivBy
2122
import org.openedx.core.module.DownloadWorkerController
2223
import org.openedx.core.module.db.DownloadDao
2324
import org.openedx.core.module.db.DownloadModel
@@ -201,12 +202,12 @@ class CourseOfflineViewModel(
201202
completedDownloads: List<DownloadModel>,
202203
downloadedBlocks: List<Block>
203204
) {
204-
val downloadedSize = getFilesSize(downloadedBlocks)
205+
val downloadedSize = getFilesSize(downloadedBlocks).toFloat()
205206
val realDownloadedSize = completedDownloads.sumOf { it.size }
206207
val largestDownloads = completedDownloads
207208
.sortedByDescending { it.size }
208209
.take(n = 5)
209-
val progressBarValue = downloadedSize.toFloat() / totalDownloadableSize.toFloat()
210+
val progressBarValue = downloadedSize.safeDivBy(totalDownloadableSize.toFloat())
210211
val readyToDownloadSize = if (progressBarValue >= 1) {
211212
0
212213
} else {

course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import org.openedx.core.domain.model.AssignmentProgress
8484
import org.openedx.core.domain.model.Block
8585
import org.openedx.core.domain.model.BlockCounts
8686
import org.openedx.core.domain.model.CourseDatesBannerInfo
87+
import org.openedx.core.extension.safeDivBy
8788
import org.openedx.core.module.db.DownloadModel
8889
import org.openedx.core.module.db.DownloadedState
8990
import org.openedx.core.module.db.FileType
@@ -260,11 +261,7 @@ fun OfflineQueueCard(
260261
maxLines = 1
261262
)
262263

263-
val progress = if (progressSize == 0L) {
264-
0f
265-
} else {
266-
progressValue.toFloat() / progressSize
267-
}
264+
val progress = progressValue.toFloat().safeDivBy(progressSize.toFloat())
268265
LinearProgressIndicator(
269266
modifier = Modifier
270267
.fillMaxWidth()

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,11 @@ fun CourseItem(
435435
.fillMaxWidth()
436436
.height(90.dp)
437437
)
438-
val progress: Float = try {
439-
course.progress.assignmentsCompleted.toFloat() / course.progress.totalAssignmentsCount.toFloat()
440-
} catch (_: ArithmeticException) {
441-
0f
442-
}
443438
LinearProgressIndicator(
444439
modifier = Modifier
445440
.fillMaxWidth()
446441
.height(8.dp),
447-
progress = progress,
442+
progress = course.progress.value,
448443
color = MaterialTheme.appColors.primary,
449444
backgroundColor = MaterialTheme.appColors.divider
450445
)

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,17 +734,11 @@ private fun PrimaryCourseCaption(
734734
contentScale = ContentScale.Crop,
735735
modifier = imageModifier,
736736
)
737-
val progress: Float = try {
738-
primaryCourse.progress.assignmentsCompleted.toFloat() /
739-
primaryCourse.progress.totalAssignmentsCount.toFloat()
740-
} catch (_: ArithmeticException) {
741-
0f
742-
}
743737
LinearProgressIndicator(
744738
modifier = Modifier
745739
.fillMaxWidth()
746740
.height(8.dp),
747-
progress = progress,
741+
progress = primaryCourse.progress.value,
748742
color = MaterialTheme.appColors.primary,
749743
backgroundColor = MaterialTheme.appColors.divider
750744
)

downloads/src/main/java/org/openedx/downloads/presentation/download/DownloadsScreen.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import androidx.compose.ui.unit.dp
7171
import coil.compose.AsyncImage
7272
import coil.request.ImageRequest
7373
import org.openedx.core.domain.model.DownloadCoursePreview
74+
import org.openedx.core.extension.safeDivBy
7475
import org.openedx.core.module.db.DownloadModel
7576
import org.openedx.core.module.db.DownloadedState
7677
import org.openedx.core.module.db.DownloadedState.LOADING_COURSE_STRUCTURE
@@ -285,11 +286,7 @@ private fun CourseItem(
285286
.sumOf { it.size }
286287
val availableSize = downloadCoursePreview.totalSize - downloadedSize
287288
val availableSizeString = availableSize.toFileSize(space = false, round = 1)
288-
val progress: Float = try {
289-
downloadedSize.toFloat() / downloadCoursePreview.totalSize.toFloat()
290-
} catch (_: ArithmeticException) {
291-
0f
292-
}
289+
val progress = downloadedSize.toFloat().safeDivBy(downloadCoursePreview.totalSize.toFloat())
293290
Card(
294291
modifier = modifier
295292
.fillMaxWidth(),

0 commit comments

Comments
 (0)