DeleteConversationAction.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "DeleteConversationAction.h"
  21. #import "GroupProxy.h"
  22. #import "EntityManager.h"
  23. #import "NotificationManager.h"
  24. @interface DeleteConversationAction ()
  25. @property Conversation *conversation;
  26. @property (copy) void (^onCompletion)(BOOL didCancel);
  27. @end
  28. @implementation DeleteConversationAction
  29. + (instancetype)deleteActionForConversation:(Conversation *)conversation {
  30. DeleteConversationAction *action = [[DeleteConversationAction alloc] init];
  31. action.conversation = conversation;
  32. return action;
  33. }
  34. - (void)executeOnCompletion:(void (^)(BOOL didCancel))onCompletion {
  35. _onCompletion = onCompletion;
  36. /* If this was a group conversation with members, ask for confirmation */
  37. if (_conversation.groupId != nil && _conversation.members.count > 0) {
  38. [self presentAlertController];
  39. } else {
  40. [self deleteConversationInDB];
  41. }
  42. }
  43. - (NSString *)alertMessage {
  44. NSString *message;
  45. if (_conversation.isGroup && _conversation.contact == nil) {
  46. message = NSLocalizedString(@"group_admin_delete_confirm", nil);
  47. } else {
  48. message = NSLocalizedString(@"group_delete_confirm", nil);
  49. }
  50. return message;
  51. }
  52. - (void)presentAlertController {
  53. NSString *title = [self alertMessage];
  54. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  55. UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"leave", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull alertAction) {
  56. [self deleteConversationInDB];
  57. }];
  58. [alertController addAction:action];
  59. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  60. if (_onCompletion) {
  61. _onCompletion(NO);
  62. }
  63. }];
  64. [alertController addAction:cancelAction];
  65. [_presentingViewController presentViewController:alertController animated:YES completion:nil];
  66. }
  67. - (void)deleteConversationInDB {
  68. if (_conversation == nil)
  69. return;
  70. if ([_conversation isGroup]) {
  71. GroupProxy *group = [GroupProxy groupProxyForConversation:_conversation];
  72. [group adminDeleteGroup];
  73. }
  74. /* Remove from Core Data */
  75. EntityManager *entityManager = [[EntityManager alloc] init];
  76. [entityManager performSyncBlockAndSafe:^{
  77. [[entityManager entityDestroyer] deleteObjectWithObject:_conversation];
  78. }];
  79. [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
  80. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
  81. _conversation, kKeyConversation,
  82. nil];
  83. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDeletedConversation object:nil userInfo:info];
  84. if (_onCompletion) {
  85. _onCompletion(YES);
  86. }
  87. }
  88. @end