Skip to content

Commit 6e59c7f

Browse files
fix: ProgressBarRangeInfo IllegalArgumentException - current must not be NaN (#443)
1 parent 0b254f7 commit 6e59c7f

File tree

7 files changed

+30
-30
lines changed

7 files changed

+30
-30
lines changed

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
@@ -14,6 +14,7 @@ import kotlinx.coroutines.launch
1414
import org.openedx.core.BlockType
1515
import org.openedx.core.data.storage.CorePreferences
1616
import org.openedx.core.domain.model.Block
17+
import org.openedx.core.extension.safeDivBy
1718
import org.openedx.core.module.DownloadWorkerController
1819
import org.openedx.core.module.db.DownloadDao
1920
import 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 {

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)