Skip to content

Commit f6b5b92

Browse files
fix: update downloading state if new blocks was added
1 parent 3ff18de commit f6b5b92

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ private fun CourseItem(
284284
.filter { it.downloadedState == DownloadedState.DOWNLOADED }
285285
.sumOf { it.size }
286286
val availableSize = downloadCoursePreview.totalSize - downloadedSize
287-
val availableSizeString = availableSize.toFileSize(space = false)
287+
val availableSizeString = availableSize.toFileSize(space = false, round = 1)
288288
val progress: Float = try {
289-
downloadedSize.toFloat() / availableSize.toFloat()
289+
downloadedSize.toFloat() / downloadCoursePreview.totalSize.toFloat()
290290
} catch (_: ArithmeticException) {
291291
0f
292292
}
@@ -350,7 +350,7 @@ private fun CourseItem(
350350
color = MaterialTheme.appColors.successGreen,
351351
text = stringResource(
352352
R.string.downloaded_downloaded_size,
353-
downloadedSize.toFileSize(space = false)
353+
downloadedSize.toFileSize(space = false, round = 1)
354354
)
355355
)
356356
}

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.openedx.downloads.presentation.download
22

3-
import androidx.compose.material.icons.Icons
4-
import androidx.compose.material.icons.automirrored.outlined.InsertDriveFile
53
import androidx.fragment.app.FragmentManager
64
import androidx.lifecycle.viewModelScope
75
import kotlinx.coroutines.Dispatchers
@@ -138,7 +136,13 @@ class DownloadsViewModel(
138136
val computedState = if (blockStates.isEmpty()) {
139137
DownloadedState.NOT_DOWNLOADED
140138
} else {
141-
determineCourseState(blockStates)
139+
val downloadedSize = _uiState.value.downloadModels
140+
.filter { it.courseId == courseId }
141+
.sumOf { it.size }
142+
val courseSize = _uiState.value.downloadCoursePreviews
143+
.find { it.id == courseId }?.totalSize ?: 0
144+
val isSizeMatch: Boolean = downloadedSize.toDouble() / courseSize >= 0.95
145+
determineCourseState(blockStates, isSizeMatch)
142146
}
143147
if (currentCourseState == DownloadedState.LOADING_COURSE_STRUCTURE &&
144148
computedState == DownloadedState.NOT_DOWNLOADED
@@ -156,9 +160,12 @@ class DownloadsViewModel(
156160
}
157161
}
158162

159-
private fun determineCourseState(blockStates: List<DownloadedState>): DownloadedState {
163+
private fun determineCourseState(
164+
blockStates: List<DownloadedState>,
165+
isSizeMatch: Boolean
166+
): DownloadedState {
160167
return when {
161-
blockStates.all { it == DownloadedState.DOWNLOADED } -> DownloadedState.DOWNLOADED
168+
blockStates.all { it == DownloadedState.DOWNLOADED } && isSizeMatch -> DownloadedState.DOWNLOADED
162169
blockStates.all { it == DownloadedState.WAITING } -> DownloadedState.WAITING
163170
blockStates.any { it == DownloadedState.DOWNLOADING } -> DownloadedState.DOWNLOADING
164171
else -> DownloadedState.NOT_DOWNLOADED
@@ -169,7 +176,9 @@ class DownloadsViewModel(
169176
viewModelScope.launch(Dispatchers.IO) {
170177
updateLoadingState(isLoading = !refresh, isRefreshing = refresh)
171178
interactor.getDownloadCoursesPreview(refresh)
172-
.onCompletion { resetLoadingState() }
179+
.onCompletion {
180+
resetLoadingState()
181+
}
173182
.catch { e ->
174183
emitErrorMessage(e)
175184
}
@@ -183,7 +192,11 @@ class DownloadsViewModel(
183192
.forEach { addDownloadableChildrenForSequentialBlock(it) }
184193
initDownloadModelsStatus()
185194
_uiState.update { state ->
186-
state.copy(downloadCoursePreviews = downloadCoursePreviews)
195+
state.copy(
196+
downloadCoursePreviews = downloadCoursePreviews,
197+
isLoading = false,
198+
isRefreshing = false
199+
)
187200
}
188201
}
189202
}
@@ -195,12 +208,6 @@ class DownloadsViewModel(
195208
}
196209
}
197210

198-
private fun resetLoadingState() {
199-
_uiState.update { state ->
200-
state.copy(isLoading = false, isRefreshing = false)
201-
}
202-
}
203-
204211
private suspend fun emitErrorMessage(e: Throwable) {
205212
val text = if (e.isInternetError()) {
206213
R.string.core_error_no_connection
@@ -213,7 +220,6 @@ class DownloadsViewModel(
213220
}
214221

215222
fun refreshData() {
216-
_uiState.update { it.copy(isRefreshing = true) }
217223
fetchDownloads(refresh = true)
218224
}
219225

@@ -258,7 +264,6 @@ class DownloadsViewModel(
258264
val downloadDialogItem = DownloadDialogItem(
259265
title = title,
260266
size = totalSize,
261-
icon = Icons.AutoMirrored.Outlined.InsertDriveFile
262267
)
263268
downloadDialogManager.showRemoveDownloadModelPopup(
264269
downloadDialogItem = downloadDialogItem,
@@ -343,6 +348,12 @@ class DownloadsViewModel(
343348
)
344349
}
345350

351+
private fun resetLoadingState() {
352+
_uiState.update { state ->
353+
state.copy(isLoading = false, isRefreshing = false)
354+
}
355+
}
356+
346357
private fun updateCourseState(courseId: String, state: DownloadedState) {
347358
_uiState.update { currentState ->
348359
currentState.copy(

downloads/src/test/java/org/openedx/downloads/DownloadsViewModelTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.openedx.core.presentation.CoreAnalytics
4343
import org.openedx.core.presentation.DownloadsAnalytics
4444
import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogManager
4545
import org.openedx.core.system.connection.NetworkConnection
46+
import org.openedx.core.system.notifier.CourseNotifier
4647
import org.openedx.core.system.notifier.DiscoveryNotifier
4748
import org.openedx.downloads.domain.interactor.DownloadInteractor
4849
import org.openedx.downloads.presentation.DownloadsRouter
@@ -76,6 +77,7 @@ class DownloadsViewModelTest {
7677
private val downloadHelper = mockk<DownloadHelper>(relaxed = true)
7778
private val router = mockk<DownloadsRouter>(relaxed = true)
7879
private val discoveryNotifier = mockk<DiscoveryNotifier>(relaxed = true)
80+
private val courseNotifier = mockk<CourseNotifier>(relaxed = true)
7981

8082
private val noInternet = "No connection"
8183
private val unknownError = "Unknown error"
@@ -220,6 +222,7 @@ class DownloadsViewModelTest {
220222
config,
221223
analytics,
222224
discoveryNotifier,
225+
courseNotifier,
223226
router,
224227
preferencesManager,
225228
coreAnalytics,
@@ -246,6 +249,7 @@ class DownloadsViewModelTest {
246249
config,
247250
analytics,
248251
discoveryNotifier,
252+
courseNotifier,
249253
router,
250254
preferencesManager,
251255
coreAnalytics,
@@ -287,6 +291,7 @@ class DownloadsViewModelTest {
287291
config,
288292
analytics,
289293
discoveryNotifier,
294+
courseNotifier,
290295
router,
291296
preferencesManager,
292297
coreAnalytics,
@@ -320,6 +325,7 @@ class DownloadsViewModelTest {
320325
config,
321326
analytics,
322327
discoveryNotifier,
328+
courseNotifier,
323329
router,
324330
preferencesManager,
325331
coreAnalytics,
@@ -359,6 +365,7 @@ class DownloadsViewModelTest {
359365
config,
360366
analytics,
361367
discoveryNotifier,
368+
courseNotifier,
362369
router,
363370
preferencesManager,
364371
coreAnalytics,

0 commit comments

Comments
 (0)