Skip to content

Commit 32897ab

Browse files
feat: show course item on dialog
1 parent b03927c commit 32897ab

File tree

5 files changed

+148
-102
lines changed

5 files changed

+148
-102
lines changed

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

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.openedx.core.BlockType
99
import org.openedx.core.data.storage.CorePreferences
1010
import org.openedx.core.domain.interactor.CourseInteractor
1111
import org.openedx.core.domain.model.Block
12+
import org.openedx.core.domain.model.CourseStructure
1213
import org.openedx.core.module.DownloadWorkerController
1314
import org.openedx.core.module.db.DownloadModel
1415
import org.openedx.core.system.StorageManager
@@ -109,6 +110,8 @@ class DownloadDialogManager(
109110
subSectionsBlocks: List<Block>,
110111
courseId: String,
111112
isBlocksDownloaded: Boolean,
113+
showCourseItem: Boolean = false,
114+
courseName: String = "",
112115
onlyVideoBlocks: Boolean = false,
113116
fragmentManager: FragmentManager,
114117
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
@@ -121,6 +124,8 @@ class DownloadDialogManager(
121124
courseId = courseId,
122125
fragmentManager = fragmentManager,
123126
isBlocksDownloaded = isBlocksDownloaded,
127+
showCourseItem = showCourseItem,
128+
courseName = courseName,
124129
onlyVideoBlocks = onlyVideoBlocks,
125130
removeDownloadModels = removeDownloadModels,
126131
saveDownloadModels = saveDownloadModels,
@@ -217,6 +222,8 @@ class DownloadDialogManager(
217222
courseId: String,
218223
fragmentManager: FragmentManager,
219224
isBlocksDownloaded: Boolean,
225+
showCourseItem: Boolean,
226+
courseName: String,
220227
onlyVideoBlocks: Boolean,
221228
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
222229
saveDownloadModels: (blockId: String) -> Unit,
@@ -226,25 +233,31 @@ class DownloadDialogManager(
226233
coroutineScope.launch {
227234
val courseStructure = interactor.getCourseStructure(courseId, false)
228235
val downloadModelIds = interactor.getAllDownloadModels().map { it.id }
229-
230-
val downloadDialogItems = subSectionsBlocks.mapNotNull { subSectionBlock ->
231-
val verticalBlocks =
232-
courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
233-
val blocks = verticalBlocks.flatMap { verticalBlock ->
234-
courseStructure.blockData.filter {
235-
it.id in verticalBlock.descendants &&
236-
(isBlocksDownloaded == (it.id in downloadModelIds)) &&
237-
(!onlyVideoBlocks || it.type == BlockType.VIDEO)
238-
}
236+
val downloadDialogItems = if (showCourseItem) {
237+
val totalSize = subSectionsBlocks.sumOf { subSection ->
238+
calculateSubSectionSize(
239+
subSection = subSection,
240+
courseStructure = courseStructure,
241+
downloadModelIds = downloadModelIds,
242+
onlyVideoBlocks = onlyVideoBlocks,
243+
isBlocksDownloaded = isBlocksDownloaded
244+
)
239245
}
240-
val size = blocks.sumOf { it.getFileSize() }
241-
if (size > 0) {
242-
DownloadDialogItem(
243-
title = subSectionBlock.displayName,
244-
size = size
246+
listOf(DownloadDialogItem(title = courseName, size = totalSize))
247+
} else {
248+
subSectionsBlocks.mapNotNull { subSection ->
249+
val size = calculateSubSectionSize(
250+
subSection = subSection,
251+
courseStructure = courseStructure,
252+
downloadModelIds = downloadModelIds,
253+
onlyVideoBlocks = onlyVideoBlocks,
254+
isBlocksDownloaded = isBlocksDownloaded
245255
)
246-
} else {
247-
null
256+
if (size > 0) {
257+
DownloadDialogItem(title = subSection.displayName, size = size)
258+
} else {
259+
null
260+
}
248261
}
249262
}
250263

@@ -256,18 +269,33 @@ class DownloadDialogManager(
256269
sizeSum = downloadDialogItems.sumOf { it.size },
257270
fragmentManager = fragmentManager,
258271
removeDownloadModels = {
259-
subSectionsBlocks.forEach {
260-
removeDownloadModels(
261-
it.id,
262-
courseId
263-
)
264-
}
272+
subSectionsBlocks.forEach { removeDownloadModels(it.id, courseId) }
273+
},
274+
saveDownloadModels = {
275+
subSectionsBlocks.forEach { saveDownloadModels(it.id) }
265276
},
266-
saveDownloadModels = { subSectionsBlocks.forEach { saveDownloadModels(it.id) } },
267277
onDismissClick = onDismissClick,
268278
onConfirmClick = onConfirmClick,
269279
)
270280
)
271281
}
272282
}
283+
284+
private fun calculateSubSectionSize(
285+
subSection: Block,
286+
courseStructure: CourseStructure,
287+
downloadModelIds: List<String>,
288+
isBlocksDownloaded: Boolean,
289+
onlyVideoBlocks: Boolean
290+
): Long {
291+
val verticalBlocks = courseStructure.blockData.filter { it.id in subSection.descendants }
292+
val blocks = verticalBlocks.flatMap { verticalBlock ->
293+
courseStructure.blockData.filter {
294+
it.id in verticalBlock.descendants &&
295+
(isBlocksDownloaded == (it.id in downloadModelIds)) &&
296+
(!onlyVideoBlocks || it.type == BlockType.VIDEO)
297+
}
298+
}
299+
return blocks.sumOf { it.getFileSize() }
300+
}
273301
}

course/src/test/java/org/openedx/course/presentation/outline/CourseOutlineViewModelTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ class CourseOutlineViewModelTest {
264264
any(),
265265
any(),
266266
any(),
267-
any()
267+
any(),
268+
any(),
269+
any(),
268270
)
269271
} returns Unit
270272
coEvery { interactor.getCourseStatusFlow(any()) } returns flow { throw UnknownHostException() }

course/src/test/java/org/openedx/course/presentation/videos/CourseVideoViewModelTest.kt

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,19 @@ class CourseVideoViewModelTest {
198198
every { config.getApiHostURL() } returns "http://localhost:8000"
199199
every { courseNotifier.notifier } returns flowOf(CourseLoading(false))
200200
every { preferencesManager.isRelativeDatesEnabled } returns true
201-
every { downloadDialogManager.showPopup(any(), any(), any(), any(), any(), any(), any()) } returns Unit
201+
every {
202+
downloadDialogManager.showPopup(
203+
any(),
204+
any(),
205+
any(),
206+
any(),
207+
any(),
208+
any(),
209+
any(),
210+
any(),
211+
any(),
212+
)
213+
} returns Unit
202214
}
203215

204216
@After
@@ -373,88 +385,90 @@ class CourseVideoViewModelTest {
373385
}
374386

375387
@Test
376-
fun `saveDownloadModels only wifi download, with connection`() = runTest(UnconfinedTestDispatcher()) {
377-
every { config.getCourseUIConfig().isCourseDropdownNavigationEnabled } returns false
378-
every { preferencesManager.videoSettings } returns VideoSettings.default
379-
val viewModel = CourseVideoViewModel(
380-
"",
381-
"",
382-
config,
383-
interactor,
384-
resourceManager,
385-
networkConnection,
386-
preferencesManager,
387-
courseNotifier,
388-
videoNotifier,
389-
analytics,
390-
downloadDialogManager,
391-
fileUtil,
392-
courseRouter,
393-
coreAnalytics,
394-
downloadDao,
395-
workerController,
396-
downloadHelper,
397-
)
398-
coEvery { interactor.getCourseStructureForVideos(any()) } returns courseStructure
399-
coEvery { downloadDao.getAllDataFlow() } returns flow { emit(listOf(downloadModelEntity)) }
400-
every { preferencesManager.videoSettings.wifiDownloadOnly } returns true
401-
every { networkConnection.isWifiConnected() } returns true
402-
coEvery { workerController.saveModels(any()) } returns Unit
403-
coEvery { downloadDao.getAllDataFlow() } returns flow {
404-
emit(listOf(DownloadModelEntity.createFrom(downloadModel)))
405-
}
406-
every { coreAnalytics.logEvent(any(), any()) } returns Unit
407-
val message = async {
408-
withTimeoutOrNull(5000) {
409-
viewModel.uiMessage.first() as? UIMessage.SnackBarMessage
388+
fun `saveDownloadModels only wifi download, with connection`() =
389+
runTest(UnconfinedTestDispatcher()) {
390+
every { config.getCourseUIConfig().isCourseDropdownNavigationEnabled } returns false
391+
every { preferencesManager.videoSettings } returns VideoSettings.default
392+
val viewModel = CourseVideoViewModel(
393+
"",
394+
"",
395+
config,
396+
interactor,
397+
resourceManager,
398+
networkConnection,
399+
preferencesManager,
400+
courseNotifier,
401+
videoNotifier,
402+
analytics,
403+
downloadDialogManager,
404+
fileUtil,
405+
courseRouter,
406+
coreAnalytics,
407+
downloadDao,
408+
workerController,
409+
downloadHelper,
410+
)
411+
coEvery { interactor.getCourseStructureForVideos(any()) } returns courseStructure
412+
coEvery { downloadDao.getAllDataFlow() } returns flow { emit(listOf(downloadModelEntity)) }
413+
every { preferencesManager.videoSettings.wifiDownloadOnly } returns true
414+
every { networkConnection.isWifiConnected() } returns true
415+
coEvery { workerController.saveModels(any()) } returns Unit
416+
coEvery { downloadDao.getAllDataFlow() } returns flow {
417+
emit(listOf(DownloadModelEntity.createFrom(downloadModel)))
418+
}
419+
every { coreAnalytics.logEvent(any(), any()) } returns Unit
420+
val message = async {
421+
withTimeoutOrNull(5000) {
422+
viewModel.uiMessage.first() as? UIMessage.SnackBarMessage
423+
}
410424
}
411-
}
412425

413-
viewModel.saveDownloadModels("", "", "")
414-
advanceUntilIdle()
426+
viewModel.saveDownloadModels("", "", "")
427+
advanceUntilIdle()
415428

416-
assert(message.await()?.message.isNullOrEmpty())
417-
}
429+
assert(message.await()?.message.isNullOrEmpty())
430+
}
418431

419432
@Test
420-
fun `saveDownloadModels only wifi download, without connection`() = runTest(UnconfinedTestDispatcher()) {
421-
every { config.getCourseUIConfig().isCourseDropdownNavigationEnabled } returns false
422-
every { preferencesManager.videoSettings } returns VideoSettings.default
423-
val viewModel = CourseVideoViewModel(
424-
"",
425-
"",
426-
config,
427-
interactor,
428-
resourceManager,
429-
networkConnection,
430-
preferencesManager,
431-
courseNotifier,
432-
videoNotifier,
433-
analytics,
434-
downloadDialogManager,
435-
fileUtil,
436-
courseRouter,
437-
coreAnalytics,
438-
downloadDao,
439-
workerController,
440-
downloadHelper,
441-
)
442-
every { preferencesManager.videoSettings.wifiDownloadOnly } returns true
443-
every { networkConnection.isWifiConnected() } returns false
444-
every { networkConnection.isOnline() } returns false
445-
coEvery { interactor.getCourseStructureForVideos(any()) } returns courseStructure
446-
coEvery { downloadDao.getAllDataFlow() } returns flow { emit(listOf(downloadModelEntity)) }
447-
coEvery { workerController.saveModels(any()) } returns Unit
448-
val message = async {
449-
withTimeoutOrNull(5000) {
450-
viewModel.uiMessage.first() as? UIMessage.SnackBarMessage
433+
fun `saveDownloadModels only wifi download, without connection`() =
434+
runTest(UnconfinedTestDispatcher()) {
435+
every { config.getCourseUIConfig().isCourseDropdownNavigationEnabled } returns false
436+
every { preferencesManager.videoSettings } returns VideoSettings.default
437+
val viewModel = CourseVideoViewModel(
438+
"",
439+
"",
440+
config,
441+
interactor,
442+
resourceManager,
443+
networkConnection,
444+
preferencesManager,
445+
courseNotifier,
446+
videoNotifier,
447+
analytics,
448+
downloadDialogManager,
449+
fileUtil,
450+
courseRouter,
451+
coreAnalytics,
452+
downloadDao,
453+
workerController,
454+
downloadHelper,
455+
)
456+
every { preferencesManager.videoSettings.wifiDownloadOnly } returns true
457+
every { networkConnection.isWifiConnected() } returns false
458+
every { networkConnection.isOnline() } returns false
459+
coEvery { interactor.getCourseStructureForVideos(any()) } returns courseStructure
460+
coEvery { downloadDao.getAllDataFlow() } returns flow { emit(listOf(downloadModelEntity)) }
461+
coEvery { workerController.saveModels(any()) } returns Unit
462+
val message = async {
463+
withTimeoutOrNull(5000) {
464+
viewModel.uiMessage.first() as? UIMessage.SnackBarMessage
465+
}
451466
}
452-
}
453467

454-
viewModel.saveDownloadModels("", "", "")
468+
viewModel.saveDownloadModels("", "", "")
455469

456-
advanceUntilIdle()
470+
advanceUntilIdle()
457471

458-
assert(message.await()?.message.isNullOrEmpty())
459-
}
472+
assert(message.await()?.message.isNullOrEmpty())
473+
}
460474
}

downloads/src/main/java/org/openedx/downloads/presentation/download/DownloadsViewModel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ class DownloadsViewModel(
315315
subSectionsBlocks = notDownloadedSubSectionBlocks,
316316
courseId = courseId,
317317
isBlocksDownloaded = false,
318+
showCourseItem = true,
319+
courseName = courseStructure.name,
318320
fragmentManager = fragmentManager,
319321
removeDownloadModels = ::removeDownloadModels,
320322
saveDownloadModels = { blockId ->

downloads/src/test/java/org/openedx/downloads/DownloadsViewModelTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class DownloadsViewModelTest {
263263

264264
coVerify(exactly = 1) {
265265
downloadDialogManager.showPopup(
266-
any(), any(), any(), any(), any(), any(), any(), any(), any()
266+
any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()
267267
)
268268
}
269269
}

0 commit comments

Comments
 (0)