Skip to content

Commit 74b79cf

Browse files
fix: download tab UI fix (#467)
1 parent fa82716 commit 74b79cf

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineScreen.kt

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,17 @@ private fun CourseOfflineUI(
155155
} else {
156156
NoDownloadableBlocksProgress()
157157
}
158-
if (uiState.progressBarValue != 1f && !uiState.isDownloading && hasInternetConnection) {
158+
if (
159+
uiState.progressBarValue != 1f &&
160+
!uiState.isDownloading &&
161+
hasInternetConnection &&
162+
!uiState.isAllDownloaded
163+
) {
159164
Spacer(modifier = Modifier.height(20.dp))
160165
OpenEdXButton(
161166
text = stringResource(R.string.core_download_all),
162167
backgroundColor = MaterialTheme.appColors.secondaryButtonBackground,
163168
onClick = onDownloadAllClick,
164-
enabled = uiState.isHaveDownloadableBlocks,
165169
content = {
166170
val textColor = if (uiState.isHaveDownloadableBlocks) {
167171
MaterialTheme.appColors.primaryButtonText
@@ -365,15 +369,17 @@ private fun DownloadProgress(
365369
horizontalArrangement = Arrangement.SpaceBetween
366370
) {
367371
Text(
368-
text = uiState.downloadedSize,
372+
text = uiState.downloadedSize.toFileSize(1, false),
369373
style = MaterialTheme.appTypography.titleLarge,
370374
color = MaterialTheme.appColors.successGreen
371375
)
372-
Text(
373-
text = uiState.readyToDownloadSize,
374-
style = MaterialTheme.appTypography.titleLarge,
375-
color = MaterialTheme.appColors.textDark
376-
)
376+
if (uiState.readyToDownloadSize > 0) {
377+
Text(
378+
text = uiState.readyToDownloadSize.toFileSize(1, false),
379+
style = MaterialTheme.appTypography.titleLarge,
380+
color = MaterialTheme.appColors.textDark
381+
)
382+
}
377383
}
378384
Spacer(modifier = Modifier.height(4.dp))
379385
Row(
@@ -388,20 +394,22 @@ private fun DownloadProgress(
388394
color = MaterialTheme.appColors.successGreen,
389395
textStyle = MaterialTheme.appTypography.labelLarge
390396
)
391-
if (!uiState.isDownloading) {
392-
IconText(
393-
text = stringResource(R.string.core_ready_to_download),
394-
icon = Icons.Outlined.CloudDownload,
395-
color = MaterialTheme.appColors.textDark,
396-
textStyle = MaterialTheme.appTypography.labelLarge
397-
)
398-
} else {
399-
IconText(
400-
text = stringResource(R.string.core_downloading),
401-
icon = Icons.Outlined.CloudDownload,
402-
color = MaterialTheme.appColors.textDark,
403-
textStyle = MaterialTheme.appTypography.labelLarge
404-
)
397+
if (uiState.readyToDownloadSize > 0) {
398+
if (!uiState.isDownloading) {
399+
IconText(
400+
text = stringResource(R.string.core_ready_to_download),
401+
icon = Icons.Outlined.CloudDownload,
402+
color = MaterialTheme.appColors.textDark,
403+
textStyle = MaterialTheme.appTypography.labelLarge
404+
)
405+
} else {
406+
IconText(
407+
text = stringResource(R.string.core_downloading),
408+
icon = Icons.Outlined.CloudDownload,
409+
color = MaterialTheme.appColors.textDark,
410+
textStyle = MaterialTheme.appTypography.labelLarge
411+
)
412+
}
405413
}
406414
}
407415
if (uiState.progressBarValue != 0f) {
@@ -462,10 +470,11 @@ private fun CourseOfflineUIPreview() {
462470
hasInternetConnection = true,
463471
uiState = CourseOfflineUIState(
464472
isHaveDownloadableBlocks = true,
465-
readyToDownloadSize = "159MB",
466-
downloadedSize = "0MB",
473+
readyToDownloadSize = 100000L,
474+
downloadedSize = 0L,
467475
progressBarValue = 0f,
468476
isDownloading = true,
477+
isAllDownloaded = true,
469478
largestDownloads = listOf(
470479
DownloadModel(
471480
"",

course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineUIState.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import org.openedx.core.module.db.DownloadModel
44

55
data class CourseOfflineUIState(
66
val isHaveDownloadableBlocks: Boolean,
7+
val isAllDownloaded: Boolean,
78
val largestDownloads: List<DownloadModel>,
89
val isDownloading: Boolean,
9-
val readyToDownloadSize: String,
10-
val downloadedSize: String,
10+
val readyToDownloadSize: Long,
11+
val downloadedSize: Long,
1112
val progressBarValue: Float,
1213
)

course/src/main/java/org/openedx/course/presentation/offline/CourseOfflineViewModel.kt

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.openedx.core.extension.safeDivBy
1818
import org.openedx.core.module.DownloadWorkerController
1919
import org.openedx.core.module.db.DownloadDao
2020
import org.openedx.core.module.db.DownloadModel
21+
import org.openedx.core.module.db.DownloadedState
2122
import org.openedx.core.module.db.FileType
2223
import org.openedx.core.module.download.BaseDownloadViewModel
2324
import org.openedx.core.module.download.DownloadHelper
@@ -28,7 +29,6 @@ import org.openedx.core.system.connection.NetworkConnection
2829
import org.openedx.core.system.notifier.CourseNotifier
2930
import org.openedx.core.system.notifier.CourseStructureGot
3031
import org.openedx.course.domain.interactor.CourseInteractor
31-
import org.openedx.foundation.extension.toFileSize
3232
import org.openedx.foundation.utils.FileUtil
3333

3434
class CourseOfflineViewModel(
@@ -56,8 +56,9 @@ class CourseOfflineViewModel(
5656
isHaveDownloadableBlocks = false,
5757
largestDownloads = emptyList(),
5858
isDownloading = false,
59-
readyToDownloadSize = "",
60-
downloadedSize = "",
59+
isAllDownloaded = false,
60+
readyToDownloadSize = 0L,
61+
downloadedSize = 0L,
6162
progressBarValue = 0f,
6263
)
6364
)
@@ -163,20 +164,24 @@ class CourseOfflineViewModel(
163164
viewModelScope.launch {
164165
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
165166
val totalDownloadableSize = getFilesSize(courseStructure.blockData)
166-
167-
if (totalDownloadableSize == 0L) return@launch
168-
169167
courseInteractor.getDownloadModels().collect { downloadModels ->
168+
val courseDownloadModels = downloadModels.filter { it.courseId == courseId }
170169
val completedDownloads =
171-
downloadModels.filter { it.downloadedState.isDownloaded && it.courseId == courseId }
172-
val completedDownloadIds = completedDownloads.map { it.id }
173-
val downloadedBlocks =
174-
courseStructure.blockData.filter { it.id in completedDownloadIds }
170+
courseDownloadModels.filter { it.downloadedState.isDownloaded }
171+
val downloadedBlocks = courseStructure.blockData.filter {
172+
it.id in completedDownloads.map { it.id }
173+
}
174+
val isAllDownloaded =
175+
courseDownloadModels.all { it.downloadedState == DownloadedState.DOWNLOADED } &&
176+
courseDownloadModels.isNotEmpty()
177+
val isHaveDownloadableBlocks = courseStructure.blockData.any { it.isDownloadable }
175178

176179
updateUIState(
177180
totalDownloadableSize,
178181
completedDownloads,
179-
downloadedBlocks
182+
downloadedBlocks,
183+
isAllDownloaded,
184+
isHaveDownloadableBlocks
180185
)
181186
}
182187
}
@@ -185,7 +190,9 @@ class CourseOfflineViewModel(
185190
private fun updateUIState(
186191
totalDownloadableSize: Long,
187192
completedDownloads: List<DownloadModel>,
188-
downloadedBlocks: List<Block>
193+
downloadedBlocks: List<Block>,
194+
isAllDownloaded: Boolean,
195+
isHaveDownloadableBlocks: Boolean,
189196
) {
190197
val downloadedSize = getFilesSize(downloadedBlocks).toFloat()
191198
val realDownloadedSize = completedDownloads.sumOf { it.size }
@@ -200,10 +207,11 @@ class CourseOfflineViewModel(
200207
}
201208
_uiState.update {
202209
it.copy(
203-
isHaveDownloadableBlocks = true,
210+
isHaveDownloadableBlocks = isHaveDownloadableBlocks,
211+
isAllDownloaded = isAllDownloaded,
204212
largestDownloads = largestDownloads,
205-
readyToDownloadSize = readyToDownloadSize.toFileSize(1, false),
206-
downloadedSize = realDownloadedSize.toFileSize(1, false),
213+
readyToDownloadSize = readyToDownloadSize,
214+
downloadedSize = realDownloadedSize,
207215
progressBarValue = progressBarValue
208216
)
209217
}

0 commit comments

Comments
 (0)