EditGroupViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "EditGroupViewController.h"
  22. #import "AvatarMaker.h"
  23. #import "AppDelegate.h"
  24. #import "ModalPresenter.h"
  25. #import "UIDefines.h"
  26. #import "UIImage+Resize.h"
  27. #import "EntityManager.h"
  28. #import "MessageSender.h"
  29. #import "EntityManager.h"
  30. #import "GroupPhotoSender.h"
  31. #import "CreateGroupNavigationController.h"
  32. @interface EditGroupViewController () <EditableAvatarViewDelegate, UITextFieldDelegate>
  33. @property NSString *groupName;
  34. @property NSData *avatarImageData;
  35. @end
  36. @implementation EditGroupViewController
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. _avatarView.presentingViewController = self;
  41. _avatarView.delegate = self;
  42. _avatarView.canDeleteImage = YES;
  43. _avatarView.canChooseImage = YES;
  44. _nameTextField.delegate = self;
  45. _nameTextField.placeholder = NSLocalizedString(@"group name", nil);
  46. [Colors updateKeyboardAppearanceFor:self.nameTextField];
  47. if ([self.navigationController isKindOfClass:[CreateGroupNavigationController class]]) {
  48. NSData *cloneGroupId = ((CreateGroupNavigationController *)self.navigationController).cloneGroupId;
  49. if (cloneGroupId) {
  50. EntityManager *entityManager = [[EntityManager alloc] init];
  51. Conversation *conversation = [entityManager.entityFetcher conversationForGroupId:cloneGroupId];
  52. if (conversation) {
  53. _nameTextField.text = conversation.groupName;
  54. if (conversation.groupImage.data) {
  55. _avatarView.imageData = conversation.groupImage.data;
  56. _avatarImageData = conversation.groupImage.data;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. - (void)setGroup:(GroupProxy *)group {
  63. _group = group;
  64. _groupName = _group.conversation.groupName;
  65. if (_group.conversation.groupImage) {
  66. _avatarImageData = _group.conversation.groupImage.data;
  67. }
  68. }
  69. - (void)viewWillAppear:(BOOL)animated {
  70. [self updateView];
  71. [super viewWillAppear:animated];
  72. }
  73. - (void)viewWillDisappear:(BOOL)animated {
  74. [self triggerNameUpdate];
  75. [self triggerAvatarImageUpdate];
  76. [super viewWillDisappear:animated];
  77. }
  78. - (BOOL)changedName {
  79. if ([_nameTextField.text isEqualToString:_group.conversation.groupName]) {
  80. return NO;
  81. }
  82. return YES;
  83. }
  84. - (BOOL)changedImage {
  85. if (_group.conversation.groupImage.data == _avatarImageData) {
  86. return NO;
  87. }
  88. return YES;
  89. }
  90. - (void)triggerNameUpdate {
  91. if ([self changedName]) {
  92. if (self.nameTextField.text.length > 0) {
  93. [_delegate group:_group updatedName:self.nameTextField.text];
  94. } else {
  95. [_delegate group:_group updatedName:nil];
  96. }
  97. };
  98. }
  99. - (void)triggerAvatarImageUpdate {
  100. if ([self changedImage]) {
  101. [_delegate group:_group updatedImage:_avatarImageData];
  102. };
  103. }
  104. - (void)updateView {
  105. if (_group) {
  106. self.title = NSLocalizedString(@"edit group", nil);
  107. _nameTextField.text = _groupName;
  108. if (_group.conversation.groupImage) {
  109. _avatarView.imageData = _avatarImageData;
  110. }
  111. } else {
  112. self.title = NSLocalizedString(@"new group", nil);
  113. BOOL hasName = self.nameTextField.text.length > 0;
  114. [self.navigationItem.rightBarButtonItem setEnabled:hasName];
  115. }
  116. }
  117. - (BOOL)resignFirstResponder {
  118. [_nameTextField resignFirstResponder];
  119. return YES;
  120. }
  121. #pragma mark - Edit group delegate
  122. - (void)saveName {
  123. if ([self changedName] == NO) {
  124. return;
  125. }
  126. EntityManager *entityManager = [[EntityManager alloc] init];
  127. [entityManager performSyncBlockAndSafe:^{
  128. _group.conversation.groupName = self.nameTextField.text;
  129. }];
  130. [MessageSender sendGroupRenameMessageForConversation:_group.conversation addSystemMessage:YES];
  131. }
  132. - (void)saveImage {
  133. if ([self changedImage] == NO) {
  134. return;
  135. }
  136. GroupPhotoSender *sender = [[GroupPhotoSender alloc] init];
  137. Conversation *groupConversation = _group.conversation;
  138. [sender startWithImageData:_avatarImageData inConversation:groupConversation toMember:nil onCompletion:^{
  139. EntityManager *entityManager = [[EntityManager alloc] init];
  140. [entityManager performSyncBlockAndSafe:^{
  141. // Delete old image
  142. if (groupConversation.groupImage != nil) {
  143. [[entityManager entityDestroyer] deleteObjectWithObject:groupConversation.groupImage];
  144. groupConversation.groupImage = nil;
  145. }
  146. // Save new image if there is any
  147. if (_avatarImageData != nil) {
  148. ImageData *dbImage = [entityManager.entityCreator imageData];
  149. dbImage.data = _avatarImageData;
  150. groupConversation.groupImage = dbImage;
  151. }
  152. }];
  153. } onError:^(NSError *error) {
  154. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:error.localizedDescription message:error.localizedFailureReason actionOk:nil];
  155. }];
  156. }
  157. #pragma mark - UITextFieldDelegate
  158. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  159. NSUInteger bytes = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  160. if (bytes > 256 && ![string isEqualToString:@""]) {
  161. return NO;
  162. }
  163. NSString *newValue = [textField.text stringByReplacingCharactersInRange:range withString:string];
  164. if (newValue.length > 0) {
  165. [self.navigationItem.rightBarButtonItem setEnabled:YES];
  166. } else {
  167. [self.navigationItem.rightBarButtonItem setEnabled:NO];
  168. }
  169. return YES;
  170. }
  171. - (BOOL)textFieldShouldClear:(UITextField *)textField {
  172. [self.navigationItem.rightBarButtonItem setEnabled:NO];
  173. return YES;
  174. }
  175. #pragma mark - Avatar delegate
  176. - (void)avatarImageUpdated:(NSData *)newImageData {
  177. _avatarImageData = newImageData;
  178. }
  179. #pragma mark - Actions
  180. - (IBAction)cancelAction:(id)sender {
  181. [self dismissViewControllerAnimated:YES completion:nil];
  182. }
  183. - (IBAction)saveAction:(id)sender {
  184. [self saveName];
  185. [self saveImage];
  186. [self dismissViewControllerAnimated:YES completion:nil];
  187. }
  188. @end