SyncExclusionListViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "SyncExclusionListViewController.h"
  21. #import "UserSettings.h"
  22. #import "ProtocolDefines.h"
  23. #import "UIImage+ColoredImage.h"
  24. @interface SyncExclusionListViewController ()
  25. @end
  26. @implementation SyncExclusionListViewController {
  27. NSArray *syncExclusionList;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  32. }
  33. - (void)viewWillAppear:(BOOL)animated {
  34. [super viewWillAppear:animated];
  35. syncExclusionList = [UserSettings sharedUserSettings].syncExclusionList;
  36. [self.tableView reloadData];
  37. }
  38. - (void)saveToSettings {
  39. [UserSettings sharedUserSettings].syncExclusionList = syncExclusionList;
  40. }
  41. #pragma mark - Table view data source
  42. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  43. return 1;
  44. }
  45. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  46. return syncExclusionList.count+1;
  47. }
  48. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  49. if (indexPath.row < syncExclusionList.count) {
  50. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExclusionCell"];
  51. cell.textLabel.text = [syncExclusionList objectAtIndex:indexPath.row];
  52. return cell;
  53. } else {
  54. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddCell"];
  55. cell.imageView.image = [UIImage imageNamed:@"AddMember" inColor:[Colors main]];
  56. return cell;
  57. }
  58. }
  59. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  60. {
  61. if (editingStyle == UITableViewCellEditingStyleDelete) {
  62. NSMutableArray *newArray = [NSMutableArray arrayWithArray:syncExclusionList];
  63. [newArray removeObjectAtIndex:indexPath.row];
  64. syncExclusionList = newArray;
  65. [self saveToSettings];
  66. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  67. }
  68. }
  69. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  70. if (section == 0)
  71. return NSLocalizedString(@"sync_exclusion_footer", nil);
  72. return nil;
  73. }
  74. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  75. if (indexPath.row == syncExclusionList.count) {
  76. /* add cell */
  77. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"enter_id_to_exclude", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  78. [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  79. }];
  80. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  81. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  82. NSString *excludeId = [[alert.textFields objectAtIndex:0].text uppercaseString];
  83. if (excludeId.length == kIdentityLen) {
  84. NSMutableSet *newSet = [NSMutableSet setWithArray:syncExclusionList];
  85. [newSet addObject:excludeId];
  86. syncExclusionList = [[newSet allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  87. [self saveToSettings];
  88. [self.tableView reloadData];
  89. }
  90. }]];
  91. [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  92. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  93. }]];
  94. [self presentViewController:alert animated:YES completion:nil];
  95. }
  96. }
  97. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  98. if (indexPath.row < syncExclusionList.count)
  99. return YES;
  100. return NO;
  101. }
  102. @end