瀏覽代碼

remove keyboardWillShow/keyboardWillHide observer from ChatViewController, let ChatInputBar handle that

cyberta 4 年之前
父節點
當前提交
1199e74910
共有 2 個文件被更改,包括 22 次插入57 次删除
  1. 13 49
      deltachat-ios/Chat/ChatViewController.swift
  2. 9 8
      deltachat-ios/Chat/Views/ChatInputBar.swift

+ 13 - 49
deltachat-ios/Chat/ChatViewController.swift

@@ -24,8 +24,6 @@ class ChatViewController: UITableViewController {
     var isDismissing = false
     var foregroundObserver: Any?
     var backgroundObserver: Any?
-    var keyboardWillShowObserver: Any?
-    var keyboardWillHideObserver: Any?
 
     lazy var isGroupChat: Bool = {
         return dcContext.getChat(chatId: chatId).isGroup
@@ -275,36 +273,6 @@ class ChatViewController: UITableViewController {
         }
     }
 
-    @objc func keyboardWillShow(_ notification: Notification) {
-        if isDismissing {
-            return
-        }
-        guard let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
-        let keyboardRectangle = keyboardFrame.cgRectValue
-        let keyboardHeight = keyboardRectangle.height
-        tableView.contentInset = UIEdgeInsets(top: self.getTopInsetHeight(), left: 0, bottom: keyboardHeight, right: 0)
-        // adapt the scroll position if the table view is not already moving and if the last row is visible
-        if self.isLastRowVisible() && !tableView.isDragging && !tableView.isDecelerating {
-            if self.messageInputBar.keyboardHeight > 0 {
-                logger.debug("scrolling keyboardWillShow - animated: true")
-                self.scrollToBottom(animated: true)
-            } else { // inputbar height increased, probably because of draft area changes
-                logger.debug("scrolling keyboardWillShow - animated: false")
-                self.scrollToBottom(animated: false)
-            }
-        }
-    }
-
-    @objc func keyboardWillHide(_ notification: Notification) {
-        if isDismissing {
-            return
-        }
-        guard let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
-        let keyboardRectangle = keyboardFrame.cgRectValue
-        let keyboardHeight = keyboardRectangle.height
-        tableView.contentInset = UIEdgeInsets(top: getTopInsetHeight(), left: 0, bottom: keyboardHeight, right: 0)
-    }
-
     private func getTopInsetHeight() -> CGFloat {
         return UIApplication.shared.statusBarFrame.height + (navigationController?.navigationBar.bounds.height ?? 0)
     }
@@ -432,17 +400,6 @@ class ChatViewController: UITableViewController {
                                             name: UIApplication.willResignActiveNotification,
                                             object: nil)
 
-        keyboardWillShowObserver = nc.addObserver(self,
-                                          selector: #selector(keyboardWillShow(_:)),
-                                          name: UIResponder.keyboardWillShowNotification,
-                                          object: nil)
-
-        keyboardWillHideObserver = nc.addObserver(self,
-                                                  selector: #selector(keyboardWillHide(_:)),
-                                                  name: UIResponder.keyboardWillHideNotification,
-                                                  object: nil)
-
-
         // things that do not affect the chatview
         // and are delayed after the view is displayed
         DispatchQueue.global(qos: .background).async { [weak self] in
@@ -483,12 +440,6 @@ class ChatViewController: UITableViewController {
         if let backgroundObserver = self.backgroundObserver {
             nc.removeObserver(backgroundObserver)
         }
-        if let keyboardWillShowObserver = self.keyboardWillShowObserver {
-            nc.removeObserver(keyboardWillShowObserver)
-        }
-        if let keyboardWillHideObserver = self.keyboardWillHideObserver {
-            nc.removeObserver(keyboardWillHideObserver)
-        }
         audioController.stopAnyOngoingPlaying()
 
     }
@@ -1477,6 +1428,19 @@ extension ChatViewController: InputBarAccessoryViewDelegate {
         draft.text = text
         evaluateInputBar(draft: draft)
     }
+
+    func inputBar(_ inputBar: InputBarAccessoryView, didChangeIntrinsicContentTo size: CGSize) {
+        if isDismissing {
+            return
+        }
+        self.tableView.contentInset = UIEdgeInsets(top: self.getTopInsetHeight(),
+                                                   left: 0,
+                                                   bottom: size.height + messageInputBar.keyboardHeight,
+                                                   right: 0)
+        if isLastRowVisible() {
+            self.scrollToBottom(animated: true)
+        }
+    }
 }
 
 // MARK: - DraftPreviewDelegate

+ 9 - 8
deltachat-ios/Chat/Views/ChatInputBar.swift

@@ -28,16 +28,14 @@ public class ChatInputBar: InputBarAccessoryView {
         NotificationCenter.default.addObserver(
             self,
             selector: #selector(keyboardChanged),
-            name: UIResponder.keyboardWillShowNotification,
-            object: nil
-        )
-        NotificationCenter.default.addObserver(
-            self,
-            selector: #selector(keyboardChanged),
-            name: UIResponder.keyboardDidHideNotification,
+            name: UIResponder.keyboardWillChangeFrameNotification,
             object: nil
         )
     }
+
+    deinit {
+        NotificationCenter.default.removeObserver(self)
+    }
     
     override open func calculateMaxTextViewHeight() -> CGFloat {
         if traitCollection.verticalSizeClass == .regular {
@@ -74,8 +72,10 @@ public class ChatInputBar: InputBarAccessoryView {
     @objc func keyboardChanged(_ notification: Notification) {
         if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
             let keyboardRectangle = keyboardFrame.cgRectValue
+            invalidateIntrinsicContentSize()
             keyboardHeight = keyboardRectangle.height - intrinsicContentSize.height
             updateTextViewHeight()
+            delegate?.inputBar(self, didChangeIntrinsicContentTo: intrinsicContentSize)
         }
     }
 
@@ -83,7 +83,9 @@ public class ChatInputBar: InputBarAccessoryView {
         super.traitCollectionDidChange(previousTraitCollection)
         if (self.traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass)
                 || (self.traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass) {
+            invalidateIntrinsicContentSize()
             updateTextViewHeight()
+            delegate?.inputBar(self, didChangeIntrinsicContentTo: intrinsicContentSize)
         }
     }
 
@@ -94,6 +96,5 @@ public class ChatInputBar: InputBarAccessoryView {
         } else if shouldForceTextViewMaxHeight {
             setShouldForceMaxTextViewHeight(to: false, animated: false)
         }
-        invalidateIntrinsicContentSize()
     }
 }