Skip to content

Commit

Permalink
Byte gallery-view-controller-observers
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Jul 12, 2024
1 parent ba158bf commit 21bb337
Showing 1 changed file with 6 additions and 30 deletions.
36 changes: 6 additions & 30 deletions deltachat-ios/Controller/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ class GalleryViewController: UIViewController {
private var galleryItemCache: [Int: GalleryItem] = [:]
private let gridDefaultSpacing: CGFloat = 5

private var msgChangedObserver: NSObjectProtocol?
private var msgReadDeliveredReactionFailedObserver: NSObjectProtocol?
private var incomingMsgObserver: NSObjectProtocol?

private lazy var gridLayout: GridCollectionViewFlowLayout = {
let layout = GridCollectionViewFlowLayout()
layout.minimumLineSpacing = gridDefaultSpacing
Expand Down Expand Up @@ -53,6 +49,10 @@ class GalleryViewController: UIViewController {
self.dcContext = context
self.chatId = chatId
super.init(nibName: nil, bundle: nil)

NotificationCenter.default.addObserver(self, selector: #selector(GalleryViewController.handleMessagesChanged(_:)), name: .messagesChanged, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GalleryViewController.handleMessageReadDeliveredFailedReaction(_:)), name: .messageReadDeliveredFailedReaction, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GalleryViewController.handleIncomingMessage(_:)), name: .incomingMessage, object: nil)
}

required init?(coder: NSCoder) {
Expand All @@ -73,8 +73,6 @@ class GalleryViewController: UIViewController {
super.willMove(toParent: parent)
if parent == nil {
removeObservers()

Check failure on line 75 in deltachat-ios/Controller/GalleryViewController.swift

View workflow job for this annotation

GitHub Actions / build

cannot find 'removeObservers' in scope
} else {
addObservers()
}
}
override func didReceiveMemoryWarning() {
Expand Down Expand Up @@ -107,31 +105,9 @@ class GalleryViewController: UIViewController {
}

// MARK: - Notifications
private func addObservers() {
msgReadDeliveredReactionFailedObserver = NotificationCenter.default.addObserver(
forName: .messageReadDeliveredFailedReaction, object: nil, queue: nil) { [weak self] notification in
self?.handleMessagesChanged(notification)
}
msgChangedObserver = NotificationCenter.default.addObserver(
forName: .messagesChanged, object: nil, queue: nil) { [weak self] _ in
self?.refreshInBg()
}
incomingMsgObserver = NotificationCenter.default.addObserver(
forName: .incomingMessage, object: nil, queue: nil) { [weak self] notification in
self?.handleIncomingMessage(notification)
}
}

private func removeObservers() {
if let msgChangedObserver {
NotificationCenter.default.removeObserver(msgChangedObserver)
}
if let msgReadDeliveredReactionFailedObserver {
NotificationCenter.default.removeObserver(msgReadDeliveredReactionFailedObserver)
}
if let incomingMsgObserver {
NotificationCenter.default.removeObserver(incomingMsgObserver)
}
@objc private func handleMessageReadDeliveredFailedReaction(_ notification: Notification) {
refreshInBg()
}

@objc private func handleMessagesChanged(_ notification: Notification) {
Expand Down

0 comments on commit 21bb337

Please sign in to comment.