@@ -25,6 +25,8 @@ import org.openedx.core.presentation.CoreAnalytics
2525import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogItem
2626import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogManager
2727import org.openedx.core.system.connection.NetworkConnection
28+ import org.openedx.core.system.notifier.CourseNotifier
29+ import org.openedx.core.system.notifier.CourseStructureGot
2830import org.openedx.course.domain.interactor.CourseInteractor
2931import org.openedx.foundation.extension.toFileSize
3032import 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