NewMessageToaster.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-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 "NewMessageToaster.h"
  21. #import "BaseMessage.h"
  22. #import "UserSettings.h"
  23. #import "SystemMessage.h"
  24. #import "Conversation.h"
  25. #import "Contact.h"
  26. #import "ChatViewController.h"
  27. #import "AppDelegate.h"
  28. #import "UIDefines.h"
  29. #import "ConversationsViewController.h"
  30. #import "AvatarMaker.h"
  31. #import "TextStyleUtils.h"
  32. #import "PushSetting.h"
  33. #import "Threema-Swift.h"
  34. #ifdef DEBUG
  35. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  36. #else
  37. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  38. #endif
  39. @implementation NewMessageToaster {
  40. NSMutableArray *queue;
  41. }
  42. - (instancetype)init
  43. {
  44. self = [super init];
  45. if (self) {
  46. queue = [NSMutableArray array];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessageReceived:) name:@"ThreemaNewMessageReceived" object:nil];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(conversationOpened:) name:@"ThreemaConversationOpened" object:nil];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenRotated:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
  51. }
  52. return self;
  53. }
  54. - (void)dealloc
  55. {
  56. [[NSNotificationCenter defaultCenter] removeObserver:self];
  57. }
  58. - (void)newMessageReceived:(NSNotification*)notification {
  59. BaseMessage *message = notification.object;
  60. DDLogVerbose(@"newMessageReceived: %@", notification);
  61. if (![message.read boolValue]) {
  62. NSString *identity = message.sender.identity;
  63. if (identity == nil) {
  64. identity = message.conversation.contact.identity;
  65. }
  66. // don't show toast for suppressed group ids
  67. PushSetting *pushSetting = [PushSetting findPushSettingForIdentity:identity];
  68. if (pushSetting != nil) {
  69. if (![pushSetting canSendPushForBaseMessage:message]) {
  70. return;
  71. }
  72. }
  73. /* No toast if disabled, a system message or passcode showing */
  74. if (![UserSettings sharedUserSettings].inAppPreview ||
  75. [message isKindOfClass:[SystemMessage class]] || [AppDelegate sharedAppDelegate].isAppLocked)
  76. return;
  77. /* Are we currently in the foreground? */
  78. if (![AppDelegate sharedAppDelegate].active) {
  79. [queue addObject:notification];
  80. return;
  81. }
  82. /* Is this for the currently visible conversation? */
  83. UITabBarController *mainTabBar = [AppDelegate getMainTabBarController];
  84. if ([mainTabBar viewControllers].count <= kChatTabBarIndex) {
  85. return;
  86. }
  87. UINavigationController *chatNavVc = [[mainTabBar viewControllers] objectAtIndex:kChatTabBarIndex];
  88. DDLogVerbose(@"curNavController: %@", chatNavVc);
  89. if ([chatNavVc.topViewController isKindOfClass:[ChatViewController class]]) {
  90. ChatViewController *curChatVc = (ChatViewController*)chatNavVc.topViewController;
  91. if (curChatVc.conversation == message.conversation)
  92. return;
  93. }
  94. [NotificationBannerHelper newBannerWithMessage: message];
  95. }
  96. }
  97. - (void)conversationOpened:(NSNotification*)notification {
  98. Conversation *conversation = notification.object;
  99. if (conversation != nil) {
  100. [NotificationBannerHelper dismissAllNotificationsFor:conversation];
  101. }
  102. }
  103. - (void)screenRotated:(NSNotification*)notification {
  104. DDLogVerbose(@"screenRotated");
  105. [NotificationBannerHelper dismissAllNotifications];
  106. }
  107. - (void)didBecomeActive:(NSNotification*)notification {
  108. DDLogVerbose(@"didBecomeActive");
  109. if (![AppDelegate sharedAppDelegate].active)
  110. return; /* safety check */
  111. /* show any queued notifications now */
  112. for (NSNotification *queuedNotification in queue) {
  113. [self newMessageReceived:queuedNotification];
  114. }
  115. [queue removeAllObjects];
  116. }
  117. @end