File tree Expand file tree Collapse file tree 7 files changed +30
-30
lines changed
core/src/main/java/org/openedx/core
course/src/main/java/org/openedx/course/presentation
dashboard/src/main/java/org/openedx/courses/presentation
downloads/src/main/java/org/openedx/downloads/presentation/download Expand file tree Collapse file tree 7 files changed +30
-30
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package org.openedx.core.domain.model
33import android.os.Parcelable
44import kotlinx.parcelize.IgnoredOnParcel
55import kotlinx.parcelize.Parcelize
6+ import org.openedx.core.extension.safeDivBy
67
78@Parcelize
89data 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 )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import kotlinx.coroutines.launch
1414import org.openedx.core.BlockType
1515import org.openedx.core.data.storage.CorePreferences
1616import org.openedx.core.domain.model.Block
17+ import org.openedx.core.extension.safeDivBy
1718import org.openedx.core.module.DownloadWorkerController
1819import org.openedx.core.module.db.DownloadDao
1920import org.openedx.core.module.db.DownloadModel
@@ -187,12 +188,12 @@ class CourseOfflineViewModel(
187188 completedDownloads : List <DownloadModel >,
188189 downloadedBlocks : List <Block >
189190 ) {
190- val downloadedSize = getFilesSize(downloadedBlocks)
191+ val downloadedSize = getFilesSize(downloadedBlocks).toFloat()
191192 val realDownloadedSize = completedDownloads.sumOf { it.size }
192193 val largestDownloads = completedDownloads
193194 .sortedByDescending { it.size }
194195 .take(n = 5 )
195- val progressBarValue = downloadedSize.toFloat() / totalDownloadableSize.toFloat()
196+ val progressBarValue = downloadedSize.safeDivBy( totalDownloadableSize.toFloat() )
196197 val readyToDownloadSize = if (progressBarValue >= 1 ) {
197198 0
198199 } else {
Original file line number Diff line number Diff line change @@ -84,6 +84,7 @@ import org.openedx.core.domain.model.AssignmentProgress
8484import org.openedx.core.domain.model.Block
8585import org.openedx.core.domain.model.BlockCounts
8686import org.openedx.core.domain.model.CourseDatesBannerInfo
87+ import org.openedx.core.extension.safeDivBy
8788import org.openedx.core.module.db.DownloadModel
8889import org.openedx.core.module.db.DownloadedState
8990import 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()
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ import androidx.compose.ui.unit.dp
7171import coil.compose.AsyncImage
7272import coil.request.ImageRequest
7373import org.openedx.core.domain.model.DownloadCoursePreview
74+ import org.openedx.core.extension.safeDivBy
7475import org.openedx.core.module.db.DownloadModel
7576import org.openedx.core.module.db.DownloadedState
7677import 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(),
You can’t perform that action at this time.
0 commit comments