EditProfileViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <MobileCoreServices/UTCoreTypes.h>
  21. #import "EditProfileViewController.h"
  22. #import "AvatarMaker.h"
  23. #import "AppDelegate.h"
  24. #import "ModalPresenter.h"
  25. #import "ProtocolDefines.h"
  26. #import "UIDefines.h"
  27. #import "UIImage+Resize.h"
  28. #import "BundleUtil.h"
  29. #import "MyIdentityStore.h"
  30. #import "EntityManager.h"
  31. #import "ContactStore.h"
  32. #import "UserSettings.h"
  33. #import "PickContactsViewController.h"
  34. #import "LicenseStore.h"
  35. #import "ValidationLogger.h"
  36. #import "MDMSetup.h"
  37. @interface EditProfileViewController () <EditableAvatarViewDelegate, UITextFieldDelegate>
  38. @property (nonatomic, strong) NSIndexPath *indexPathForPicker;
  39. @end
  40. @implementation EditProfileViewController {
  41. MDMSetup *mdmSetup;
  42. }
  43. - (instancetype)initWithCoder:(NSCoder *)coder
  44. {
  45. self = [super initWithCoder:coder];
  46. if (self) {
  47. mdmSetup = [[MDMSetup alloc] initWithSetup:NO];
  48. }
  49. return self;
  50. }
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. _avatarView.presentingViewController = self;
  55. _avatarView.delegate = self;
  56. _avatarView.canDeleteImage = ![mdmSetup readonlyProfile];
  57. _avatarView.canChooseImage = ![mdmSetup readonlyProfile];
  58. _profileCell.contentView.isAccessibilityElement = NO;
  59. _profileCell.contentView.accessibilityLabel = nil;
  60. _nickNameTextField.delegate = self;
  61. [self setupColors];
  62. }
  63. - (void)viewWillAppear:(BOOL)animated {
  64. [super viewWillAppear:animated];
  65. [self updateView];
  66. _indexPathForPicker = nil;
  67. _profilePictureSettingCell.userInteractionEnabled = ![mdmSetup disableSendProfilePicture];
  68. if ([mdmSetup readonlyProfile]) {
  69. _nickNameTextField.enabled = NO;
  70. } else {
  71. if(!UIAccessibilityIsVoiceOverRunning()) {
  72. [_nickNameTextField becomeFirstResponder];
  73. }
  74. }
  75. }
  76. - (void)viewWillDisappear:(BOOL)animated {
  77. [super viewWillDisappear:animated];
  78. NSString *newNickname = _nickNameTextField.text;
  79. if (newNickname.length == 0) {
  80. newNickname = nil;
  81. }
  82. [MyIdentityStore sharedMyIdentityStore].pushFromName = newNickname;
  83. [[LicenseStore sharedLicenseStore] performUpdateWorkInfo];
  84. [_nickNameTextField resignFirstResponder];
  85. }
  86. #pragma mark - Private functions
  87. - (void)setupColors {
  88. [Colors updateKeyboardAppearanceFor:self.nickNameTextField];
  89. [_nickNameTitleLabel setTextColor:[Colors fontVeryLight]];
  90. _nickNameTitleLabel.shadowColor = nil;
  91. }
  92. - (void)updateView {
  93. NSMutableDictionary *profile = [[MyIdentityStore sharedMyIdentityStore] profilePicture];
  94. NSData *data = profile[@"ProfilePicture"];
  95. if (data) {
  96. _avatarView.imageData = data;
  97. _avatarView.canDeleteImage = ![mdmSetup readonlyProfile];
  98. } else {
  99. _avatarView.imageData = nil;
  100. _avatarView.canDeleteImage = NO;
  101. }
  102. _nickNameTextField.text = [[MyIdentityStore sharedMyIdentityStore] pushFromName];
  103. _nickNameTextField.placeholder = [MyIdentityStore sharedMyIdentityStore].identity;
  104. _nickNameTextField.accessibilityLabel = NSLocalizedString(@"id_completed_nickname", @"");
  105. _contactsSettingValue.text = [self getLabelForSendProfilePicture:[UserSettings sharedUserSettings].sendProfilePicture];
  106. [self disabledCellsForMDM];
  107. }
  108. - (BOOL)resignFirstResponder {
  109. [_nickNameTextField resignFirstResponder];
  110. return YES;
  111. }
  112. - (NSString *)getLabelForSendProfilePicture:(enum SendProfilePicture)sendProfilePicture {
  113. switch (sendProfilePicture) {
  114. case 0:
  115. return NSLocalizedString(@"send_profileimage_off", nil);
  116. case 1:
  117. return NSLocalizedString(@"send_profileimage_on", nil);
  118. case 2:
  119. return NSLocalizedString(@"send_profileimage_contacts", nil);
  120. default:
  121. return nil;
  122. }
  123. }
  124. - (void)disabledCellsForMDM {
  125. // isReadonlyProfile
  126. self.profilePictureSettingCell.userInteractionEnabled = ![mdmSetup disableSendProfilePicture];
  127. self.profilePictureSettingCell.textLabel.enabled = ![mdmSetup disableSendProfilePicture];
  128. }
  129. #pragma mark - UITableViewDelegates
  130. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  131. [Colors updateTableViewCell:cell];
  132. }
  133. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  134. NSMutableString *footer = [NSMutableString stringWithString:NSLocalizedString(@"edit_profile_footer", nil)];
  135. if ([mdmSetup readonlyProfile] || [mdmSetup disableSendProfilePicture]) {
  136. [footer appendString:@"\n\n"];
  137. [footer appendString:NSLocalizedString(@"disabled_by_device_policy", nil)];
  138. }
  139. return footer;
  140. }
  141. #pragma mark - Avatar delegate
  142. - (void)avatarImageUpdated:(NSData *)newImageData {
  143. [[AvatarMaker sharedAvatarMaker] clearCacheForProfilePicture];
  144. if (newImageData == nil) {
  145. _avatarView.canDeleteImage = NO;
  146. }
  147. NSMutableDictionary *profile = [[MyIdentityStore sharedMyIdentityStore] profilePicture];
  148. if (newImageData == profile[@"ProfilePicture"]) {
  149. return;
  150. }
  151. if (!profile)
  152. profile = [NSMutableDictionary new];
  153. [profile setValue:newImageData forKey:@"ProfilePicture"];
  154. [profile removeObjectForKey:@"LastUpload"];
  155. [[MyIdentityStore sharedMyIdentityStore] setProfilePicture:profile];
  156. [[ContactStore sharedContactStore] removeProfilePictureFlagForAllContacts];
  157. }
  158. #pragma mark - UITextFieldDelegate
  159. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  160. NSUInteger bytes = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  161. if (bytes > 32 && ![string isEqualToString:@""]) {
  162. return NO;
  163. }
  164. return YES;
  165. }
  166. @end