Skip to content

Commit b63e96c

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

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

app/src/main/java/org/openedx/app/di/ScreenModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ val screenModule = module {
494494
get(),
495495
get(),
496496
get(),
497+
get()
497498
)
498499
}
499500
viewModel { (courseId: String) ->

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

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import org.openedx.core.presentation.CoreAnalytics
2525
import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogItem
2626
import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogManager
2727
import org.openedx.core.system.connection.NetworkConnection
28+
import org.openedx.core.system.notifier.CourseNotifier
29+
import org.openedx.core.system.notifier.CourseStructureGot
2830
import org.openedx.course.domain.interactor.CourseInteractor
2931
import org.openedx.foundation.extension.toFileSize
3032
import org.openedx.foundation.utils.FileUtil
@@ -37,6 +39,7 @@ class CourseOfflineViewModel(
3739
private val downloadDialogManager: DownloadDialogManager,
3840
private val fileUtil: FileUtil,
3941
private val networkConnection: NetworkConnection,
42+
private val courseNotifier: CourseNotifier,
4043
coreAnalytics: CoreAnalytics,
4144
downloadDao: DownloadDao,
4245
workerController: DownloadWorkerController,
@@ -71,38 +74,36 @@ class CourseOfflineViewModel(
7174
_uiState.update { it.copy(isDownloading = isDownloading) }
7275
}
7376
}
74-
75-
viewModelScope.launch {
76-
async { initDownloadFragment() }.await()
77-
getOfflineData()
78-
}
77+
collectCourseNotifier()
7978
}
8079

8180
fun downloadAllBlocks(fragmentManager: FragmentManager) {
8281
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 }
82+
runCatching {
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 }
92+
}
93+
if (notDownloadedBlocks.isNotEmpty()) subSection else null
9294
}
93-
if (notDownloadedBlocks.isNotEmpty()) subSection else null
94-
}
9595

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-
)
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+
)
106+
}
106107
}
107108
}
108109

@@ -224,4 +225,17 @@ class CourseOfflineViewModel(
224225
}
225226
}
226227
}
228+
229+
private fun collectCourseNotifier() {
230+
viewModelScope.launch {
231+
courseNotifier.notifier.collect { event ->
232+
when (event) {
233+
is CourseStructureGot -> {
234+
async { initDownloadFragment() }.await()
235+
getOfflineData()
236+
}
237+
}
238+
}
239+
}
240+
}
227241
}

0 commit comments

Comments
 (0)