|
@@ -1,9 +1,10 @@
|
|
import UIKit
|
|
import UIKit
|
|
import DcCore
|
|
import DcCore
|
|
|
|
|
|
-class ChatListController: UITableViewController {
|
|
|
|
|
|
+class ChatListController: UITableViewController, AccountSwitcherHandler {
|
|
var viewModel: ChatListViewModel?
|
|
var viewModel: ChatListViewModel?
|
|
let dcContext: DcContext
|
|
let dcContext: DcContext
|
|
|
|
+ internal let dcAccounts: DcAccounts
|
|
var isArchive: Bool
|
|
var isArchive: Bool
|
|
|
|
|
|
private let chatCellReuseIdentifier = "chat_cell"
|
|
private let chatCellReuseIdentifier = "chat_cell"
|
|
@@ -13,6 +14,7 @@ class ChatListController: UITableViewController {
|
|
private var msgChangedObserver: NSObjectProtocol?
|
|
private var msgChangedObserver: NSObjectProtocol?
|
|
private var msgsNoticedObserver: NSObjectProtocol?
|
|
private var msgsNoticedObserver: NSObjectProtocol?
|
|
private var incomingMsgObserver: NSObjectProtocol?
|
|
private var incomingMsgObserver: NSObjectProtocol?
|
|
|
|
+ private var incomingMsgAnyAccountObserver: NSObjectProtocol?
|
|
private var chatModifiedObserver: NSObjectProtocol?
|
|
private var chatModifiedObserver: NSObjectProtocol?
|
|
private var contactsChangedObserver: NSObjectProtocol?
|
|
private var contactsChangedObserver: NSObjectProtocol?
|
|
private var connectivityChangedObserver: NSObjectProtocol?
|
|
private var connectivityChangedObserver: NSObjectProtocol?
|
|
@@ -69,8 +71,9 @@ class ChatListController: UITableViewController {
|
|
|
|
|
|
private var editingConstraints: NSLayoutConstraintSet?
|
|
private var editingConstraints: NSLayoutConstraintSet?
|
|
|
|
|
|
- init(dcContext: DcContext, isArchive: Bool) {
|
|
|
|
|
|
+ init(dcContext: DcContext, dcAccounts: DcAccounts, isArchive: Bool) {
|
|
self.dcContext = dcContext
|
|
self.dcContext = dcContext
|
|
|
|
+ self.dcAccounts = dcAccounts
|
|
self.isArchive = isArchive
|
|
self.isArchive = isArchive
|
|
super.init(style: .grouped)
|
|
super.init(style: .grouped)
|
|
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
|
|
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
|
|
@@ -185,6 +188,12 @@ class ChatListController: UITableViewController {
|
|
queue: nil) { [weak self] _ in
|
|
queue: nil) { [weak self] _ in
|
|
self?.refreshInBg()
|
|
self?.refreshInBg()
|
|
}
|
|
}
|
|
|
|
+ incomingMsgAnyAccountObserver = nc.addObserver(
|
|
|
|
+ forName: dcNotificationIncomingAnyAccount,
|
|
|
|
+ object: nil,
|
|
|
|
+ queue: nil) { [weak self] _ in
|
|
|
|
+ self?.updateAccountButton()
|
|
|
|
+ }
|
|
chatModifiedObserver = nc.addObserver(
|
|
chatModifiedObserver = nc.addObserver(
|
|
forName: dcNotificationChatModified,
|
|
forName: dcNotificationChatModified,
|
|
object: nil,
|
|
object: nil,
|
|
@@ -222,6 +231,9 @@ class ChatListController: UITableViewController {
|
|
if let incomingMsgObserver = self.incomingMsgObserver {
|
|
if let incomingMsgObserver = self.incomingMsgObserver {
|
|
nc.removeObserver(incomingMsgObserver)
|
|
nc.removeObserver(incomingMsgObserver)
|
|
}
|
|
}
|
|
|
|
+ if let incomingMsgAnyAccountObserver = self.incomingMsgAnyAccountObserver {
|
|
|
|
+ nc.removeObserver(incomingMsgAnyAccountObserver)
|
|
|
|
+ }
|
|
if let msgsNoticedObserver = self.msgsNoticedObserver {
|
|
if let msgsNoticedObserver = self.msgsNoticedObserver {
|
|
nc.removeObserver(msgsNoticedObserver)
|
|
nc.removeObserver(msgsNoticedObserver)
|
|
}
|
|
}
|
|
@@ -522,6 +534,62 @@ class ChatListController: UITableViewController {
|
|
view.isHidden = false
|
|
view.isHidden = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ lazy var accountButtonAvatar: InitialsBadge = {
|
|
|
|
+ let badge = InitialsBadge(size: 37, accessibilityLabel: String.localized("switch_account"))
|
|
|
|
+ badge.setLabelFont(UIFont.systemFont(ofSize: 14))
|
|
|
|
+ badge.accessibilityTraits = .button
|
|
|
|
+ return badge
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ private let accountButtonUnreadMessageCounter: MessageCounter = {
|
|
|
|
+ let view = MessageCounter(count: 0, size: 20)
|
|
|
|
+ view.backgroundColor = DcColors.unreadBadge
|
|
|
|
+ view.isHidden = true
|
|
|
|
+ return view
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ private lazy var accountButton: UIBarButtonItem = {
|
|
|
|
+ let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 37, height: 37))
|
|
|
|
+ containerView.addSubview(accountButtonAvatar)
|
|
|
|
+ containerView.addSubview(accountButtonUnreadMessageCounter)
|
|
|
|
+
|
|
|
|
+ let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showSwitchAccount))
|
|
|
|
+ containerView.addGestureRecognizer(tapGestureRecognizer)
|
|
|
|
+
|
|
|
|
+ return UIBarButtonItem(customView: containerView)
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ private func updateAccountButton() {
|
|
|
|
+ let unreadCount = getUnreadCounterOfOtherAccounts()
|
|
|
|
+ accountButtonUnreadMessageCounter.setCount(unreadCount)
|
|
|
|
+ accountButtonUnreadMessageCounter.isHidden = unreadCount == 0
|
|
|
|
+
|
|
|
|
+ let contact = dcContext.getContact(id: Int(DC_CONTACT_ID_SELF))
|
|
|
|
+ accountButtonAvatar.setColor(contact.color)
|
|
|
|
+ accountButtonAvatar.setName(contact.displayName)
|
|
|
|
+ if let image = contact.profileImage {
|
|
|
|
+ accountButtonAvatar.setImage(image)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private func getUnreadCounterOfOtherAccounts() -> Int {
|
|
|
|
+ var unreadCount = 0
|
|
|
|
+ let selectedAccountId = dcAccounts.getSelected().id
|
|
|
|
+
|
|
|
|
+ for accountId in dcAccounts.getAll() {
|
|
|
|
+ if accountId == selectedAccountId {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ unreadCount += dcAccounts.get(id: accountId).getFreshMessages().count
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return unreadCount
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @objc private func showSwitchAccount() {
|
|
|
|
+ showSwitchAccountMenu()
|
|
|
|
+ }
|
|
|
|
|
|
// MARK: updates
|
|
// MARK: updates
|
|
private func updateTitle() {
|
|
private func updateTitle() {
|
|
@@ -546,6 +614,8 @@ class ChatListController: UITableViewController {
|
|
titleView.accessibilityHint = "\(String.localized("connectivity_connected")): \(String.localized("a11y_connectivity_hint"))"
|
|
titleView.accessibilityHint = "\(String.localized("connectivity_connected")): \(String.localized("a11y_connectivity_hint"))"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ navigationItem.setLeftBarButton(accountButton, animated: false)
|
|
|
|
+ updateAccountButton()
|
|
}
|
|
}
|
|
titleView.isUserInteractionEnabled = !tableView.isEditing
|
|
titleView.isUserInteractionEnabled = !tableView.isEditing
|
|
titleView.sizeToFit()
|
|
titleView.sizeToFit()
|
|
@@ -741,7 +811,7 @@ class ChatListController: UITableViewController {
|
|
}
|
|
}
|
|
|
|
|
|
public func showArchive(animated: Bool) {
|
|
public func showArchive(animated: Bool) {
|
|
- let controller = ChatListController(dcContext: dcContext, isArchive: true)
|
|
|
|
|
|
+ let controller = ChatListController(dcContext: dcContext, dcAccounts: dcAccounts, isArchive: true)
|
|
navigationController?.pushViewController(controller, animated: animated)
|
|
navigationController?.pushViewController(controller, animated: animated)
|
|
}
|
|
}
|
|
|
|
|