From e493bb64cd07d10de9c0a0f37c989f76df67c8a3 Mon Sep 17 00:00:00 2001 From: PavloNetrebchuk Date: Wed, 4 Oct 2023 12:50:52 +0300 Subject: [PATCH] Set CourseSubsectionItem icon by block type --- .../src/main/java/org/openedx/core/BlockType.kt | 17 ++++++++++++++++- .../java/org/openedx/core/data/model/Block.kt | 16 ++++++++++++++-- .../core/data/model/CourseStructureModel.kt | 2 +- .../org/openedx/core/data/model/room/BlockDb.kt | 14 +++++++++++++- .../data/model/room/CourseStructureEntity.kt | 2 +- .../java/org/openedx/core/domain/model/Block.kt | 1 + .../outline/CourseOutlineFragment.kt | 2 ++ .../section/CourseSectionFragment.kt | 3 ++- .../openedx/course/presentation/ui/CourseUI.kt | 1 + .../presentation/videos/CourseVideosFragment.kt | 2 ++ .../src/main/res/drawable/ic_course_block.xml | 12 ++++++------ .../outline/CourseOutlineViewModelTest.kt | 3 +++ .../section/CourseSectionViewModelTest.kt | 3 +++ .../CourseUnitContainerViewModelTest.kt | 4 ++++ .../videos/CourseVideoViewModelTest.kt | 3 +++ 15 files changed, 72 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/openedx/core/BlockType.kt b/core/src/main/java/org/openedx/core/BlockType.kt index dc1862395..9edfbdafd 100644 --- a/core/src/main/java/org/openedx/core/BlockType.kt +++ b/core/src/main/java/org/openedx/core/BlockType.kt @@ -26,9 +26,24 @@ enum class BlockType { return try { BlockType.valueOf(actualType.uppercase()) } catch (e : Exception){ - BlockType.OTHERS + OTHERS } } + + fun sortByPriority(blockTypes: List): List { + val priorityMap = mapOf( + PROBLEM to 1, + VIDEO to 2, + DISCUSSION to 3, + HTML to 4 + ) + val comparator = Comparator { blockType1, blockType2 -> + val priority1 = priorityMap[blockType1] ?: Int.MAX_VALUE + val priority2 = priorityMap[blockType2] ?: Int.MAX_VALUE + priority1 - priority2 + } + return blockTypes.sortedWith(comparator) + } } } diff --git a/core/src/main/java/org/openedx/core/data/model/Block.kt b/core/src/main/java/org/openedx/core/data/model/Block.kt index 4ddc549a0..ea1ea3507 100644 --- a/core/src/main/java/org/openedx/core/data/model/Block.kt +++ b/core/src/main/java/org/openedx/core/data/model/Block.kt @@ -32,16 +32,28 @@ data class Block( @SerializedName("completion") val completion: Double? ) { - fun mapToDomain(): Block { + fun mapToDomain(blockData: Map): Block { + val blockType = BlockType.getBlockType(type ?: "") + val descendantsType = if (blockType == BlockType.VERTICAL) { + val types = descendants?.map { descendant -> + BlockType.getBlockType(blockData[descendant]?.type ?: "") + } ?: emptyList() + val sortedBlockTypes = BlockType.sortByPriority(types) + sortedBlockTypes.firstOrNull() ?: blockType + } else { + blockType + } + return org.openedx.core.domain.model.Block( id = id ?: "", blockId = blockId ?: "", lmsWebUrl = lmsWebUrl ?: "", legacyWebUrl = legacyWebUrl ?: "", studentViewUrl = studentViewUrl ?: "", - type = BlockType.getBlockType(type ?: ""), + type = blockType, displayName = displayName ?: "", descendants = descendants ?: emptyList(), + descendantsType = descendantsType, graded = graded ?: false, studentViewData = studentViewData?.mapToDomain(), studentViewMultiDevice = studentViewMultiDevice ?: false, diff --git a/core/src/main/java/org/openedx/core/data/model/CourseStructureModel.kt b/core/src/main/java/org/openedx/core/data/model/CourseStructureModel.kt index ac2552c26..004061305 100644 --- a/core/src/main/java/org/openedx/core/data/model/CourseStructureModel.kt +++ b/core/src/main/java/org/openedx/core/data/model/CourseStructureModel.kt @@ -41,7 +41,7 @@ data class CourseStructureModel( return CourseStructure( root = root, blockData = blockData.map { - it.value.mapToDomain() + it.value.mapToDomain(blockData) }, id = id ?: "", name = name ?: "", diff --git a/core/src/main/java/org/openedx/core/data/model/room/BlockDb.kt b/core/src/main/java/org/openedx/core/data/model/room/BlockDb.kt index 26d83e81e..5774729f2 100644 --- a/core/src/main/java/org/openedx/core/data/model/room/BlockDb.kt +++ b/core/src/main/java/org/openedx/core/data/model/room/BlockDb.kt @@ -33,7 +33,18 @@ data class BlockDb( @ColumnInfo("completion") val completion: Double ) { - fun mapToDomain(): Block { + fun mapToDomain(blocks: List): Block { + val blockType = BlockType.getBlockType(type) + val descendantsType = if (blockType == BlockType.VERTICAL) { + val types = descendants.map { descendant -> + BlockType.getBlockType(blocks.find { it.id == descendant }?.type ?: "") + } + val sortedBlockTypes = BlockType.sortByPriority(types) + sortedBlockTypes.firstOrNull() ?: blockType + } else { + blockType + } + return Block( id = id, blockId = blockId, @@ -47,6 +58,7 @@ data class BlockDb( studentViewMultiDevice = studentViewMultiDevice, blockCounts = blockCounts.mapToDomain(), descendants = descendants, + descendantsType = descendantsType, completion = completion, ) } diff --git a/core/src/main/java/org/openedx/core/data/model/room/CourseStructureEntity.kt b/core/src/main/java/org/openedx/core/data/model/room/CourseStructureEntity.kt index 3c0ac045b..72d793ef5 100644 --- a/core/src/main/java/org/openedx/core/data/model/room/CourseStructureEntity.kt +++ b/core/src/main/java/org/openedx/core/data/model/room/CourseStructureEntity.kt @@ -45,7 +45,7 @@ data class CourseStructureEntity( fun mapToDomain(): CourseStructure { return CourseStructure( root, - blocks.map { it.mapToDomain() }, + blocks.map { it.mapToDomain(blocks) }, id, name, number, diff --git a/core/src/main/java/org/openedx/core/domain/model/Block.kt b/core/src/main/java/org/openedx/core/domain/model/Block.kt index 4e384ff5b..36163e1c1 100644 --- a/core/src/main/java/org/openedx/core/domain/model/Block.kt +++ b/core/src/main/java/org/openedx/core/domain/model/Block.kt @@ -22,6 +22,7 @@ data class Block( val studentViewMultiDevice: Boolean, val blockCounts: BlockCounts, val descendants: List, + val descendantsType: BlockType, val completion: Double, val downloadModel: DownloadModel? = null ) { diff --git a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineFragment.kt b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineFragment.kt index b0e2586a5..cb14aaa47 100644 --- a/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineFragment.kt +++ b/course/src/main/java/org/openedx/course/presentation/outline/CourseOutlineFragment.kt @@ -541,6 +541,7 @@ private val mockChapterBlock = Block( studentViewMultiDevice = false, blockCounts = BlockCounts(1), descendants = emptyList(), + descendantsType = BlockType.CHAPTER, completion = 0.0 ) private val mockSequentialBlock = Block( @@ -556,6 +557,7 @@ private val mockSequentialBlock = Block( studentViewMultiDevice = false, blockCounts = BlockCounts(1), descendants = emptyList(), + descendantsType = BlockType.CHAPTER, completion = 0.0 ) diff --git a/course/src/main/java/org/openedx/course/presentation/section/CourseSectionFragment.kt b/course/src/main/java/org/openedx/course/presentation/section/CourseSectionFragment.kt index ed68c0d85..7fbd8dfb8 100644 --- a/course/src/main/java/org/openedx/course/presentation/section/CourseSectionFragment.kt +++ b/course/src/main/java/org/openedx/course/presentation/section/CourseSectionFragment.kt @@ -373,7 +373,7 @@ private fun CourseSubsectionItem( } private fun getUnitBlockIcon(block: Block): Int { - return when (block.type) { + return when (block.descendantsType) { BlockType.VIDEO -> R.drawable.ic_course_video BlockType.PROBLEM -> R.drawable.ic_course_pen BlockType.DISCUSSION -> R.drawable.ic_course_discussion @@ -447,5 +447,6 @@ private val mockBlock = Block( studentViewMultiDevice = false, blockCounts = BlockCounts(0), descendants = emptyList(), + descendantsType = BlockType.HTML, completion = 0.0 ) \ No newline at end of file diff --git a/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt b/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt index ea5d4fdee..d02557a81 100644 --- a/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt +++ b/course/src/main/java/org/openedx/course/presentation/ui/CourseUI.kt @@ -734,5 +734,6 @@ private val mockChapterBlock = Block( studentViewMultiDevice = false, blockCounts = BlockCounts(1), descendants = emptyList(), + descendantsType = BlockType.CHAPTER, completion = 0.0 ) \ No newline at end of file diff --git a/course/src/main/java/org/openedx/course/presentation/videos/CourseVideosFragment.kt b/course/src/main/java/org/openedx/course/presentation/videos/CourseVideosFragment.kt index 854ba9cbc..83faabd68 100644 --- a/course/src/main/java/org/openedx/course/presentation/videos/CourseVideosFragment.kt +++ b/course/src/main/java/org/openedx/course/presentation/videos/CourseVideosFragment.kt @@ -418,6 +418,7 @@ private val mockChapterBlock = Block( studentViewMultiDevice = false, blockCounts = BlockCounts(1), descendants = emptyList(), + descendantsType = BlockType.CHAPTER, completion = 0.0 ) @@ -434,6 +435,7 @@ private val mockSequentialBlock = Block( studentViewMultiDevice = false, blockCounts = BlockCounts(1), descendants = emptyList(), + descendantsType = BlockType.SEQUENTIAL, completion = 0.0 ) diff --git a/course/src/main/res/drawable/ic_course_block.xml b/course/src/main/res/drawable/ic_course_block.xml index 441f42562..9445f48f0 100644 --- a/course/src/main/res/drawable/ic_course_block.xml +++ b/course/src/main/res/drawable/ic_course_block.xml @@ -7,42 +7,42 @@