Skip to content

Commit

Permalink
Set CourseSubsectionItem icon by block type
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloNetrebchuk committed Oct 4, 2023
1 parent 3362a2c commit e493bb6
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 13 deletions.
17 changes: 16 additions & 1 deletion core/src/main/java/org/openedx/core/BlockType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@ enum class BlockType {
return try {
BlockType.valueOf(actualType.uppercase())
} catch (e : Exception){
BlockType.OTHERS
OTHERS
}
}

fun sortByPriority(blockTypes: List<BlockType>): List<BlockType> {
val priorityMap = mapOf(
PROBLEM to 1,
VIDEO to 2,
DISCUSSION to 3,
HTML to 4
)
val comparator = Comparator<BlockType> { blockType1, blockType2 ->
val priority1 = priorityMap[blockType1] ?: Int.MAX_VALUE
val priority2 = priorityMap[blockType2] ?: Int.MAX_VALUE
priority1 - priority2
}
return blockTypes.sortedWith(comparator)
}
}
}

16 changes: 14 additions & 2 deletions core/src/main/java/org/openedx/core/data/model/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@ data class Block(
@SerializedName("completion")
val completion: Double?
) {
fun mapToDomain(): Block {
fun mapToDomain(blockData: Map<String, org.openedx.core.data.model.Block>): 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?: "",
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/org/openedx/core/data/model/room/BlockDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ data class BlockDb(
@ColumnInfo("completion")
val completion: Double
) {
fun mapToDomain(): Block {
fun mapToDomain(blocks: List<BlockDb>): 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,
Expand All @@ -47,6 +58,7 @@ data class BlockDb(
studentViewMultiDevice = studentViewMultiDevice,
blockCounts = blockCounts.mapToDomain(),
descendants = descendants,
descendantsType = descendantsType,
completion = completion,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/openedx/core/domain/model/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data class Block(
val studentViewMultiDevice: Boolean,
val blockCounts: BlockCounts,
val descendants: List<String>,
val descendantsType: BlockType,
val completion: Double,
val downloadModel: DownloadModel? = null
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -556,6 +557,7 @@ private val mockSequentialBlock = Block(
studentViewMultiDevice = false,
blockCounts = BlockCounts(1),
descendants = emptyList(),
descendantsType = BlockType.CHAPTER,
completion = 0.0
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -447,5 +447,6 @@ private val mockBlock = Block(
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = emptyList(),
descendantsType = BlockType.HTML,
completion = 0.0
)
Original file line number Diff line number Diff line change
Expand Up @@ -734,5 +734,6 @@ private val mockChapterBlock = Block(
studentViewMultiDevice = false,
blockCounts = BlockCounts(1),
descendants = emptyList(),
descendantsType = BlockType.CHAPTER,
completion = 0.0
)
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ private val mockChapterBlock = Block(
studentViewMultiDevice = false,
blockCounts = BlockCounts(1),
descendants = emptyList(),
descendantsType = BlockType.CHAPTER,
completion = 0.0
)

Expand All @@ -434,6 +435,7 @@ private val mockSequentialBlock = Block(
studentViewMultiDevice = false,
blockCounts = BlockCounts(1),
descendants = emptyList(),
descendantsType = BlockType.SEQUENTIAL,
completion = 0.0
)

Expand Down
12 changes: 6 additions & 6 deletions course/src/main/res/drawable/ic_course_block.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@
<clip-path
android:pathData="M0,0h24v24h-24z"/>
<path
android:pathData="M9,6H20"
android:pathData="M6,15H21"
android:strokeLineJoin="round"
android:strokeWidth="1.75"
android:fillColor="#00000000"
android:strokeColor="#ffffff"
android:strokeLineCap="round"/>
<path
android:pathData="M9,12H20"
android:pathData="M21,19H6"
android:strokeLineJoin="round"
android:strokeWidth="1.75"
android:fillColor="#00000000"
android:strokeColor="#ffffff"
android:strokeLineCap="round"/>
<path
android:pathData="M9,18H20"
android:pathData="M15,11H21"
android:strokeLineJoin="round"
android:strokeWidth="1.75"
android:fillColor="#00000000"
android:strokeColor="#ffffff"
android:strokeLineCap="round"/>
<path
android:pathData="M5,6V6.01"
android:pathData="M21,7H15"
android:strokeLineJoin="round"
android:strokeWidth="1.75"
android:fillColor="#00000000"
android:strokeColor="#ffffff"
android:strokeLineCap="round"/>
<path
android:pathData="M5,12V12.01"
android:pathData="M9,9H10C10.198,9 10.391,9.059 10.556,9.169C10.72,9.278 10.848,9.435 10.924,9.617C11,9.8 11.019,10.001 10.981,10.195C10.942,10.389 10.847,10.567 10.707,10.707C10.567,10.847 10.389,10.942 10.195,10.981C10.001,11.019 9.8,11 9.617,10.924C9.435,10.848 9.278,10.72 9.169,10.556C9.059,10.391 9,10.198 9,10V7.5C9,6.97 9.211,6.461 9.586,6.086C9.961,5.711 10.47,5.5 11,5.5"
android:strokeLineJoin="round"
android:strokeWidth="1.75"
android:fillColor="#00000000"
android:strokeColor="#ffffff"
android:strokeLineCap="round"/>
<path
android:pathData="M5,18V18.01"
android:pathData="M3,9H4C4.198,9 4.391,9.059 4.556,9.169C4.72,9.278 4.848,9.435 4.924,9.617C5,9.8 5.019,10.001 4.981,10.195C4.942,10.389 4.847,10.567 4.707,10.707C4.567,10.847 4.389,10.942 4.195,10.981C4.001,11.019 3.8,11 3.617,10.924C3.435,10.848 3.278,10.72 3.169,10.556C3.059,10.391 3,10.198 3,10V7.5C3,6.97 3.211,6.461 3.586,6.086C3.961,5.711 4.47,5.5 5,5.5"
android:strokeLineJoin="round"
android:strokeWidth="1.75"
android:fillColor="#00000000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class CourseOutlineViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("1", "id1"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -82,6 +83,7 @@ class CourseOutlineViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("id2"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -97,6 +99,7 @@ class CourseOutlineViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = emptyList(),
descendantsType = BlockType.HTML,
completion = 0.0
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CourseSectionViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("1", "id1"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -89,6 +90,7 @@ class CourseSectionViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("id2"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -104,6 +106,7 @@ class CourseSectionViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = emptyList(),
descendantsType = BlockType.HTML,
completion = 0.0
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class CourseUnitContainerViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("id2", "id1"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -65,6 +66,7 @@ class CourseUnitContainerViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("id2", "id"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -80,6 +82,7 @@ class CourseUnitContainerViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = emptyList(),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -95,6 +98,7 @@ class CourseUnitContainerViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = emptyList(),
descendantsType = BlockType.HTML,
completion = 0.0
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CourseVideoViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("1", "id1"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -78,6 +79,7 @@ class CourseVideoViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = listOf("id2"),
descendantsType = BlockType.HTML,
completion = 0.0
),
Block(
Expand All @@ -93,6 +95,7 @@ class CourseVideoViewModelTest {
studentViewMultiDevice = false,
blockCounts = BlockCounts(0),
descendants = emptyList(),
descendantsType = BlockType.HTML,
completion = 0.0
)
)
Expand Down

0 comments on commit e493bb6

Please sign in to comment.