CreateGroupNavigationController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "CreateGroupNavigationController.h"
  21. #import "PickGroupMembersViewController.h"
  22. #import "EditGroupViewController.h"
  23. #import "EntityManager.h"
  24. #import "NaClCrypto.h"
  25. #import "MyIdentityStore.h"
  26. #import "MessageSender.h"
  27. #import "GroupPhotoSender.h"
  28. #import "ModalPresenter.h"
  29. #import "UserSettings.h"
  30. @interface CreateGroupNavigationController () <EditGroupDelegate>
  31. @property NSData *groupImageData;
  32. @property NSString *groupName;
  33. @property NSSet *groupMembers;
  34. @end
  35. @implementation CreateGroupNavigationController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. self.dismissOnTapOutside = NO;
  39. if ([self.topViewController isKindOfClass:[EditGroupViewController class]]) {
  40. EditGroupViewController *editGroupController = (EditGroupViewController *)self.topViewController;
  41. editGroupController.delegate = self;
  42. UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"cancel", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction:)];
  43. editGroupController.navigationItem.leftBarButtonItem = cancelButton;
  44. UIBarButtonItem *nextButton;
  45. if (_cloneGroupId) {
  46. nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"save", nil) style:UIBarButtonItemStyleDone target:self action:@selector(saveAction:)];
  47. } else {
  48. nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"next", nil) style:UIBarButtonItemStyleDone target:self action:@selector(nextAction:)];
  49. }
  50. editGroupController.navigationItem.rightBarButtonItem = nextButton;
  51. }
  52. }
  53. - (void)createNewGroup {
  54. __block Conversation *conversation;
  55. EntityManager *entityManager = [[EntityManager alloc] init];
  56. [entityManager performSyncBlockAndSafe:^{
  57. conversation = [entityManager.entityCreator conversation];
  58. conversation.groupId = [[NaClCrypto sharedCrypto] randomBytes:kGroupIdLen];
  59. conversation.groupMyIdentity = [MyIdentityStore sharedMyIdentityStore].identity;
  60. conversation.groupName = _groupName;
  61. [conversation addMembers:_groupMembers];
  62. if (_groupImageData) {
  63. ImageData *dbImage = [entityManager.entityCreator imageData];
  64. dbImage.data = _groupImageData;
  65. conversation.groupImage = dbImage;
  66. }
  67. }];
  68. /* send group create messages to all members */
  69. if ([conversation isGroup]) {
  70. GroupProxy *group = [GroupProxy groupProxyForConversation:conversation];
  71. [group syncGroupInfoToAll];
  72. }
  73. if (_groupName) {
  74. [MessageSender sendGroupRenameMessageForConversation:conversation addSystemMessage:YES];
  75. }
  76. if (_groupImageData) {
  77. [self sendGroupPhotoMessageForConversation:conversation];
  78. }
  79. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
  80. conversation, kKeyConversation,
  81. [NSNumber numberWithBool:YES], kKeyForceCompose,
  82. nil];
  83. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowConversation object:nil
  84. userInfo:info];
  85. }
  86. - (void)sendGroupPhotoMessageForConversation:(Conversation *)conversation {
  87. GroupPhotoSender *sender = [[GroupPhotoSender alloc] init];
  88. [sender startWithImageData:_groupImageData inConversation:conversation toMember:nil onCompletion:^{
  89. ;//nop
  90. } onError:^(NSError *error) {
  91. [UIAlertTemplate showAlertWithOwner:self title:error.localizedDescription message:error.localizedFailureReason actionOk:nil];
  92. }];
  93. }
  94. #pragma mark - Navigation controller
  95. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  96. if ([viewController isKindOfClass:[PickGroupMembersViewController class]]) {
  97. PickGroupMembersViewController *pickMembersController = (PickGroupMembersViewController *)viewController;
  98. pickMembersController.delegate = self;
  99. [pickMembersController setMembers:_groupMembers];
  100. UIBarButtonItem *createButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"create", nil) style:UIBarButtonItemStyleDone target:self action:@selector(saveAction:)];
  101. pickMembersController.navigationItem.rightBarButtonItem = createButton;
  102. pickMembersController.navigationItem.leftBarButtonItem = nil;
  103. }
  104. [super pushViewController:viewController animated:animated];
  105. }
  106. #pragma mark - actions
  107. - (void)cancelAction:(id)sender {
  108. [self dismissViewControllerAnimated:YES completion:nil];
  109. }
  110. - (void)nextAction:(id)sender {
  111. @try {
  112. [self.topViewController performSegueWithIdentifier:@"nextSegue" sender:self];
  113. }
  114. @catch (NSException *exception) {
  115. ;//ignore
  116. }
  117. }
  118. - (void)saveAction:(id)sender {
  119. [self dismissViewControllerAnimated:YES completion:^{
  120. if (_cloneGroupId) {
  121. // add members except me
  122. EntityManager *entityManager = [[EntityManager alloc] init];
  123. Conversation *conversation = [entityManager.entityFetcher conversationForGroupId:_cloneGroupId];
  124. Contact *me = [entityManager.entityFetcher contactForId:[MyIdentityStore sharedMyIdentityStore].identity];
  125. if (me != nil && [conversation.members containsObject:me]) {
  126. NSMutableSet *cloneGroupMembers = [[NSMutableSet alloc] init];
  127. for (Contact *member in conversation.members) {
  128. if ([member isEqual:me] == NO) {
  129. [cloneGroupMembers addObject:member];
  130. }
  131. }
  132. _groupMembers = cloneGroupMembers;
  133. } else {
  134. _groupMembers = conversation.members;
  135. }
  136. }
  137. [self createNewGroup];
  138. }];
  139. }
  140. #pragma mark - Edit group delegate
  141. - (void)group:(GroupProxy *)group updatedName:(NSString *)newName {
  142. _groupName = newName;
  143. }
  144. - (void)group:(GroupProxy *)group updatedImage:(NSData *)newImageData {
  145. _groupImageData = newImageData;
  146. }
  147. - (void)group:(GroupProxy *)group updatedMembers:(NSSet *)newMembers {
  148. _groupMembers = newMembers;
  149. }
  150. @end