From dfd9097ee6f8120a14bcd51f3e70ae2a055a24ce Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 20 Sep 2024 18:22:36 +0200 Subject: [PATCH] Don't update back-button-counter for current chat (#2280) --- DcCore/DcCore/DC/events.swift | 8 +++++- deltachat-ios/Chat/ChatViewController.swift | 12 ++++++++ .../Controller/ChatListViewController.swift | 28 ++++++++++++++++--- .../ContactDetailViewController.swift | 3 +- .../Controller/NewChatViewController.swift | 8 ++++-- .../Controller/NewContactController.swift | 3 +- .../Controller/NewGroupController.swift | 3 +- .../Coordinator/AppCoordinator.swift | 5 ++-- 8 files changed, 57 insertions(+), 13 deletions(-) diff --git a/DcCore/DcCore/DC/events.swift b/DcCore/DcCore/DC/events.swift index a4c5247a2..d546e290e 100644 --- a/DcCore/DcCore/DC/events.swift +++ b/DcCore/DcCore/DC/events.swift @@ -105,6 +105,7 @@ public class DcEventHandler { NotificationCenter.default.post(name: Event.messagesNoticed, object: nil, userInfo: [ "chat_id": Int(data1), + "account_id": accountId ]) case DC_EVENT_CHAT_MODIFIED: @@ -129,7 +130,11 @@ public class DcEventHandler { case DC_EVENT_INCOMING_MSG: - NotificationCenter.default.post(name: Event.incomingMessageOnAnyAccount, object: nil) + NotificationCenter.default.post(name: Event.incomingMessageOnAnyAccount, object: nil, userInfo: [ + "chat_id": Int(data1), + "account_id": accountId + ]) + if accountId != dcAccounts.getSelected().id { return } @@ -138,6 +143,7 @@ public class DcEventHandler { NotificationCenter.default.post(name: Event.incomingMessage, object: nil, userInfo: [ "message_id": Int(data2), "chat_id": Int(data1), + "account_id": accountId ]) case DC_EVENT_CONTACTS_CHANGED: diff --git a/deltachat-ios/Chat/ChatViewController.swift b/deltachat-ios/Chat/ChatViewController.swift index 4325e5f98..4aa303390 100644 --- a/deltachat-ios/Chat/ChatViewController.swift +++ b/deltachat-ios/Chat/ChatViewController.swift @@ -2842,3 +2842,15 @@ extension ChatViewController: ReactionsOverviewViewControllerDelegate { navigationController?.pushViewController(contactDetailController, animated: true) } } + +// MARK: - ChatListViewControllerDataSource + +extension ChatViewController: BackButtonUpdateable { + func shouldUpdateBackButton(_ viewController: UIViewController, chatId: Int, accountId: Int) -> Bool { + if chatId == self.chatId && accountId == dcContext.id { + return false + } else { + return true + } + } +} diff --git a/deltachat-ios/Controller/ChatListViewController.swift b/deltachat-ios/Controller/ChatListViewController.swift index ca4325104..3e4b193bf 100644 --- a/deltachat-ios/Controller/ChatListViewController.swift +++ b/deltachat-ios/Controller/ChatListViewController.swift @@ -1,12 +1,17 @@ import UIKit import DcCore +protocol BackButtonUpdateable: AnyObject { + func shouldUpdateBackButton(_ viewController: UIViewController, chatId: Int, accountId: Int) -> Bool +} + class ChatListViewController: UITableViewController { var viewModel: ChatListViewModel? let dcContext: DcContext internal let dcAccounts: DcAccounts var isArchive: Bool private var accountSwitchTransitioningDelegate: PartialScreenModalTransitioningDelegate! + weak var backButtonUpdateableDataSource: BackButtonUpdateable? private weak var timer: Timer? @@ -230,9 +235,15 @@ class ChatListViewController: UITableViewController { } @objc private func handleIncomingMessageOnAnyAccount(_ notification: Notification) { + + guard let userInfo = notification.userInfo, + let chatId = userInfo["chat_id"] as? Int, + let accountId = userInfo["account_id"] as? Int + else { return } + DispatchQueue.main.async { [weak self] in self?.updateAccountButton() - self?.updateNextScreensBackButton() + self?.updateNextScreensBackButton(accountId: accountId, chatId: chatId) } } @@ -241,13 +252,20 @@ class ChatListViewController: UITableViewController { updateNextScreensBackButton() } - private func updateNextScreensBackButton() { + private func updateNextScreensBackButton(accountId: Int? = nil, chatId: Int? = nil) { let numberOfUnreadMessages = DcAccounts.shared.getFreshMessageCount() if isArchive { navigationItem.backBarButtonItem = nil navigationItem.backButtonTitle = String.localized("chat_archived_label") } else if numberOfUnreadMessages > 0, #available(iOS 13, *) { + + if let backButtonUpdateableDataSource, let accountId, let chatId, + backButtonUpdateableDataSource.shouldUpdateBackButton(self, chatId: chatId, accountId: accountId) == false { + return + } + + // we need chatId and everything let symbolName: String if numberOfUnreadMessages > 50 { symbolName = "circle.fill" @@ -848,9 +866,11 @@ class ChatListViewController: UITableViewController { if searchController.isActive { searchController.searchBar.resignFirstResponder() } + let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId, highlightedMsg: highlightedMsg) + backButtonUpdateableDataSource = chatViewController updateNextScreensBackButton() - let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId, highlightedMsg: highlightedMsg) - navigationController?.pushViewController(chatVC, animated: animated) + + navigationController?.pushViewController(chatViewController, animated: animated) } public func showArchive(animated: Bool) { diff --git a/deltachat-ios/Controller/ContactDetailViewController.swift b/deltachat-ios/Controller/ContactDetailViewController.swift index c03bdbb67..192643375 100644 --- a/deltachat-ios/Controller/ContactDetailViewController.swift +++ b/deltachat-ios/Controller/ContactDetailViewController.swift @@ -534,8 +534,9 @@ class ContactDetailViewController: UITableViewController { // MARK: - coordinator private func showChat(chatId: Int) { - if let chatlistViewController = navigationController?.viewControllers[0] { + if let chatlistViewController = navigationController?.viewControllers[0] as? ChatListViewController { let chatViewController = ChatViewController(dcContext: viewModel.context, chatId: chatId) + chatlistViewController.backButtonUpdateableDataSource = chatViewController navigationController?.setViewControllers([chatlistViewController, chatViewController], animated: true) } } diff --git a/deltachat-ios/Controller/NewChatViewController.swift b/deltachat-ios/Controller/NewChatViewController.swift index cc4a667ec..b79cfc258 100644 --- a/deltachat-ios/Controller/NewChatViewController.swift +++ b/deltachat-ios/Controller/NewChatViewController.swift @@ -318,9 +318,11 @@ class NewChatViewController: UITableViewController { } private func showChat(chatId: Int) { - let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId) - navigationController?.pushViewController(chatViewController, animated: true) - navigationController?.viewControllers.remove(at: 1) + if let chatlistViewController = navigationController?.viewControllers[0] as? ChatListViewController { + let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId) + chatlistViewController.backButtonUpdateableDataSource = chatViewController + navigationController?.setViewControllers([chatlistViewController, chatViewController], animated: true) + } } private func showContactDetail(contactId: Int) { diff --git a/deltachat-ios/Controller/NewContactController.swift b/deltachat-ios/Controller/NewContactController.swift index d1631285d..a6413b1b7 100644 --- a/deltachat-ios/Controller/NewContactController.swift +++ b/deltachat-ios/Controller/NewContactController.swift @@ -119,8 +119,9 @@ class NewContactController: UITableViewController { // MARK: - coordinator private func showChat(chatId: Int) { - if let chatlistViewController = navigationController?.viewControllers[0] { + if let chatlistViewController = navigationController?.viewControllers[0] as? ChatListViewController { let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId) + chatlistViewController.backButtonUpdateableDataSource = chatViewController navigationController?.setViewControllers([chatlistViewController, chatViewController], animated: true) } } diff --git a/deltachat-ios/Controller/NewGroupController.swift b/deltachat-ios/Controller/NewGroupController.swift index 447b395c7..9a4c0ef68 100644 --- a/deltachat-ios/Controller/NewGroupController.swift +++ b/deltachat-ios/Controller/NewGroupController.swift @@ -305,8 +305,9 @@ class NewGroupController: UITableViewController, MediaPickerDelegate { // MARK: - coordinator private func showGroupChat(chatId: Int) { - if let chatlistViewController = navigationController?.viewControllers[0] { + if let chatlistViewController = navigationController?.viewControllers[0] as? ChatListViewController { let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId) + chatlistViewController.backButtonUpdateableDataSource = chatViewController navigationController?.setViewControllers([chatlistViewController, chatViewController], animated: true) } } diff --git a/deltachat-ios/Coordinator/AppCoordinator.swift b/deltachat-ios/Coordinator/AppCoordinator.swift index 2ebf1e1a7..bf63191b4 100644 --- a/deltachat-ios/Coordinator/AppCoordinator.swift +++ b/deltachat-ios/Coordinator/AppCoordinator.swift @@ -121,9 +121,10 @@ class AppCoordinator: NSObject { let chatListViewController = rootController.viewControllers.first as? ChatListViewController { if let msgId = msgId, openHighlightedMsg { let dcContext = dcAccounts.getSelected() - let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId, highlightedMsg: msgId) + let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId, highlightedMsg: msgId) + chatListViewController.backButtonUpdateableDataSource = chatViewController let webxdcVC = WebxdcViewController(dcContext: dcContext, messageId: msgId) - let controllers: [UIViewController] = [chatListViewController, chatVC, webxdcVC] + let controllers: [UIViewController] = [chatListViewController, chatViewController, webxdcVC] rootController.setViewControllers(controllers, animated: animated) } else { if clearViewControllerStack {