From 077b884e153d75d1ff554e72bdb0db3b80232db5 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sun, 14 Jan 2018 16:02:00 +0800 Subject: [PATCH] code clean && recover message handle logic --- .../Sources/ConversationViewController.swift | 6 ++-- .../MessagesViewController+DataSource.swift | 35 ++++++++----------- .../MessagesViewController+Keyboard.swift | 12 +++---- .../MessagesViewController+Menu.swift | 8 ++--- .../MessagesCollectionViewFlowLayout.swift | 2 +- Sources/Models/Sender.swift | 2 +- 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index 184b5333..ffd9d70c 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -58,17 +58,17 @@ class ConversationViewController: MessagesViewController { maintainPositionOnKeyboardFrameChanged = true // default false messagesCollectionView.addSubview(refreshControl) - refreshControl.addTarget(self, action: #selector(loadMoreMessages), for: .valueChanged) + refreshControl.addTarget(self, action: #selector(ConversationViewController.loadMoreMessages), for: .valueChanged) navigationItem.rightBarButtonItems = [ UIBarButtonItem(image: UIImage(named: "ic_keyboard"), style: .plain, target: self, - action: #selector(handleKeyboardButton)), + action: #selector(ConversationViewController.handleKeyboardButton)), UIBarButtonItem(image: UIImage(named: "ic_typing"), style: .plain, target: self, - action: #selector(handleTyping)) + action: #selector(ConversationViewController.handleTyping)) ] } diff --git a/Sources/Controllers/MessagesViewController+DataSource.swift b/Sources/Controllers/MessagesViewController+DataSource.swift index fc280553..68abe2e0 100644 --- a/Sources/Controllers/MessagesViewController+DataSource.swift +++ b/Sources/Controllers/MessagesViewController+DataSource.swift @@ -40,7 +40,7 @@ extension MessagesViewController: UICollectionViewDataSource { } let messageCount = collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0 // There will only ever be 1 message per section - return messageCount > 0 ? 2 : 0 + return messageCount > 0 ? 1 : 0 } open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { @@ -53,28 +53,21 @@ extension MessagesViewController: UICollectionViewDataSource { fatalError(MessageKitError.nilMessagesDataSource) } - let isMessageCell = indexPath.row == 0 - let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView) - if isMessageCell { - - switch message.data { - case .text, .attributedText, .emoji: - let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath) - cell.configure(with: message, at: indexPath, and: messagesCollectionView) - return cell - case .photo, .video: - let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath) - cell.configure(with: message, at: indexPath, and: messagesCollectionView) - return cell - case .location: - let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath) - cell.configure(with: message, at: indexPath, and: messagesCollectionView) - return cell - } - } else { - return messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath) + switch message.data { + case .text, .attributedText, .emoji: + let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath) + cell.configure(with: message, at: indexPath, and: messagesCollectionView) + return cell + case .photo, .video: + let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath) + cell.configure(with: message, at: indexPath, and: messagesCollectionView) + return cell + case .location: + let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath) + cell.configure(with: message, at: indexPath, and: messagesCollectionView) + return cell } } diff --git a/Sources/Controllers/MessagesViewController+Keyboard.swift b/Sources/Controllers/MessagesViewController+Keyboard.swift index ef342068..a3535a56 100644 --- a/Sources/Controllers/MessagesViewController+Keyboard.swift +++ b/Sources/Controllers/MessagesViewController+Keyboard.swift @@ -29,9 +29,9 @@ extension MessagesViewController { // MARK: - Register / Unregister Observers func addKeyboardObservers() { - NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidChangeState), name: .UIKeyboardWillChangeFrame, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(handleTextViewDidBeginEditing), name: .UITextViewTextDidBeginEditing, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(adjustScrollViewInset), name: .UIDeviceOrientationDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleKeyboardDidChangeState(_:)), name: .UIKeyboardWillChangeFrame, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleTextViewDidBeginEditing(_:)), name: .UITextViewTextDidBeginEditing, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.adjustScrollViewInset), name: .UIDeviceOrientationDidChange, object: nil) } func removeKeyboardObservers() { @@ -43,7 +43,7 @@ extension MessagesViewController { // MARK: - Notification Handlers @objc - fileprivate func handleTextViewDidBeginEditing(_ notification: Notification) { + private func handleTextViewDidBeginEditing(_ notification: Notification) { if scrollsToBottomOnKeybordBeginsEditing { guard let inputTextView = notification.object as? InputTextView, inputTextView === messageInputBar.inputTextView else { return } messagesCollectionView.scrollToBottom(animated: true) @@ -51,7 +51,7 @@ extension MessagesViewController { } @objc - fileprivate func handleKeyboardDidChangeState(_ notification: Notification) { + private func handleKeyboardDidChangeState(_ notification: Notification) { guard let keyboardEndFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return } if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > UIScreen.main.bounds.height { @@ -96,7 +96,7 @@ extension MessagesViewController { /// for the MessagesCollectionView. /// /// - Returns: The safeAreaInsets.bottom if its an iPhoneX, else 0 - fileprivate var iPhoneXBottomInset: CGFloat { + private var iPhoneXBottomInset: CGFloat { if #available(iOS 11.0, *) { guard UIScreen.main.nativeBounds.height == 2436 else { return 0 } return view.safeAreaInsets.bottom diff --git a/Sources/Controllers/MessagesViewController+Menu.swift b/Sources/Controllers/MessagesViewController+Menu.swift index 93485d10..a1fa9de6 100644 --- a/Sources/Controllers/MessagesViewController+Menu.swift +++ b/Sources/Controllers/MessagesViewController+Menu.swift @@ -30,7 +30,7 @@ extension MessagesViewController { /// Add observer for `UIMenuControllerWillShowMenu` notification func addMenuControllerObservers() { - NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.menuControllerWillShow(notification:)), name: .UIMenuControllerWillShowMenu, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.menuControllerWillShow(_:)), name: .UIMenuControllerWillShowMenu, object: nil) } /// Remove observer for `UIMenuControllerWillShowMenu` notification @@ -42,7 +42,7 @@ extension MessagesViewController { /// Show menuController and set target rect to selected bubble @objc - fileprivate func menuControllerWillShow(notification: Notification) { + private func menuControllerWillShow(_ notification: Notification) { guard let currentMenuController = notification.object as? UIMenuController, let selectedIndexPath = selectedIndexPathForMenu else { return } @@ -50,7 +50,7 @@ extension MessagesViewController { NotificationCenter.default.removeObserver(self, name: .UIMenuControllerWillShowMenu, object: nil) defer { NotificationCenter.default.addObserver(self, - selector: #selector(MessagesViewController.menuControllerWillShow(notification:)), + selector: #selector(MessagesViewController.menuControllerWillShow(_:)), name: .UIMenuControllerWillShowMenu, object: nil) selectedIndexPathForMenu = nil } @@ -92,7 +92,7 @@ extension MessagesViewController { // MARK: - Helpers - fileprivate var navigationBarFrame: CGRect { + private var navigationBarFrame: CGRect { guard let navigationController = navigationController, !navigationController.navigationBar.isHidden else { return .zero } diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 698c2128..5053c470 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -104,7 +104,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { sectionInset = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8) - NotificationCenter.default.addObserver(self, selector: #selector(handleOrientationChange), name: .UIDeviceOrientationDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(MessagesCollectionViewFlowLayout.handleOrientationChange(_:)), name: .UIDeviceOrientationDidChange, object: nil) } required public init?(coder aDecoder: NSCoder) { diff --git a/Sources/Models/Sender.swift b/Sources/Models/Sender.swift index 575fe42b..c937ac5f 100644 --- a/Sources/Models/Sender.swift +++ b/Sources/Models/Sender.swift @@ -50,7 +50,7 @@ public struct Sender { extension Sender: Equatable { /// Two senders are considered equal if they have the same id. - static public func == (left: Sender, right: Sender) -> Bool { + public static func == (left: Sender, right: Sender) -> Bool { return left.id == right.id }