Refactor sections as enum types

This commit is contained in:
Andy Uhnak
2022-04-04 09:24:22 +01:00
parent 6cde2f4461
commit 3f34fd655a
10 changed files with 454 additions and 296 deletions
@@ -26,8 +26,6 @@
@interface UnifiedSearchRecentsDataSource()
{
NSInteger searchedRoomIdOrAliasSection; // used to display the potential room id or alias typed during search.
// The potential room id or alias typed in search input.
NSString *roomIdOrAlias;
}
@@ -40,13 +38,36 @@
self = [super initWithMatrixSession:mxSession recentsListService:recentsListService];
if (self)
{
searchedRoomIdOrAliasSection = -1;
_hideRecents = NO;
}
return self;
}
#pragma mark - Sections
- (RecentsDataSourceSections *)makeDataSourceSections
{
NSMutableArray *types = [NSMutableArray array];
if (roomIdOrAlias.length)
{
// The current search pattern corresponds to a valid room id or room alias
[types addObject:@(RecentsDataSourceSectionTypeSearchedRoom)];
}
// The public rooms directory cell is then visible whatever the search activity.
if (RiotSettings.shared.unifiedSearchScreenShowPublicDirectory)
{
[types addObject:@(RecentsDataSourceSectionTypeDirectory)];
}
if (!_hideRecents) {
NSArray *existingTypes = [[super makeDataSourceSections] sectionTypes];
[types addObjectsFromArray:existingTypes];
}
return [[RecentsDataSourceSections alloc] initWithSectionTypes:types.copy];
}
#pragma mark -
- (void)setPublicRoomsDirectoryDataSource:(PublicRoomsDirectoryDataSource *)publicRoomsDirectoryDataSource
@@ -69,74 +90,16 @@
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger sectionsCount = 0;
// Check whether all data sources are ready before rendering recents
if (self.state == MXKDataSourceStateReady)
{
sectionsCount = [super numberOfSectionsInTableView:tableView];
NSInteger sectionsOffset = 0;
if (roomIdOrAlias.length)
{
// The current search pattern corresponds to a valid room id or room alias
searchedRoomIdOrAliasSection = sectionsOffset++;
}
// The public rooms directory cell is then visible whatever the search activity.
if (RiotSettings.shared.unifiedSearchScreenShowPublicDirectory)
{
self.directorySection = sectionsOffset++;
}
if (_hideRecents)
{
self.invitesSection = self.favoritesSection = self.peopleSection = self.conversationSection = self.lowPrioritySection = self.serverNoticeSection = -1;
sectionsCount = sectionsOffset;
}
else
{
if (self.invitesSection != -1)
{
self.invitesSection += sectionsOffset;
}
if (self.favoritesSection != -1)
{
self.favoritesSection += sectionsOffset;
}
if (self.peopleSection != -1)
{
self.peopleSection += sectionsOffset;
}
if (self.conversationSection != -1)
{
self.conversationSection += sectionsOffset;
}
if (self.lowPrioritySection != -1)
{
self.lowPrioritySection += sectionsOffset;
}
if (self.serverNoticeSection != -1)
{
self.serverNoticeSection += sectionsOffset;
}
sectionsCount += sectionsOffset;
}
}
return sectionsCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = 0;
if (section == searchedRoomIdOrAliasSection)
RecentsDataSourceSectionType sectionType = [self.sections sectionTypeForSectionIndex:section];
if (sectionType == RecentsDataSourceSectionTypeSearchedRoom)
{
count = 1;
}
else if (section == self.directorySection)
else if (sectionType == RecentsDataSourceSectionTypeDirectory)
{
count = 1;
}
@@ -152,7 +115,7 @@
{
UIView *sectionHeader = nil;
if (section != searchedRoomIdOrAliasSection)
if ([self.sections sectionTypeForSectionIndex:section] != RecentsDataSourceSectionTypeSearchedRoom)
{
sectionHeader = [super viewForHeaderInSection:section withFrame:frame inTableView:tableView];
}
@@ -162,7 +125,8 @@
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == searchedRoomIdOrAliasSection)
RecentsDataSourceSectionType sectionType = [self.sections sectionTypeForSectionIndex:indexPath.section];
if (sectionType == RecentsDataSourceSectionTypeSearchedRoom)
{
RoomIdOrAliasTableViewCell *roomIdOrAliasCell = [tableView dequeueReusableCellWithIdentifier:RoomIdOrAliasTableViewCell.defaultReuseIdentifier];
if (!roomIdOrAliasCell)
@@ -174,7 +138,7 @@
return roomIdOrAliasCell;
}
else if (indexPath.section == self.directorySection)
else if (sectionType == RecentsDataSourceSectionTypeDirectory)
{
// For the cell showing the public rooms directory search result,
// skip the MatrixKit mechanism and return directly the UITableViewCell
@@ -194,12 +158,13 @@
- (CGFloat)cellHeightAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == searchedRoomIdOrAliasSection)
RecentsDataSourceSectionType sessionType = [self.sections sectionTypeForSectionIndex:indexPath.section];
if (sessionType == RecentsDataSourceSectionTypeSearchedRoom)
{
return RoomIdOrAliasTableViewCell.cellHeight;
}
if (indexPath.section == self.directorySection)
if (sessionType == RecentsDataSourceSectionTypeDirectory)
{
// For the cell showing the public rooms directory search result,
// skip the MatrixKit mechanism and return directly the cell height