EditContactViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-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 <ThreemaFramework/Constants.h>
  22. #import "EditContactViewController.h"
  23. #import "Contact.h"
  24. #import "EntityManager.h"
  25. #import "AvatarMaker.h"
  26. #import "AppDelegate.h"
  27. #import "ModalPresenter.h"
  28. #import "ProtocolDefines.h"
  29. #import "UIDefines.h"
  30. #import "UIImage+Resize.h"
  31. #import "BundleUtil.h"
  32. #import "UserSettings.h"
  33. @interface EditContactViewController () <EditableAvatarViewDelegate, UITextFieldDelegate>
  34. @end
  35. @implementation EditContactViewController
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. _avatarView.presentingViewController = self;
  40. _avatarView.delegate = self;
  41. _avatarView.canChooseImage = YES;
  42. _firstNameTextField.delegate = self;
  43. _lastNameTextField.delegate = self;
  44. [self setupColors];
  45. self.tableView.rowHeight = 100.0;
  46. self.tableView.estimatedRowHeight = 100.0;
  47. }
  48. - (void)setupColors {
  49. [Colors updateKeyboardAppearanceFor:self.firstNameTextField];
  50. [Colors updateKeyboardAppearanceFor:self.lastNameTextField];
  51. }
  52. - (void)viewWillAppear:(BOOL)animated {
  53. [super viewWillAppear:animated];
  54. [self updateView];
  55. }
  56. - (void)viewWillDisappear:(BOOL)animated {
  57. [[[EntityManager alloc] init] performSyncBlockAndSafe:^{
  58. if (_firstNameTextField.text.length > 0) {
  59. _contact.firstName = _firstNameTextField.text;
  60. } else {
  61. _contact.firstName = nil;
  62. }
  63. if (_lastNameTextField.text.length > 0) {
  64. _contact.lastName = _lastNameTextField.text;
  65. } else {
  66. _contact.lastName = nil;
  67. }
  68. }];
  69. [super viewWillDisappear:animated];
  70. }
  71. - (void)updateView {
  72. if (_contact.contactImage && [UserSettings sharedUserSettings].showProfilePictures) {
  73. _avatarView.imageData = _contact.contactImage.data;
  74. _avatarView.isReceivedImage = YES;
  75. _avatarView.canDeleteImage = NO;
  76. } else if (_contact.imageData) {
  77. _avatarView.imageData = _contact.imageData;
  78. _avatarView.isReceivedImage = NO;
  79. _avatarView.canDeleteImage = YES;
  80. }
  81. _firstNameTextField.text = _contact.firstName;
  82. _lastNameTextField.text = _contact.lastName;
  83. if (_firstNameTextField.text.length < 1) {
  84. [_firstNameTextField becomeFirstResponder];
  85. }
  86. }
  87. - (BOOL)resignFirstResponder {
  88. [_firstNameTextField resignFirstResponder];
  89. [_lastNameTextField resignFirstResponder];
  90. return YES;
  91. }
  92. #pragma mark - UITableViewDelegate
  93. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  94. return self.tableView.rowHeight;
  95. }
  96. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  97. return self.tableView.rowHeight;
  98. }
  99. #pragma mark - Avatar delegate
  100. - (void)avatarImageUpdated:(NSData *)newImageData {
  101. if (newImageData == nil) {
  102. _avatarView.canDeleteImage = NO;
  103. if (_contact.contactImage)
  104. _avatarView.imageData = _contact.contactImage.data;
  105. }
  106. if (newImageData == _contact.imageData) {
  107. return;
  108. }
  109. [[[EntityManager alloc] init] performSyncBlockAndSafe:^{
  110. _contact.imageData = newImageData;
  111. }];
  112. }
  113. #pragma mark - UITextFieldDelegate
  114. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  115. NSUInteger bytes = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  116. if (bytes > 256 && ![string isEqualToString:@""]) {
  117. return NO;
  118. }
  119. return YES;
  120. }
  121. @end