Эх сурвалжийг харах

Move scroll down button to ChatInputBar. This way vertical movements of the scroll down button are done without any further manual computation

cyberta 4 жил өмнө
parent
commit
24f8ce6d96

+ 4 - 35
deltachat-ios/Chat/ChatViewController.swift

@@ -63,13 +63,6 @@ class ChatViewController: UITableViewController {
     lazy var navBarTap: UITapGestureRecognizer = {
         UITapGestureRecognizer(target: self, action: #selector(chatProfilePressed))
     }()
-    
-    private lazy var scrollDownButton: UIButton = {
-        let button = UIButton(frame: .zero)
-        button.translatesAutoresizingMaskIntoConstraints = false
-        button.addTarget(self, action: #selector(onScrollDownButtonPressed), for: .touchUpInside)
-        return button
-    }()
 
     private var locationStreamingItem: UIBarButtonItem = {
         let indicator = LocationStreamingIndicator()
@@ -340,7 +333,7 @@ class ChatViewController: UITableViewController {
                         self.highlightedMsg = nil
                         self.isInitial = false
                         self.ignoreInputBarChange = false
-                        self.scrollDownButton.isHidden = self.isLastRowVisible()
+                        self.messageInputBar.scrollDownButton.isHidden = self.isLastRowVisible()
                     }
                 })
             } else {
@@ -441,10 +434,6 @@ class ChatViewController: UITableViewController {
         }
         
         handleUserVisibility(isVisible: true)
-        if let view = UIApplication.shared.keyWindow {
-            view.addSubview(scrollDownButton)
-            setupScrollDownButton()
-        }
     }
 
     override func viewWillDisappear(_ animated: Bool) {
@@ -476,10 +465,6 @@ class ChatViewController: UITableViewController {
         nc.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
         audioController.stopAnyOngoingPlaying()
         
-        
-        if let view = UIApplication.shared.keyWindow, scrollDownButton.isDescendant(of: view) {
-            scrollDownButton.removeFromSuperview()
-        }
     }
 
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
@@ -617,7 +602,7 @@ class ChatViewController: UITableViewController {
                     showAvatar: showAvatar,
                     showName: showName)
 
-        scrollDownButton.isHidden = isLastRowVisible()
+        messageInputBar.scrollDownButton.isHidden = isLastRowVisible()
 
         return cell
     }
@@ -735,7 +720,6 @@ class ChatViewController: UITableViewController {
     }
     
     override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
-        scrollDownButton.layer.borderColor = DcColors.colorDisabled.cgColor
         messageInputBar.inputTextView.layer.borderColor = DcColors.colorDisabled.cgColor
     }
 
@@ -839,8 +823,7 @@ class ChatViewController: UITableViewController {
         return tableView.indexPathsForVisibleRows?.contains(lastIndexPath) ?? false
     }
     
-    
-    @objc func onScrollDownButtonPressed() {
+    private func scrollToBottom() {
         scrollToBottom(animated: true)
     }
     
@@ -942,6 +925,7 @@ class ChatViewController: UITableViewController {
         if let inputTextView = messageInputBar.inputTextView as? ChatInputTextView {
             inputTextView.imagePasteDelegate = self
         }
+        messageInputBar.onScrollDownButtonPressed = scrollToBottom
     }
 
     private func evaluateInputBar(draft: DraftModel) {
@@ -1000,21 +984,6 @@ class ChatViewController: UITableViewController {
                     item.backgroundColor = DcColors.colorDisabled
                 })}
     }
-    
-    func setupScrollDownButton() {
-        NSLayoutConstraint.activate([
-            scrollDownButton.constraintAlignBottomTo(tableView, paddingBottom: 64),
-            scrollDownButton.constraintAlignTrailingTo(tableView, paddingTrailing: 12),
-            scrollDownButton.constraintHeightTo(40),
-            scrollDownButton.constraintWidthTo(40)
-        ])
-        scrollDownButton.backgroundColor = DcColors.defaultBackgroundColor
-        scrollDownButton.setImage(UIImage(named: "ic_scrolldown")?.sd_tintedImage(with: .systemBlue), for: .normal)
-        scrollDownButton.layer.cornerRadius = 20
-        scrollDownButton.layer.borderColor = DcColors.colorDisabled.cgColor
-        scrollDownButton.layer.borderWidth = 1
-        scrollDownButton.layer.masksToBounds = true
-    }
 
     @objc private func chatProfilePressed() {
         if chatId != DC_CHAT_ID_DEADDROP {

+ 52 - 0
deltachat-ios/Chat/Views/ChatInputBar.swift

@@ -8,6 +8,16 @@ public class ChatInputBar: InputBarAccessoryView {
     var hasDraft: Bool = false
     var hasQuote: Bool = false
     var keyboardHeight: CGFloat = 0
+    
+    var onScrollDownButtonPressed: (() -> Void)?
+    
+    lazy var scrollDownButton: UIButton = {
+        let button = UIButton(frame: .zero)
+        button.translatesAutoresizingMaskIntoConstraints = false
+        button.addTarget(self, action: #selector(onScrollDownPressed), for: .touchUpInside)
+        return button
+    }()
+    
 
     public convenience init() {
         self.init(frame: .zero)
@@ -26,8 +36,15 @@ public class ChatInputBar: InputBarAccessoryView {
 
     override open func setup() {
         replaceInputBar()
+        setupScrollDownButton()
         super.setup()
     }
+    
+    @objc func onScrollDownPressed() {
+        if let callback = onScrollDownButtonPressed {
+            callback()
+        }
+    }
 
     func replaceInputBar() {
         inputTextView = ChatInputTextView()
@@ -101,6 +118,7 @@ public class ChatInputBar: InputBarAccessoryView {
             updateTextViewHeight()
             delegate?.inputBar(self, didChangeIntrinsicContentTo: intrinsicContentSize)
         }
+        scrollDownButton.layer.borderColor = DcColors.colorDisabled.cgColor
     }
 
     private func updateTextViewHeight() {
@@ -113,4 +131,38 @@ public class ChatInputBar: InputBarAccessoryView {
             setShouldForceMaxTextViewHeight(to: false, animated: false)
         }
     }
+    
+    func setupScrollDownButton() {
+        self.addSubview(scrollDownButton)
+        NSLayoutConstraint.activate([
+            scrollDownButton.constraintAlignTopTo(self, paddingTop: -52),
+            scrollDownButton.constraintAlignTrailingTo(self, paddingTrailing: 12),
+            scrollDownButton.constraintHeightTo(40),
+            scrollDownButton.constraintWidthTo(40)
+        ])
+        scrollDownButton.backgroundColor = DcColors.defaultBackgroundColor
+        scrollDownButton.setImage(UIImage(named: "ic_scrolldown")?.sd_tintedImage(with: .systemBlue), for: .normal)
+        scrollDownButton.layer.cornerRadius = 20
+        scrollDownButton.layer.borderColor = DcColors.colorDisabled.cgColor
+        scrollDownButton.layer.borderWidth = 1
+        scrollDownButton.layer.masksToBounds = true
+        scrollDownButton.accessibilityLabel = String.localized("menu_scroll_to_bottom")
+    }
+    
+    public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
+        if !scrollDownButton.isHidden {
+            let scrollButtonViewPoint = self.scrollDownButton.convert(point, from: self)
+            if let view = scrollDownButton.hitTest(scrollButtonViewPoint, with: event) {
+                return view
+            }
+        }
+        return super.hitTest(point, with: event)
+    }
+    
+    public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
+        if !scrollDownButton.isHidden && scrollDownButton.point(inside: convert(point, to: scrollDownButton), with: event) {
+            return true
+        }
+        return super.point(inside: point, with: event)
+    }
 }