Skip to content

Commit

Permalink
Don't update back-button-counter for current chat (#2280)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Sep 20, 2024
1 parent 6e496fa commit dfd9097
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
8 changes: 7 additions & 1 deletion DcCore/DcCore/DC/events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
28 changes: 24 additions & 4 deletions deltachat-ios/Controller/ChatListViewController.swift
Original file line number Diff line number Diff line change
@@ -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?

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion deltachat-ios/Controller/ContactDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
8 changes: 5 additions & 3 deletions deltachat-ios/Controller/NewChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion deltachat-ios/Controller/NewContactController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion deltachat-ios/Controller/NewGroupController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
5 changes: 3 additions & 2 deletions deltachat-ios/Coordinator/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dfd9097

Please sign in to comment.