Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show number of unread messages in back-button (#2280) #2303

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2784,3 +2784,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
}
}
}
55 changes: 51 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 @@ -206,6 +211,9 @@ class ChatListViewController: UITableViewController {

@objc private func handleMessagesNoticed(_ notification: Notification) {
refreshInBg()
DispatchQueue.main.async { [weak self] in
self?.updateNextScreensBackButton()
}
}

@objc private func handleMessagesChanged(_ notification: Notification) {
Expand All @@ -227,14 +235,48 @@ 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(accountId: accountId, chatId: chatId)
}
}

private func setupSubviews() {
emptyStateLabel.addCenteredTo(parentView: view)
navigationItem.backButtonTitle = isArchive ? String.localized("chat_archived_label") : String.localized("pref_chats")
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
}

let symbolName: String
if numberOfUnreadMessages > 50 {
symbolName = "circle.fill"
} else {
symbolName = "\(numberOfUnreadMessages).circle.fill"
}

navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(systemName: symbolName), style: .plain, target: nil, action: nil)
} else { // if numberOfUnreadMessages == 0 or iOS 12
navigationItem.backBarButtonItem = nil
navigationItem.backButtonTitle = String.localized("pref_chats")
}
}

@objc
Expand Down Expand Up @@ -823,8 +865,11 @@ class ChatListViewController: UITableViewController {
if searchController.isActive {
searchController.searchBar.resignFirstResponder()
}
let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId, highlightedMsg: highlightedMsg)
navigationController?.pushViewController(chatVC, animated: animated)
let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId, highlightedMsg: highlightedMsg)
backButtonUpdateableDataSource = chatViewController
updateNextScreensBackButton(accountId: dcContext.id, chatId: chatId)

navigationController?.pushViewController(chatViewController, animated: animated)
}

public func showArchive(animated: Bool) {
Expand All @@ -838,7 +883,7 @@ class ChatListViewController: UITableViewController {
}
}

// MARK: - uisearchbardelegate
// MARK: - UISearchBarDelegate
extension ChatListViewController: UISearchBarDelegate {
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
viewModel?.beginSearch()
Expand All @@ -860,6 +905,7 @@ extension ChatListViewController: UISearchBarDelegate {
}
}

// MARK: - ContactCellDelegate
extension ChatListViewController: ContactCellDelegate {
func onLongTap(at indexPath: IndexPath) {
if let searchActive = viewModel?.searchActive,
Expand All @@ -875,6 +921,7 @@ extension ChatListViewController: ContactCellDelegate {
}
}

// MARK: - ChatListEditingBarDelegate
extension ChatListViewController: ChatListEditingBarDelegate {
func onPinButtonPressed() {
viewModel?.pinChatsToggle(indexPaths: tableView.indexPathsForSelectedRows)
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
Loading