Skip to content

Commit 8eb6be8

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

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ class DownloadsViewModel(
138138
val computedState = if (blockStates.isEmpty()) {
139139
DownloadedState.NOT_DOWNLOADED
140140
} else {
141-
determineCourseState(blockStates)
141+
val downloadedSize = _uiState.value.downloadModels
142+
.filter { it.courseId == courseId }
143+
.sumOf { it.size }
144+
val courseSize = _uiState.value.downloadCoursePreviews
145+
.find { it.id == courseId }?.totalSize ?: 0
146+
val isSizeMatch: Boolean = downloadedSize.toDouble() / courseSize >= 0.95
147+
determineCourseState(blockStates, isSizeMatch)
142148
}
143149
if (currentCourseState == DownloadedState.LOADING_COURSE_STRUCTURE &&
144150
computedState == DownloadedState.NOT_DOWNLOADED
@@ -156,9 +162,12 @@ class DownloadsViewModel(
156162
}
157163
}
158164

159-
private fun determineCourseState(blockStates: List<DownloadedState>): DownloadedState {
165+
private fun determineCourseState(
166+
blockStates: List<DownloadedState>,
167+
isSizeMatch: Boolean
168+
): DownloadedState {
160169
return when {
161-
blockStates.all { it == DownloadedState.DOWNLOADED } -> DownloadedState.DOWNLOADED
170+
blockStates.all { it == DownloadedState.DOWNLOADED } && isSizeMatch -> DownloadedState.DOWNLOADED
162171
blockStates.all { it == DownloadedState.WAITING } -> DownloadedState.WAITING
163172
blockStates.any { it == DownloadedState.DOWNLOADING } -> DownloadedState.DOWNLOADING
164173
else -> DownloadedState.NOT_DOWNLOADED
@@ -169,7 +178,9 @@ class DownloadsViewModel(
169178
viewModelScope.launch(Dispatchers.IO) {
170179
updateLoadingState(isLoading = !refresh, isRefreshing = refresh)
171180
interactor.getDownloadCoursesPreview(refresh)
172-
.onCompletion { resetLoadingState() }
181+
.onCompletion {
182+
resetLoadingState()
183+
}
173184
.catch { e ->
174185
emitErrorMessage(e)
175186
}
@@ -183,7 +194,11 @@ class DownloadsViewModel(
183194
.forEach { addDownloadableChildrenForSequentialBlock(it) }
184195
initDownloadModelsStatus()
185196
_uiState.update { state ->
186-
state.copy(downloadCoursePreviews = downloadCoursePreviews)
197+
state.copy(
198+
downloadCoursePreviews = downloadCoursePreviews,
199+
isLoading = false,
200+
isRefreshing = false
201+
)
187202
}
188203
}
189204
}
@@ -195,12 +210,6 @@ class DownloadsViewModel(
195210
}
196211
}
197212

198-
private fun resetLoadingState() {
199-
_uiState.update { state ->
200-
state.copy(isLoading = false, isRefreshing = false)
201-
}
202-
}
203-
204213
private suspend fun emitErrorMessage(e: Throwable) {
205214
val text = if (e.isInternetError()) {
206215
R.string.core_error_no_connection
@@ -213,7 +222,6 @@ class DownloadsViewModel(
213222
}
214223

215224
fun refreshData() {
216-
_uiState.update { it.copy(isRefreshing = true) }
217225
fetchDownloads(refresh = true)
218226
}
219227

@@ -343,6 +351,12 @@ class DownloadsViewModel(
343351
)
344352
}
345353

354+
private fun resetLoadingState() {
355+
_uiState.update { state ->
356+
state.copy(isLoading = false, isRefreshing = false)
357+
}
358+
}
359+
346360
private fun updateCourseState(courseId: String, state: DownloadedState) {
347361
_uiState.update { currentState ->
348362
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)