Pass lambda to updateSection method

This commit is contained in:
Florian Renaud 2022-03-24 11:58:17 +01:00
parent a362d5427d
commit a97d3eae7e
2 changed files with 46 additions and 36 deletions

View File

@ -162,12 +162,12 @@ class RoomListFragment @Inject constructor(
actualBlock.section = actualBlock.section.copy( actualBlock.section = actualBlock.section.copy(
isExpanded = isRoomSectionExpanded isExpanded = isRoomSectionExpanded
) )
actualBlock.sectionHeaderAdapter.updateSection( actualBlock.sectionHeaderAdapter.updateSection {
actualBlock.sectionHeaderAdapter.roomsSectionData.copy( it.copy(
isExpanded = isRoomSectionExpanded, isExpanded = isRoomSectionExpanded,
shouldShowExpandedArrow = sectionsCount > 1 shouldShowExpandedArrow = sectionsCount > 1
) )
) }
} }
} }
@ -276,32 +276,34 @@ class RoomListFragment @Inject constructor(
val concatAdapter = ConcatAdapter() val concatAdapter = ConcatAdapter()
roomListViewModel.sections.forEach { section -> roomListViewModel.sections.forEach { section ->
val sectionAdapter = SectionHeaderAdapter { val sectionAdapter = SectionHeaderAdapter(SectionHeaderAdapter.RoomsSectionData(section.sectionName)) {
roomListViewModel.handle(RoomListAction.ToggleSection(section)) roomListViewModel.handle(RoomListAction.ToggleSection(section))
}.also {
it.updateSection(SectionHeaderAdapter.RoomsSectionData(section.sectionName))
} }
val contentAdapter = val contentAdapter =
when { when {
section.livePages != null -> { section.livePages != null -> {
pagedControllerFactory.createRoomSummaryPagedController() pagedControllerFactory.createRoomSummaryPagedController()
.also { controller -> .also { controller ->
section.livePages.observe(viewLifecycleOwner) { pl -> section.livePages.observe(viewLifecycleOwner) { pl ->
controller.submitList(pl) controller.submitList(pl)
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy( sectionAdapter.updateSection {
isHidden = pl.isEmpty(), it.copy(
isLoading = false isHidden = pl.isEmpty(),
)) isLoading = false
)
}
refreshCollapseStates() refreshCollapseStates()
checkEmptyState() checkEmptyState()
} }
observeItemCount(section, sectionAdapter) observeItemCount(section, sectionAdapter)
section.notificationCount.observe(viewLifecycleOwner) { counts -> section.notificationCount.observe(viewLifecycleOwner) { counts ->
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy( sectionAdapter.updateSection {
notificationCount = counts.totalCount, it.copy(
isHighlighted = counts.isHighlight, notificationCount = counts.totalCount,
)) isHighlighted = counts.isHighlight,
)
}
} }
section.isExpanded.observe(viewLifecycleOwner) { _ -> section.isExpanded.observe(viewLifecycleOwner) { _ ->
refreshCollapseStates() refreshCollapseStates()
@ -314,10 +316,12 @@ class RoomListFragment @Inject constructor(
.also { controller -> .also { controller ->
section.liveSuggested.observe(viewLifecycleOwner) { info -> section.liveSuggested.observe(viewLifecycleOwner) { info ->
controller.setData(info) controller.setData(info)
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy( sectionAdapter.updateSection {
isHidden = info.rooms.isEmpty(), it.copy(
isLoading = false isHidden = info.rooms.isEmpty(),
)) isLoading = false
)
}
refreshCollapseStates() refreshCollapseStates()
checkEmptyState() checkEmptyState()
} }
@ -333,19 +337,23 @@ class RoomListFragment @Inject constructor(
.also { controller -> .also { controller ->
section.liveList?.observe(viewLifecycleOwner) { list -> section.liveList?.observe(viewLifecycleOwner) { list ->
controller.setData(list) controller.setData(list)
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy( sectionAdapter.updateSection {
isHidden = list.isEmpty(), it.copy(
isLoading = false, isHidden = list.isEmpty(),
)) isLoading = false,
)
}
refreshCollapseStates() refreshCollapseStates()
checkEmptyState() checkEmptyState()
} }
observeItemCount(section, sectionAdapter) observeItemCount(section, sectionAdapter)
section.notificationCount.observe(viewLifecycleOwner) { counts -> section.notificationCount.observe(viewLifecycleOwner) { counts ->
sectionAdapter.updateSection(sectionAdapter.roomsSectionData.copy( sectionAdapter.updateSection {
notificationCount = counts.totalCount, it.copy(
isHighlighted = counts.isHighlight notificationCount = counts.totalCount,
)) isHighlighted = counts.isHighlight
)
}
} }
section.isExpanded.observe(viewLifecycleOwner) { _ -> section.isExpanded.observe(viewLifecycleOwner) { _ ->
refreshCollapseStates() refreshCollapseStates()
@ -393,9 +401,9 @@ class RoomListFragment @Inject constructor(
section.itemCount section.itemCount
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.collect { count -> .collect { count ->
sectionAdapter.updateSection( sectionAdapter.updateSection {
sectionAdapter.roomsSectionData.copy(itemCount = count) it.copy(itemCount = count)
) }
} }
} }
} }

View File

@ -29,6 +29,7 @@ import im.vector.app.databinding.ItemRoomCategoryBinding
import im.vector.app.features.themes.ThemeUtils import im.vector.app.features.themes.ThemeUtils
class SectionHeaderAdapter constructor( class SectionHeaderAdapter constructor(
roomsSectionData: RoomsSectionData,
private val onClickAction: ClickListener private val onClickAction: ClickListener
) : RecyclerView.Adapter<SectionHeaderAdapter.VH>() { ) : RecyclerView.Adapter<SectionHeaderAdapter.VH>() {
@ -44,11 +45,12 @@ class SectionHeaderAdapter constructor(
val shouldShowExpandedArrow: Boolean = false val shouldShowExpandedArrow: Boolean = false
) )
lateinit var roomsSectionData: RoomsSectionData var roomsSectionData: RoomsSectionData = roomsSectionData
private set private set
fun updateSection(newRoomsSectionData: RoomsSectionData) { fun updateSection(block: (RoomsSectionData) -> RoomsSectionData) {
if (!::roomsSectionData.isInitialized || newRoomsSectionData != roomsSectionData) { val newRoomsSectionData = block(roomsSectionData)
if (roomsSectionData != newRoomsSectionData) {
roomsSectionData = newRoomsSectionData roomsSectionData = newRoomsSectionData
notifyDataSetChanged() notifyDataSetChanged()
} }