iOS web browser with a focus on security and privacy
at master 197 lines 5.6 kB view raw
1/* 2 * Endless 3 * Copyright (c) 2014-2017 joshua stein <jcs@jcs.org> 4 * 5 * See LICENSE file for redistribution terms. 6 */ 7 8#import "RuleEditorController.h" 9 10@implementation RuleEditorController 11 12UISearchDisplayController *searchDisplayController; 13 14- (id)initWithStyle:(UITableViewStyle)style 15{ 16 self = [super initWithStyle:style]; 17 18 self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 19 20 self.sortedRuleRows = [[NSMutableArray alloc] init]; 21 self.inUseRuleRows = [[NSMutableArray alloc] init]; 22 23 self.searchResult = [[NSMutableArray alloc] init]; 24 25 return self; 26} 27 28- (void)viewDidLoad 29{ 30 [super viewDidLoad]; 31 32 self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 33 34 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]; 35 searchDisplayController.delegate = self; 36 searchDisplayController.searchResultsDataSource = self; 37 searchDisplayController.searchResultsTableView.delegate = self; 38 39 self.tableView.delegate = self; 40 41 [[self tableView] setTableHeaderView:self.searchBar]; 42} 43 44#pragma mark - Table view data source 45 46- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 47{ 48 return 2; 49} 50 51- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 52{ 53 if (section == 0) 54 return NSLocalizedString(@"Rules in use on current page", nil); 55 else 56 return NSLocalizedString(@"All rules", nil); 57} 58 59- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 60{ 61 if (section == 0) 62 return [self.inUseRuleRows count]; 63 else if (tableView == self.searchDisplayController.searchResultsTableView) 64 return [self.searchResult count]; 65 else 66 return [self.sortedRuleRows count]; 67} 68 69- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 70{ 71 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rule"]; 72 if (cell == nil) 73 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rule"]; 74 75 /* TODO: once we have a per-rule view page, enable this */ 76 //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 77 78 RuleEditorRow *row = [self ruleForTableView:tableView atIndexPath:indexPath]; 79 80 cell.textLabel.text = [row textLabel]; 81 82 NSString *disabled = [self ruleDisabledReason:row]; 83 if (disabled == nil) { 84 cell.textLabel.textColor = [UIColor darkTextColor]; 85 cell.detailTextLabel.text = [row detailTextLabel]; 86 cell.detailTextLabel.textColor = [UIColor darkGrayColor]; 87 } 88 else { 89 cell.textLabel.textColor = [UIColor redColor]; 90 if ([row detailTextLabel] == nil || [[row detailTextLabel] isEqualToString:@""]) 91 cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Disabled: %@", nil), disabled]; 92 else 93 cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%@ (Disabled: %@)", nil), [row detailTextLabel], disabled]; 94 cell.detailTextLabel.textColor = [UIColor redColor]; 95 } 96 97 return cell; 98} 99 100- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 101{ 102 return YES; 103} 104 105- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 106{ 107 return NO; 108} 109 110- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 111{ 112 if (editingStyle == UITableViewCellEditingStyleDelete) { 113 RuleEditorRow *row = [self ruleForTableView:tableView atIndexPath:indexPath]; 114 115 if ([self ruleDisabledReason:row] == nil) 116 [self disableRuleForRow:row withReason:NSLocalizedString(@"User disabled", nil)]; 117 else 118 [self enableRuleForRow:row]; 119 } 120 121 [tableView reloadData]; 122} 123 124- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 125{ 126 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 127} 128 129- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 130{ 131 [self.searchResult removeAllObjects]; 132 133 for (RuleEditorRow *row in self.sortedRuleRows) { 134 if ([row textLabel] != nil) { 135 NSRange range = [[row textLabel] rangeOfString:searchString options:NSCaseInsensitiveSearch]; 136 137 if (range.length > 0) { 138 [self.searchResult addObject:row]; 139 continue; 140 } 141 } 142 if ([row detailTextLabel] != nil) { 143 NSRange range = [[row detailTextLabel] rangeOfString:searchString options:NSCaseInsensitiveSearch]; 144 145 if (range.length > 0) { 146 [self.searchResult addObject:row]; 147 continue; 148 } 149 } 150 } 151 152 return YES; 153} 154 155- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(nonnull NSIndexPath *)indexPath 156{ 157 RuleEditorRow *row = [self ruleForTableView:tableView atIndexPath:indexPath]; 158 159 if ([self ruleDisabledReason:row] == nil) 160 return NSLocalizedString(@"Disable", nil); 161 else 162 return NSLocalizedString(@"Enable", nil); 163} 164 165- (RuleEditorRow *)ruleForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath 166{ 167 NSMutableArray *group; 168 169 if ([indexPath section] == 0) 170 group = [self inUseRuleRows]; 171 else if (tableView == self.searchDisplayController.searchResultsTableView) 172 group = [self searchResult]; 173 else 174 group = [self sortedRuleRows]; 175 176 if (group && [group count] > [indexPath row]) 177 return [group objectAtIndex:indexPath.row]; 178 else 179 return nil; 180} 181 182- (NSString *)ruleDisabledReason:(RuleEditorRow *)row 183{ 184 return nil; 185} 186 187- (void)disableRuleForRow:(RuleEditorRow *)row withReason:(NSString *)reason 188{ 189 abort(); 190} 191 192- (void)enableRuleForRow:(RuleEditorRow *)row 193{ 194 abort(); 195} 196 197@end