Skip to content

Commit 4f0d788

Browse files
fix: handle NoCachedDataException
1 parent 4df0b02 commit 4f0d788

File tree

2 files changed

+92
-82
lines changed

2 files changed

+92
-82
lines changed

core/src/main/java/org/openedx/core/presentation/dialog/downloaddialog/DownloadDialogManager.kt

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -209,51 +209,53 @@ class DownloadDialogManager(
209209
fragmentManager: FragmentManager,
210210
) {
211211
coroutineScope.launch {
212-
val courseIds = downloadModels.map { it.courseId }.distinct()
213-
val blockIds = downloadModels.map { it.id }
214-
val notDownloadedSubSections = mutableListOf<Block>()
215-
val allDownloadDialogItems = mutableListOf<DownloadDialogItem>()
216-
217-
courseIds.forEach { courseId ->
218-
val courseStructure = interactor.getCourseStructureFromCache(courseId)
219-
val allSubSectionBlocks =
220-
courseStructure.blockData.filter { it.type == BlockType.SEQUENTIAL }
221-
222-
allSubSectionBlocks.forEach { subSectionBlock ->
223-
val verticalBlocks =
224-
courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
225-
val blocks = courseStructure.blockData.filter {
226-
it.id in verticalBlocks.flatMap { it.descendants } && it.id in blockIds
227-
}
228-
val totalSize = blocks.sumOf { it.getFileSize() }
229-
230-
if (blocks.isNotEmpty()) notDownloadedSubSections.add(subSectionBlock)
231-
if (totalSize > 0) {
232-
allDownloadDialogItems.add(
233-
DownloadDialogItem(
234-
title = subSectionBlock.displayName,
235-
size = totalSize
212+
runCatching {
213+
val courseIds = downloadModels.map { it.courseId }.distinct()
214+
val blockIds = downloadModels.map { it.id }
215+
val notDownloadedSubSections = mutableListOf<Block>()
216+
val allDownloadDialogItems = mutableListOf<DownloadDialogItem>()
217+
218+
courseIds.forEach { courseId ->
219+
val courseStructure = interactor.getCourseStructureFromCache(courseId)
220+
val allSubSectionBlocks =
221+
courseStructure.blockData.filter { it.type == BlockType.SEQUENTIAL }
222+
223+
allSubSectionBlocks.forEach { subSectionBlock ->
224+
val verticalBlocks =
225+
courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
226+
val blocks = courseStructure.blockData.filter {
227+
it.id in verticalBlocks.flatMap { it.descendants } && it.id in blockIds
228+
}
229+
val totalSize = blocks.sumOf { it.getFileSize() }
230+
231+
if (blocks.isNotEmpty()) notDownloadedSubSections.add(subSectionBlock)
232+
if (totalSize > 0) {
233+
allDownloadDialogItems.add(
234+
DownloadDialogItem(
235+
title = subSectionBlock.displayName,
236+
size = totalSize
237+
)
236238
)
237-
)
239+
}
238240
}
239241
}
240-
}
241242

242-
uiState.emit(
243-
DownloadDialogUIState(
244-
downloadDialogItems = allDownloadDialogItems,
245-
isAllBlocksDownloaded = false,
246-
isDownloadFailed = true,
247-
sizeSum = allDownloadDialogItems.sumOf { it.size },
248-
fragmentManager = fragmentManager,
249-
removeDownloadModels = {},
250-
saveDownloadModels = {
251-
coroutineScope.launch {
252-
workerController.saveModels(downloadModels)
243+
uiState.emit(
244+
DownloadDialogUIState(
245+
downloadDialogItems = allDownloadDialogItems,
246+
isAllBlocksDownloaded = false,
247+
isDownloadFailed = true,
248+
sizeSum = allDownloadDialogItems.sumOf { it.size },
249+
fragmentManager = fragmentManager,
250+
removeDownloadModels = {},
251+
saveDownloadModels = {
252+
coroutineScope.launch {
253+
workerController.saveModels(downloadModels)
254+
}
253255
}
254-
}
256+
)
255257
)
256-
)
258+
}
257259
}
258260
}
259261

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

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,31 @@ class CourseOfflineViewModel(
8080

8181
fun downloadAllBlocks(fragmentManager: FragmentManager) {
8282
viewModelScope.launch {
83-
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
84-
val downloadModels = courseInteractor.getAllDownloadModels()
85-
val subSectionsBlocks = allBlocks.values.filter { it.type == BlockType.SEQUENTIAL }
86-
val notDownloadedSubSectionBlocks = subSectionsBlocks.mapNotNull { subSection ->
87-
val verticalBlocks = allBlocks.values.filter { it.id in subSection.descendants }
88-
val notDownloadedBlocks = courseStructure.blockData.filter { block ->
89-
block.id in verticalBlocks.flatMap { it.descendants } &&
90-
block.isDownloadable &&
91-
downloadModels.none { it.id == block.id }
83+
runCatching {
84+
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
85+
val downloadModels = courseInteractor.getAllDownloadModels()
86+
val subSectionsBlocks = allBlocks.values.filter { it.type == BlockType.SEQUENTIAL }
87+
val notDownloadedSubSectionBlocks = subSectionsBlocks.mapNotNull { subSection ->
88+
val verticalBlocks = allBlocks.values.filter { it.id in subSection.descendants }
89+
val notDownloadedBlocks = courseStructure.blockData.filter { block ->
90+
block.id in verticalBlocks.flatMap { it.descendants } &&
91+
block.isDownloadable &&
92+
downloadModels.none { it.id == block.id }
93+
}
94+
if (notDownloadedBlocks.isNotEmpty()) subSection else null
9295
}
93-
if (notDownloadedBlocks.isNotEmpty()) subSection else null
94-
}
9596

96-
downloadDialogManager.showPopup(
97-
subSectionsBlocks = notDownloadedSubSectionBlocks,
98-
courseId = courseId,
99-
isBlocksDownloaded = false,
100-
fragmentManager = fragmentManager,
101-
removeDownloadModels = ::removeDownloadModels,
102-
saveDownloadModels = { blockId ->
103-
saveDownloadModels(fileUtil.getExternalAppDir().path, courseId, blockId)
104-
}
105-
)
97+
downloadDialogManager.showPopup(
98+
subSectionsBlocks = notDownloadedSubSectionBlocks,
99+
courseId = courseId,
100+
isBlocksDownloaded = false,
101+
fragmentManager = fragmentManager,
102+
removeDownloadModels = ::removeDownloadModels,
103+
saveDownloadModels = { blockId ->
104+
saveDownloadModels(fileUtil.getExternalAppDir().path, courseId, blockId)
105+
}
106+
)
107+
}
106108
}
107109
}
108110

@@ -153,32 +155,38 @@ class CourseOfflineViewModel(
153155
}
154156

155157
private suspend fun initDownloadFragment() {
156-
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
157-
setBlocks(courseStructure.blockData)
158-
allBlocks.values
159-
.filter { it.type == BlockType.SEQUENTIAL }
160-
.forEach { addDownloadableChildrenForSequentialBlock(it) }
158+
runCatching {
159+
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
160+
setBlocks(courseStructure.blockData)
161+
allBlocks.values
162+
.filter { it.type == BlockType.SEQUENTIAL }
163+
.forEach { addDownloadableChildrenForSequentialBlock(it) }
164+
}
161165
}
162166

163167
private fun getOfflineData() {
164168
viewModelScope.launch {
165-
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
166-
val totalDownloadableSize = getFilesSize(courseStructure.blockData)
167-
168-
if (totalDownloadableSize == 0L) return@launch
169-
170-
courseInteractor.getDownloadModels().collect { downloadModels ->
171-
val completedDownloads =
172-
downloadModels.filter { it.downloadedState.isDownloaded && it.courseId == courseId }
173-
val completedDownloadIds = completedDownloads.map { it.id }
174-
val downloadedBlocks =
175-
courseStructure.blockData.filter { it.id in completedDownloadIds }
176-
177-
updateUIState(
178-
totalDownloadableSize,
179-
completedDownloads,
180-
downloadedBlocks
181-
)
169+
try {
170+
val courseStructure = courseInteractor.getCourseStructureFromCache(courseId)
171+
val totalDownloadableSize = getFilesSize(courseStructure.blockData)
172+
173+
if (totalDownloadableSize == 0L) return@launch
174+
175+
courseInteractor.getDownloadModels().collect { downloadModels ->
176+
val completedDownloads =
177+
downloadModels.filter { it.downloadedState.isDownloaded && it.courseId == courseId }
178+
val completedDownloadIds = completedDownloads.map { it.id }
179+
val downloadedBlocks =
180+
courseStructure.blockData.filter { it.id in completedDownloadIds }
181+
182+
updateUIState(
183+
totalDownloadableSize,
184+
completedDownloads,
185+
downloadedBlocks
186+
)
187+
}
188+
} catch (e: Exception) {
189+
e.printStackTrace()
182190
}
183191
}
184192
}

0 commit comments

Comments
 (0)