Skip to content

Commit 4b5871a

Browse files
volodymyr-chekyrtakaustavb12
authored andcommitted
fix: ProgressBarRangeInfo IllegalArgumentException - current must not be NaN (openedx#443)
(cherry picked from commit 6e59c7f)
1 parent 1a20ad7 commit 4b5871a

File tree

6 files changed

+36
-27
lines changed

6 files changed

+36
-27
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: 10 additions & 4 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,19 +188,24 @@ 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-
196+
val progressBarValue = downloadedSize.safeDivBy(totalDownloadableSize.toFloat())
197+
val readyToDownloadSize = if (progressBarValue >= 1) {
198+
0
199+
} else {
200+
totalDownloadableSize - realDownloadedSize
201+
}
196202
_uiState.update {
197203
it.copy(
198204
isHaveDownloadableBlocks = true,
199205
largestDownloads = largestDownloads,
200-
readyToDownloadSize = (totalDownloadableSize - downloadedSize).toFileSize(1, false),
206+
readyToDownloadSize = readyToDownloadSize.toFileSize(1, false),
201207
downloadedSize = realDownloadedSize.toFileSize(1, false),
202-
progressBarValue = downloadedSize.toFloat() / totalDownloadableSize.toFloat()
208+
progressBarValue = progressBarValue
203209
)
204210
}
205211
}

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
@@ -431,16 +431,11 @@ fun CourseItem(
431431
.fillMaxWidth()
432432
.height(90.dp)
433433
)
434-
val progress: Float = try {
435-
course.progress.assignmentsCompleted.toFloat() / course.progress.totalAssignmentsCount.toFloat()
436-
} catch (_: ArithmeticException) {
437-
0f
438-
}
439434
LinearProgressIndicator(
440435
modifier = Modifier
441436
.fillMaxWidth()
442437
.height(8.dp),
443-
progress = progress,
438+
progress = course.progress.value,
444439
color = MaterialTheme.appColors.primary,
445440
backgroundColor = MaterialTheme.appColors.divider
446441
)

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,12 @@ private fun PrimaryCourseCard(
557557
.fillMaxWidth()
558558
.height(140.dp)
559559
)
560-
val progress: Float = try {
561-
primaryCourse.progress.assignmentsCompleted.toFloat() /
562-
primaryCourse.progress.totalAssignmentsCount.toFloat()
563-
} catch (_: ArithmeticException) {
564-
0f
565-
}
560+
566561
LinearProgressIndicator(
567562
modifier = Modifier
568563
.fillMaxWidth()
569564
.height(8.dp),
570-
progress = progress,
565+
progress = primaryCourse.progress.value,
571566
color = MaterialTheme.appColors.primary,
572567
backgroundColor = MaterialTheme.appColors.divider
573568
)

0 commit comments

Comments
 (0)