From a97d3eae7ee19ab3c083bc457c7d5751c78f448c Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 24 Mar 2022 11:58:17 +0100 Subject: [PATCH] Pass lambda to updateSection method --- .../home/room/list/RoomListFragment.kt | 74 ++++++++++--------- .../home/room/list/SectionHeaderAdapter.kt | 8 +- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListFragment.kt index 9ed170f7c5..d92fc0403f 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListFragment.kt @@ -162,12 +162,12 @@ class RoomListFragment @Inject constructor( actualBlock.section = actualBlock.section.copy( isExpanded = isRoomSectionExpanded ) - actualBlock.sectionHeaderAdapter.updateSection( - actualBlock.sectionHeaderAdapter.roomsSectionData.copy( - isExpanded = isRoomSectionExpanded, - shouldShowExpandedArrow = sectionsCount > 1 - ) - ) + actualBlock.sectionHeaderAdapter.updateSection { + it.copy( + isExpanded = isRoomSectionExpanded, + shouldShowExpandedArrow = sectionsCount > 1 + ) + } } } @@ -276,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, - )) + sectionAdapter.updateSection { + it.copy( + notificationCount = counts.totalCount, + isHighlighted = counts.isHighlight, + ) + } } section.isExpanded.observe(viewLifecycleOwner) { _ -> refreshCollapseStates() @@ -314,10 +316,12 @@ 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() } @@ -333,19 +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, - )) + 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 - )) + sectionAdapter.updateSection { + it.copy( + notificationCount = counts.totalCount, + isHighlighted = counts.isHighlight + ) + } } section.isExpanded.observe(viewLifecycleOwner) { _ -> refreshCollapseStates() @@ -393,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) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/SectionHeaderAdapter.kt b/vector/src/main/java/im/vector/app/features/home/room/list/SectionHeaderAdapter.kt index cd2879cf28..0267151bd1 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/SectionHeaderAdapter.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/SectionHeaderAdapter.kt @@ -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() { @@ -44,11 +45,12 @@ class SectionHeaderAdapter constructor( val shouldShowExpandedArrow: Boolean = false ) - lateinit var roomsSectionData: RoomsSectionData + var roomsSectionData: RoomsSectionData = roomsSectionData 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() }