AddThreemaChannelController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2016-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 "AddThreemaChannelController.h"
  21. #import "ContactStore.h"
  22. #import "Contact.h"
  23. #import "MessageSender.h"
  24. #import "ChatViewControllerCache.h"
  25. @implementation AddThreemaChannelController {
  26. NSMutableArray *initialMessages;
  27. NSDictionary *notificationInfo;
  28. }
  29. - (void)addThreemaChannel {
  30. // Check if the Threema channel has already been added to the contacts
  31. Contact *contact = [[ContactStore sharedContactStore] contactForIdentity:@"*THREEMA"];
  32. if (contact != nil) {
  33. // Contact exists; open chat
  34. [self showConversationForContact:contact];
  35. return;
  36. }
  37. // Ask user if he wants to add the contact
  38. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"threema_channel_intro", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  39. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]];
  40. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  41. [self addContact];
  42. }]];
  43. [self.parentViewController presentViewController:alertController animated:YES completion:nil];
  44. }
  45. - (void)addContact {
  46. // Add contact, open conversation and send initial messages
  47. [[ContactStore sharedContactStore] addContactWithIdentity:@"*THREEMA" verificationLevel:kVerificationLevelUnverified onCompletion:^(Contact *contact, BOOL alreadyExists) {
  48. notificationInfo = [self showConversationForContact:contact];
  49. [self createInitialMessages];
  50. [self dispatchNextInitialMessage];
  51. } onError:^(NSError *error) {
  52. }];
  53. }
  54. - (void)dispatchNextInitialMessage {
  55. if (initialMessages.count == 0)
  56. return;
  57. NSString *message = [initialMessages objectAtIndex:0];
  58. [initialMessages removeObjectAtIndex:0];
  59. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  60. Conversation *conversation = [ChatViewControllerCache getConversationForNotificationInfo:notificationInfo];
  61. if (conversation != nil) {
  62. [MessageSender sendMessage:message inConversation:conversation async:YES quickReply:NO requestId:nil onCompletion:^(TextMessage *message, Conversation *conv) {}];
  63. }
  64. [self dispatchNextInitialMessage];
  65. });
  66. }
  67. - (void)createInitialMessages {
  68. initialMessages = [NSMutableArray array];
  69. // If the system language is not German, change channel language to English first
  70. if (![[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] hasPrefix:@"de"]) {
  71. [initialMessages addObject:@"en"];
  72. }
  73. [initialMessages addObject:@"Start News"];
  74. [initialMessages addObject:@"Start iOS"];
  75. [initialMessages addObject:@"Info"];
  76. }
  77. - (NSDictionary*)showConversationForContact:(Contact*)contact {
  78. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
  79. contact, kKeyContact,
  80. [NSNumber numberWithBool:YES], kKeyForceCompose,
  81. nil];
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowConversation object:nil userInfo:info];
  84. });
  85. return info;
  86. }
  87. @end