diff --git a/Riot/Modules/Settings/SettingsViewController.m b/Riot/Modules/Settings/SettingsViewController.m index 3b9157c9b..759668ad8 100644 --- a/Riot/Modules/Settings/SettingsViewController.m +++ b/Riot/Modules/Settings/SettingsViewController.m @@ -42,9 +42,6 @@ #import "GroupsDataSource.h" #import "GroupTableViewCellWithSwitch.h" -#import "Row.h" -#import "Section.h" - #import "GBDeviceInfo_iOS.h" #import "Riot-Swift.h" @@ -253,7 +250,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> @property (nonatomic, strong) SecureBackupSetupCoordinatorBridgePresenter *secureBackupSetupCoordinatorBridgePresenter; @property (nonatomic, strong) AuthenticatedSessionViewControllerFactory *authenticatedSessionViewControllerFactory; -@property (nonatomic, strong) NSArray *sections; +@property (nonatomic, strong) TableViewSections *tableViewSections; @end @@ -470,78 +467,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> } // update sections - self.sections = tmpSections; -} - -- (void)setSections:(NSArray
*)sections -{ - _sections = sections; - - // reload table - [self.tableView reloadData]; -} - -/// Returns index of section for the given tag. If cannot find, return `NSNotFound` -/// @param tag Tag for section -- (NSInteger)indexOfSectionForTag:(NSInteger)tag -{ - return [_sections indexOfObjectPassingTest:^BOOL(Section * _Nonnull section, NSUInteger idx, BOOL * _Nonnull stop) { - return section.tag == tag; - }]; -} - -/// Finds the exact indexpath for the given row and section tag. If cannot find, returns nil -/// @param rowTag Tag for row -/// @param sectionTag Tag for section -- (NSIndexPath *)exactIndexPathForRowTag:(NSInteger)rowTag sectionTag:(NSInteger)sectionTag -{ - NSInteger sectionIndex = [self indexOfSectionForTag:sectionTag]; - if (sectionIndex != NSNotFound) - { - Section *section = _sections[sectionIndex]; - NSInteger rowIndex = [section indexOfRowForTag:rowTag]; - if (rowIndex != NSNotFound) - { - return [NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex]; - } - } - return nil; -} - -/// Finds the nearest next indexPath for given row tag and section tag. If the section finishes, also checks for the next section. If cannot find any row available, returns nil. -/// @param rowTag Tag for row -/// @param sectionTag Tag for section -- (NSIndexPath *)nearestIndexPathForRowTag:(NSInteger)rowTag sectionTag:(NSInteger)sectionTag -{ - NSInteger sectionIndex = [self indexOfSectionForTag:sectionTag]; - if (sectionIndex != NSNotFound) - { - Section *section = _sections[sectionIndex]; - NSInteger rowIndex = [section indexOfRowForTag:rowTag]; - if (rowIndex != NSNotFound) - { - // exact row found, return it - return [NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex]; - } - else if (rowTag + 1 < section.rows.count) - { - // try to return next row in the same section - return [self nearestIndexPathForRowTag:rowTag + 1 sectionTag:sectionTag]; - } - else if (sectionTag + 1 < _sections.count) - { - // try to return the first row of the next section - return [self nearestIndexPathForRowTag:0 sectionTag:sectionTag + 1]; - } - - return nil; - } - else if (sectionTag + 1 < _sections.count) - { - // try to return the first row of the next section - return [self nearestIndexPathForRowTag:0 sectionTag:sectionTag + 1]; - } - return nil; + _tableViewSections.sections = tmpSections; } - (void)viewDidLoad @@ -624,6 +550,8 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> self.signOutAlertPresenter = [SignOutAlertPresenter new]; self.signOutAlertPresenter.delegate = self; + _tableViewSections = [TableViewSections new]; + _tableViewSections.delegate = self; [self updateSections]; } @@ -881,7 +809,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> [self.tableView beginUpdates]; // Refresh the corresponding table view cell with animation - NSIndexPath *addEmailIndexPath = [self exactIndexPathForRowTag:USER_SETTINGS_ADD_EMAIL_INDEX + NSIndexPath *addEmailIndexPath = [_tableViewSections exactIndexPathForRowTag:USER_SETTINGS_ADD_EMAIL_INDEX sectionTag:SECTION_TAG_USER_SETTINGS]; if (addEmailIndexPath) { @@ -912,7 +840,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> [self.tableView beginUpdates]; // Refresh the corresponding table view cell with animation - NSIndexPath *addPhoneIndexPath = [self exactIndexPathForRowTag:USER_SETTINGS_ADD_PHONENUMBER_INDEX + NSIndexPath *addPhoneIndexPath = [_tableViewSections exactIndexPathForRowTag:USER_SETTINGS_ADD_PHONENUMBER_INDEX sectionTag:SECTION_TAG_USER_SETTINGS]; if (addPhoneIndexPath) { @@ -1400,16 +1328,12 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> // update the save button if there is an update [self updateSaveButtonStatus]; - return _sections.count; + return _tableViewSections.sections.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - Section *sectionObject = nil; - if (section < _sections.count) - { - sectionObject = _sections[section]; - } + Section *sectionObject = [_tableViewSections sectionAtIndex:section]; return sectionObject.rows.count; } @@ -1496,19 +1420,9 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - Section *sectionObj = nil; - NSInteger section = NSNotFound; - NSInteger row = NSNotFound; - - if (indexPath.section < _sections.count) - { - sectionObj = _sections[indexPath.section]; - section = sectionObj.tag; - if (indexPath.row < sectionObj.rows.count) - { - row = sectionObj.rows[indexPath.row].tag; - } - } + NSIndexPath *tagsIndexPath = [_tableViewSections tagsIndexPathFromTableViewIndexPath:indexPath]; + NSInteger section = tagsIndexPath.section; + NSInteger row = tagsIndexPath.row; // set the cell to a default value to avoid application crashes UITableViewCell *cell = [[UITableViewCell alloc] init]; @@ -2369,11 +2283,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { - Section *sectionObj = nil; - if (section < _sections.count) - { - sectionObj = _sections[section]; - } + Section *sectionObj = [_tableViewSections sectionAtIndex:section]; return sectionObj.headerTitle; } @@ -2390,19 +2300,9 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { - Section *sectionObj = nil; - NSInteger section = NSNotFound; - NSInteger row = NSNotFound; - - if (indexPath.section < _sections.count) - { - sectionObj = _sections[indexPath.section]; - section = sectionObj.tag; - if (indexPath.row < sectionObj.rows.count) - { - row = sectionObj.rows[indexPath.row].tag; - } - } + NSIndexPath *tagsIndexPath = [_tableViewSections tagsIndexPathFromTableViewIndexPath:indexPath]; + NSInteger section = tagsIndexPath.section; + NSInteger row = tagsIndexPath.row; if (section == SECTION_TAG_USER_SETTINGS) { @@ -2456,19 +2356,9 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { - Section *sectionObj = nil; - NSInteger section = NSNotFound; - NSInteger row = NSNotFound; - - if (indexPath.section < _sections.count) - { - sectionObj = _sections[indexPath.section]; - section = sectionObj.tag; - if (indexPath.row < sectionObj.rows.count) - { - row = sectionObj.rows[indexPath.row].tag; - } - } + NSIndexPath *tagsIndexPath = [_tableViewSections tagsIndexPathFromTableViewIndexPath:indexPath]; + NSInteger section = tagsIndexPath.section; + NSInteger row = tagsIndexPath.row; NSMutableArray* actions; @@ -2501,19 +2391,9 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> { if (self.tableView == tableView) { - Section *sectionObj = nil; - NSInteger section = NSNotFound; - NSInteger row = NSNotFound; - - if (indexPath.section < _sections.count) - { - sectionObj = _sections[indexPath.section]; - section = sectionObj.tag; - if (indexPath.row < sectionObj.rows.count) - { - row = sectionObj.rows[indexPath.row].tag; - } - } + NSIndexPath *tagsIndexPath = [_tableViewSections tagsIndexPathFromTableViewIndexPath:indexPath]; + NSInteger section = tagsIndexPath.section; + NSInteger row = tagsIndexPath.row; if (section == SECTION_TAG_USER_INTERFACE) { @@ -2535,7 +2415,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> // settingsDiscoveryTableViewSection is a dynamic section, so check number of rows before scroll to avoid crashes if (self.settingsDiscoveryTableViewSection.numberOfRows > 0) { - NSIndexPath *discoveryIndexPath = [self exactIndexPathForRowTag:0 sectionTag:SECTION_TAG_DISCOVERY]; + NSIndexPath *discoveryIndexPath = [_tableViewSections exactIndexPathForRowTag:0 sectionTag:SECTION_TAG_DISCOVERY]; if (discoveryIndexPath) { [tableView scrollToRowAtIndexPath:discoveryIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; @@ -2544,7 +2424,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> else { // this won't be precise in scroll location, but seems the best option for now - NSIndexPath *discoveryIndexPath = [self nearestIndexPathForRowTag:0 sectionTag:SECTION_TAG_DISCOVERY]; + NSIndexPath *discoveryIndexPath = [_tableViewSections nearestIndexPathForRowTag:0 sectionTag:SECTION_TAG_DISCOVERY]; if (discoveryIndexPath) { [tableView scrollToRowAtIndexPath:discoveryIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; @@ -2744,19 +2624,9 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> - (void)onRemove3PID:(NSIndexPath*)indexPath { - Section *sectionObj = nil; - NSInteger section = NSNotFound; - NSInteger row = NSNotFound; - - if (indexPath.section < _sections.count) - { - sectionObj = _sections[indexPath.section]; - section = sectionObj.tag; - if (indexPath.row < sectionObj.rows.count) - { - row = sectionObj.rows[indexPath.row].tag; - } - } + NSIndexPath *tagsIndexPath = [_tableViewSections tagsIndexPathFromTableViewIndexPath:indexPath]; + NSInteger section = tagsIndexPath.section; + NSInteger row = tagsIndexPath.row; if (section == SECTION_TAG_USER_SETTINGS) { @@ -3623,8 +3493,8 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> SingleImagePickerPresenter *singleImagePickerPresenter = [[SingleImagePickerPresenter alloc] initWithSession:self.mainSession]; singleImagePickerPresenter.delegate = self; - NSIndexPath *indexPath = [self exactIndexPathForRowTag:USER_SETTINGS_PROFILE_PICTURE_INDEX - sectionTag:SECTION_TAG_USER_SETTINGS]; + NSIndexPath *indexPath = [_tableViewSections exactIndexPathForRowTag:USER_SETTINGS_PROFILE_PICTURE_INDEX + sectionTag:SECTION_TAG_USER_SETTINGS]; if (indexPath) { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; @@ -3723,7 +3593,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> style:UIAlertActionStyleCancel handler:nil]]; - NSIndexPath *indexPath = [self exactIndexPathForRowTag:USER_INTERFACE_THEME_INDEX + NSIndexPath *indexPath = [_tableViewSections exactIndexPathForRowTag:USER_INTERFACE_THEME_INDEX sectionTag:SECTION_TAG_USER_INTERFACE]; if (indexPath) { @@ -4280,7 +4150,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> } else if ([tableViewCellClass isEqual:[MXKTableViewCellWithTextView class]]) { - NSIndexPath *indexPath = [self exactIndexPathForRowTag:forRow sectionTag:SECTION_TAG_DISCOVERY]; + NSIndexPath *indexPath = [_tableViewSections exactIndexPathForRowTag:forRow sectionTag:SECTION_TAG_DISCOVERY]; if (indexPath) { tableViewCell = [self textViewCellForTableView:self.tableView atIndexPath:indexPath]; @@ -4307,7 +4177,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> } else if ([tableViewCellClass isEqual:[MXKTableViewCellWithLabelAndSwitch class]]) { - NSIndexPath *indexPath = [self exactIndexPathForRowTag:forRow sectionTag:SECTION_TAG_DISCOVERY]; + NSIndexPath *indexPath = [_tableViewSections exactIndexPathForRowTag:forRow sectionTag:SECTION_TAG_DISCOVERY]; if (indexPath) { tableViewCell = [self getLabelAndSwitchCell:self.tableView forIndexPath:indexPath]; @@ -4336,7 +4206,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> - (void)settingsDiscoveryViewModelDidTapUserSettingsLink:(SettingsDiscoveryViewModel *)viewModel { - NSIndexPath *discoveryIndexPath = [self exactIndexPathForRowTag:USER_SETTINGS_ADD_EMAIL_INDEX + NSIndexPath *discoveryIndexPath = [_tableViewSections exactIndexPathForRowTag:USER_SETTINGS_ADD_EMAIL_INDEX sectionTag:SECTION_TAG_USER_SETTINGS]; if (discoveryIndexPath) { @@ -4363,4 +4233,11 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> [self refreshSettings]; } +#pragma mark - TableViewSectionsDelegate + +- (void)tableViewSectionsDidUpdateSections:(TableViewSections *)sections +{ + [self.tableView reloadData]; +} + @end