ProfilePictureSettingViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2017-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 "ProfilePictureSettingViewController.h"
  21. #import "UserSettings.h"
  22. #import "PickContactsViewController.h"
  23. #import "ModalPresenter.h"
  24. @interface ProfilePictureSettingViewController ()
  25. @end
  26. @implementation ProfilePictureSettingViewController {
  27. NSIndexPath *selectedIndexPath;
  28. }
  29. - (void)viewWillAppear:(BOOL)animated {
  30. [super viewWillAppear:animated];
  31. }
  32. #pragma mark - Private functions
  33. - (NSString *)getLabelForSendProfilePicture:(enum SendProfilePicture)sendProfilePicture {
  34. switch (sendProfilePicture) {
  35. case 0:
  36. return NSLocalizedString(@"send_profileimage_off", nil);
  37. case 1:
  38. return NSLocalizedString(@"send_profileimage_on", nil);
  39. case 2:
  40. return NSLocalizedString(@"send_profileimage_contacts", nil);
  41. default:
  42. return nil;
  43. }
  44. }
  45. #pragma mark - Table view data source
  46. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  47. if ([UserSettings sharedUserSettings].sendProfilePicture == SendProfilePictureContacts) {
  48. return 2;
  49. }
  50. return 1;
  51. }
  52. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  53. if (section == 1)
  54. return 1;
  55. else
  56. return 3;
  57. }
  58. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  59. if (indexPath.section == 0) {
  60. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProfilePictureSettingCell"];
  61. cell.accessoryType = UITableViewCellAccessoryNone;
  62. cell.textLabel.textColor = [Colors fontNormal];
  63. switch (indexPath.row) {
  64. case 0:
  65. cell.textLabel.text = [self getLabelForSendProfilePicture:SendProfilePictureNone];
  66. if ([UserSettings sharedUserSettings].sendProfilePicture == SendProfilePictureNone) {
  67. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  68. selectedIndexPath = indexPath;
  69. }
  70. break;
  71. case 1:
  72. cell.textLabel.text = [self getLabelForSendProfilePicture:SendProfilePictureAll];
  73. if ([UserSettings sharedUserSettings].sendProfilePicture == SendProfilePictureAll) {
  74. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  75. selectedIndexPath = indexPath;
  76. }
  77. break;
  78. case 2:
  79. cell.textLabel.text = [self getLabelForSendProfilePicture:SendProfilePictureContacts];
  80. if ([UserSettings sharedUserSettings].sendProfilePicture == SendProfilePictureContacts) {
  81. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  82. selectedIndexPath = indexPath;
  83. }
  84. break;
  85. default:
  86. break;
  87. }
  88. return cell;
  89. } else {
  90. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SendProfilePictureContactsCell"];
  91. cell.textLabel.textColor = [Colors fontNormal];
  92. return cell;
  93. }
  94. }
  95. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  96. if (section == 0 && [UserSettings sharedUserSettings].sendProfilePicture == SendProfilePictureAll) {
  97. return NSLocalizedString(@"profileimage_setting_all_footer", nil);
  98. }
  99. else if (section == 1) {
  100. return NSLocalizedString(@"profileimage_setting_contacts_footer", nil);
  101. }
  102. return nil;
  103. }
  104. #pragma mark - Table view delegate
  105. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. if (indexPath.section == 0) {
  108. switch (indexPath.row) {
  109. case 0:
  110. [[UserSettings sharedUserSettings] setSendProfilePicture:SendProfilePictureNone];
  111. break;
  112. case 1:
  113. [[UserSettings sharedUserSettings] setSendProfilePicture:SendProfilePictureAll];
  114. break;
  115. case 2:
  116. [[UserSettings sharedUserSettings] setSendProfilePicture:SendProfilePictureContacts];
  117. break;
  118. default:
  119. break;
  120. }
  121. BOOL shouldShowPicker = indexPath.row == 2 && selectedIndexPath.row != 2;
  122. [self.tableView beginUpdates];
  123. [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
  124. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  125. if (shouldShowPicker) {
  126. [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
  127. }
  128. else if (selectedIndexPath.row == 2 && indexPath.row != 2) {
  129. [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
  130. }
  131. [self.tableView endUpdates];
  132. selectedIndexPath = indexPath;
  133. if (shouldShowPicker) {
  134. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ProfilePicture" bundle:nil];
  135. PickContactsViewController *pickContactsVC = [storyboard instantiateViewControllerWithIdentifier:@"PickContactsViewController"];
  136. UINavigationController *navigationVC = [[UINavigationController alloc] initWithRootViewController:pickContactsVC];
  137. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  138. [ModalPresenter present:navigationVC on:self fromRect:cell.frame inView:self.view];
  139. }
  140. } else {
  141. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ProfilePicture" bundle:nil];
  142. PickContactsViewController *pickContactsVC = [storyboard instantiateViewControllerWithIdentifier:@"PickContactsViewController"];
  143. UINavigationController *navigationVC = [[UINavigationController alloc] initWithRootViewController:pickContactsVC];
  144. if (@available(iOS 11.0, *)) {
  145. navigationVC.navigationBar.prefersLargeTitles = NO;
  146. }
  147. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  148. [ModalPresenter present:navigationVC on:self fromRect:cell.frame inView:self.view];
  149. if (SYSTEM_IS_IPAD) {
  150. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  151. }
  152. }
  153. }
  154. @end