From 4daac36a3f75f62a79df0de34886303ddef973de Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 31 Jul 2020 13:00:01 +1000 Subject: [PATCH 1/3] WIP: fix notification bugs --- Signal/src/Loki/View Controllers/HomeVC.swift | 12 ++++++++++-- SignalMessaging/utils/ThreadUtil.m | 2 +- SignalServiceKit/src/Messages/OWSMessageManager.m | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index 39f913d70..69897504a 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -5,6 +5,8 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol private var isViewVisible = false { didSet { updateIsObservingDatabase() } } private var tableViewTopConstraint: NSLayoutConstraint! + private var hasDatabaseModifiedNotificationWhenInvisible = false + private var threads: YapDatabaseViewMappings = { let result = YapDatabaseViewMappings(groups: [ TSInboxGroup ], view: TSThreadDatabaseViewExtensionName) result.setIsReversed(true, forGroup: TSInboxGroup) @@ -229,10 +231,13 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol @objc private func handleYapDatabaseModifiedNotification(_ notification: Notification) { AssertIsOnMainThread() - guard isObservingDatabase else { return } let notifications = uiDatabaseConnection.beginLongLivedReadTransaction() let ext = uiDatabaseConnection.ext(TSThreadDatabaseViewExtensionName) as! YapDatabaseViewConnection let hasChanges = ext.hasChanges(forGroup: TSInboxGroup, in: notifications) + guard isObservingDatabase else { + hasDatabaseModifiedNotificationWhenInvisible = hasChanges + return + } guard hasChanges else { uiDatabaseConnection.read { transaction in self.threads.update(with: transaction) @@ -279,7 +284,10 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol @objc private func handleApplicationDidBecomeActiveNotification(_ notification: Notification) { updateIsObservingDatabase() - updateYDBThreadMapping() + if (hasDatabaseModifiedNotificationWhenInvisible) { + reload() + hasDatabaseModifiedNotificationWhenInvisible = false + } } @objc private func handleApplicationWillResignActiveNotification(_ notification: Notification) { diff --git a/SignalMessaging/utils/ThreadUtil.m b/SignalMessaging/utils/ThreadUtil.m index 4806bc202..22e717d35 100644 --- a/SignalMessaging/utils/ThreadUtil.m +++ b/SignalMessaging/utils/ThreadUtil.m @@ -193,6 +193,7 @@ typedef void (^BuildOutgoingMessageCompletionBlock)(TSOutgoingMessage *savedMess quotedMessage:[quotedReplyModel buildQuotedMessageForSending] contactShare:nil linkPreview:nil]; + [message save]; [BenchManager benchAsyncWithTitle:@"Saving outgoing message" @@ -200,7 +201,6 @@ typedef void (^BuildOutgoingMessageCompletionBlock)(TSOutgoingMessage *savedMess // To avoid blocking the send flow, we dispatch an async write from within this read // transaction [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull writeTransaction) { - [message saveWithTransaction:writeTransaction]; OWSLinkPreview *_Nullable linkPreview = [self linkPreviewForLinkPreviewDraft:linkPreviewDraft diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index dd72453fe..1c97dc5ba 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1614,7 +1614,7 @@ NS_ASSUME_NONNULL_BEGIN // Update thread preview in inbox [masterThread touchWithTransaction:transaction]; - if (CurrentAppContext().isMainAppAndActive) { + if (CurrentAppContext().isMainApp) { [SSKEnvironment.shared.notificationsManager notifyUserForIncomingMessage:incomingMessage inThread:masterThread transaction:transaction]; } From 4fe78bdda482db7dd8655541c3a1cfa9d936259b Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 31 Jul 2020 14:20:09 +1000 Subject: [PATCH 2/3] fix homeVC not refreshing because of PN --- .../NotificationServiceExtension.swift | 8 ++++++++ Signal/src/AppDelegate.m | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/LokiPushNotificationService/NotificationServiceExtension.swift b/LokiPushNotificationService/NotificationServiceExtension.swift index f5aa06a9e..fe2e628dd 100644 --- a/LokiPushNotificationService/NotificationServiceExtension.swift +++ b/LokiPushNotificationService/NotificationServiceExtension.swift @@ -16,6 +16,14 @@ final class NotificationServiceExtension : UNNotificationServiceExtension { self.contentHandler = contentHandler notificationContent = (request.content.mutableCopy() as? UNMutableNotificationContent) + var isMainAppActive = false + if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") { + isMainAppActive = sharedUserDefaults.bool(forKey: "isMainAppActive") + } + print("[Ryan debug] isMainAppActive \(isMainAppActive)") + // If the main app is running, skip the whole process + guard !isMainAppActive else { return self.completeWithFailure(content: notificationContent!) } + // The code using DispatchQueue.main.async { self.setUpIfNecessary() { Modify the notification content } } will somehow cause a freeze when a second PN comes DispatchQueue.main.sync { self.setUpIfNecessary() {} } diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 4c4767cea..02f47365f 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -306,6 +306,10 @@ static NSTimeInterval launchStartedAt; if (CurrentAppContext().isRunningTests) { return; } + + NSUserDefaults *sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.loki-project.loki-messenger"]; + [sharedUserDefaults setBool:true forKey:@"isMainAppActive"]; + [sharedUserDefaults synchronize]; [self ensureRootViewController]; @@ -333,6 +337,10 @@ static NSTimeInterval launchStartedAt; } [self clearAllNotificationsAndRestoreBadgeCount]; + + NSUserDefaults *sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.loki-project.loki-messenger"]; + [sharedUserDefaults setBool:false forKey:@"isMainAppActive"]; + [sharedUserDefaults synchronize]; [DDLog flushLog]; } From d953f727d9ab7e8acda554ac8abd28cbdd1d455b Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Mon, 3 Aug 2020 10:08:09 +1000 Subject: [PATCH 3/3] fix conversation view not refreshing --- .../ConversationView/ConversationViewController.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 51b7d009e..33f3667f4 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -842,6 +842,8 @@ typedef enum : NSUInteger { - (void)applicationDidBecomeActive:(NSNotification *)notification { [self startReadTimer]; + [self.conversationViewModel viewDidLoad]; + [self.conversationViewModel loadAnotherPageOfMessages]; } - (void)dismissPresentedViewControllerIfNecessary