Skip to content

Commit 733bfd1

Browse files
refactor: improved code for better readability, optimized downloading logic
1 parent 32897ab commit 733bfd1

File tree

3 files changed

+220
-210
lines changed

3 files changed

+220
-210
lines changed

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

Lines changed: 86 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +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
12+
import org.openedx.core.domain.model.DownloadCoursePreview
1313
import org.openedx.core.module.DownloadWorkerController
1414
import org.openedx.core.module.db.DownloadModel
1515
import org.openedx.core.system.StorageManager
@@ -110,8 +110,6 @@ class DownloadDialogManager(
110110
subSectionsBlocks: List<Block>,
111111
courseId: String,
112112
isBlocksDownloaded: Boolean,
113-
showCourseItem: Boolean = false,
114-
courseName: String = "",
115113
onlyVideoBlocks: Boolean = false,
116114
fragmentManager: FragmentManager,
117115
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
@@ -124,8 +122,6 @@ class DownloadDialogManager(
124122
courseId = courseId,
125123
fragmentManager = fragmentManager,
126124
isBlocksDownloaded = isBlocksDownloaded,
127-
showCourseItem = showCourseItem,
128-
courseName = courseName,
129125
onlyVideoBlocks = onlyVideoBlocks,
130126
removeDownloadModels = removeDownloadModels,
131127
saveDownloadModels = saveDownloadModels,
@@ -134,6 +130,26 @@ class DownloadDialogManager(
134130
)
135131
}
136132

133+
fun showPopup(
134+
coursePreview: DownloadCoursePreview,
135+
isBlocksDownloaded: Boolean,
136+
fragmentManager: FragmentManager,
137+
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
138+
saveDownloadModels: () -> Unit,
139+
onDismissClick: () -> Unit = {},
140+
onConfirmClick: () -> Unit = {},
141+
) {
142+
createCourseDownloadItems(
143+
coursePreview = coursePreview,
144+
fragmentManager = fragmentManager,
145+
isBlocksDownloaded = isBlocksDownloaded,
146+
removeDownloadModels = removeDownloadModels,
147+
saveDownloadModels = saveDownloadModels,
148+
onDismissClick = onDismissClick,
149+
onConfirmClick = onConfirmClick
150+
)
151+
}
152+
137153
fun showRemoveDownloadModelPopup(
138154
downloadDialogItem: DownloadDialogItem,
139155
fragmentManager: FragmentManager,
@@ -222,8 +238,6 @@ class DownloadDialogManager(
222238
courseId: String,
223239
fragmentManager: FragmentManager,
224240
isBlocksDownloaded: Boolean,
225-
showCourseItem: Boolean,
226-
courseName: String,
227241
onlyVideoBlocks: Boolean,
228242
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
229243
saveDownloadModels: (blockId: String) -> Unit,
@@ -233,31 +247,25 @@ class DownloadDialogManager(
233247
coroutineScope.launch {
234248
val courseStructure = interactor.getCourseStructure(courseId, false)
235249
val downloadModelIds = interactor.getAllDownloadModels().map { it.id }
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-
)
250+
251+
val downloadDialogItems = subSectionsBlocks.mapNotNull { subSectionBlock ->
252+
val verticalBlocks =
253+
courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
254+
val blocks = verticalBlocks.flatMap { verticalBlock ->
255+
courseStructure.blockData.filter {
256+
it.id in verticalBlock.descendants &&
257+
(isBlocksDownloaded == (it.id in downloadModelIds)) &&
258+
(!onlyVideoBlocks || it.type == BlockType.VIDEO)
259+
}
245260
}
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
261+
val size = blocks.sumOf { it.getFileSize() }
262+
if (size > 0) {
263+
DownloadDialogItem(
264+
title = subSectionBlock.displayName,
265+
size = size
255266
)
256-
if (size > 0) {
257-
DownloadDialogItem(title = subSection.displayName, size = size)
258-
} else {
259-
null
260-
}
267+
} else {
268+
null
261269
}
262270
}
263271

@@ -269,33 +277,63 @@ class DownloadDialogManager(
269277
sizeSum = downloadDialogItems.sumOf { it.size },
270278
fragmentManager = fragmentManager,
271279
removeDownloadModels = {
272-
subSectionsBlocks.forEach { removeDownloadModels(it.id, courseId) }
273-
},
274-
saveDownloadModels = {
275-
subSectionsBlocks.forEach { saveDownloadModels(it.id) }
280+
subSectionsBlocks.forEach {
281+
removeDownloadModels(
282+
it.id,
283+
courseId
284+
)
285+
}
276286
},
287+
saveDownloadModels = { subSectionsBlocks.forEach { saveDownloadModels(it.id) } },
277288
onDismissClick = onDismissClick,
278289
onConfirmClick = onConfirmClick,
279290
)
280291
)
281292
}
282293
}
283294

284-
private fun calculateSubSectionSize(
285-
subSection: Block,
286-
courseStructure: CourseStructure,
287-
downloadModelIds: List<String>,
295+
private fun createCourseDownloadItems(
296+
coursePreview: DownloadCoursePreview,
297+
fragmentManager: FragmentManager,
288298
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-
}
299+
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
300+
saveDownloadModels: () -> Unit,
301+
onDismissClick: () -> Unit = {},
302+
onConfirmClick: () -> Unit = {},
303+
) {
304+
coroutineScope.launch {
305+
val downloadDialogItems = listOf(
306+
DownloadDialogItem(
307+
title = coursePreview.name,
308+
size = coursePreview.totalSize
309+
)
310+
)
311+
312+
uiState.emit(
313+
DownloadDialogUIState(
314+
downloadDialogItems = downloadDialogItems,
315+
isAllBlocksDownloaded = isBlocksDownloaded,
316+
isDownloadFailed = false,
317+
sizeSum = downloadDialogItems.sumOf { it.size },
318+
fragmentManager = fragmentManager,
319+
removeDownloadModels = {
320+
coroutineScope.launch {
321+
val downloadModels = interactor.getAllDownloadModels().filter {
322+
it.courseId == coursePreview.id
323+
}
324+
downloadModels.forEach {
325+
removeDownloadModels(
326+
it.id,
327+
coursePreview.id
328+
)
329+
}
330+
}
331+
},
332+
saveDownloadModels = saveDownloadModels,
333+
onDismissClick = onDismissClick,
334+
onConfirmClick = onConfirmClick,
335+
)
336+
)
298337
}
299-
return blocks.sumOf { it.getFileSize() }
300338
}
301339
}

0 commit comments

Comments
 (0)