@@ -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