diff --git a/core/src/main/java/org/openedx/core/domain/model/Progress.kt b/core/src/main/java/org/openedx/core/domain/model/Progress.kt index 800a9c292..edbcf0f90 100644 --- a/core/src/main/java/org/openedx/core/domain/model/Progress.kt +++ b/core/src/main/java/org/openedx/core/domain/model/Progress.kt @@ -3,6 +3,7 @@ package org.openedx.core.domain.model import android.os.Parcelable import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.Parcelize +import org.openedx.core.extension.safeDivBy @Parcelize data class Progress( @@ -11,11 +12,7 @@ data class Progress( ) : Parcelable { @IgnoredOnParcel - val value: Float = try { - assignmentsCompleted.toFloat() / totalAssignmentsCount.toFloat() - } catch (_: ArithmeticException) { - 0f - } + val value: Float = assignmentsCompleted.toFloat().safeDivBy(totalAssignmentsCount.toFloat()) companion object { val DEFAULT_PROGRESS = Progress(0, 0) diff --git a/core/src/main/java/org/openedx/core/extension/FloatExt.kt b/core/src/main/java/org/openedx/core/extension/FloatExt.kt new file mode 100644 index 000000000..b59d3ea6a --- /dev/null +++ b/core/src/main/java/org/openedx/core/extension/FloatExt.kt @@ -0,0 +1,20 @@ +package org.openedx.core.extension + +/** + * Safely divides this Float by [divisor], returning 0f if: + * - [divisor] is zero, + * - the result is NaN. + * + * Workaround for accessibility issue: + * https://github.com/openedx/openedx-app-android/issues/442 + */ +fun Float.safeDivBy(divisor: Float): Float = try { + var result = this / divisor + if (result.isNaN()) { + result = 0f + } + result +} catch (_: ArithmeticException) { + 0f +} + diff --git a/course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineViewModel.kt b/course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineViewModel.kt index 19d67f79b..951433e1e 100644 --- a/course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineViewModel.kt +++ b/course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineViewModel.kt @@ -14,6 +14,7 @@ import kotlinx.coroutines.launch import org.openedx.core.BlockType import org.openedx.core.data.storage.CorePreferences import org.openedx.core.domain.model.Block +import org.openedx.core.extension.safeDivBy import org.openedx.core.module.DownloadWorkerController import org.openedx.core.module.db.DownloadDao import org.openedx.core.module.db.DownloadModel @@ -187,19 +188,25 @@ class CourseOfflineViewModel( completedDownloads: List, downloadedBlocks: List ) { - val downloadedSize = getFilesSize(downloadedBlocks) + val downloadedSize = getFilesSize(downloadedBlocks).toFloat() val realDownloadedSize = completedDownloads.sumOf { it.size } val largestDownloads = completedDownloads .sortedByDescending { it.size } .take(n = 5) + val progressBarValue = downloadedSize.safeDivBy(totalDownloadableSize.toFloat()) + val readyToDownloadSize = if (progressBarValue >= 1) { + 0 + } else { + totalDownloadableSize - realDownloadedSize + } _uiState.update { it.copy( isHaveDownloadableBlocks = true, largestDownloads = largestDownloads, - readyToDownloadSize = (totalDownloadableSize - downloadedSize).toFileSize(1, false), + readyToDownloadSize = readyToDownloadSize.toFileSize(1, false), downloadedSize = realDownloadedSize.toFileSize(1, false), - progressBarValue = downloadedSize.toFloat() / totalDownloadableSize.toFloat() + progressBarValue = progressBarValue ) } } diff --git a/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt b/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt index 2598ad8ac..f0a87d8f4 100644 --- a/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt +++ b/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt @@ -84,6 +84,7 @@ import org.openedx.core.domain.model.AssignmentProgress import org.openedx.core.domain.model.Block import org.openedx.core.domain.model.BlockCounts import org.openedx.core.domain.model.CourseDatesBannerInfo +import org.openedx.core.extension.safeDivBy import org.openedx.core.module.db.DownloadModel import org.openedx.core.module.db.DownloadedState import org.openedx.core.module.db.FileType @@ -260,11 +261,7 @@ fun OfflineQueueCard( maxLines = 1 ) - val progress = if (progressSize == 0L) { - 0f - } else { - progressValue.toFloat() / progressSize - } + val progress = progressValue.toFloat().safeDivBy(progressSize.toFloat()) LinearProgressIndicator( modifier = Modifier .fillMaxWidth() diff --git a/dashboard/src/main/java/org/openedx/courses/presentation/AllEnrolledCoursesView.kt b/dashboard/src/main/java/org/openedx/courses/presentation/AllEnrolledCoursesView.kt index 4f5f09080..b204d94d8 100644 --- a/dashboard/src/main/java/org/openedx/courses/presentation/AllEnrolledCoursesView.kt +++ b/dashboard/src/main/java/org/openedx/courses/presentation/AllEnrolledCoursesView.kt @@ -432,16 +432,11 @@ fun CourseItem( .fillMaxWidth() .aspectRatio(16f / 9f) ) - val progress: Float = try { - course.progress.assignmentsCompleted.toFloat() / course.progress.totalAssignmentsCount.toFloat() - } catch (_: ArithmeticException) { - 0f - } LinearProgressIndicator( modifier = Modifier .fillMaxWidth() .height(8.dp), - progress = progress, + progress = course.progress.value, color = MaterialTheme.appColors.primary, backgroundColor = MaterialTheme.appColors.divider ) diff --git a/dashboard/src/main/java/org/openedx/courses/presentation/DashboardGalleryView.kt b/dashboard/src/main/java/org/openedx/courses/presentation/DashboardGalleryView.kt index e036238d5..5e81aba4a 100644 --- a/dashboard/src/main/java/org/openedx/courses/presentation/DashboardGalleryView.kt +++ b/dashboard/src/main/java/org/openedx/courses/presentation/DashboardGalleryView.kt @@ -575,17 +575,11 @@ private fun PrimaryCourseCard( modifier = Modifier .fillMaxWidth() ) - val progress: Float = try { - primaryCourse.progress.assignmentsCompleted.toFloat() / - primaryCourse.progress.totalAssignmentsCount.toFloat() - } catch (_: ArithmeticException) { - 0f - } LinearProgressIndicator( modifier = Modifier .fillMaxWidth() .height(8.dp), - progress = progress, + progress = primaryCourse.progress.value, color = MaterialTheme.appColors.primary, backgroundColor = MaterialTheme.appColors.divider )