Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rooms section collapsing #5616

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ class RoomListFragment @Inject constructor(
}

private fun refreshCollapseStates() {
val sectionsCount = adapterInfosList.count { !it.sectionHeaderAdapter.roomsSectionData.isHidden }
roomListViewModel.sections.forEachIndexed { index, roomsSection ->
val actualBlock = adapterInfosList[index]
val isRoomSectionExpanded = roomsSection.isExpanded.value.orTrue()
val isRoomSectionExpanded = roomsSection.isExpanded.value.orTrue() || sectionsCount == 1
if (actualBlock.section.isExpanded && !isRoomSectionExpanded) {
// mark controller as collapsed
actualBlock.contentEpoxyController.setCollapsed(true)
Expand All @@ -161,9 +162,12 @@ class RoomListFragment @Inject constructor(
actualBlock.section = actualBlock.section.copy(
isExpanded = isRoomSectionExpanded
)
actualBlock.sectionHeaderAdapter.updateSection(
actualBlock.sectionHeaderAdapter.roomsSectionData.copy(isExpanded = isRoomSectionExpanded)
)
actualBlock.sectionHeaderAdapter.updateSection {
it.copy(
isExpanded = isRoomSectionExpanded,
shouldShowExpandedArrow = sectionsCount > 1
)
}
}
}

Expand Down Expand Up @@ -272,32 +276,34 @@ class RoomListFragment @Inject constructor(
val concatAdapter = ConcatAdapter()

roomListViewModel.sections.forEach { section ->
val sectionAdapter = SectionHeaderAdapter {
val sectionAdapter = SectionHeaderAdapter(SectionHeaderAdapter.RoomsSectionData(section.sectionName)) {
roomListViewModel.handle(RoomListAction.ToggleSection(section))
}.also {
it.updateSection(SectionHeaderAdapter.RoomsSectionData(section.sectionName))
}

val contentAdapter =
when {
section.livePages != null -> {
section.livePages != null -> {
pagedControllerFactory.createRoomSummaryPagedController()
.also { controller ->
section.livePages.observe(viewLifecycleOwner) { pl ->
controller.submitList(pl)
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy(
isHidden = pl.isEmpty(),
isLoading = false
))
sectionAdapter.updateSection {
it.copy(
isHidden = pl.isEmpty(),
isLoading = false
)
}
refreshCollapseStates()
checkEmptyState()
}
observeItemCount(section, sectionAdapter)
section.notificationCount.observe(viewLifecycleOwner) { counts ->
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy(
notificationCount = counts.totalCount,
isHighlighted = counts.isHighlight,
shouldShowExpandedArrow = shouldShowExpendedArrow()
))
sectionAdapter.updateSection {
it.copy(
notificationCount = counts.totalCount,
isHighlighted = counts.isHighlight,
)
}
}
section.isExpanded.observe(viewLifecycleOwner) { _ ->
refreshCollapseStates()
Expand All @@ -310,10 +316,13 @@ class RoomListFragment @Inject constructor(
.also { controller ->
section.liveSuggested.observe(viewLifecycleOwner) { info ->
controller.setData(info)
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy(
isHidden = info.rooms.isEmpty(),
isLoading = false
))
sectionAdapter.updateSection {
it.copy(
isHidden = info.rooms.isEmpty(),
isLoading = false
)
}
refreshCollapseStates()
checkEmptyState()
}
observeItemCount(section, sectionAdapter)
Expand All @@ -328,20 +337,23 @@ class RoomListFragment @Inject constructor(
.also { controller ->
section.liveList?.observe(viewLifecycleOwner) { list ->
controller.setData(list)
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy(
isHidden = list.isEmpty(),
isLoading = false,
shouldShowExpandedArrow = shouldShowExpendedArrow()
))
sectionAdapter.updateSection {
it.copy(
isHidden = list.isEmpty(),
isLoading = false,
)
}
refreshCollapseStates()
checkEmptyState()
}
observeItemCount(section, sectionAdapter)
section.notificationCount.observe(viewLifecycleOwner) { counts ->
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy(
notificationCount = counts.totalCount,
isHighlighted = counts.isHighlight,
shouldShowExpandedArrow = shouldShowExpendedArrow()
))
sectionAdapter.updateSection {
it.copy(
notificationCount = counts.totalCount,
isHighlighted = counts.isHighlight
)
}
}
section.isExpanded.observe(viewLifecycleOwner) { _ ->
refreshCollapseStates()
Expand Down Expand Up @@ -389,9 +401,9 @@ class RoomListFragment @Inject constructor(
section.itemCount
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.collect { count ->
sectionAdapter.updateSection(
sectionAdapter.roomsSectionData.copy(itemCount = count)
)
sectionAdapter.updateSection {
it.copy(itemCount = count)
}
}
}
}
Expand Down Expand Up @@ -448,10 +460,6 @@ class RoomListFragment @Inject constructor(
footerController.setData(state)
}

private fun shouldShowExpendedArrow(): Boolean {
return adapterInfosList.filter { !it.sectionHeaderAdapter.roomsSectionData.isHidden }.size >= 2
}

private fun checkEmptyState() {
val shouldShowEmpty = adapterInfosList.all { it.sectionHeaderAdapter.roomsSectionData.isHidden } &&
!adapterInfosList.any { it.sectionHeaderAdapter.roomsSectionData.isLoading }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import im.vector.app.databinding.ItemRoomCategoryBinding
import im.vector.app.features.themes.ThemeUtils

class SectionHeaderAdapter constructor(
roomsSectionData: RoomsSectionData,
private val onClickAction: ClickListener
) : RecyclerView.Adapter<SectionHeaderAdapter.VH>() {

Expand All @@ -44,11 +45,12 @@ class SectionHeaderAdapter constructor(
val shouldShowExpandedArrow: Boolean = false
)

lateinit var roomsSectionData: RoomsSectionData
var roomsSectionData: RoomsSectionData = roomsSectionData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange line of code 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah it's using the value from the constructor. A bit confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is to prevent the lateinit declaration of the attribute and the initialization check of the attribute which was previously done in the updateSection method. I force passing the value as a constructor param and I init the attribute with that value. Previously, the attribute was initialized directly after creating the class object. I could set a different name to the param for clarity.

private set

fun updateSection(newRoomsSectionData: RoomsSectionData) {
if (!::roomsSectionData.isInitialized || newRoomsSectionData != roomsSectionData) {
fun updateSection(block: (RoomsSectionData) -> RoomsSectionData) {
val newRoomsSectionData = block(roomsSectionData)
if (roomsSectionData != newRoomsSectionData) {
roomsSectionData = newRoomsSectionData
notifyDataSetChanged()
}
Expand Down