From a86bc8e6b1b153a1a04eef7941df3087cfd7c14b Mon Sep 17 00:00:00 2001 From: Sid McLaughlin Date: Wed, 1 Nov 2017 22:06:20 -0700 Subject: [PATCH 01/59] Refactor `initals` to `initials`. (#315) * Refactor `initals` to `initials`. * Update changelog. * Be more specific with changes. * Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ Sources/Models/Avatar.swift | 6 +++--- Sources/Views/AvatarView.swift | 14 +++++++------- Tests/ModelTests/AvatarTests.swift | 2 +- .../MessagesDisplayDelegateTests.swift | 2 +- Tests/ViewsTests/AvatarViewTests.swift | 10 +++++----- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f78f1d47..3debc9fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ## Upcoming release +### Fixed + ++- **Breaking Change** Fixed all instances of misspelled `inital` property. `Avatar.inital` has changed to `Avatar.initial` +and the initializer has changed from `public init(image: UIImage? = nil, initals: String = "?")` to `public init(image: UIImage? = nil, initials: String = "?")`. +[#298](https://github.com/MessageKit/MessageKit/issues/298) by [@sidmclaughlin](https://github.com/sidmclaughlin). + ## [[Prerelease] 0.10.1](https://github.com/MessageKit/MessageKit/releases/tag/0.10.1) ### Fixed diff --git a/Sources/Models/Avatar.swift b/Sources/Models/Avatar.swift index a7db2354..64993700 100644 --- a/Sources/Models/Avatar.swift +++ b/Sources/Models/Avatar.swift @@ -35,13 +35,13 @@ public struct Avatar { /// The placeholder initials to be used in the case where no image is provided. /// /// The default value of this property is "?". - public var initals: String = "?" + public var initials: String = "?" // MARK: - Initializer - public init(image: UIImage? = nil, initals: String = "?") { + public init(image: UIImage? = nil, initials: String = "?") { self.image = image - self.initals = initals + self.initials = initials } } diff --git a/Sources/Views/AvatarView.swift b/Sources/Views/AvatarView.swift index 44d5f229..3fdd1df9 100644 --- a/Sources/Views/AvatarView.swift +++ b/Sources/Views/AvatarView.swift @@ -83,7 +83,7 @@ open class AvatarView: UIView { self.init(frame: .zero) } - private func getImageFrom(initals: String) -> UIImage { + private func getImageFrom(initials: String) -> UIImage { let width = frame.width let height = frame.height if width == 0 || height == 0 {return UIImage()} @@ -95,8 +95,8 @@ open class AvatarView: UIView { //// Text Drawing let textRect = calculateTextRect(outerViewWidth: width, outerViewHeight: height) if adjustsFontSizeToFitWidth, - initals.width(considering: textRect.height, and: font) > textRect.width { - let newFontSize = calculateFontSize(text: initals, font: font, width: textRect.width, height: textRect.height) + initials.width(considering: textRect.height, and: font) > textRect.width { + let newFontSize = calculateFontSize(text: initials, font: font, width: textRect.width, height: textRect.height) font = placeholderFont.withSize(newFontSize) } @@ -104,10 +104,10 @@ open class AvatarView: UIView { textStyle.alignment = .center let textFontAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: placeholderTextColor, NSAttributedStringKey.paragraphStyle: textStyle] - let textTextHeight: CGFloat = initals.boundingRect(with: CGSize(width: textRect.width, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).height + let textTextHeight: CGFloat = initials.boundingRect(with: CGSize(width: textRect.width, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).height context.saveGState() context.clip(to: textRect) - initals.draw(in: CGRect(x: textRect.minX, y: textRect.minY + (textRect.height - textTextHeight) / 2, width: textRect.width, height: textTextHeight), withAttributes: textFontAttributes) + initials.draw(in: CGRect(x: textRect.minX, y: textRect.minY + (textRect.height - textTextHeight) / 2, width: textRect.width, height: textTextHeight), withAttributes: textFontAttributes) context.restoreGState() guard let renderedImage = UIGraphicsGetImageFromCurrentImageContext() else { assertionFailure("Could not create image from context"); return UIImage()} return renderedImage @@ -162,14 +162,14 @@ open class AvatarView: UIView { imageView.contentMode = .scaleAspectFill imageView.frame = frame addSubview(imageView) - imageView.image = avatar.image ?? getImageFrom(initals: avatar.initals) + imageView.image = avatar.image ?? getImageFrom(initials: avatar.initials) setCorner(radius: nil) } // MARK: - Open setters open func set(avatar: Avatar) { - imageView.image = avatar.image ?? getImageFrom(initals: avatar.initals) + imageView.image = avatar.image ?? getImageFrom(initials: avatar.initials) } open func setCorner(radius: CGFloat?) { diff --git a/Tests/ModelTests/AvatarTests.swift b/Tests/ModelTests/AvatarTests.swift index b77c545a..db2cf982 100644 --- a/Tests/ModelTests/AvatarTests.swift +++ b/Tests/ModelTests/AvatarTests.swift @@ -30,7 +30,7 @@ class AvatarTests: XCTestCase { func testDefaultInit() { let avatar = Avatar() XCTAssertNil(avatar.image) - XCTAssertEqual(avatar.initals, "?") + XCTAssertEqual(avatar.initials, "?") } } diff --git a/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift b/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift index e1e3f675..95a599e4 100644 --- a/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift +++ b/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift @@ -88,7 +88,7 @@ class MessagesDisplayDelegateTests: XCTestCase { func testAvatarDefaultState() { XCTAssertNotNil(sut.dataProvider.avatar(for: sut.dataProvider.messages[0], at: IndexPath(item: 0, section: 0), - in: sut.messagesCollectionView).initals) + in: sut.messagesCollectionView).initials) } func testCellTopLabelDefaultState() { diff --git a/Tests/ViewsTests/AvatarViewTests.swift b/Tests/ViewsTests/AvatarViewTests.swift index f0000e7a..aeceb264 100644 --- a/Tests/ViewsTests/AvatarViewTests.swift +++ b/Tests/ViewsTests/AvatarViewTests.swift @@ -41,7 +41,7 @@ class AvatarViewTests: XCTestCase { } func testNoParams() { - XCTAssertEqual(avatarView.avatar.initals, "?") + XCTAssertEqual(avatarView.avatar.initials, "?") XCTAssertEqual(avatarView.layer.cornerRadius, 15.0) XCTAssertEqual(avatarView.backgroundColor, UIColor.gray) } @@ -49,15 +49,15 @@ class AvatarViewTests: XCTestCase { func testWithImage() { let avatar = Avatar(image: UIImage()) avatarView.set(avatar: avatar) - XCTAssertEqual(avatar.initals, "?") + XCTAssertEqual(avatar.initials, "?") XCTAssertEqual(avatarView.layer.cornerRadius, 15.0) XCTAssertEqual(avatarView.backgroundColor, UIColor.gray) } - func testInitalsOnly() { - let avatar = Avatar(initals: "DL") + func testInitialsOnly() { + let avatar = Avatar(initials: "DL") avatarView.set(avatar: avatar) - XCTAssertEqual(avatar.initals, "DL") + XCTAssertEqual(avatar.initials, "DL") XCTAssertEqual(avatarView.layer.cornerRadius, 15.0) XCTAssertEqual(avatarView.backgroundColor, UIColor.gray) } From eea80108f76c8a3105fd471013de3e79903e101d Mon Sep 17 00:00:00 2001 From: Nathan Tannar Date: Mon, 23 Oct 2017 22:16:39 -0700 Subject: [PATCH 02/59] Add .top InputStackView and Docs --- Sources/Views/InputStackView.swift | 2 +- Sources/Views/MessageInputBar.swift | 276 +++++++++++++++++++++------- 2 files changed, 210 insertions(+), 68 deletions(-) diff --git a/Sources/Views/InputStackView.swift b/Sources/Views/InputStackView.swift index 522047f2..347d7d9a 100644 --- a/Sources/Views/InputStackView.swift +++ b/Sources/Views/InputStackView.swift @@ -40,7 +40,7 @@ open class InputStackView: UIStackView { /// - right: Bottom Stack View /// - bottom: Left Stack View public enum Position { - case left, right, bottom + case left, right, bottom, top } // MARK: Initialization diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index af5baa97..7737196e 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -24,6 +24,7 @@ import UIKit +/// A powerful InputAccessoryView ideal for messaging applications open class MessageInputBar: UIView { public enum UIStackViewPosition { @@ -32,28 +33,61 @@ open class MessageInputBar: UIView { // MARK: - Properties + /// A delegate to broadcast notifications from the MessageInputBar open weak var delegate: MessageInputBarDelegate? - /// A background view that adds a blur effect. Shown when 'isTransparent' is set to TRUE. Hidden by default. - open let blurView: UIView = { - let blurEffect = UIBlurEffect(style: .extraLight) - let view = UIVisualEffectView(effect: blurEffect) + /// The background UIView anchored to the bottom, left, and right of the MessageInputBar + /// with a top anchor equal to the bottom of the top InputStackView + open var backgroundView: UIView = { + let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false - view.isHidden = true + view.backgroundColor = .white return view }() - /// When set to true, the blurView in the background is shown and the backgroundColor is set to .clear. Default is FALSE - open var isTranslucent: Bool = false { + /// Also sets the backgroundView's backgroundColor to the newValue + open override var backgroundColor: UIColor? { didSet { - blurView.isHidden = !isTranslucent - backgroundColor = isTranslucent ? .clear : .white + backgroundView.backgroundColor = backgroundColor } } - /// A boarder line anchored to the top of the view + /** + A UIVisualEffectView that adds a blur effect to make the view appear transparent. + + ## Important Notes ## + 1. The blurView is initially not added to the backgroundView to improve performance when not needed. When `isTranslucent` is set to TRUE for the first time the blurView is added and anchored to the `backgroundView`s edge anchors + */ + open var blurView: UIVisualEffectView = { + let blurEffect = UIBlurEffect(style: .light) + let view = UIVisualEffectView(effect: blurEffect) + view.translatesAutoresizingMaskIntoConstraints = false + return view + }() + + /// Determines if the MessageInputBar should have a translucent effect + open var isTranslucent: Bool = false { + didSet { + if isTranslucent && blurView.superview == nil { + backgroundView.addSubview(blurView) + blurView.fillSuperview() + } + blurView.isHidden = !isTranslucent + backgroundView.backgroundColor = isTranslucent ? (backgroundView.backgroundColor?.withAlphaComponent(0.7) ?? UIColor.white.withAlphaComponent(0.7)) : .white + } + } + + /// A SeparatorLine that is initially placed in the topStackView open let separatorLine = SeparatorLine() + /** + The InputStackView at the InputStackView.top position + + ## Important Notes ## + 1. It's axis is initially set to .vertical + */ + open let topStackView = InputStackView(axis: .vertical, spacing: 0) + /** The InputStackView at the InputStackView.left position @@ -78,24 +112,16 @@ open class MessageInputBar: UIView { 2. It's spacing is initially set to 15 */ open let bottomStackView = InputStackView(axis: .horizontal, spacing: 15) - + + /// The InputTextView a user can input a message in open lazy var inputTextView: InputTextView = { [weak self] in let textView = InputTextView() textView.translatesAutoresizingMaskIntoConstraints = false textView.messageInputBar = self return textView }() - - /// The padding around the textView that separates it from the stackViews - open var textViewPadding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8) { - didSet { - textViewLayoutSet?.bottom?.constant = -textViewPadding.bottom - textViewLayoutSet?.left?.constant = textViewPadding.left - textViewLayoutSet?.right?.constant = -textViewPadding.right - bottomStackViewLayoutSet?.top?.constant = textViewPadding.bottom - } - } + /// A InputBarButtonItem used as the send button and initially placed in the rightStackView open var sendButton: InputBarButtonItem = { return InputBarButtonItem() .configure { @@ -108,46 +134,82 @@ open class MessageInputBar: UIView { } }() - /// The anchor contants used by the UIStackViews and InputTextView to create padding within the InputBarAccessoryView + /** + The anchor contants used by the InputStackView's and InputTextView to create padding + within the MessageInputBar + + ## Important Notes ## + + ```` + V:|...-[InputStackView.top]-(padding.top)-[InputTextView]-(textViewPadding.bottom)-[InputStackView.bottom]-(padding.bottom)-| + + H:|-(padding.left)-[InputStackView.left(leftStackViewWidthConstant)]-(textViewPadding.left)-[InputTextView]-(textViewPadding.right)-[InputStackView.right(rightStackViewWidthConstant)]-(padding.right)-| + ```` + + */ open var padding: UIEdgeInsets = UIEdgeInsets(top: 6, left: 12, bottom: 6, right: 12) { didSet { - updateViewContraints() + updatePadding() + } + } + + /** + The anchor constants used by the top InputStackView + + ## Important Notes ## + 1. The topStackViewPadding.bottom property is not used. Use padding.top + + ```` + V:|-(topStackViewPadding.top)-[InputStackView.top]-(padding.top)-[InputTextView]-...| + + H:|-(topStackViewPadding.left)-[InputStackView.top]-(topStackViewPadding.right)-| + ```` + + */ + open var topStackViewPadding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) { + didSet { + updateTopStackViewPadding() + } + } + + /** + The anchor constants used by the InputStackView + + ## Important Notes ## + 1. The inputTextViewPadding.top property is not used. Use padding.top + + ```` + V:|...-(padding.top)-[InputTextView]-(inputTextViewPadding.bottom)-[InputStackView.bottom]-...| + + H:|...-[InputStackView.left]-(inputTextViewPadding.left)-[InputTextView]-(inputTextViewPadding.left)-[InputStackView.left.right]-...| + ```` + + */ + open var textViewPadding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8) { + didSet { + updateTextViewPadding() } } open override var intrinsicContentSize: CGSize { - let maxSize = CGSize(width: inputTextView.bounds.width, height: .greatestFiniteMagnitude) - let sizeToFit = inputTextView.sizeThatFits(maxSize) - var heightToFit = sizeToFit.height.rounded() + padding.top + padding.bottom - - if heightToFit >= maxHeight { - if !isOverMaxHeight { - textViewHeightAnchor?.isActive = true - inputTextView.isScrollEnabled = true - isOverMaxHeight = true - } - heightToFit = maxHeight - } else { - if isOverMaxHeight { - textViewHeightAnchor?.isActive = false - inputTextView.isScrollEnabled = false - isOverMaxHeight = false - } - } - - let size = CGSize(width: bounds.width, height: heightToFit) - + let size = calculateIntrinsicContentSize() if previousIntrinsicContentSize != size { delegate?.messageInputBar(self, didChangeIntrinsicContentTo: size) previousIntrinsicContentSize = size } - return size } - private(set) var isOverMaxHeight = false + /// The intrinsicContentSize can change a lot so the delegate method + /// `inputBar(self, didChangeIntrinsicContentTo: size)` only needs to be called + /// when it's different + private var previousIntrinsicContentSize: CGSize? - /// The maximum intrinsicContentSize height. When reached the delegate 'didChangeIntrinsicContentTo' will be called. + /// A boolean that indicates if the maxTextViewHeight has been met. Keeping track of this + /// improves the performance + private(set) public var isOverMaxTextViewHeight = false + + /// The maximum height that the InputTextView can reach open var maxHeight: CGFloat = UIScreen.main.bounds.height / 3 { didSet { textViewHeightAnchor?.constant = maxHeight @@ -156,27 +218,30 @@ open class MessageInputBar: UIView { } /// The fixed widthAnchor constant of the leftStackView - private(set) var leftStackViewWidthContant: CGFloat = 0 { + private(set) var leftStackViewWidthConstant: CGFloat = 0 { didSet { - leftStackViewLayoutSet?.width?.constant = leftStackViewWidthContant + leftStackViewLayoutSet?.width?.constant = leftStackViewWidthConstant } } /// The fixed widthAnchor constant of the rightStackView - private(set) var rightStackViewWidthContant: CGFloat = 52 { + private(set) var rightStackViewWidthConstant: CGFloat = 52 { didSet { - rightStackViewLayoutSet?.width?.constant = rightStackViewWidthContant + rightStackViewLayoutSet?.width?.constant = rightStackViewWidthConstant } } /// The InputBarItems held in the leftStackView - private(set) var leftStackViewItems: [InputBarButtonItem] = [] + private(set) public var leftStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held in the rightStackView - private(set) var rightStackViewItems: [InputBarButtonItem] = [] + private(set) public var rightStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held in the bottomStackView - private(set) var bottomStackViewItems: [InputBarButtonItem] = [] + private(set) public var bottomStackViewItems: [InputBarButtonItem] = [] + + /// The InputBarItems held in the topStackView + private(set) public var topStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held to make use of their hooks but they are not automatically added to a UIStackView open var nonStackViewItems: [InputBarButtonItem] = [] @@ -190,10 +255,11 @@ open class MessageInputBar: UIView { private var textViewLayoutSet: NSLayoutConstraintSet? private var textViewHeightAnchor: NSLayoutConstraint? + private var topStackViewHeightAnchor: NSLayoutConstraint? + private var topStackViewLayoutSet: NSLayoutConstraintSet? private var leftStackViewLayoutSet: NSLayoutConstraintSet? private var rightStackViewLayoutSet: NSLayoutConstraintSet? private var bottomStackViewLayoutSet: NSLayoutConstraintSet? - private var previousIntrinsicContentSize: CGSize? // MARK: - Initialization @@ -217,6 +283,7 @@ open class MessageInputBar: UIView { // MARK: - Setup + /// Sets up the default properties open func setup() { backgroundColor = .inputBarGray @@ -226,24 +293,32 @@ open class MessageInputBar: UIView { setupObservers() } + /// Adds all of the subviews private func setupSubviews() { - addSubview(blurView) + addSubview(backgroundView) + addSubview(topStackView) addSubview(inputTextView) addSubview(leftStackView) addSubview(rightStackView) addSubview(bottomStackView) - addSubview(separatorLine) + topStackView.addArrangedSubview(separatorLine) setStackViewItems([sendButton], forStack: .right, animated: false) } + /// Sets up the initial constraints of each subview private func setupConstraints() { - separatorLine.addConstraints(topAnchor, left: leftAnchor, right: rightAnchor, heightConstant: 0.5) - blurView.fillSuperview() + topStackViewLayoutSet = NSLayoutConstraintSet( + top: topStackView.topAnchor.constraint(equalTo: topAnchor, constant: topStackViewPadding.top), + bottom: topStackView.bottomAnchor.constraint(equalTo: inputTextView.topAnchor, constant: -padding.top), + left: topStackView.leftAnchor.constraint(equalTo: leftAnchor, constant: topStackViewPadding.left), + right: topStackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -topStackViewPadding.right) + ) + backgroundView.addConstraints(topStackView.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor) textViewLayoutSet = NSLayoutConstraintSet( - top: inputTextView.topAnchor.constraint(equalTo: topAnchor, constant: padding.top), + top: inputTextView.topAnchor.constraint(equalTo: topStackView.bottomAnchor, constant: padding.top), bottom: inputTextView.bottomAnchor.constraint(equalTo: bottomStackView.topAnchor, constant: -textViewPadding.bottom), left: inputTextView.leftAnchor.constraint(equalTo: leftStackView.rightAnchor, constant: textViewPadding.left), right: inputTextView.rightAnchor.constraint(equalTo: rightStackView.leftAnchor, constant: -textViewPadding.right) @@ -254,14 +329,14 @@ open class MessageInputBar: UIView { top: leftStackView.topAnchor.constraint(equalTo: topAnchor, constant: padding.top), bottom: leftStackView.bottomAnchor.constraint(equalTo: inputTextView.bottomAnchor, constant: 0), left: leftStackView.leftAnchor.constraint(equalTo: leftAnchor, constant: padding.left), - width: leftStackView.widthAnchor.constraint(equalToConstant: leftStackViewWidthContant) + width: leftStackView.widthAnchor.constraint(equalToConstant: leftStackViewWidthConstant) ) rightStackViewLayoutSet = NSLayoutConstraintSet( top: rightStackView.topAnchor.constraint(equalTo: topAnchor, constant: padding.top), bottom: rightStackView.bottomAnchor.constraint(equalTo: inputTextView.bottomAnchor, constant: 0), right: rightStackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -padding.right), - width: rightStackView.widthAnchor.constraint(equalToConstant: rightStackViewWidthContant) + width: rightStackView.widthAnchor.constraint(equalToConstant: rightStackViewWidthConstant) ) bottomStackViewLayoutSet = NSLayoutConstraintSet( @@ -273,19 +348,22 @@ open class MessageInputBar: UIView { if #available(iOS 11.0, *) { // Switch to safeAreaLayoutGuide + topStackViewLayoutSet?.left = topStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: topStackViewPadding.left) + topStackViewLayoutSet?.right = topStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: topStackViewPadding.right) leftStackViewLayoutSet?.left = leftStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: padding.left) rightStackViewLayoutSet?.right = rightStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -padding.right) bottomStackViewLayoutSet?.bottom = bottomStackView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -padding.bottom) bottomStackViewLayoutSet?.left = bottomStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: padding.left) bottomStackViewLayoutSet?.right = bottomStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -padding.right) } + topStackViewLayoutSet?.activate() leftStackViewLayoutSet?.activate() rightStackViewLayoutSet?.activate() bottomStackViewLayoutSet?.activate() } - private func updateViewContraints() { - + /// Updates the constraint constants that correspond to the padding UIEdgeInsets + private func updatePadding() { textViewLayoutSet?.top?.constant = padding.top leftStackViewLayoutSet?.top?.constant = padding.top leftStackViewLayoutSet?.left?.constant = padding.left @@ -296,6 +374,22 @@ open class MessageInputBar: UIView { bottomStackViewLayoutSet?.bottom?.constant = -padding.bottom } + /// Updates the constraint constants that correspond to the inputTextViewPadding UIEdgeInsets + private func updateTextViewPadding() { + textViewLayoutSet?.left?.constant = textViewPadding.left + textViewLayoutSet?.right?.constant = -textViewPadding.right + textViewLayoutSet?.bottom?.constant = -textViewPadding.bottom + bottomStackViewLayoutSet?.top?.constant = textViewPadding.bottom + } + + /// Updates the constraint constants that correspond to the topStackViewPadding UIEdgeInsets + private func updateTopStackViewPadding() { + topStackViewLayoutSet?.top?.constant = topStackViewPadding.top + topStackViewLayoutSet?.left?.constant = topStackViewPadding.left + topStackViewLayoutSet?.right?.constant = -topStackViewPadding.right + } + + /// Adds the required notification observers private func setupObservers() { NotificationCenter.default.addObserver(self, @@ -312,6 +406,30 @@ open class MessageInputBar: UIView { name: .UITextViewTextDidEndEditing, object: nil) } + /// Calculates the correct intrinsicContentSize of the MessageInputBar + /// + /// - Returns: The required intrinsicContentSize + open func calculateIntrinsicContentSize() -> CGSize { + + let maxTextViewSize = CGSize(width: inputTextView.bounds.width, height: .greatestFiniteMagnitude) + var heightToFit = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() + if heightToFit >= maxHeight { + if !isOverMaxTextViewHeight { + textViewHeightAnchor?.isActive = true + inputTextView.isScrollEnabled = true + isOverMaxTextViewHeight = true + } + heightToFit = maxHeight + } else { + if isOverMaxTextViewHeight { + textViewHeightAnchor?.isActive = false + inputTextView.isScrollEnabled = false + isOverMaxTextViewHeight = false + } + } + return CGSize(width: bounds.width, height: heightToFit) + } + // MARK: - Layout Helper Methods /// Layout the given UIStackView's @@ -330,6 +448,9 @@ open class MessageInputBar: UIView { case .bottom: bottomStackView.setNeedsLayout() bottomStackView.layoutIfNeeded() + case .top: + bottomStackView.setNeedsLayout() + bottomStackView.layoutIfNeeded() } } } @@ -345,6 +466,7 @@ open class MessageInputBar: UIView { leftStackViewLayoutSet?.deactivate() rightStackViewLayoutSet?.deactivate() bottomStackViewLayoutSet?.deactivate() + topStackViewLayoutSet?.deactivate() if animated { DispatchQueue.main.async { UIView.animate(withDuration: 0.3, animations: animations) @@ -356,11 +478,12 @@ open class MessageInputBar: UIView { leftStackViewLayoutSet?.activate() rightStackViewLayoutSet?.activate() bottomStackViewLayoutSet?.activate() + topStackViewLayoutSet?.activate() } // MARK: - UIStackView InputBarItem Methods - /// Removes all of the arranged subviews from the UIStackView and adds the given items. Sets the inputBarAccessoryView property of the InputBarButtonItem + /// Removes all of the arranged subviews from the UIStackView and adds the given items. Sets the messageInputBar property of the InputBarButtonItem /// /// - Parameters: /// - items: New UIStackView arranged views @@ -397,6 +520,15 @@ open class MessageInputBar: UIView { bottomStackView.addArrangedSubview($0) } bottomStackView.layoutIfNeeded() + case .top: + topStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } + topStackViewItems = items + topStackViewItems.forEach { + $0.messageInputBar = self + $0.parentStackViewPosition = position + topStackView.addArrangedSubview($0) + } + topStackView.layoutIfNeeded() } } @@ -412,7 +544,7 @@ open class MessageInputBar: UIView { /// - animated: If the layout should be animated open func setLeftStackViewWidthConstant(to newValue: CGFloat, animated: Bool) { performLayout(animated) { - self.leftStackViewWidthContant = newValue + self.leftStackViewWidthConstant = newValue self.layoutStackViews([.left]) self.layoutIfNeeded() } @@ -425,7 +557,7 @@ open class MessageInputBar: UIView { /// - animated: If the layout should be animated open func setRightStackViewWidthConstant(to newValue: CGFloat, animated: Bool) { performLayout(animated) { - self.rightStackViewWidthContant = newValue + self.rightStackViewWidthConstant = newValue self.layoutStackViews([.right]) self.layoutIfNeeded() } @@ -433,11 +565,16 @@ open class MessageInputBar: UIView { // MARK: - Notifications/Hooks + /// Invalidates the intrinsicContentSize @objc open func orientationDidChange() { invalidateIntrinsicContentSize() } + /// Enables/Disables the sendButton based on the InputTextView's text being empty + /// Calls each items `textViewDidChangeAction` method + /// Calls the delegates `textViewTextDidChangeTo` method + /// Invalidates the intrinsicContentSize @objc open func textViewDidChange() { let trimmedText = inputTextView.text.trimmingCharacters(in: .whitespacesAndNewlines) @@ -451,11 +588,13 @@ open class MessageInputBar: UIView { invalidateIntrinsicContentSize() } + /// Calls each items `keyboardEditingBeginsAction` method @objc open func textViewDidBeginEditing() { self.items.forEach { $0.keyboardEditingBeginsAction() } } + /// Calls each items `keyboardEditingEndsAction` method @objc open func textViewDidEndEditing() { self.items.forEach { $0.keyboardEditingEndsAction() } @@ -463,6 +602,9 @@ open class MessageInputBar: UIView { // MARK: - User Actions + /// Calls the delegates `didPressSendButtonWith` method + /// Assumes that the InputTextView's text has been set to empty and calls `inputTextViewDidChange()` + /// Invalidates each of the inputManagers open func didSelectSendButton() { delegate?.messageInputBar(self, didPressSendButtonWith: inputTextView.text) textViewDidChange() From 35eb42382c09d21cad5f0096454282dea47fa1f8 Mon Sep 17 00:00:00 2001 From: Nathan Tannar Date: Thu, 2 Nov 2017 20:44:40 -0700 Subject: [PATCH 03/59] Rebase onto dev with some minor fixes --- Sources/Views/MessageInputBar.swift | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 7737196e..dea01d55 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -73,7 +73,7 @@ open class MessageInputBar: UIView { blurView.fillSuperview() } blurView.isHidden = !isTranslucent - backgroundView.backgroundColor = isTranslucent ? (backgroundView.backgroundColor?.withAlphaComponent(0.7) ?? UIColor.white.withAlphaComponent(0.7)) : .white + backgroundView.backgroundColor = isTranslucent ? (backgroundView.backgroundColor?.withAlphaComponent(0.75) ?? UIColor.white.withAlphaComponent(0.75)) : .white } } @@ -85,8 +85,13 @@ open class MessageInputBar: UIView { ## Important Notes ## 1. It's axis is initially set to .vertical + 2. It's alignment is initially set to .fill */ - open let topStackView = InputStackView(axis: .vertical, spacing: 0) + open let topStackView: InputStackView = { + let stackView = InputStackView(axis: .vertical, spacing: 0) + stackView.alignment = .fill + return stackView + }() /** The InputStackView at the InputStackView.left position @@ -203,7 +208,7 @@ open class MessageInputBar: UIView { /// The intrinsicContentSize can change a lot so the delegate method /// `inputBar(self, didChangeIntrinsicContentTo: size)` only needs to be called /// when it's different - private var previousIntrinsicContentSize: CGSize? + private(set) public var previousIntrinsicContentSize: CGSize? /// A boolean that indicates if the maxTextViewHeight has been met. Keeping track of this /// improves the performance @@ -218,14 +223,14 @@ open class MessageInputBar: UIView { } /// The fixed widthAnchor constant of the leftStackView - private(set) var leftStackViewWidthConstant: CGFloat = 0 { + private(set) public var leftStackViewWidthConstant: CGFloat = 0 { didSet { leftStackViewLayoutSet?.width?.constant = leftStackViewWidthConstant } } /// The fixed widthAnchor constant of the rightStackView - private(set) var rightStackViewWidthConstant: CGFloat = 52 { + private(set) public var rightStackViewWidthConstant: CGFloat = 52 { didSet { rightStackViewLayoutSet?.width?.constant = rightStackViewWidthConstant } @@ -255,7 +260,6 @@ open class MessageInputBar: UIView { private var textViewLayoutSet: NSLayoutConstraintSet? private var textViewHeightAnchor: NSLayoutConstraint? - private var topStackViewHeightAnchor: NSLayoutConstraint? private var topStackViewLayoutSet: NSLayoutConstraintSet? private var leftStackViewLayoutSet: NSLayoutConstraintSet? private var rightStackViewLayoutSet: NSLayoutConstraintSet? @@ -425,6 +429,7 @@ open class MessageInputBar: UIView { textViewHeightAnchor?.isActive = false inputTextView.isScrollEnabled = false isOverMaxTextViewHeight = false + inputTextView.invalidateIntrinsicContentSize() } } return CGSize(width: bounds.width, height: heightToFit) @@ -435,7 +440,7 @@ open class MessageInputBar: UIView { /// Layout the given UIStackView's /// /// - Parameter positions: The UIStackView's to layout - public func layoutStackViews(_ positions: [InputStackView.Position] = [.left, .right, .bottom]) { + public func layoutStackViews(_ positions: [InputStackView.Position] = [.left, .right, .bottom, .top]) { for position in positions { switch position { From a1fd00c3269b719645e4ebddd8c526264ea15fec Mon Sep 17 00:00:00 2001 From: Nathan Tannar Date: Thu, 2 Nov 2017 21:03:37 -0700 Subject: [PATCH 04/59] Typo Correction in Example --- Example/Sources/SampleData.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Example/Sources/SampleData.swift b/Example/Sources/SampleData.swift index 95f3a1c6..786524b4 100644 --- a/Example/Sources/SampleData.swift +++ b/Example/Sources/SampleData.swift @@ -185,11 +185,11 @@ final class SampleData { func getAvatarFor(sender: Sender) -> Avatar { switch sender { case dan: - return Avatar(image: #imageLiteral(resourceName: "Dan-Leonard"), initals: "DL") + return Avatar(image: #imageLiteral(resourceName: "Dan-Leonard"), initials: "DL") case steven: - return Avatar(initals: "S") + return Avatar(initials: "S") case jobs: - return Avatar(image: #imageLiteral(resourceName: "Steve-Jobs"), initals: "SJ") + return Avatar(image: #imageLiteral(resourceName: "Steve-Jobs"), initials: "SJ") case cook: return Avatar(image: #imageLiteral(resourceName: "Tim-Cook")) default: From 85d57ca32cff51bc9e8e634b4e6e99bcf5a8909b Mon Sep 17 00:00:00 2001 From: Nathan Tannar Date: Thu, 2 Nov 2017 21:58:52 -0700 Subject: [PATCH 05/59] Update Tests --- Tests/ViewsTests/MessageInputBarTests.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Tests/ViewsTests/MessageInputBarTests.swift b/Tests/ViewsTests/MessageInputBarTests.swift index 8b8fbdd1..a90622fe 100644 --- a/Tests/ViewsTests/MessageInputBarTests.swift +++ b/Tests/ViewsTests/MessageInputBarTests.swift @@ -43,23 +43,17 @@ class MessageInputBarTests: XCTestCase { XCTAssertFalse(sut.blurView.translatesAutoresizingMaskIntoConstraints) } - func testBlurEffectIsHidden_isTrueAfterInit() { - XCTAssertTrue(sut.blurView.isHidden) - } - func testIsTranslucent_isFalseForDefault() { XCTAssertFalse(sut.isTranslucent) } func testUISetups_forIsTranslucentIsTrue() { sut.isTranslucent = true - XCTAssertEqual(sut.backgroundColor, UIColor.clear) XCTAssertFalse(sut.blurView.isHidden) } func testUISetups_forIsTranslucentIsFalse() { sut.isTranslucent = false - XCTAssertEqual(sut.backgroundColor, UIColor.white) XCTAssertTrue(sut.blurView.isHidden) } From 0cdf0bdf36d61cd96317877c5e1183b155152585 Mon Sep 17 00:00:00 2001 From: Nathan Tannar Date: Thu, 2 Nov 2017 22:14:25 -0700 Subject: [PATCH 06/59] Suggestions from PR #320 --- CHANGELOG.md | 5 +++++ Sources/Views/InputStackView.swift | 1 + Sources/Views/MessageInputBar.swift | 19 ++++++++++--------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3debc9fc..b89ce880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ## Upcoming release +### Added + ++- **Breaking Change** Added a top `InputStackView` to `MessageInputBar`. This adds the addition of the `.top` case to `InputStackView.Position`. +[#320](https://github.com/MessageKit/MessageKit/issues/320) by [@nathantannar4](https://github.com/nathantannar4). + ### Fixed +- **Breaking Change** Fixed all instances of misspelled `inital` property. `Avatar.inital` has changed to `Avatar.initial` diff --git a/Sources/Views/InputStackView.swift b/Sources/Views/InputStackView.swift index 347d7d9a..0c86c315 100644 --- a/Sources/Views/InputStackView.swift +++ b/Sources/Views/InputStackView.swift @@ -39,6 +39,7 @@ open class InputStackView: UIStackView { /// - left: Left Stack View /// - right: Bottom Stack View /// - bottom: Left Stack View + /// - top: Top Stack View public enum Position { case left, right, bottom, top } diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index dea01d55..d4441b06 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -73,7 +73,8 @@ open class MessageInputBar: UIView { blurView.fillSuperview() } blurView.isHidden = !isTranslucent - backgroundView.backgroundColor = isTranslucent ? (backgroundView.backgroundColor?.withAlphaComponent(0.75) ?? UIColor.white.withAlphaComponent(0.75)) : .white + let color: UIColor = backgroundView.backgroundColor ?? .white + backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : .white } } @@ -208,11 +209,11 @@ open class MessageInputBar: UIView { /// The intrinsicContentSize can change a lot so the delegate method /// `inputBar(self, didChangeIntrinsicContentTo: size)` only needs to be called /// when it's different - private(set) public var previousIntrinsicContentSize: CGSize? + public private(set) var previousIntrinsicContentSize: CGSize? /// A boolean that indicates if the maxTextViewHeight has been met. Keeping track of this /// improves the performance - private(set) public var isOverMaxTextViewHeight = false + public private(set) var isOverMaxTextViewHeight = false /// The maximum height that the InputTextView can reach open var maxHeight: CGFloat = UIScreen.main.bounds.height / 3 { @@ -223,30 +224,30 @@ open class MessageInputBar: UIView { } /// The fixed widthAnchor constant of the leftStackView - private(set) public var leftStackViewWidthConstant: CGFloat = 0 { + public private(set) var leftStackViewWidthConstant: CGFloat = 0 { didSet { leftStackViewLayoutSet?.width?.constant = leftStackViewWidthConstant } } /// The fixed widthAnchor constant of the rightStackView - private(set) public var rightStackViewWidthConstant: CGFloat = 52 { + public private(set) var rightStackViewWidthConstant: CGFloat = 52 { didSet { rightStackViewLayoutSet?.width?.constant = rightStackViewWidthConstant } } /// The InputBarItems held in the leftStackView - private(set) public var leftStackViewItems: [InputBarButtonItem] = [] + public private(set) var leftStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held in the rightStackView - private(set) public var rightStackViewItems: [InputBarButtonItem] = [] + public private(set) var rightStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held in the bottomStackView - private(set) public var bottomStackViewItems: [InputBarButtonItem] = [] + public private(set) var bottomStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held in the topStackView - private(set) public var topStackViewItems: [InputBarButtonItem] = [] + public private(set) var topStackViewItems: [InputBarButtonItem] = [] /// The InputBarItems held to make use of their hooks but they are not automatically added to a UIStackView open var nonStackViewItems: [InputBarButtonItem] = [] From 117dd63fe0482e1cf2409fce638dd0a2229bfd26 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 3 Nov 2017 23:01:18 +0800 Subject: [PATCH 07/59] Fix MessageInputBar layout issues && code optimize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Fix ambiguous layout issues when MessageInputBar run layoutIfNeeded method before added to superview. 2. Just reload new added message when click send button. 3. Change class-only protocols declaration. 4. Change placeholderLabel’s centerX、centerY priority because we need satisfy left/right、top/bottom constraint first . --- .../Sources/ConversationViewController.swift | 2 +- Sources/Protocols/MessageCellDelegate.swift | 2 +- .../Protocols/MessageInputBarDelegate.swift | 2 +- Sources/Protocols/MessageLabelDelegate.swift | 2 +- Sources/Protocols/MessagesDataSource.swift | 2 +- .../Protocols/MessagesDisplayDelegate.swift | 2 +- .../Protocols/MessagesLayoutDelegate.swift | 2 +- Sources/Views/InputTextView.swift | 7 +++++-- Sources/Views/MessageInputBar.swift | 21 ++++++++++++++++++- 9 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index 39c3fe5c..b3777844 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -382,7 +382,7 @@ extension ConversationViewController: MessageInputBarDelegate { func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) { messageList.append(MockMessage(text: text, sender: currentSender(), messageId: UUID().uuidString, date: Date())) inputBar.inputTextView.text = String() - messagesCollectionView.reloadData() + messagesCollectionView.insertSections([messageList.count - 1]) messagesCollectionView.scrollToBottom() } diff --git a/Sources/Protocols/MessageCellDelegate.swift b/Sources/Protocols/MessageCellDelegate.swift index 0366a458..f6289794 100644 --- a/Sources/Protocols/MessageCellDelegate.swift +++ b/Sources/Protocols/MessageCellDelegate.swift @@ -25,7 +25,7 @@ import Foundation /// A protocol used by `MessageCollectionViewCell` subclasses to detect taps in the cell's contents. -public protocol MessageCellDelegate: class, MessageLabelDelegate { +public protocol MessageCellDelegate: MessageLabelDelegate { /// Triggered when a touch occurs in the `MessageContainerView`. /// diff --git a/Sources/Protocols/MessageInputBarDelegate.swift b/Sources/Protocols/MessageInputBarDelegate.swift index 7aa057dc..6c1e57a0 100644 --- a/Sources/Protocols/MessageInputBarDelegate.swift +++ b/Sources/Protocols/MessageInputBarDelegate.swift @@ -26,7 +26,7 @@ import Foundation import UIKit /// MessageInputBarDelegate is a protocol that can recieve notifications from the MessageInputBar -public protocol MessageInputBarDelegate: class { +public protocol MessageInputBarDelegate: AnyObject { /// Called when the default send button has been selected /// diff --git a/Sources/Protocols/MessageLabelDelegate.swift b/Sources/Protocols/MessageLabelDelegate.swift index 9640032f..692aeb7a 100644 --- a/Sources/Protocols/MessageLabelDelegate.swift +++ b/Sources/Protocols/MessageLabelDelegate.swift @@ -24,7 +24,7 @@ import Foundation -public protocol MessageLabelDelegate: class { +public protocol MessageLabelDelegate: AnyObject { func didSelectAddress(_ addressComponents: [String: String]) diff --git a/Sources/Protocols/MessagesDataSource.swift b/Sources/Protocols/MessagesDataSource.swift index bbccc7c0..b1aad9f0 100644 --- a/Sources/Protocols/MessagesDataSource.swift +++ b/Sources/Protocols/MessagesDataSource.swift @@ -24,7 +24,7 @@ import UIKit -public protocol MessagesDataSource: class { +public protocol MessagesDataSource: AnyObject { /// The `Sender` of new messages in the `MessagesCollectionView`. func currentSender() -> Sender diff --git a/Sources/Protocols/MessagesDisplayDelegate.swift b/Sources/Protocols/MessagesDisplayDelegate.swift index df7c52be..25e1f26d 100644 --- a/Sources/Protocols/MessagesDisplayDelegate.swift +++ b/Sources/Protocols/MessagesDisplayDelegate.swift @@ -25,7 +25,7 @@ import Foundation /// A protocol used by the `MessagesViewController` to customize the appearance of a `TextMessageCell`. -public protocol TextMessageDisplayDelegate: class { +public protocol TextMessageDisplayDelegate: AnyObject { /// Specifies the color of the text for a `TextMessageCell`. /// diff --git a/Sources/Protocols/MessagesLayoutDelegate.swift b/Sources/Protocols/MessagesLayoutDelegate.swift index 0f3cd31a..d4c9215c 100644 --- a/Sources/Protocols/MessagesLayoutDelegate.swift +++ b/Sources/Protocols/MessagesLayoutDelegate.swift @@ -26,7 +26,7 @@ import Foundation /// A protocol used by the `MessagesCollectionViewFlowLayout` object to determine /// the size and layout of a `MessageCollectionViewCell` and its contents. -public protocol MessagesLayoutDelegate: class { +public protocol MessagesLayoutDelegate: AnyObject { /// Specifies the insets for the text rect of the `MessageLabel` in a `TextMessageCell`. /// diff --git a/Sources/Views/InputTextView.swift b/Sources/Views/InputTextView.swift index 5afa0e62..31981171 100644 --- a/Sources/Views/InputTextView.swift +++ b/Sources/Views/InputTextView.swift @@ -159,10 +159,13 @@ open class InputTextView: UITextView { top: placeholderLabel.topAnchor.constraint(equalTo: topAnchor, constant: placeholderLabelInsets.top), bottom: placeholderLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -placeholderLabelInsets.bottom), left: placeholderLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: placeholderLabelInsets.left), - right: placeholderLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: (placeholderLabelInsets.left + placeholderLabelInsets.right)), + right: placeholderLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -placeholderLabelInsets.right), centerX: placeholderLabel.centerXAnchor.constraint(equalTo: centerXAnchor), centerY: placeholderLabel.centerYAnchor.constraint(equalTo: centerYAnchor) - ).activate() + ) + placeholderLabelConstraintSet?.centerX?.priority = .defaultLow + placeholderLabelConstraintSet?.centerY?.priority = .defaultLow + placeholderLabelConstraintSet?.activate() } /// Updates the placeholderLabels constraint constants to match the placeholderLabelInsets diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index af5baa97..f9e1fdad 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -79,7 +79,7 @@ open class MessageInputBar: UIView { */ open let bottomStackView = InputStackView(axis: .horizontal, spacing: 15) - open lazy var inputTextView: InputTextView = { [weak self] in + open lazy var inputTextView: InputTextView = { let textView = InputTextView() textView.translatesAutoresizingMaskIntoConstraints = false textView.messageInputBar = self @@ -319,6 +319,10 @@ open class MessageInputBar: UIView { /// - Parameter positions: The UIStackView's to layout public func layoutStackViews(_ positions: [InputStackView.Position] = [.left, .right, .bottom]) { + guard let _ = superview else { + return + } + for position in positions { switch position { case .left: @@ -378,6 +382,9 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position leftStackView.addArrangedSubview($0) } + guard let _ = superview else { + return + } leftStackView.layoutIfNeeded() case .right: rightStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -387,6 +394,9 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position rightStackView.addArrangedSubview($0) } + guard let _ = superview else { + return + } rightStackView.layoutIfNeeded() case .bottom: bottomStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -396,6 +406,9 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position bottomStackView.addArrangedSubview($0) } + guard let _ = superview else { + return + } bottomStackView.layoutIfNeeded() } } @@ -414,6 +427,9 @@ open class MessageInputBar: UIView { performLayout(animated) { self.leftStackViewWidthContant = newValue self.layoutStackViews([.left]) + guard let _ = self.superview else { + return + } self.layoutIfNeeded() } } @@ -427,6 +443,9 @@ open class MessageInputBar: UIView { performLayout(animated) { self.rightStackViewWidthContant = newValue self.layoutStackViews([.right]) + guard let _ = self.superview else { + return + } self.layoutIfNeeded() } } From 272162ddb778b9b23d27627ff7d229d0796c9c71 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 6 Nov 2017 14:49:20 +0800 Subject: [PATCH 08/59] remove unnecessary guard statement --- Sources/Views/MessageInputBar.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 07cd87d7..3a1b9ea9 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -443,8 +443,6 @@ open class MessageInputBar: UIView { /// - Parameter positions: The UIStackView's to layout public func layoutStackViews(_ positions: [InputStackView.Position] = [.left, .right, .bottom, .top]) { - guard superview != nil else { return } - for position in positions { switch position { case .left: @@ -509,7 +507,6 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position leftStackView.addArrangedSubview($0) } - guard superview != nil else { return } leftStackView.layoutIfNeeded() case .right: rightStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -519,7 +516,6 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position rightStackView.addArrangedSubview($0) } - guard superview != nil else { return } rightStackView.layoutIfNeeded() case .bottom: bottomStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -529,7 +525,6 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position bottomStackView.addArrangedSubview($0) } - guard superview != nil else { return } bottomStackView.layoutIfNeeded() case .top: topStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -539,7 +534,6 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position topStackView.addArrangedSubview($0) } - guard superview != nil else { return } topStackView.layoutIfNeeded() } } From 71b60faf4a1a4eb584344ce246fde8c050f20749 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 6 Nov 2017 15:00:20 +0800 Subject: [PATCH 09/59] Revert "remove unnecessary guard statement" This reverts commit 272162ddb778b9b23d27627ff7d229d0796c9c71. --- Sources/Views/MessageInputBar.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 3a1b9ea9..07cd87d7 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -443,6 +443,8 @@ open class MessageInputBar: UIView { /// - Parameter positions: The UIStackView's to layout public func layoutStackViews(_ positions: [InputStackView.Position] = [.left, .right, .bottom, .top]) { + guard superview != nil else { return } + for position in positions { switch position { case .left: @@ -507,6 +509,7 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position leftStackView.addArrangedSubview($0) } + guard superview != nil else { return } leftStackView.layoutIfNeeded() case .right: rightStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -516,6 +519,7 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position rightStackView.addArrangedSubview($0) } + guard superview != nil else { return } rightStackView.layoutIfNeeded() case .bottom: bottomStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -525,6 +529,7 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position bottomStackView.addArrangedSubview($0) } + guard superview != nil else { return } bottomStackView.layoutIfNeeded() case .top: topStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } @@ -534,6 +539,7 @@ open class MessageInputBar: UIView { $0.parentStackViewPosition = position topStackView.addArrangedSubview($0) } + guard superview != nil else { return } topStackView.layoutIfNeeded() } } From 6aaee6bbccf93f1ee098bd58a17df654264a9354 Mon Sep 17 00:00:00 2001 From: Hemang Date: Tue, 7 Nov 2017 12:24:55 +0530 Subject: [PATCH 10/59] Update MessageKit+Availability.swift https://github.com/MessageKit/MessageKit/issues/328 --- .../Supporting/MessageKit+Availability.swift | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index 25dc335f..039dfbd5 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -39,44 +39,3 @@ public extension MessagesViewController { } } - -// MARK: - MessagesCollectionView - -public extension MessagesCollectionView { - - @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use dequeueReusableHeaderView") - public func dequeueMessageHeaderView(withReuseIdentifier identifier: String = "MessageHeaderView", for indexPath: IndexPath) -> MessageHeaderView { - return dequeueReusableHeaderView(MessageHeaderView.self, for: indexPath) - } - - @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use dequeueReusableFooterView") - public func dequeueMessageFooterView(withReuseIdentifier identifier: String = "MessageFooterView", for indexPath: IndexPath) -> MessageFooterView { - return dequeueReusableFooterView(MessageFooterView.self, for: indexPath) - } - -} - -// MARK: - MessagesCollectionViewFlowLayout - -public extension MessagesCollectionViewFlowLayout { - - @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use messageLabelInsets(for:indexPath:messagesCollectionView)") - public var messageLabelInsets: UIEdgeInsets { - return UIEdgeInsets(top: 7, left: 14, bottom: 7, right: 14) - } - - @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use associated value of LabelAlignment") - public var cellTopLabelInsets: UIEdgeInsets { - return .zero - } - @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use associated value of LabelAlignment") - public var cellBottomLabelInsets: UIEdgeInsets { - return .zero - } - - @available(*, deprecated: 0.9.0, message: "Removed in MessageKit 0.9.0. Please use messagePadding(for:at:in) method of MessagesLayoutDelegate") - public var messageToViewEdgePadding: CGFloat { - return 30 - } - -} From 6985ec588a239d1baed8f9bd49bc5b8fd30642d8 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Fri, 3 Nov 2017 02:39:14 -0500 Subject: [PATCH 11/59] Refactor AvatarAlignment -> AvatarPosition --- .../MessageIntermediateLayoutAttributes.swift | 3 +- .../MessagesCollectionViewFlowLayout.swift | 31 ++++++++------ ...ssagesCollectionViewLayoutAttributes.swift | 2 - .../Protocols/MessagesLayoutDelegate.swift | 42 +++++++++++++++++-- .../Supporting/MessageKit+Availability.swift | 11 +++++ 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index 75a3d211..b5c8135d 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -40,8 +40,7 @@ final class MessageIntermediateLayoutAttributes { // AvatarView var avatarSize: CGSize = .zero - var avatarVertical: AvatarAlignment = .messageBottom - var avatarHorizontal: AvatarHorizontalAlignment = .cellLeading + var avatarPosition = AvatarPosition(horizontal: .cellLeading, vertical: .cellBottom) // MessageContainerView var messageContainerSize: CGSize = .zero diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 717ed15d..cd7877c5 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -264,8 +264,7 @@ fileprivate extension MessagesCollectionViewFlowLayout { let attributes = MessageIntermediateLayoutAttributes(message: message, indexPath: indexPath) // None of these are dependent on other attributes - attributes.avatarVertical = avatarVerticalAlignment(for: attributes) - attributes.avatarHorizontal = avatarHorizontalAlignment(for: attributes) + attributes.avatarPosition = avatarPosition(for: attributes) attributes.avatarSize = avatarSize(for: attributes) attributes.messageContainerPadding = messageContainerPadding(for: attributes) attributes.messageLabelInsets = messageLabelInsets(for: attributes) @@ -320,7 +319,6 @@ fileprivate extension MessagesCollectionViewFlowLayout { attributes.messageLabelFont = messageLabelFont } - attributes.avatarHorizontalAlignment = intermediateAttributes.avatarHorizontal attributes.messagePadding = intermediateAttributes.messageContainerPadding attributes.messageLabelInsets = intermediateAttributes.messageLabelInsets attributes.cellTopLabelInsets = intermediateAttributes.cellTopLabelAlignment.insets @@ -369,6 +367,13 @@ fileprivate extension MessagesCollectionViewFlowLayout { return messagesLayoutDelegate.avatarAlignment(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) } + func avatarPosition(for attributes: MessageIntermediateLayoutAttributes) -> AvatarPosition { + var position = messagesLayoutDelegate.avatarPosition(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) + if avatarAlwaysLeading { position.horizontal = .cellLeading } + if avatarAlwaysTrailing { position.horizontal = .cellTrailing } + return position + } + } // MARK: - General Label Size Calculations @@ -535,8 +540,8 @@ private extension MessagesCollectionViewFlowLayout { guard attributes.cellBottomLabelText != nil else { return 0 } let labelHorizontal = attributes.cellBottomLabelAlignment - let avatarHorizontal = attributes.avatarHorizontal - let avatarVertical = attributes.avatarVertical + let avatarHorizontal = attributes.avatarPosition.horizontal + let avatarVertical = attributes.avatarPosition.vertical let avatarWidth = attributes.avatarSize.width switch (labelHorizontal, avatarHorizontal, avatarVertical) { @@ -619,8 +624,8 @@ private extension MessagesCollectionViewFlowLayout { guard attributes.cellTopLabelText != nil else { return 0 } let labelHorizontal = attributes.cellTopLabelAlignment - let avatarHorizontal = attributes.avatarHorizontal - let avatarVertical = attributes.avatarVertical + let avatarHorizontal = attributes.avatarPosition.horizontal + let avatarVertical = attributes.avatarPosition.vertical let avatarWidth = attributes.avatarSize.width switch (labelHorizontal, avatarHorizontal, avatarVertical) { @@ -681,7 +686,7 @@ private extension MessagesCollectionViewFlowLayout { var cellHeight: CGFloat = 0 - switch attributes.avatarVertical { + switch attributes.avatarPosition.vertical { case .cellTop: cellHeight += max(attributes.avatarSize.height, attributes.cellTopLabelSize.height) cellHeight += attributes.cellBottomLabelSize.height @@ -720,14 +725,14 @@ fileprivate extension MessagesCollectionViewFlowLayout { var origin = CGPoint.zero - switch attributes.avatarHorizontal { + switch attributes.avatarPosition.horizontal { case .cellLeading: origin.x = 0 case .cellTrailing: origin.x = contentFrame.width - attributes.avatarSize.width } - switch attributes.avatarVertical { + switch attributes.avatarPosition.vertical { case .cellTop: origin.y = 0 case .cellBottom: @@ -757,7 +762,7 @@ fileprivate extension MessagesCollectionViewFlowLayout { var origin = CGPoint.zero - switch attributes.avatarHorizontal { + switch attributes.avatarPosition.horizontal { case .cellLeading: origin.x = attributes.avatarSize.width + attributes.messageContainerPadding.left origin.y = attributes.cellTopLabelSize.height + attributes.messageContainerPadding.top @@ -782,7 +787,7 @@ fileprivate extension MessagesCollectionViewFlowLayout { var origin = CGPoint(x: 0, y: attributes.cellTopLabelSize.height + attributes.messageContainerSize.height + attributes.messageVerticalPadding) - switch (attributes.cellBottomLabelAlignment, attributes.avatarHorizontal) { + switch (attributes.cellBottomLabelAlignment, attributes.avatarPosition.horizontal) { case (.cellLeading, _): origin.x = 0 case (.cellCenter, _): @@ -815,7 +820,7 @@ fileprivate extension MessagesCollectionViewFlowLayout { var origin = CGPoint.zero - switch (attributes.cellTopLabelAlignment, attributes.avatarHorizontal) { + switch (attributes.cellTopLabelAlignment, attributes.avatarPosition.horizontal) { case (.cellLeading, _): origin.x = 0 case (.cellCenter, _): diff --git a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift index e0ec7cb8..5cb32f1e 100644 --- a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift +++ b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift @@ -29,7 +29,6 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib // MARK: - Properties - var avatarHorizontalAlignment = AvatarHorizontalAlignment.cellLeading var avatarFrame: CGRect = .zero var messageContainerFrame: CGRect = .zero @@ -48,7 +47,6 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib override func copy(with zone: NSZone? = nil) -> Any { // swiftlint:disable force_cast let copy = super.copy(with: zone) as! MessagesCollectionViewLayoutAttributes - copy.avatarHorizontalAlignment = avatarHorizontalAlignment copy.avatarFrame = avatarFrame copy.messageContainerFrame = messageContainerFrame copy.messageLabelFont = messageLabelFont diff --git a/Sources/Protocols/MessagesLayoutDelegate.swift b/Sources/Protocols/MessagesLayoutDelegate.swift index 0f3cd31a..874bf4f0 100644 --- a/Sources/Protocols/MessagesLayoutDelegate.swift +++ b/Sources/Protocols/MessagesLayoutDelegate.swift @@ -22,6 +22,31 @@ SOFTWARE. */ +public struct AvatarPosition { + + public enum Horizontal { + case cellLeading + case cellTrailing + } + + public enum Vertical { + case cellTop + case cellBottom + case messageTop + case messageBottom + case messageCenter + } + + public var horizontal: Horizontal + public var vertical: Vertical + + public init(horizontal: Horizontal, vertical: Vertical) { + self.horizontal = horizontal + self.vertical = vertical + } + +} + import Foundation /// A protocol used by the `MessagesCollectionViewFlowLayout` object to determine @@ -65,6 +90,8 @@ public protocol MessagesLayoutDelegate: class { /// /// The default value returned by this method is `AvatarAlignment.cellBottom`. func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment + + func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition /// Specifies the horizontal alignment of a `MessageCollectionViewCell`'s top label. /// @@ -145,9 +172,16 @@ public extension MessagesLayoutDelegate { return UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 30) } } - - func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment { - return .cellBottom + + func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition { + guard let dataSource = messagesCollectionView.messagesDataSource else { + return AvatarPosition(horizontal: .cellLeading, vertical: .messageBottom) + } + if dataSource.isFromCurrentSender(message: message) { + return AvatarPosition(horizontal: .cellTrailing, vertical: .messageBottom) + } else { + return AvatarPosition(horizontal: .cellLeading, vertical: .messageBottom) + } } func cellTopLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment { @@ -175,3 +209,5 @@ public extension MessagesLayoutDelegate { } } + + diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index 039dfbd5..88a32813 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -24,6 +24,17 @@ import Foundation +// MARK: - MessagesLayoutDelegate + +extension MessagesLayoutDelegate { + + @available(*, deprecated: 0.10.0, message: "Removed in MessageKit 0.11.0. Please use avatarPosition(for:at:in:) instead.") + func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment { + fatalError("Please use avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition instead.") + } + +} + // MARK: - MessagesViewController public extension MessagesViewController { From 477eeb568c5df57b47b94ed9806bca1d1e472e1d Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 7 Nov 2017 03:28:48 -0600 Subject: [PATCH 12/59] Remove avatarAlwaysLeading and avatarAlwaysTrailing properties --- .../Supporting/MessageKit+Availability.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index 88a32813..b2f59030 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -50,3 +50,29 @@ public extension MessagesViewController { } } + +extension MessagesCollectionViewFlowLayout { + + /// A Boolean value that determines if the `AvatarView` is always on the leading + /// side of a MessageCollectionViewCell. + /// + /// Setting this property to `true` causes `avatarAlwaysTrailing` to be set to `false`. + /// + /// The default value of this property is `false`. + @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use the avatarPosition(for:at:in) delegate method.") + open var avatarAlwaysLeading: Bool { + fatalError("Fatal Error: avatarAlwaysLeading is no longer supported") + } + + /// A Boolean value that determines if the `AvatarView` is always on the trailing + /// side of a `MessageCollectionViewCell`. + /// + /// Setting this property to `true` causes `avatarAlwaysLeading` to be set to `false`. + /// + /// The default value of this property is `false`. + @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use the avatarPosition(for:at:in) delegate method.") + open var avatarAlwaysTrailing: Bool { + fatalError("Fatal Error: avatarAlwaysTrailing is no longer supported") + } + +} From f17b87a1551c7e4eeff910f42f71faa3d2741bbf Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 7 Nov 2017 03:52:13 -0600 Subject: [PATCH 13/59] Remove AvatarAlignment and AvatarHorizontalAlignment --- Sources/Models/AvatarAlignment.swift | 45 ------------------- .../Models/AvatarHorizontalAlignment.swift | 38 ---------------- .../Supporting/MessageKit+Availability.swift | 37 ++++++++++----- 3 files changed, 25 insertions(+), 95 deletions(-) delete mode 100644 Sources/Models/AvatarAlignment.swift delete mode 100644 Sources/Models/AvatarHorizontalAlignment.swift diff --git a/Sources/Models/AvatarAlignment.swift b/Sources/Models/AvatarAlignment.swift deleted file mode 100644 index b2a20656..00000000 --- a/Sources/Models/AvatarAlignment.swift +++ /dev/null @@ -1,45 +0,0 @@ -/* - MIT License - - Copyright (c) 2017 MessageKit - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - */ - -import Foundation - -/// An enum representing the verical alignment for an `AvatarView`. -public enum AvatarAlignment { - - /// Aligns the `AvatarView`'s top edge to the cell's top edge. - case cellTop - - /// Aligns the `AvatarView`'s bottom edge to the cell's bottom edge. - case cellBottom - - /// Aligns the `AvatarView`'s top edge to the `MessageContainerView`'s top edge. - case messageTop - - /// Aligns the `AvatarView`'s bottom edge to the `MessageContainerView`s bottom edge. - case messageBottom - - /// Aligns the `AvatarView` center to the `MessageContainerView` center. - case messageCenter - -} diff --git a/Sources/Models/AvatarHorizontalAlignment.swift b/Sources/Models/AvatarHorizontalAlignment.swift deleted file mode 100644 index 51862de4..00000000 --- a/Sources/Models/AvatarHorizontalAlignment.swift +++ /dev/null @@ -1,38 +0,0 @@ -/* - MIT License - - Copyright (c) 2017 MessageKit - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - */ - -import Foundation - -// MARK: - AvatarVerticalAlignment - -/// An enum representing the horizontal alignment of an `AvatarView`. -internal enum AvatarHorizontalAlignment { - - /// Positions the `AvatarView` on the side closest to the cell's leading edge. - case cellLeading - - /// Positions the `AvatarView` on the side closest to the cell's trailing edge. - case cellTrailing - -} diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index b2f59030..cba8e441 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -1,18 +1,18 @@ /* MIT License - + Copyright (c) 2017 MessageKit - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -24,21 +24,34 @@ import Foundation +// MARK: - AvatarAlignment + +@available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use AvatarPosition instead.") +public enum AvatarAlignment {} + // MARK: - MessagesLayoutDelegate extension MessagesLayoutDelegate { - @available(*, deprecated: 0.10.0, message: "Removed in MessageKit 0.11.0. Please use avatarPosition(for:at:in:) instead.") - func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment { + /// Specifies the vertical alignment for the `AvatarView` in a `MessageCollectionViewCell`. + /// + /// - Parameters: + /// - message: The `MessageType` that will be displayed by this cell. + /// - indexPath: The `IndexPath` of the cell. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method is `AvatarAlignment.cellBottom`. + @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use avatarPosition(for:at:in:) instead.") + public func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment { fatalError("Please use avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition instead.") } - + } // MARK: - MessagesViewController public extension MessagesViewController { - + @available(*, deprecated: 0.10.0, message: "Removed in MessageKit 0.10.0. Please use the messagesCollectionView.contentInsets.top property.") public var additionalTopContentInset: CGFloat { get { @@ -48,7 +61,7 @@ public extension MessagesViewController { messagesCollectionView.contentInset.top = newValue } } - + } extension MessagesCollectionViewFlowLayout { @@ -60,7 +73,7 @@ extension MessagesCollectionViewFlowLayout { /// /// The default value of this property is `false`. @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use the avatarPosition(for:at:in) delegate method.") - open var avatarAlwaysLeading: Bool { + public var avatarAlwaysLeading: Bool { fatalError("Fatal Error: avatarAlwaysLeading is no longer supported") } @@ -70,8 +83,8 @@ extension MessagesCollectionViewFlowLayout { /// Setting this property to `true` causes `avatarAlwaysLeading` to be set to `false`. /// /// The default value of this property is `false`. - @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use the avatarPosition(for:at:in) delegate method.") - open var avatarAlwaysTrailing: Bool { + @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use the avatarPosition(for:at:in) delegate method instead.") + public var avatarAlwaysTrailing: Bool { fatalError("Fatal Error: avatarAlwaysTrailing is no longer supported") } From 2a26523e9748250b6b9dbdb49d635f17496185db Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 7 Nov 2017 03:52:35 -0600 Subject: [PATCH 14/59] Add AvatarPosition with Horizontal.natural case --- MessageKit.xcodeproj/project.pbxproj | 12 +-- .../MessagesCollectionViewFlowLayout.swift | 81 ++++++------------- Sources/Models/AvatarPosition.swift | 81 +++++++++++++++++++ .../Protocols/MessagesLayoutDelegate.swift | 49 ++--------- 4 files changed, 118 insertions(+), 105 deletions(-) create mode 100644 Sources/Models/AvatarPosition.swift diff --git a/MessageKit.xcodeproj/project.pbxproj b/MessageKit.xcodeproj/project.pbxproj index 91cb7a93..9eac4004 100644 --- a/MessageKit.xcodeproj/project.pbxproj +++ b/MessageKit.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1F82D1431FB1B75B00B81A88 /* AvatarPosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F82D1421FB1B75B00B81A88 /* AvatarPosition.swift */; }; 38C57C791F9AE3E50043CC03 /* SeparatorLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38C57C781F9AE3E50043CC03 /* SeparatorLine.swift */; }; 38C57C7C1F9AE4890043CC03 /* InputStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38C57C7B1F9AE4870043CC03 /* InputStackView.swift */; }; 88916B2D1CF0DF2F00469F91 /* MessageKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 88916B221CF0DF2F00469F91 /* MessageKit.framework */; }; @@ -36,11 +37,9 @@ B7A03F281F866895006AEF79 /* LocationMessageSnapshotOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F1D1F866895006AEF79 /* LocationMessageSnapshotOptions.swift */; }; B7A03F291F866895006AEF79 /* Sender.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F1E1F866895006AEF79 /* Sender.swift */; }; B7A03F2A1F866895006AEF79 /* MessageStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F1F1F866895006AEF79 /* MessageStyle.swift */; }; - B7A03F2B1F866895006AEF79 /* AvatarHorizontalAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F201F866895006AEF79 /* AvatarHorizontalAlignment.swift */; }; B7A03F2C1F866895006AEF79 /* DetectorType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F211F866895006AEF79 /* DetectorType.swift */; }; B7A03F2D1F866895006AEF79 /* LabelAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F221F866895006AEF79 /* LabelAlignment.swift */; }; B7A03F2E1F866895006AEF79 /* MessageData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F231F866895006AEF79 /* MessageData.swift */; }; - B7A03F2F1F866895006AEF79 /* AvatarAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F241F866895006AEF79 /* AvatarAlignment.swift */; }; B7A03F331F866940006AEF79 /* MessageDateHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F301F866940006AEF79 /* MessageDateHeaderView.swift */; }; B7A03F341F866940006AEF79 /* MessageFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F311F866940006AEF79 /* MessageFooterView.swift */; }; B7A03F351F866940006AEF79 /* MessageHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F321F866940006AEF79 /* MessageHeaderView.swift */; }; @@ -88,6 +87,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 1F82D1421FB1B75B00B81A88 /* AvatarPosition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarPosition.swift; sourceTree = ""; }; 38C57C781F9AE3E50043CC03 /* SeparatorLine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeparatorLine.swift; sourceTree = ""; }; 38C57C7B1F9AE4870043CC03 /* InputStackView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputStackView.swift; sourceTree = ""; }; 88916B221CF0DF2F00469F91 /* MessageKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MessageKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -119,11 +119,9 @@ B7A03F1D1F866895006AEF79 /* LocationMessageSnapshotOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationMessageSnapshotOptions.swift; sourceTree = ""; }; B7A03F1E1F866895006AEF79 /* Sender.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sender.swift; sourceTree = ""; }; B7A03F1F1F866895006AEF79 /* MessageStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageStyle.swift; sourceTree = ""; }; - B7A03F201F866895006AEF79 /* AvatarHorizontalAlignment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarHorizontalAlignment.swift; sourceTree = ""; }; B7A03F211F866895006AEF79 /* DetectorType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetectorType.swift; sourceTree = ""; }; B7A03F221F866895006AEF79 /* LabelAlignment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LabelAlignment.swift; sourceTree = ""; }; B7A03F231F866895006AEF79 /* MessageData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageData.swift; sourceTree = ""; }; - B7A03F241F866895006AEF79 /* AvatarAlignment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarAlignment.swift; sourceTree = ""; }; B7A03F301F866940006AEF79 /* MessageDateHeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageDateHeaderView.swift; sourceTree = ""; }; B7A03F311F866940006AEF79 /* MessageFooterView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageFooterView.swift; sourceTree = ""; }; B7A03F321F866940006AEF79 /* MessageHeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageHeaderView.swift; sourceTree = ""; }; @@ -333,8 +331,7 @@ isa = PBXGroup; children = ( B7A03F1C1F866895006AEF79 /* Avatar.swift */, - B7A03F241F866895006AEF79 /* AvatarAlignment.swift */, - B7A03F201F866895006AEF79 /* AvatarHorizontalAlignment.swift */, + 1F82D1421FB1B75B00B81A88 /* AvatarPosition.swift */, B7A03F211F866895006AEF79 /* DetectorType.swift */, B7A03F221F866895006AEF79 /* LabelAlignment.swift */, B7A03F1D1F866895006AEF79 /* LocationMessageSnapshotOptions.swift */, @@ -538,7 +535,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - B7A03F2B1F866895006AEF79 /* AvatarHorizontalAlignment.swift in Sources */, B7A03F3C1F866946006AEF79 /* LocationMessageCell.swift in Sources */, 38C57C7C1F9AE4890043CC03 /* InputStackView.swift in Sources */, B7A03F5B1F8669CA006AEF79 /* MessageType.swift in Sources */, @@ -588,7 +584,7 @@ 38C57C791F9AE3E50043CC03 /* SeparatorLine.swift in Sources */, B7A03F2C1F866895006AEF79 /* DetectorType.swift in Sources */, B7A03F271F866895006AEF79 /* Avatar.swift in Sources */, - B7A03F2F1F866895006AEF79 /* AvatarAlignment.swift in Sources */, + 1F82D1431FB1B75B00B81A88 /* AvatarPosition.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index cd7877c5..578f8829 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -40,34 +40,6 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { } } - /// A Boolean value that determines if the `AvatarView` is always on the leading - /// side of a MessageCollectionViewCell. - /// - /// Setting this property to `true` causes `avatarAlwaysTrailing` to be set to `false`. - /// - /// The default value of this property is `false`. - open var avatarAlwaysLeading: Bool { - willSet { - if newValue { - avatarAlwaysTrailing = false - } - } - } - - /// A Boolean value that determines if the `AvatarView` is always on the trailing - /// side of a `MessageCollectionViewCell`. - /// - /// Setting this property to `true` causes `avatarAlwaysLeading` to be set to `false`. - /// - /// The default value of this property is `false`. - open var avatarAlwaysTrailing: Bool { - willSet { - if newValue { - avatarAlwaysLeading = false - } - } - } - /// Determines the maximum number of `MessageCollectionViewCell` attributes to cache. /// /// The default value of this property is 500. @@ -127,9 +99,6 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { messageLabelFont = UIFont.preferredFont(forTextStyle: .body) emojiLabelFont = messageLabelFont.withSize(2 * messageLabelFont.pointSize) - avatarAlwaysLeading = false - avatarAlwaysTrailing = false - super.init() sectionInset = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8) @@ -224,8 +193,6 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { let attributes = messageIntermediateLayoutAttributes(for: indexPath) return CGSize(width: itemWidth, height: attributes.itemHeight) } - - } @@ -334,19 +301,24 @@ fileprivate extension MessagesCollectionViewFlowLayout { // A - /// Returns the horizontal alignment of the `AvatarView` for a given `MessageType`. + /// Returns the `AvatarPosition` for a given `MessageType`. /// /// - Parameters: /// - attributes: The `MessageIntermediateLayoutAttributes` containing the `MessageType` object. - func avatarHorizontalAlignment(for attributes: MessageIntermediateLayoutAttributes) -> AvatarHorizontalAlignment { + func avatarPosition(for attributes: MessageIntermediateLayoutAttributes) -> AvatarPosition { + var position = messagesLayoutDelegate.avatarPosition(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) - if avatarAlwaysTrailing { return .cellTrailing } - if avatarAlwaysLeading { return .cellLeading } - - return messagesDataSource.isFromCurrentSender(message: attributes.message) ? .cellTrailing : .cellLeading + switch position.horizontal { + case .cellTrailing, .cellLeading: + break + case .natural: + position.horizontal = messagesDataSource.isFromCurrentSender(message: attributes.message) ? .cellTrailing : .cellLeading + } + return position } + // B /// Returns the size of the `AvatarView` for a given `MessageType`. @@ -357,23 +329,6 @@ fileprivate extension MessagesCollectionViewFlowLayout { return messagesLayoutDelegate.avatarSize(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) } - // C - - /// Returns the vertical alignment of the `AvatarView` for a given `MessageType`. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` containing the `MessageType` object. - func avatarVerticalAlignment(for attributes: MessageIntermediateLayoutAttributes) -> AvatarAlignment { - return messagesLayoutDelegate.avatarAlignment(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) - } - - func avatarPosition(for attributes: MessageIntermediateLayoutAttributes) -> AvatarPosition { - var position = messagesLayoutDelegate.avatarPosition(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) - if avatarAlwaysLeading { position.horizontal = .cellLeading } - if avatarAlwaysTrailing { position.horizontal = .cellTrailing } - return position - } - } // MARK: - General Label Size Calculations @@ -566,6 +521,9 @@ private extension MessagesCollectionViewFlowLayout { case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): return attributes.messageContainerSize.width + avatarWidth - attributes.cellBottomLabelHorizontalInsets + + case (_, .natural, _): + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } } @@ -650,6 +608,9 @@ private extension MessagesCollectionViewFlowLayout { case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): return attributes.messageContainerSize.width + avatarWidth - attributes.cellTopLabelHorizontalInsets + + case (_, .natural, _): + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } } @@ -730,6 +691,8 @@ fileprivate extension MessagesCollectionViewFlowLayout { origin.x = 0 case .cellTrailing: origin.x = contentFrame.width - attributes.avatarSize.width + case .natural: + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } switch attributes.avatarPosition.vertical { @@ -769,6 +732,8 @@ fileprivate extension MessagesCollectionViewFlowLayout { case .cellTrailing: origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerSize.width - attributes.messageContainerPadding.right origin.y = attributes.cellTopLabelSize.height + attributes.messageContainerPadding.top + case .natural: + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } return origin @@ -802,6 +767,8 @@ fileprivate extension MessagesCollectionViewFlowLayout { origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerPadding.right - attributes.cellBottomLabelSize.width case (.messageTrailing, .cellLeading): origin.x = attributes.avatarSize.width + attributes.messageContainerPadding.left + attributes.messageContainerSize.width - attributes.cellBottomLabelSize.width + case (_, .natural): + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } return origin @@ -835,6 +802,8 @@ fileprivate extension MessagesCollectionViewFlowLayout { origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerPadding.right - attributes.cellTopLabelSize.width case (.messageTrailing, .cellLeading): origin.x = contentFrame.width - attributes.messageContainerPadding.right - attributes.cellTopLabelSize.width + case (_, .natural): + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } return origin diff --git a/Sources/Models/AvatarPosition.swift b/Sources/Models/AvatarPosition.swift new file mode 100644 index 00000000..576a275d --- /dev/null +++ b/Sources/Models/AvatarPosition.swift @@ -0,0 +1,81 @@ +/* + MIT License + + Copyright (c) 2017 MessageKit + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ + +import Foundation + +/// Used to determine the `Horizontal` and `Vertical` position of +// an `AvatarView` in a `MessagesCollectionViewCell`. +public struct AvatarPosition { + + /// An enum representing the horizontal alignment of an `AvatarView`. + public enum Horizontal { + + /// Positions the `AvatarView` on the side closest to the cell's leading edge. + case cellLeading + + /// Positions the `AvatarView` on the side closest to the cell's trailing edge. + case cellTrailing + + /// Positions the `AvatarView` based on whether the message is from the current Sender. + /// The cell is positioned `.cellTrailling` if `isFromCurrentSender` is true + /// and `.cellLeading` if false. + case natural + } + + /// An enum representing the verical alignment for an `AvatarView`. + public enum Vertical { + + /// Aligns the `AvatarView`'s top edge to the cell's top edge. + case cellTop + + /// Aligns the `AvatarView`'s bottom edge to the cell's bottom edge. + case cellBottom + + /// Aligns the `AvatarView`'s top edge to the `MessageContainerView`'s top edge. + case messageTop + + /// Aligns the `AvatarView`'s bottom edge to the `MessageContainerView`s bottom edge. + case messageBottom + + /// Aligns the `AvatarView` center to the `MessageContainerView` center. + case messageCenter + } + + // MARK: - Properties + + // The vertical position + public var vertical: Vertical + + // The horizontal position + public var horizontal: Horizontal + + // MARK: - Initializers + + public init(horizontal: Horizontal, vertical: Vertical) { + self.horizontal = horizontal + self.vertical = vertical + + } + +} diff --git a/Sources/Protocols/MessagesLayoutDelegate.swift b/Sources/Protocols/MessagesLayoutDelegate.swift index 874bf4f0..05bf5d7f 100644 --- a/Sources/Protocols/MessagesLayoutDelegate.swift +++ b/Sources/Protocols/MessagesLayoutDelegate.swift @@ -22,31 +22,6 @@ SOFTWARE. */ -public struct AvatarPosition { - - public enum Horizontal { - case cellLeading - case cellTrailing - } - - public enum Vertical { - case cellTop - case cellBottom - case messageTop - case messageBottom - case messageCenter - } - - public var horizontal: Horizontal - public var vertical: Vertical - - public init(horizontal: Horizontal, vertical: Vertical) { - self.horizontal = horizontal - self.vertical = vertical - } - -} - import Foundation /// A protocol used by the `MessagesCollectionViewFlowLayout` object to determine @@ -80,17 +55,16 @@ public protocol MessagesLayoutDelegate: class { /// /// All other Senders: `UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 30)` func messagePadding(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets - - /// Specifies the vertical alignment for the `AvatarView` in a `MessageCollectionViewCell`. + + /// Specifies the vertical and horizontal alignment for the `AvatarView` in a `MessageCollectionViewCell`. /// /// - Parameters: /// - message: The `MessageType` that will be displayed by this cell. /// - indexPath: The `IndexPath` of the cell. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. /// - /// The default value returned by this method is `AvatarAlignment.cellBottom`. - func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment - + /// The default value returned by this method is an `AvatarPosition` with + /// `Horizontal.natural` and `Vertical.messageBottom`. func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition /// Specifies the horizontal alignment of a `MessageCollectionViewCell`'s top label. @@ -172,17 +146,6 @@ public extension MessagesLayoutDelegate { return UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 30) } } - - func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition { - guard let dataSource = messagesCollectionView.messagesDataSource else { - return AvatarPosition(horizontal: .cellLeading, vertical: .messageBottom) - } - if dataSource.isFromCurrentSender(message: message) { - return AvatarPosition(horizontal: .cellTrailing, vertical: .messageBottom) - } else { - return AvatarPosition(horizontal: .cellLeading, vertical: .messageBottom) - } - } func cellTopLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment { guard let dataSource = messagesCollectionView.messagesDataSource else { return .cellCenter(.zero) } @@ -197,6 +160,10 @@ public extension MessagesLayoutDelegate { func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize { return CGSize(width: 30, height: 30) } + + func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition { + return AvatarPosition(horizontal: .natural, vertical: .messageBottom) + } func headerViewSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize { guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else { return .zero } From 7a0679c5ff4d359b046a564c95ea18b9c37baf4b Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 7 Nov 2017 04:00:00 -0600 Subject: [PATCH 15/59] Add CHANGELOG entry for #322, closes #318 --- CHANGELOG.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79ef93f2..10a3fbde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,13 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ### Added -+- **Breaking Change** Added a top `InputStackView` to `MessageInputBar`. This adds the addition of the `.top` case to `InputStackView.Position`. +- **Breaking Change** Added a top `InputStackView` to `MessageInputBar`. This adds the addition of the `.top` case to `InputStackView.Position`. [#320](https://github.com/MessageKit/MessageKit/issues/320) by [@nathantannar4](https://github.com/nathantannar4). +- **Breaking Change** Added `AvatarPosition` and `avatarPosition(for:at:in)` to configure an +`AvatarView`'s vertical and horizontal position in a `MessageCollectionViewCell`. +[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). + ### Fixed - **Breaking Change** Fixed all instances of misspelled `inital` property. `Avatar.inital` has changed to `Avatar.initial` @@ -21,6 +25,15 @@ and the initializer has changed from `public init(image: UIImage? = nil, initals origin Y so that the `cellBottomLabel` is always under the `MessageContainerView`. [#326](https://github.com/MessageKit/MessageKit/pull/326) by [@SD10](https://github.com/sd10). +### Removed + +- **Breaking Change** Removed `AvatarAlignment` and `avatarAlignment(for:at:in)` delegate method +in favor of new `AvatarPosition` representing both vertical and horizontal alignments. +[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). + +- **Breaking Change** Removed the `avatarAlwaysLeading` and `avatarAlwaysTrailing` properties of `MessagesCollectionViewFlowLayout`. +[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). + ## [[Prerelease] 0.10.1](https://github.com/MessageKit/MessageKit/releases/tag/0.10.1) From 619a5bbd3dcdb27febcf4b8140b5ca6f0ecd8d86 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 7 Nov 2017 04:12:08 -0600 Subject: [PATCH 16/59] Fix compile error in example app from deprecation --- Example/Sources/ConversationViewController.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index 573c421f..b5b5e82d 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -281,10 +281,6 @@ extension ConversationViewController: MessagesLayoutDelegate { } } - func avatarAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarAlignment { - return .messageBottom - } - func footerViewSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize { return CGSize(width: messagesCollectionView.bounds.width, height: 10) From aeddab2d66e85db4ca83a0948737c125a48aa402 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 8 Nov 2017 14:52:16 +0800 Subject: [PATCH 17/59] replace class to AnyObject for Swift4 --- Sources/Extensions/UICollectionView+Extensions.swift | 2 +- Sources/Protocols/MessagesDisplayDelegate.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Extensions/UICollectionView+Extensions.swift b/Sources/Extensions/UICollectionView+Extensions.swift index dc5d1ff1..7ca793cc 100644 --- a/Sources/Extensions/UICollectionView+Extensions.swift +++ b/Sources/Extensions/UICollectionView+Extensions.swift @@ -25,7 +25,7 @@ import Foundation /// Optional Cell Protocol to Simplify registration/cell type loading in a generic way -public protocol CollectionViewReusable: class { +public protocol CollectionViewReusable: AnyObject { static func reuseIdentifier() -> String } diff --git a/Sources/Protocols/MessagesDisplayDelegate.swift b/Sources/Protocols/MessagesDisplayDelegate.swift index 25e1f26d..6dfcccf3 100644 --- a/Sources/Protocols/MessagesDisplayDelegate.swift +++ b/Sources/Protocols/MessagesDisplayDelegate.swift @@ -67,7 +67,7 @@ public extension TextMessageDisplayDelegate { } /// A protocol used by the `MessagesViewController` to customize the appearance of a `MessagesCollectionViewCell`. -public protocol MessagesDisplayDelegate: class { +public protocol MessagesDisplayDelegate: AnyObject { /// Specifies the `MessageStyle` to be used for a `MessageContainerView`. /// From fa958435835f969100e94d95bdf0e942f750bc94 Mon Sep 17 00:00:00 2001 From: Nathan Tannar Date: Sun, 12 Nov 2017 13:03:12 -0800 Subject: [PATCH 18/59] SeparatorLine Fix on iPhone X --- Sources/Views/MessageInputBar.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 07cd87d7..c3c86ef8 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -307,13 +307,14 @@ open class MessageInputBar: UIView { addSubview(leftStackView) addSubview(rightStackView) addSubview(bottomStackView) - topStackView.addArrangedSubview(separatorLine) + addSubview(separatorLine) setStackViewItems([sendButton], forStack: .right, animated: false) } /// Sets up the initial constraints of each subview private func setupConstraints() { + separatorLine.addConstraints(topAnchor, left: leftAnchor, right: rightAnchor, heightConstant: 1) topStackViewLayoutSet = NSLayoutConstraintSet( top: topStackView.topAnchor.constraint(equalTo: topAnchor, constant: topStackViewPadding.top), bottom: topStackView.bottomAnchor.constraint(equalTo: inputTextView.topAnchor, constant: -padding.top), From f873c6d2bc979f0367a5705c255a9f35d78440e8 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 13 Nov 2017 11:06:19 +0800 Subject: [PATCH 19/59] Fix MessageInputBar translucent property --- Sources/Views/MessageInputBar.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 07cd87d7..d9b54466 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -74,7 +74,11 @@ open class MessageInputBar: UIView { } blurView.isHidden = !isTranslucent let color: UIColor = backgroundView.backgroundColor ?? .white - backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : .white + backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : color.withAlphaComponent(1.0) + + let bgColor: UIColor = backgroundColor ?? .inputBarGray + backgroundColor = isTranslucent ? bgColor.withAlphaComponent(0.75) : bgColor.withAlphaComponent(1.0) + } } From 3b48dc38dc5a85fe5300bfeffa57c41b871f8bc2 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 13 Nov 2017 11:07:47 +0800 Subject: [PATCH 20/59] Revert "Fix MessageInputBar translucent property" This reverts commit f873c6d2bc979f0367a5705c255a9f35d78440e8. --- Sources/Views/MessageInputBar.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index d9b54466..07cd87d7 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -74,11 +74,7 @@ open class MessageInputBar: UIView { } blurView.isHidden = !isTranslucent let color: UIColor = backgroundView.backgroundColor ?? .white - backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : color.withAlphaComponent(1.0) - - let bgColor: UIColor = backgroundColor ?? .inputBarGray - backgroundColor = isTranslucent ? bgColor.withAlphaComponent(0.75) : bgColor.withAlphaComponent(1.0) - + backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : .white } } From 8ef9fc8a8e7136b827ad0f20d9cdae070a03453e Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 13 Nov 2017 11:15:04 +0800 Subject: [PATCH 21/59] Fix MessageInputBar translucent property --- Sources/Views/MessageInputBar.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 07cd87d7..48fa9c2c 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -74,7 +74,10 @@ open class MessageInputBar: UIView { } blurView.isHidden = !isTranslucent let color: UIColor = backgroundView.backgroundColor ?? .white - backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : .white + backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : color.withAlphaComponent(1.0) + + let bgColor: UIColor = backgroundColor ?? .inputBarGray + backgroundColor = isTranslucent ? bgColor.withAlphaComponent(0.75) : bgColor.withAlphaComponent(1.0) } } From 9f6c494c1747214db876bb8c1065152a48c3e5d9 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 14 Nov 2017 12:20:33 +0800 Subject: [PATCH 22/59] remove MessageInputBar backgroundColor --- Sources/Views/MessageInputBar.swift | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 48fa9c2c..06ff3707 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -41,17 +41,10 @@ open class MessageInputBar: UIView { open var backgroundView: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false - view.backgroundColor = .white + view.backgroundColor = .inputBarGray return view }() - /// Also sets the backgroundView's backgroundColor to the newValue - open override var backgroundColor: UIColor? { - didSet { - backgroundView.backgroundColor = backgroundColor - } - } - /** A UIVisualEffectView that adds a blur effect to make the view appear transparent. @@ -73,11 +66,8 @@ open class MessageInputBar: UIView { blurView.fillSuperview() } blurView.isHidden = !isTranslucent - let color: UIColor = backgroundView.backgroundColor ?? .white + let color: UIColor = backgroundView.backgroundColor ?? .inputBarGray backgroundView.backgroundColor = isTranslucent ? color.withAlphaComponent(0.75) : color.withAlphaComponent(1.0) - - let bgColor: UIColor = backgroundColor ?? .inputBarGray - backgroundColor = isTranslucent ? bgColor.withAlphaComponent(0.75) : bgColor.withAlphaComponent(1.0) } } @@ -294,7 +284,6 @@ open class MessageInputBar: UIView { /// Sets up the default properties open func setup() { - backgroundColor = .inputBarGray autoresizingMask = [.flexibleHeight] setupSubviews() setupConstraints() From eb6bafb5a7ff1d8f218836238537434458b8b483 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 14 Nov 2017 14:27:23 +0800 Subject: [PATCH 23/59] Add CHANGELOG entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a3fbde..f01bbbb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,9 @@ and the initializer has changed from `public init(image: UIImage? = nil, initals origin Y so that the `cellBottomLabel` is always under the `MessageContainerView`. [#326](https://github.com/MessageKit/MessageKit/pull/326) by [@SD10](https://github.com/sd10). +- Fixed `MessageInputBar`'s `translucent` functionality. +[#348](https://github.com/MessageKit/MessageKit/pull/348) by [@zhongwuzw](https://github.com/zhongwuzw). + ### Removed - **Breaking Change** Removed `AvatarAlignment` and `avatarAlignment(for:at:in)` delegate method From a2363d316dd7d78cb6119ecf0e78492a372e30a0 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 17:14:43 -0600 Subject: [PATCH 24/59] Remove text storage from MessageIntermediateLayoutAttributes --- .../MessageIntermediateLayoutAttributes.swift | 2 - .../MessagesCollectionViewFlowLayout.swift | 44 +++++-------------- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index b5c8135d..d7686b88 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -65,7 +65,6 @@ final class MessageIntermediateLayoutAttributes { } // Cell Top Label - var cellBottomLabelText: NSAttributedString? var cellTopLabelSize: CGSize = .zero var cellTopLabelMaxWidth: CGFloat = 0 var cellTopLabelAlignment: LabelAlignment = .cellLeading(.zero) @@ -81,7 +80,6 @@ final class MessageIntermediateLayoutAttributes { } // Cell Bottom Label - var cellTopLabelText: NSAttributedString? var cellBottomLabelSize: CGSize = .zero var cellBottomLabelMaxWidth: CGFloat = 0 var cellBottomLabelAlignment: LabelAlignment = .cellTrailing(.zero) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 578f8829..6e8ce9f1 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -241,13 +241,11 @@ fileprivate extension MessagesCollectionViewFlowLayout { attributes.messageContainerSize = messageContainerSize(for: attributes) // Cell Bottom Label - attributes.cellBottomLabelText = cellBottomLabelText(for: attributes) // little concerned about storing text here TODO attributes.cellBottomLabelAlignment = cellBottomLabelAlignment(for: attributes) attributes.cellBottomLabelMaxWidth = cellBottomLabelMaxWidth(for: attributes) attributes.cellBottomLabelSize = cellBottomLabelSize(for: attributes) // Cell Top Label - attributes.cellTopLabelText = cellTopLabelText(for: attributes) // little concerned about storing text here TODO attributes.cellTopLabelAlignment = cellTopLabelAlignment(for: attributes) attributes.cellTopLabelMaxWidth = cellTopLabelMaxWidth(for: attributes) attributes.cellTopLabelSize = cellTopLabelSize(for: attributes) @@ -460,20 +458,10 @@ private extension MessagesCollectionViewFlowLayout { } -// MARK: - Cell Bottom Label Calculations [ H - K ] +// MARK: - Cell Bottom Label Calculations [ I - K ] private extension MessagesCollectionViewFlowLayout { - // H - - /// Returns the attributed text for the cell's bottom label. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` containing the `MessageType` object. - func cellBottomLabelText(for attributes: MessageIntermediateLayoutAttributes) -> NSAttributedString? { - return messagesDataSource.cellBottomLabelAttributedText(for: attributes.message, at: attributes.indexPath) - } - // I /// Returns the alignment of the cell's bottom label. @@ -492,8 +480,6 @@ private extension MessagesCollectionViewFlowLayout { /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating the max width. func cellBottomLabelMaxWidth(for attributes: MessageIntermediateLayoutAttributes) -> CGFloat { - guard attributes.cellBottomLabelText != nil else { return 0 } - let labelHorizontal = attributes.cellBottomLabelAlignment let avatarHorizontal = attributes.avatarPosition.horizontal let avatarVertical = attributes.avatarPosition.vertical @@ -535,8 +521,11 @@ private extension MessagesCollectionViewFlowLayout { /// - Parameters: /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating label's size. func cellBottomLabelSize(for attributes: MessageIntermediateLayoutAttributes) -> CGSize { - guard let bottomLabelText = attributes.cellBottomLabelText else { return .zero } + let text = messagesDataSource.cellBottomLabelAttributedText(for: attributes.message, at: attributes.indexPath) + + guard let bottomLabelText = text else { return .zero } + var bottomLabelSize = labelSize(for: bottomLabelText, considering: attributes.cellBottomLabelMaxWidth) bottomLabelSize.width += attributes.cellBottomLabelHorizontalInsets bottomLabelSize.height += attributes.cellBottomLabelVerticalInsets @@ -547,22 +536,12 @@ private extension MessagesCollectionViewFlowLayout { } -// MARK: - Cell Top Label Size Calculations [ L - O ] +// MARK: - Cell Top Label Size Calculations [ L - N ] private extension MessagesCollectionViewFlowLayout { // L - /// Returns the attributed text for the cell's top label. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` containing the `MessageType` object. - func cellTopLabelText(for attributes: MessageIntermediateLayoutAttributes) -> NSAttributedString? { - return messagesDataSource.cellTopLabelAttributedText(for: attributes.message, at: attributes.indexPath) - } - - // M - /// Returns the alignment of the cell's top label. /// /// - Parameters: @@ -571,7 +550,7 @@ private extension MessagesCollectionViewFlowLayout { return messagesLayoutDelegate.cellTopLabelAlignment(for: attributes.message, at: attributes.indexPath, in: messagesCollectionView) } - // N + // M /// Returns the max available width for the cell's top label considering the specified layout information. /// @@ -579,8 +558,6 @@ private extension MessagesCollectionViewFlowLayout { /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating the max width. func cellTopLabelMaxWidth(for attributes: MessageIntermediateLayoutAttributes) -> CGFloat { - guard attributes.cellTopLabelText != nil else { return 0 } - let labelHorizontal = attributes.cellTopLabelAlignment let avatarHorizontal = attributes.avatarPosition.horizontal let avatarVertical = attributes.avatarPosition.vertical @@ -615,15 +592,18 @@ private extension MessagesCollectionViewFlowLayout { } - // O + // N /// Returns the size of the cell's top label considering the specified layout information. /// /// - Parameters: /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating label's size. func cellTopLabelSize(for attributes: MessageIntermediateLayoutAttributes) -> CGSize { - guard let topLabelText = attributes.cellTopLabelText else { return .zero } + let text = messagesDataSource.cellTopLabelAttributedText(for: attributes.message, at: attributes.indexPath) + + guard let topLabelText = text else { return .zero } + var topLabelSize = labelSize(for: topLabelText, considering: attributes.cellTopLabelMaxWidth) topLabelSize.width += attributes.cellTopLabelHorizontalInsets topLabelSize.height += attributes.cellTopLabelVerticalInsets From e828b5d8e30222b146c1513c3b2e111fb42c7681 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 17:16:33 -0600 Subject: [PATCH 25/59] Remove unused messageContainerPadding --- Sources/Layout/MessagesCollectionViewFlowLayout.swift | 1 - Sources/Layout/MessagesCollectionViewLayoutAttributes.swift | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 6e8ce9f1..21ee1581 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -284,7 +284,6 @@ fileprivate extension MessagesCollectionViewFlowLayout { attributes.messageLabelFont = messageLabelFont } - attributes.messagePadding = intermediateAttributes.messageContainerPadding attributes.messageLabelInsets = intermediateAttributes.messageLabelInsets attributes.cellTopLabelInsets = intermediateAttributes.cellTopLabelAlignment.insets attributes.cellBottomLabelInsets = intermediateAttributes.cellBottomLabelAlignment.insets diff --git a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift index 5cb32f1e..c9f9f731 100644 --- a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift +++ b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift @@ -31,9 +31,8 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib var avatarFrame: CGRect = .zero - var messageContainerFrame: CGRect = .zero - var messagePadding: UIEdgeInsets = .zero var messageLabelFont: UIFont = UIFont.preferredFont(forTextStyle: .body) + var messageContainerFrame: CGRect = .zero var messageLabelInsets: UIEdgeInsets = .zero var cellTopLabelFrame: CGRect = .zero From 54f670f6718173f1956c9fa9a89c7e608a3e607a Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 19:19:52 -0600 Subject: [PATCH 26/59] Simplify origin calculations --- .../MessageIntermediateLayoutAttributes.swift | 190 ++++++++++--- .../MessagesCollectionViewFlowLayout.swift | 249 +++++------------- ...ssagesCollectionViewLayoutAttributes.swift | 16 +- .../Cells/MessageCollectionViewCell.swift | 8 +- 4 files changed, 223 insertions(+), 240 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index d7686b88..ad4560da 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -24,6 +24,7 @@ import Foundation + /// A intermediate context used to store recently calculated values used by /// the `MessagesCollectionViewFlowLayout` object to reduce redundant calculations. final class MessageIntermediateLayoutAttributes { @@ -31,67 +32,178 @@ final class MessageIntermediateLayoutAttributes { // Message var message: MessageType var indexPath: IndexPath + + // Cell var itemHeight: CGFloat = 0 - - init(message: MessageType, indexPath: IndexPath) { - self.message = message - self.indexPath = indexPath - } + var cellFrame: CGRect = .zero // AvatarView - var avatarSize: CGSize = .zero var avatarPosition = AvatarPosition(horizontal: .cellLeading, vertical: .cellBottom) + var avatarSize: CGSize = .zero + + lazy var avatarFrame: CGRect = { + + guard avatarSize != .zero else { return .zero } + + var origin = CGPoint.zero + + switch avatarPosition.horizontal { + case .cellLeading: + break + case .cellTrailing: + origin.x = cellFrame.maxX - avatarSize.width + case .natural: + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") + } + + switch avatarPosition.vertical { + case .cellTop: + break + case .cellBottom: + origin.y = cellFrame.maxY - avatarSize.height + case .messageTop: + origin.y = messageContainerFrame.maxY + case .messageBottom: + origin.y = messageContainerFrame.maxY - avatarSize.height + case .messageCenter: + origin.y = messageContainerFrame.midY + } + + return CGRect(origin: origin, size: avatarSize) + + }() // MessageContainerView var messageContainerSize: CGSize = .zero var messageContainerMaxWidth: CGFloat = 0 var messageContainerPadding: UIEdgeInsets = .zero var messageLabelInsets: UIEdgeInsets = .zero + + lazy var messageContainerFrame: CGRect = { + + guard messageContainerSize != .zero else { return .zero } + + var origin: CGPoint = .zero + origin.y = topLabelSize.height + messageContainerPadding.top + + switch avatarPosition.horizontal { + case .cellLeading: + origin.x = avatarSize.width + messageContainerPadding.left + case .cellTrailing: + origin.x = cellFrame.maxX - avatarSize.width - messageContainerSize.width - messageContainerPadding.right + case .natural: + fatalError("AvatarPosition Horizontal.natural needs to be resolved.") + } + + return CGRect(origin: origin, size: messageContainerSize) + + }() + + // Cell Top Label + var topLabelAlignment: LabelAlignment = .cellLeading(.zero) + var topLabelSize: CGSize = .zero + var topLabelMaxWidth: CGFloat = 0 + + lazy var topLabelFrame: CGRect = { + + guard topLabelSize != .zero else { return .zero } + + var origin = CGPoint.zero + + switch topLabelAlignment { + case .cellLeading: + break + case .cellCenter: + origin.x = cellFrame.midX + case .cellTrailing: + origin.x = cellFrame.maxX - topLabelSize.width + case .messageLeading: + origin.x = messageContainerFrame.minX + case .messageTrailing: + origin.x = messageContainerFrame.maxX - topLabelSize.width + } + + return CGRect(origin: origin, size: topLabelSize) + + }() + // Cell Bottom Label + var bottomLabelAlignment: LabelAlignment = .cellTrailing(.zero) + var bottomLabelSize: CGSize = .zero + var bottomLabelMaxWidth: CGFloat = 0 + + lazy var bottomLabelFrame: CGRect = { + + guard bottomLabelSize != .zero else { return .zero } + + var origin: CGPoint = .zero + + origin.y = messageContainerFrame.maxY + messageContainerPadding.bottom + + switch bottomLabelAlignment { + case .cellLeading: + break + case .cellCenter: + origin.x = cellFrame.midX + case .cellTrailing: + origin.x = cellFrame.maxX - bottomLabelSize.width + case .messageLeading: + origin.x = messageContainerFrame.minX + case .messageTrailing: + origin.x = messageContainerFrame.maxX - bottomLabelSize.width + } + + return CGRect(origin: origin, size: bottomLabelSize) + + }() + + // MARK: - Initializer + + init(message: MessageType, indexPath: IndexPath) { + self.message = message + self.indexPath = indexPath + } + +} + +// MARK: - Helpers + +extension MessageIntermediateLayoutAttributes { + + var bottomLabelVerticalInsets: CGFloat { + let bottomLabelInsets = bottomLabelAlignment.insets + return bottomLabelInsets.top + bottomLabelInsets.bottom + } + + var bottomLabelHorizontalInsets: CGFloat { + let bottomLabelInsets = bottomLabelAlignment.insets + return bottomLabelInsets.left + bottomLabelInsets.right + } + + var topLabelVerticalInsets: CGFloat { + let topLabelInsets = topLabelAlignment.insets + return topLabelInsets.top + topLabelInsets.bottom + } + + var topLabelHorizontalInsets: CGFloat { + let topLabelInsets = topLabelAlignment.insets + return topLabelInsets.left + topLabelInsets.right + } + var messageLabelVerticalInsets: CGFloat { return messageLabelInsets.top + messageLabelInsets.bottom } - + var messageLabelHorizontalInsets: CGFloat { return messageLabelInsets.left + messageLabelInsets.right } - + var messageVerticalPadding: CGFloat { return messageContainerPadding.top + messageContainerPadding.bottom } - + var messageHorizontalPadding: CGFloat { return messageContainerPadding.left + messageContainerPadding.right } - // Cell Top Label - var cellTopLabelSize: CGSize = .zero - var cellTopLabelMaxWidth: CGFloat = 0 - var cellTopLabelAlignment: LabelAlignment = .cellLeading(.zero) - - var cellTopLabelVerticalInsets: CGFloat { - let cellTopLabelInsets = cellTopLabelAlignment.insets - return cellTopLabelInsets.top + cellTopLabelInsets.bottom - } - - var cellTopLabelHorizontalInsets: CGFloat { - let cellTopLabelInsets = cellTopLabelAlignment.insets - return cellTopLabelInsets.left + cellTopLabelInsets.right - } - - // Cell Bottom Label - var cellBottomLabelSize: CGSize = .zero - var cellBottomLabelMaxWidth: CGFloat = 0 - var cellBottomLabelAlignment: LabelAlignment = .cellTrailing(.zero) - - var cellBottomLabelVerticalInsets: CGFloat { - let cellBottomLabelInsets = cellBottomLabelAlignment.insets - return cellBottomLabelInsets.top + cellBottomLabelInsets.bottom - } - - var cellBottomLabelHorizontalInsets: CGFloat { - let cellBottomLabelInsets = cellBottomLabelAlignment.insets - return cellBottomLabelInsets.left + cellBottomLabelInsets.right - } - } diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 21ee1581..88dab984 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -57,10 +57,11 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { /// The default value of this property is 2x the `messageLabelFont`. private var emojiLabelFont: UIFont + typealias MessageID = String + /// The cache for `MessageIntermediateLayoutAttributes`. /// The key is the `messageId` of the `MessageType`. fileprivate var intermediateAttributesCache: [MessageID: MessageIntermediateLayoutAttributes] = [:] - typealias MessageID = String /// Convenience property for accessing the layout object's `MessagesCollectionView`. fileprivate var messagesCollectionView: MessagesCollectionView { @@ -241,14 +242,14 @@ fileprivate extension MessagesCollectionViewFlowLayout { attributes.messageContainerSize = messageContainerSize(for: attributes) // Cell Bottom Label - attributes.cellBottomLabelAlignment = cellBottomLabelAlignment(for: attributes) - attributes.cellBottomLabelMaxWidth = cellBottomLabelMaxWidth(for: attributes) - attributes.cellBottomLabelSize = cellBottomLabelSize(for: attributes) + attributes.bottomLabelAlignment = cellBottomLabelAlignment(for: attributes) + attributes.bottomLabelMaxWidth = cellBottomLabelMaxWidth(for: attributes) + attributes.bottomLabelSize = cellBottomLabelSize(for: attributes) // Cell Top Label - attributes.cellTopLabelAlignment = cellTopLabelAlignment(for: attributes) - attributes.cellTopLabelMaxWidth = cellTopLabelMaxWidth(for: attributes) - attributes.cellTopLabelSize = cellTopLabelSize(for: attributes) + attributes.topLabelAlignment = cellTopLabelAlignment(for: attributes) + attributes.topLabelMaxWidth = cellTopLabelMaxWidth(for: attributes) + attributes.topLabelSize = cellTopLabelSize(for: attributes) // Cell Height attributes.itemHeight = cellHeight(for: attributes) @@ -265,17 +266,12 @@ fileprivate extension MessagesCollectionViewFlowLayout { let intermediateAttributes = messageIntermediateLayoutAttributes(for: attributes.indexPath) - let messageOrigin = messageContainerOrigin(for: intermediateAttributes, and: attributes.frame) - attributes.messageContainerFrame = CGRect(origin: messageOrigin, size: intermediateAttributes.messageContainerSize) + intermediateAttributes.cellFrame = attributes.frame - let avaOrigin = avatarOrigin(for: intermediateAttributes, and: attributes.frame) - attributes.avatarFrame = CGRect(origin: avaOrigin, size: intermediateAttributes.avatarSize) - - let topLabelOrigin = cellTopLabelOrigin(for: intermediateAttributes, and: attributes.frame) - attributes.cellTopLabelFrame = CGRect(origin: topLabelOrigin, size: intermediateAttributes.cellTopLabelSize) - - let bottomLabelOrigin = cellBottomLabelOrigin(for: intermediateAttributes, and: attributes.frame) - attributes.cellBottomLabelFrame = CGRect(origin: bottomLabelOrigin, size: intermediateAttributes.cellBottomLabelSize) + attributes.messageContainerFrame = intermediateAttributes.messageContainerFrame + attributes.topLabelFrame = intermediateAttributes.topLabelFrame + attributes.bottomLabelFrame = intermediateAttributes.bottomLabelFrame + attributes.avatarFrame = intermediateAttributes.avatarFrame switch intermediateAttributes.message.data { case .emoji: @@ -285,13 +281,30 @@ fileprivate extension MessagesCollectionViewFlowLayout { } attributes.messageLabelInsets = intermediateAttributes.messageLabelInsets - attributes.cellTopLabelInsets = intermediateAttributes.cellTopLabelAlignment.insets - attributes.cellBottomLabelInsets = intermediateAttributes.cellBottomLabelAlignment.insets + attributes.topLabelInsets = intermediateAttributes.topLabelAlignment.insets + attributes.bottomLabelInsets = intermediateAttributes.bottomLabelAlignment.insets } } +final class MessageFinalLayoutAttributes { + + + + var messageLabelFont: UIFont = UIFont.preferredFont(forTextStyle: .body) + var messageContainerFrame: CGRect = .zero + var messageLabelInsets: UIEdgeInsets = .zero + + var cellTopLabelFrame: CGRect = .zero + var cellTopLabelInsets: UIEdgeInsets = .zero + + var cellBottomLabelFrame: CGRect = .zero + var cellBottomLabelInsets: UIEdgeInsets = .zero + + +} + // MARK: - Avatar Calculations [ A - C ] fileprivate extension MessagesCollectionViewFlowLayout { @@ -479,7 +492,7 @@ private extension MessagesCollectionViewFlowLayout { /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating the max width. func cellBottomLabelMaxWidth(for attributes: MessageIntermediateLayoutAttributes) -> CGFloat { - let labelHorizontal = attributes.cellBottomLabelAlignment + let labelHorizontal = attributes.bottomLabelAlignment let avatarHorizontal = attributes.avatarPosition.horizontal let avatarVertical = attributes.avatarPosition.vertical let avatarWidth = attributes.avatarSize.width @@ -487,25 +500,25 @@ private extension MessagesCollectionViewFlowLayout { switch (labelHorizontal, avatarHorizontal, avatarVertical) { case (.cellLeading, .cellTrailing, .cellBottom), (.cellTrailing, .cellLeading, .cellBottom): - return itemWidth - avatarWidth - attributes.cellBottomLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.bottomLabelHorizontalInsets case (.cellLeading, _, _), (.cellTrailing, _, _): - return itemWidth - attributes.cellBottomLabelHorizontalInsets + return itemWidth - attributes.bottomLabelHorizontalInsets case (.cellCenter, .cellLeading, .cellBottom), (.cellCenter, .cellTrailing, .cellBottom): - return itemWidth - (avatarWidth * 2) - attributes.cellBottomLabelHorizontalInsets + return itemWidth - (avatarWidth * 2) - attributes.bottomLabelHorizontalInsets case (.cellCenter, .cellLeading, _), (.cellCenter, .cellTrailing, _): - return itemWidth - attributes.cellBottomLabelHorizontalInsets + return itemWidth - attributes.bottomLabelHorizontalInsets case (.messageTrailing, .cellLeading, .cellBottom), (.messageLeading, .cellTrailing, .cellBottom): - return attributes.messageContainerSize.width - attributes.cellBottomLabelHorizontalInsets + return attributes.messageContainerSize.width - attributes.bottomLabelHorizontalInsets case (.messageLeading, .cellLeading, _), (.messageTrailing, .cellTrailing, _): - return itemWidth - avatarWidth - attributes.cellBottomLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.bottomLabelHorizontalInsets case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): - return attributes.messageContainerSize.width + avatarWidth - attributes.cellBottomLabelHorizontalInsets + return attributes.messageContainerSize.width + avatarWidth - attributes.bottomLabelHorizontalInsets case (_, .natural, _): fatalError("AvatarPosition Horizontal.natural needs to be resolved.") @@ -525,9 +538,9 @@ private extension MessagesCollectionViewFlowLayout { guard let bottomLabelText = text else { return .zero } - var bottomLabelSize = labelSize(for: bottomLabelText, considering: attributes.cellBottomLabelMaxWidth) - bottomLabelSize.width += attributes.cellBottomLabelHorizontalInsets - bottomLabelSize.height += attributes.cellBottomLabelVerticalInsets + var bottomLabelSize = labelSize(for: bottomLabelText, considering: attributes.bottomLabelMaxWidth) + bottomLabelSize.width += attributes.bottomLabelHorizontalInsets + bottomLabelSize.height += attributes.bottomLabelVerticalInsets return bottomLabelSize @@ -557,7 +570,7 @@ private extension MessagesCollectionViewFlowLayout { /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating the max width. func cellTopLabelMaxWidth(for attributes: MessageIntermediateLayoutAttributes) -> CGFloat { - let labelHorizontal = attributes.cellTopLabelAlignment + let labelHorizontal = attributes.topLabelAlignment let avatarHorizontal = attributes.avatarPosition.horizontal let avatarVertical = attributes.avatarPosition.vertical let avatarWidth = attributes.avatarSize.width @@ -565,25 +578,25 @@ private extension MessagesCollectionViewFlowLayout { switch (labelHorizontal, avatarHorizontal, avatarVertical) { case (.cellLeading, .cellTrailing, .cellTop), (.cellTrailing, .cellLeading, .cellTop): - return itemWidth - avatarWidth - attributes.cellTopLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.topLabelHorizontalInsets case (.cellLeading, _, _), (.cellTrailing, _, _): - return itemWidth - attributes.cellTopLabelHorizontalInsets + return itemWidth - attributes.topLabelHorizontalInsets case (.cellCenter, .cellLeading, .cellTop), (.cellCenter, .cellTrailing, .cellTop): - return itemWidth - (avatarWidth * 2) - attributes.cellTopLabelHorizontalInsets + return itemWidth - (avatarWidth * 2) - attributes.topLabelHorizontalInsets case (.cellCenter, .cellLeading, _), (.cellCenter, .cellTrailing, _): - return itemWidth - attributes.cellTopLabelHorizontalInsets + return itemWidth - attributes.topLabelHorizontalInsets case (.messageTrailing, .cellLeading, .cellTop), (.messageLeading, .cellTrailing, .cellTop): - return attributes.messageContainerSize.width - attributes.cellTopLabelHorizontalInsets + return attributes.messageContainerSize.width - attributes.topLabelHorizontalInsets case (.messageLeading, .cellLeading, _), (.messageTrailing, .cellTrailing, _): - return itemWidth - avatarWidth - attributes.cellTopLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.topLabelHorizontalInsets case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): - return attributes.messageContainerSize.width + avatarWidth - attributes.cellTopLabelHorizontalInsets + return attributes.messageContainerSize.width + avatarWidth - attributes.topLabelHorizontalInsets case (_, .natural, _): fatalError("AvatarPosition Horizontal.natural needs to be resolved.") @@ -603,9 +616,9 @@ private extension MessagesCollectionViewFlowLayout { guard let topLabelText = text else { return .zero } - var topLabelSize = labelSize(for: topLabelText, considering: attributes.cellTopLabelMaxWidth) - topLabelSize.width += attributes.cellTopLabelHorizontalInsets - topLabelSize.height += attributes.cellTopLabelVerticalInsets + var topLabelSize = labelSize(for: topLabelText, considering: attributes.topLabelMaxWidth) + topLabelSize.width += attributes.topLabelHorizontalInsets + topLabelSize.height += attributes.topLabelVerticalInsets return topLabelSize } @@ -628,20 +641,20 @@ private extension MessagesCollectionViewFlowLayout { switch attributes.avatarPosition.vertical { case .cellTop: - cellHeight += max(attributes.avatarSize.height, attributes.cellTopLabelSize.height) - cellHeight += attributes.cellBottomLabelSize.height + cellHeight += max(attributes.avatarSize.height, attributes.topLabelSize.height) + cellHeight += attributes.bottomLabelSize.height cellHeight += attributes.messageContainerSize.height cellHeight += attributes.messageVerticalPadding case .cellBottom: - cellHeight += max(attributes.avatarSize.height, attributes.cellBottomLabelSize.height) - cellHeight += attributes.cellTopLabelSize.height + cellHeight += max(attributes.avatarSize.height, attributes.bottomLabelSize.height) + cellHeight += attributes.topLabelSize.height cellHeight += attributes.messageContainerSize.height cellHeight += attributes.messageVerticalPadding case .messageTop, .messageCenter, .messageBottom: cellHeight += max(attributes.avatarSize.height, attributes.messageContainerSize.height) cellHeight += attributes.messageVerticalPadding - cellHeight += attributes.cellTopLabelSize.height - cellHeight += attributes.cellBottomLabelSize.height + cellHeight += attributes.topLabelSize.height + cellHeight += attributes.bottomLabelSize.height } return cellHeight @@ -649,145 +662,3 @@ private extension MessagesCollectionViewFlowLayout { } -// MARK: - Cell Content Origin Calculations [ Q - T ] - -fileprivate extension MessagesCollectionViewFlowLayout { - - // Q - - /// Returns the origin point for the `AvatarView`'s frame. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating origin. - private func avatarOrigin(for attributes: MessageIntermediateLayoutAttributes, and contentFrame: CGRect) -> CGPoint { - - guard attributes.avatarSize != .zero else { return .zero } - - var origin = CGPoint.zero - - switch attributes.avatarPosition.horizontal { - case .cellLeading: - origin.x = 0 - case .cellTrailing: - origin.x = contentFrame.width - attributes.avatarSize.width - case .natural: - fatalError("AvatarPosition Horizontal.natural needs to be resolved.") - } - - switch attributes.avatarPosition.vertical { - case .cellTop: - origin.y = 0 - case .cellBottom: - origin.y = contentFrame.height - attributes.avatarSize.height - case .messageTop: - origin.y = attributes.cellTopLabelSize.height + attributes.messageContainerPadding.top - case .messageBottom: - origin.y = contentFrame.height - attributes.avatarSize.height - attributes.cellBottomLabelSize.height - attributes.messageContainerPadding.bottom - case .messageCenter: - let messageMidY = attributes.messageContainerSize.height / 2 - let avatarMidY = attributes.avatarSize.height / 2 - origin.y = attributes.cellTopLabelSize.height + attributes.messageContainerPadding.top + messageMidY - avatarMidY - } - - return origin - } - - // R - - /// Returns the origin point for the `MessageContainerView`'s frame. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating origin. - private func messageContainerOrigin(for attributes: MessageIntermediateLayoutAttributes, and contentFrame: CGRect) -> CGPoint { - - guard attributes.messageContainerSize != .zero else { return .zero } - - var origin = CGPoint.zero - - switch attributes.avatarPosition.horizontal { - case .cellLeading: - origin.x = attributes.avatarSize.width + attributes.messageContainerPadding.left - origin.y = attributes.cellTopLabelSize.height + attributes.messageContainerPadding.top - case .cellTrailing: - origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerSize.width - attributes.messageContainerPadding.right - origin.y = attributes.cellTopLabelSize.height + attributes.messageContainerPadding.top - case .natural: - fatalError("AvatarPosition Horizontal.natural needs to be resolved.") - } - - return origin - - } - - // S - - /// Returns the origin point for the cell's bottom label's frame. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating origin. - private func cellBottomLabelOrigin(for attributes: MessageIntermediateLayoutAttributes, and contentFrame: CGRect) -> CGPoint { - - guard attributes.cellBottomLabelSize != .zero else { return .zero } - - var origin = CGPoint(x: 0, y: attributes.cellTopLabelSize.height + attributes.messageContainerSize.height + attributes.messageVerticalPadding) - - switch (attributes.cellBottomLabelAlignment, attributes.avatarPosition.horizontal) { - case (.cellLeading, _): - origin.x = 0 - case (.cellCenter, _): - origin.x = contentFrame.width / 2 - (attributes.cellBottomLabelSize.width / 2) - case (.cellTrailing, _): - origin.x = contentFrame.width - attributes.cellBottomLabelSize.width - case (.messageLeading, .cellLeading): - origin.x = attributes.avatarSize.width + attributes.messageContainerPadding.left - case (.messageLeading, .cellTrailing): - origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerPadding.right - attributes.messageContainerSize.width - case (.messageTrailing, .cellTrailing): - origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerPadding.right - attributes.cellBottomLabelSize.width - case (.messageTrailing, .cellLeading): - origin.x = attributes.avatarSize.width + attributes.messageContainerPadding.left + attributes.messageContainerSize.width - attributes.cellBottomLabelSize.width - case (_, .natural): - fatalError("AvatarPosition Horizontal.natural needs to be resolved.") - } - - return origin - - } - - // T - - /// Returns the origin point for the cell's top label's frame. - /// - /// - Parameters: - /// - attributes: The `MessageIntermediateLayoutAttributes` to consider when calculating origin. - fileprivate func cellTopLabelOrigin(for attributes: MessageIntermediateLayoutAttributes, and contentFrame: CGRect) -> CGPoint { - - guard attributes.cellTopLabelSize != .zero else { return .zero } - - var origin = CGPoint.zero - - switch (attributes.cellTopLabelAlignment, attributes.avatarPosition.horizontal) { - case (.cellLeading, _): - origin.x = 0 - case (.cellCenter, _): - origin.x = contentFrame.width / 2 - (attributes.cellTopLabelSize.width / 2) - case (.cellTrailing, _): - origin.x = contentFrame.width - attributes.cellTopLabelSize.width - case (.messageLeading, .cellLeading): - origin.x = attributes.avatarSize.width + attributes.messageContainerPadding.left - case (.messageLeading, .cellTrailing): - origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerPadding.right - attributes.messageContainerSize.width - case (.messageTrailing, .cellTrailing): - origin.x = contentFrame.width - attributes.avatarSize.width - attributes.messageContainerPadding.right - attributes.cellTopLabelSize.width - case (.messageTrailing, .cellLeading): - origin.x = contentFrame.width - attributes.messageContainerPadding.right - attributes.cellTopLabelSize.width - case (_, .natural): - fatalError("AvatarPosition Horizontal.natural needs to be resolved.") - } - - return origin - - } - -} - diff --git a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift index c9f9f731..9de726b2 100644 --- a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift +++ b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift @@ -35,11 +35,11 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib var messageContainerFrame: CGRect = .zero var messageLabelInsets: UIEdgeInsets = .zero - var cellTopLabelFrame: CGRect = .zero - var cellTopLabelInsets: UIEdgeInsets = .zero + var topLabelFrame: CGRect = .zero + var topLabelInsets: UIEdgeInsets = .zero - var cellBottomLabelFrame: CGRect = .zero - var cellBottomLabelInsets: UIEdgeInsets = .zero + var bottomLabelFrame: CGRect = .zero + var bottomLabelInsets: UIEdgeInsets = .zero // MARK: - Methods @@ -50,10 +50,10 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib copy.messageContainerFrame = messageContainerFrame copy.messageLabelFont = messageLabelFont copy.messageLabelInsets = messageLabelInsets - copy.cellTopLabelFrame = cellTopLabelFrame - copy.cellTopLabelInsets = cellTopLabelInsets - copy.cellBottomLabelFrame = cellBottomLabelFrame - copy.cellBottomLabelInsets = cellBottomLabelInsets + copy.topLabelFrame = topLabelFrame + copy.topLabelInsets = topLabelInsets + copy.bottomLabelFrame = bottomLabelFrame + copy.bottomLabelInsets = bottomLabelInsets return copy // swiftlint:enable force_cast } diff --git a/Sources/Views/Cells/MessageCollectionViewCell.swift b/Sources/Views/Cells/MessageCollectionViewCell.swift index f3f068f3..19edea76 100644 --- a/Sources/Views/Cells/MessageCollectionViewCell.swift +++ b/Sources/Views/Cells/MessageCollectionViewCell.swift @@ -96,11 +96,11 @@ open class MessageCollectionViewCell: UICollectionViewCell, messageContainerView.frame = attributes.messageContainerFrame messageContentView.frame = messageContainerView.bounds - cellTopLabel.frame = attributes.cellTopLabelFrame - cellTopLabel.textInsets = attributes.cellTopLabelInsets + cellTopLabel.frame = attributes.topLabelFrame + cellTopLabel.textInsets = attributes.topLabelInsets - cellBottomLabel.frame = attributes.cellBottomLabelFrame - cellBottomLabel.textInsets = attributes.cellBottomLabelInsets + cellBottomLabel.frame = attributes.bottomLabelFrame + cellBottomLabel.textInsets = attributes.bottomLabelInsets } From ba506ba46d0270b42b7f40b73b9e5254ee648e85 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 19:42:39 -0600 Subject: [PATCH 27/59] Remove MessageLabel typing from cell top & bottom labels --- .../MessageIntermediateLayoutAttributes.swift | 42 ++++++++++------ .../MessagesCollectionViewFlowLayout.swift | 50 ++++++++----------- ...ssagesCollectionViewLayoutAttributes.swift | 5 -- .../Cells/MessageCollectionViewCell.swift | 16 +----- 4 files changed, 48 insertions(+), 65 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index ad4560da..58390564 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -84,7 +84,7 @@ final class MessageIntermediateLayoutAttributes { guard messageContainerSize != .zero else { return .zero } var origin: CGPoint = .zero - origin.y = topLabelSize.height + messageContainerPadding.top + origin.y = topLabelSize.height + messageContainerPadding.top + topLabelVerticalPadding switch avatarPosition.horizontal { case .cellLeading: @@ -110,17 +110,19 @@ final class MessageIntermediateLayoutAttributes { var origin = CGPoint.zero + origin.y = topLabelPadding.top + switch topLabelAlignment { case .cellLeading: - break + origin.x = topLabelPadding.left case .cellCenter: - origin.x = cellFrame.midX + origin.x = cellFrame.midX + topLabelPadding.left - topLabelPadding.right case .cellTrailing: - origin.x = cellFrame.maxX - topLabelSize.width + origin.x = cellFrame.maxX - topLabelSize.width - topLabelPadding.right case .messageLeading: - origin.x = messageContainerFrame.minX + origin.x = messageContainerFrame.minX + topLabelPadding.left case .messageTrailing: - origin.x = messageContainerFrame.maxX - topLabelSize.width + origin.x = messageContainerFrame.maxX - topLabelSize.width - topLabelPadding.right } return CGRect(origin: origin, size: topLabelSize) @@ -138,19 +140,19 @@ final class MessageIntermediateLayoutAttributes { var origin: CGPoint = .zero - origin.y = messageContainerFrame.maxY + messageContainerPadding.bottom + origin.y = messageContainerFrame.maxY + messageContainerPadding.bottom + bottomLabelPadding.top switch bottomLabelAlignment { case .cellLeading: - break + origin.x = bottomLabelPadding.left case .cellCenter: - origin.x = cellFrame.midX + origin.x = cellFrame.midX + bottomLabelPadding.left - bottomLabelPadding.right case .cellTrailing: - origin.x = cellFrame.maxX - bottomLabelSize.width + origin.x = cellFrame.maxX - bottomLabelSize.width - bottomLabelPadding.right case .messageLeading: - origin.x = messageContainerFrame.minX + origin.x = messageContainerFrame.minX + bottomLabelPadding.left case .messageTrailing: - origin.x = messageContainerFrame.maxX - bottomLabelSize.width + origin.x = messageContainerFrame.maxX - bottomLabelSize.width - bottomLabelPadding.right } return CGRect(origin: origin, size: bottomLabelSize) @@ -170,22 +172,30 @@ final class MessageIntermediateLayoutAttributes { extension MessageIntermediateLayoutAttributes { - var bottomLabelVerticalInsets: CGFloat { + var bottomLabelPadding: UIEdgeInsets { + return bottomLabelAlignment.insets + } + + var bottomLabelVerticalPadding: CGFloat { let bottomLabelInsets = bottomLabelAlignment.insets return bottomLabelInsets.top + bottomLabelInsets.bottom } - var bottomLabelHorizontalInsets: CGFloat { + var bottomLabelHorizontalPadding: CGFloat { let bottomLabelInsets = bottomLabelAlignment.insets return bottomLabelInsets.left + bottomLabelInsets.right } - var topLabelVerticalInsets: CGFloat { + var topLabelPadding: UIEdgeInsets { + return topLabelAlignment.insets + } + + var topLabelVerticalPadding: CGFloat { let topLabelInsets = topLabelAlignment.insets return topLabelInsets.top + topLabelInsets.bottom } - var topLabelHorizontalInsets: CGFloat { + var topLabelHorizontalPadding: CGFloat { let topLabelInsets = topLabelAlignment.insets return topLabelInsets.left + topLabelInsets.right } diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 88dab984..98a600fc 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -272,6 +272,7 @@ fileprivate extension MessagesCollectionViewFlowLayout { attributes.topLabelFrame = intermediateAttributes.topLabelFrame attributes.bottomLabelFrame = intermediateAttributes.bottomLabelFrame attributes.avatarFrame = intermediateAttributes.avatarFrame + attributes.messageLabelInsets = intermediateAttributes.messageLabelInsets switch intermediateAttributes.message.data { case .emoji: @@ -279,13 +280,9 @@ fileprivate extension MessagesCollectionViewFlowLayout { default: attributes.messageLabelFont = messageLabelFont } - - attributes.messageLabelInsets = intermediateAttributes.messageLabelInsets - attributes.topLabelInsets = intermediateAttributes.topLabelAlignment.insets - attributes.bottomLabelInsets = intermediateAttributes.bottomLabelAlignment.insets - + } - + } final class MessageFinalLayoutAttributes { @@ -500,25 +497,25 @@ private extension MessagesCollectionViewFlowLayout { switch (labelHorizontal, avatarHorizontal, avatarVertical) { case (.cellLeading, .cellTrailing, .cellBottom), (.cellTrailing, .cellLeading, .cellBottom): - return itemWidth - avatarWidth - attributes.bottomLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.bottomLabelHorizontalPadding case (.cellLeading, _, _), (.cellTrailing, _, _): - return itemWidth - attributes.bottomLabelHorizontalInsets + return itemWidth - attributes.bottomLabelHorizontalPadding case (.cellCenter, .cellLeading, .cellBottom), (.cellCenter, .cellTrailing, .cellBottom): - return itemWidth - (avatarWidth * 2) - attributes.bottomLabelHorizontalInsets + return itemWidth - (avatarWidth * 2) - attributes.bottomLabelHorizontalPadding case (.cellCenter, .cellLeading, _), (.cellCenter, .cellTrailing, _): - return itemWidth - attributes.bottomLabelHorizontalInsets + return itemWidth - attributes.bottomLabelHorizontalPadding case (.messageTrailing, .cellLeading, .cellBottom), (.messageLeading, .cellTrailing, .cellBottom): - return attributes.messageContainerSize.width - attributes.bottomLabelHorizontalInsets + return attributes.messageContainerSize.width - attributes.bottomLabelHorizontalPadding case (.messageLeading, .cellLeading, _), (.messageTrailing, .cellTrailing, _): - return itemWidth - avatarWidth - attributes.bottomLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.bottomLabelHorizontalPadding case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): - return attributes.messageContainerSize.width + avatarWidth - attributes.bottomLabelHorizontalInsets + return attributes.messageContainerSize.width + avatarWidth - attributes.bottomLabelHorizontalPadding case (_, .natural, _): fatalError("AvatarPosition Horizontal.natural needs to be resolved.") @@ -538,11 +535,7 @@ private extension MessagesCollectionViewFlowLayout { guard let bottomLabelText = text else { return .zero } - var bottomLabelSize = labelSize(for: bottomLabelText, considering: attributes.bottomLabelMaxWidth) - bottomLabelSize.width += attributes.bottomLabelHorizontalInsets - bottomLabelSize.height += attributes.bottomLabelVerticalInsets - - return bottomLabelSize + return labelSize(for: bottomLabelText, considering: attributes.bottomLabelMaxWidth) } @@ -578,25 +571,25 @@ private extension MessagesCollectionViewFlowLayout { switch (labelHorizontal, avatarHorizontal, avatarVertical) { case (.cellLeading, .cellTrailing, .cellTop), (.cellTrailing, .cellLeading, .cellTop): - return itemWidth - avatarWidth - attributes.topLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.topLabelHorizontalPadding case (.cellLeading, _, _), (.cellTrailing, _, _): - return itemWidth - attributes.topLabelHorizontalInsets + return itemWidth - attributes.topLabelHorizontalPadding case (.cellCenter, .cellLeading, .cellTop), (.cellCenter, .cellTrailing, .cellTop): - return itemWidth - (avatarWidth * 2) - attributes.topLabelHorizontalInsets + return itemWidth - (avatarWidth * 2) - attributes.topLabelHorizontalPadding case (.cellCenter, .cellLeading, _), (.cellCenter, .cellTrailing, _): - return itemWidth - attributes.topLabelHorizontalInsets + return itemWidth - attributes.topLabelHorizontalPadding case (.messageTrailing, .cellLeading, .cellTop), (.messageLeading, .cellTrailing, .cellTop): - return attributes.messageContainerSize.width - attributes.topLabelHorizontalInsets + return attributes.messageContainerSize.width - attributes.topLabelHorizontalPadding case (.messageLeading, .cellLeading, _), (.messageTrailing, .cellTrailing, _): - return itemWidth - avatarWidth - attributes.topLabelHorizontalInsets + return itemWidth - avatarWidth - attributes.topLabelHorizontalPadding case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): - return attributes.messageContainerSize.width + avatarWidth - attributes.topLabelHorizontalInsets + return attributes.messageContainerSize.width + avatarWidth - attributes.topLabelHorizontalPadding case (_, .natural, _): fatalError("AvatarPosition Horizontal.natural needs to be resolved.") @@ -616,11 +609,8 @@ private extension MessagesCollectionViewFlowLayout { guard let topLabelText = text else { return .zero } - var topLabelSize = labelSize(for: topLabelText, considering: attributes.topLabelMaxWidth) - topLabelSize.width += attributes.topLabelHorizontalInsets - topLabelSize.height += attributes.topLabelVerticalInsets - - return topLabelSize + return labelSize(for: topLabelText, considering: attributes.topLabelMaxWidth) + } } diff --git a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift index 9de726b2..f7bd09ee 100644 --- a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift +++ b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift @@ -36,10 +36,7 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib var messageLabelInsets: UIEdgeInsets = .zero var topLabelFrame: CGRect = .zero - var topLabelInsets: UIEdgeInsets = .zero - var bottomLabelFrame: CGRect = .zero - var bottomLabelInsets: UIEdgeInsets = .zero // MARK: - Methods @@ -51,9 +48,7 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib copy.messageLabelFont = messageLabelFont copy.messageLabelInsets = messageLabelInsets copy.topLabelFrame = topLabelFrame - copy.topLabelInsets = topLabelInsets copy.bottomLabelFrame = bottomLabelFrame - copy.bottomLabelInsets = bottomLabelInsets return copy // swiftlint:enable force_cast } diff --git a/Sources/Views/Cells/MessageCollectionViewCell.swift b/Sources/Views/Cells/MessageCollectionViewCell.swift index 19edea76..d1f24c56 100644 --- a/Sources/Views/Cells/MessageCollectionViewCell.swift +++ b/Sources/Views/Cells/MessageCollectionViewCell.swift @@ -38,11 +38,7 @@ open class MessageCollectionViewCell: UICollectionViewCell, open var avatarView: AvatarView = AvatarView() - open var cellTopLabel: MessageLabel = { - let topLabel = MessageLabel() - topLabel.enabledDetectors = [] - return topLabel - }() + open var cellTopLabel = UILabel() open var messageContentView: ContentView = { let contentView = ContentView() @@ -51,11 +47,7 @@ open class MessageCollectionViewCell: UICollectionViewCell, return contentView }() - open var cellBottomLabel: MessageLabel = { - let bottomLabel = MessageLabel() - bottomLabel.enabledDetectors = [] - return bottomLabel - }() + open var cellBottomLabel = UILabel() open weak var delegate: MessageCellDelegate? @@ -95,12 +87,8 @@ open class MessageCollectionViewCell: UICollectionViewCell, messageContainerView.frame = attributes.messageContainerFrame messageContentView.frame = messageContainerView.bounds - cellTopLabel.frame = attributes.topLabelFrame - cellTopLabel.textInsets = attributes.topLabelInsets - cellBottomLabel.frame = attributes.bottomLabelFrame - cellBottomLabel.textInsets = attributes.bottomLabelInsets } From de610f525b02753fa3cbd5721f1997f25aef9256 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 19:48:00 -0600 Subject: [PATCH 28/59] Remove unused class and gardening --- .../MessageIntermediateLayoutAttributes.swift | 12 ++++-------- .../MessagesCollectionViewFlowLayout.swift | 17 ----------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index 58390564..d3712608 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -177,13 +177,11 @@ extension MessageIntermediateLayoutAttributes { } var bottomLabelVerticalPadding: CGFloat { - let bottomLabelInsets = bottomLabelAlignment.insets - return bottomLabelInsets.top + bottomLabelInsets.bottom + return bottomLabelPadding.top + bottomLabelPadding.bottom } var bottomLabelHorizontalPadding: CGFloat { - let bottomLabelInsets = bottomLabelAlignment.insets - return bottomLabelInsets.left + bottomLabelInsets.right + return bottomLabelPadding.left + bottomLabelPadding.right } var topLabelPadding: UIEdgeInsets { @@ -191,13 +189,11 @@ extension MessageIntermediateLayoutAttributes { } var topLabelVerticalPadding: CGFloat { - let topLabelInsets = topLabelAlignment.insets - return topLabelInsets.top + topLabelInsets.bottom + return topLabelPadding.top + topLabelPadding.bottom } var topLabelHorizontalPadding: CGFloat { - let topLabelInsets = topLabelAlignment.insets - return topLabelInsets.left + topLabelInsets.right + return topLabelPadding.left + topLabelPadding.right } var messageLabelVerticalInsets: CGFloat { diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 98a600fc..07988470 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -285,23 +285,6 @@ fileprivate extension MessagesCollectionViewFlowLayout { } -final class MessageFinalLayoutAttributes { - - - - var messageLabelFont: UIFont = UIFont.preferredFont(forTextStyle: .body) - var messageContainerFrame: CGRect = .zero - var messageLabelInsets: UIEdgeInsets = .zero - - var cellTopLabelFrame: CGRect = .zero - var cellTopLabelInsets: UIEdgeInsets = .zero - - var cellBottomLabelFrame: CGRect = .zero - var cellBottomLabelInsets: UIEdgeInsets = .zero - - -} - // MARK: - Avatar Calculations [ A - C ] fileprivate extension MessagesCollectionViewFlowLayout { From 65e7d1c7ffc7ba0ad73d9a2e062b0095d9187891 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 21:14:01 -0600 Subject: [PATCH 29/59] Resolve merge conflict --- Sources/Views/MessageInputBar.swift | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 0756d9ca..70418118 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -358,11 +358,7 @@ open class MessageInputBar: UIView { rightStackViewLayoutSet?.activate() bottomStackViewLayoutSet?.activate() } - -<<<<<<< HEAD - /// Updates the constraint constants that correspond to the padding UIEdgeInsets - private func updatePadding() { -======= + open override func didMoveToWindow() { super.didMoveToWindow() if #available(iOS 11.0, *) { @@ -375,9 +371,8 @@ open class MessageInputBar: UIView { } } - private func updateViewContraints() { - ->>>>>>> master + private func updatePadding() { + textViewLayoutSet?.top?.constant = padding.top leftStackViewLayoutSet?.top?.constant = padding.top leftStackViewLayoutSet?.left?.constant = padding.left From a5c5515774445577a3f0cae312b573ad241a1273 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 21:15:31 -0600 Subject: [PATCH 30/59] Fix longpressgesture delegate typo --- Sources/Views/MessageLabel.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Views/MessageLabel.swift b/Sources/Views/MessageLabel.swift index 37cc5b0a..34153674 100644 --- a/Sources/Views/MessageLabel.swift +++ b/Sources/Views/MessageLabel.swift @@ -408,7 +408,7 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleGesture(_:))) addGestureRecognizer(longPressGesture) - tapGesture.delegate = self + longPressGesture.delegate = self isUserInteractionEnabled = true } From 8015253d5ec179305d09802823ac120abf1a031b Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 21:32:28 -0600 Subject: [PATCH 31/59] Reduce text parsing for setting non-text properties --- Sources/Views/MessageLabel.swift | 125 ++++++++++++++----------------- 1 file changed, 55 insertions(+), 70 deletions(-) diff --git a/Sources/Views/MessageLabel.swift b/Sources/Views/MessageLabel.swift index 34153674..95bfad8c 100644 --- a/Sources/Views/MessageLabel.swift +++ b/Sources/Views/MessageLabel.swift @@ -59,39 +59,30 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { open override var attributedText: NSAttributedString? { didSet { - guard attributedText != oldValue else { return } - setTextStorage() + setTextStorage(shouldParse: true) } } open override var text: String? { didSet { - guard text != oldValue else { return } - setTextStorage() + setTextStorage(shouldParse: true) } } open override var font: UIFont! { didSet { - guard font != oldValue else { return } - guard let attributedText = attributedText else { return } - textStorage.setAttributedString(attributedText) - setNeedsDisplay() + setTextStorage(shouldParse: false) } } open override var textColor: UIColor! { didSet { - guard textColor != oldValue else { return } - guard let attributedText = attributedText else { return } - textStorage.setAttributedString(attributedText) - setNeedsDisplay() + setTextStorage(shouldParse: false) } } open override var lineBreakMode: NSLineBreakMode { didSet { - guard lineBreakMode != oldValue else { return } textContainer.lineBreakMode = lineBreakMode setNeedsDisplay() } @@ -99,7 +90,6 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { open override var numberOfLines: Int { didSet { - guard numberOfLines != oldValue else { return } textContainer.maximumNumberOfLines = numberOfLines setNeedsDisplay() } @@ -107,14 +97,12 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { open override var textAlignment: NSTextAlignment { didSet { - guard textAlignment != oldValue else { return } - setTextStorage() + setTextStorage(shouldParse: false) } } open var textInsets: UIEdgeInsets = .zero { didSet { - guard textInsets != oldValue else { return } setNeedsDisplay() } } @@ -232,60 +220,53 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { // MARK: - Private Methods - private func setTextStorage() { - - // Anytime we update the text storage we need to clear the previous ranges - rangesForDetectors.removeAll() + private func setTextStorage(shouldParse: Bool) { guard let attributedText = attributedText, attributedText.length > 0 else { textStorage.setAttributedString(NSAttributedString()) setNeedsDisplay() return } - - guard let checkingResults = parse(text: attributedText, for: enabledDetectors), checkingResults.isEmpty == false else { - let textWithParagraphAttributes = addParagraphStyleAttribute(to: attributedText) - textStorage.setAttributedString(textWithParagraphAttributes) - setNeedsDisplay() - return + + let style = paragraphStyle(for: attributedText) + let range = NSRange(location: 0, length: attributedText.length) + + let mutableText = NSMutableAttributedString(attributedString: attributedText) + mutableText.addAttribute(.paragraphStyle, value: style, range: range) + + if shouldParse { + rangesForDetectors.removeAll() + let results = parse(text: mutableText) + setRangesForDetectors(in: results) + } + + for (detector, rangeTuples) in rangesForDetectors { + if enabledDetectors.contains(detector) { + let attributes = detectorAttributes(for: detector) + rangeTuples.forEach { (range, _) in + mutableText.addAttributes(attributes, range: range) + } + } } - setRangesForDetectors(in: checkingResults) - - let textWithDetectorAttributes = addDetectorAttributes(to: attributedText, for: checkingResults) - let textWithParagraphAttributes = addParagraphStyleAttribute(to: textWithDetectorAttributes) - - textStorage.setAttributedString(textWithParagraphAttributes) + let modifiedText = NSAttributedString(attributedString: mutableText) + textStorage.setAttributedString(modifiedText) setNeedsDisplay() } - - private func addParagraphStyleAttribute(to text: NSAttributedString) -> NSAttributedString { - - let mutableAttributedString = NSMutableAttributedString(attributedString: text) - var textRange = NSRange(location: 0, length: 0) - - let paragraphStyle = text.attribute(NSAttributedStringKey.paragraphStyle, at: 0, effectiveRange: &textRange) as? NSMutableParagraphStyle ?? NSMutableParagraphStyle() - paragraphStyle.lineBreakMode = lineBreakMode - paragraphStyle.alignment = textAlignment - - mutableAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: textRange) - - return mutableAttributedString - - } - - private func addDetectorAttributes(to text: NSAttributedString, for checkingResults: [NSTextCheckingResult]) -> NSAttributedString { - - let mutableAttributedString = NSMutableAttributedString(attributedString: text) - - checkingResults.forEach { result in - let attributes = detectorAttributes(for: result.resultType) - mutableAttributedString.addAttributes(attributes, range: result.range) - } - - return mutableAttributedString + + private func paragraphStyle(for text: NSAttributedString) -> NSParagraphStyle { + guard text.length > 0 else { return NSParagraphStyle() } + + var range = NSRange(location: 0, length: text.length) + let existingStyle = text.attribute(.paragraphStyle, at: 0, effectiveRange: &range) as? NSMutableParagraphStyle + let style = existingStyle ?? NSMutableParagraphStyle() + + style.lineBreakMode = lineBreakMode + style.alignment = textAlignment + + return style } private func updateAttributes(for detectorType: DetectorType) { @@ -336,16 +317,18 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { // MARK: - Parsing Text - private func parse(text: NSAttributedString, for detectorTypes: [DetectorType]) -> [NSTextCheckingResult]? { - guard detectorTypes.isEmpty == false else { return nil } - let checkingTypes = detectorTypes.reduce(0) { $0 | $1.textCheckingType.rawValue } + private func parse(text: NSAttributedString) -> [NSTextCheckingResult] { + guard enabledDetectors.isEmpty == false else { return [] } + let checkingTypes = enabledDetectors.reduce(0) { $0 | $1.textCheckingType.rawValue } let detector = try? NSDataDetector(types: checkingTypes) - - return detector?.matches(in: text.string, options: [], range: NSRange(location: 0, length: text.length)) + let range = NSRange(location: 0, length: text.length) + return detector?.matches(in: text.string, options: [], range: range) ?? [] } private func setRangesForDetectors(in checkingResults: [NSTextCheckingResult]) { + guard checkingResults.isEmpty == false else { return } + for result in checkingResults { switch result.resultType { @@ -388,15 +371,17 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { location.x -= textOffset.x location.y -= textOffset.y - let glyphIndex = layoutManager.glyphIndex(for: location, in: textContainer) - - let lineRect = layoutManager.lineFragmentUsedRect(forGlyphAt: glyphIndex, effectiveRange: nil) + let index = layoutManager.glyphIndex(for: location, in: textContainer) + let lineRect = layoutManager.lineFragmentUsedRect(forGlyphAt: index, effectiveRange: nil) + + var characterIndex: Int? + if lineRect.contains(location) { - return layoutManager.characterIndexForGlyph(at: glyphIndex) - } else { - return nil + characterIndex = layoutManager.characterIndexForGlyph(at: index) } + + return characterIndex } From 759a13103a13009fae62bba7c741d90d33c9d7e0 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 21:37:10 -0600 Subject: [PATCH 32/59] Add isConfiguring method --- Sources/Views/MessageLabel.swift | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Sources/Views/MessageLabel.swift b/Sources/Views/MessageLabel.swift index 95bfad8c..7471f79d 100644 --- a/Sources/Views/MessageLabel.swift +++ b/Sources/Views/MessageLabel.swift @@ -50,6 +50,8 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { }() private lazy var rangesForDetectors: [DetectorType: [(NSRange, MessageTextCheckingType)]] = [:] + + private var isConfiguring: Bool = false // MARK: - Public Properties @@ -84,14 +86,14 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { open override var lineBreakMode: NSLineBreakMode { didSet { textContainer.lineBreakMode = lineBreakMode - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } open override var numberOfLines: Int { didSet { textContainer.maximumNumberOfLines = numberOfLines - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } @@ -103,35 +105,35 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { open var textInsets: UIEdgeInsets = .zero { didSet { - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } open var addressAttributes: [NSAttributedStringKey: Any] = [:] { didSet { updateAttributes(for: .address) - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } open var dateAttributes: [NSAttributedStringKey: Any] = [:] { didSet { updateAttributes(for: .date) - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } open var phoneNumberAttributes: [NSAttributedStringKey: Any] = [:] { didSet { updateAttributes(for: .phoneNumber) - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } open var urlAttributes: [NSAttributedStringKey: Any] = [:] { didSet { updateAttributes(for: .url) - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } } @@ -178,6 +180,13 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { } // MARK: - Public Methods + + public func configure(block: () -> Void) { + isConfiguring = true + block() + isConfiguring = false + setNeedsDisplay() + } // MARK: UIGestureRecognizer Delegate @@ -252,7 +261,7 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { let modifiedText = NSAttributedString(attributedString: mutableText) textStorage.setAttributedString(modifiedText) - setNeedsDisplay() + if !isConfiguring { setNeedsDisplay() } } From 86a269cc21627aa935f65b0084bfcfc237419e6b Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 21:42:47 -0600 Subject: [PATCH 33/59] Disable detectors by default --- Sources/Views/Cells/TextMessageCell.swift | 16 ++++++++++++---- Sources/Views/MessageLabel.swift | 6 +++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Sources/Views/Cells/TextMessageCell.swift b/Sources/Views/Cells/TextMessageCell.swift index 278ba26d..15a796c5 100644 --- a/Sources/Views/Cells/TextMessageCell.swift +++ b/Sources/Views/Cells/TextMessageCell.swift @@ -47,8 +47,12 @@ open class TextMessageCell: MessageCollectionViewCell { super.apply(layoutAttributes) guard let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes else { return } - messageContentView.textInsets = attributes.messageLabelInsets - messageContentView.font = attributes.messageLabelFont + + messageContentView.configure { + messageContentView.textInsets = attributes.messageLabelInsets + messageContentView.font = attributes.messageLabelFont + } + } open override func prepareForReuse() { @@ -63,8 +67,12 @@ open class TextMessageCell: MessageCollectionViewCell { if let displayDelegate = messagesCollectionView.messagesDisplayDelegate as? TextMessageDisplayDelegate { let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView) let detectors = displayDelegate.enabledDetectors(for: message, at: indexPath, in: messagesCollectionView) - messageContentView.textColor = textColor - messageContentView.enabledDetectors = detectors + + messageContentView.configure { + messageContentView.textColor = textColor + messageContentView.enabledDetectors = detectors + } + } switch message.data { diff --git a/Sources/Views/MessageLabel.swift b/Sources/Views/MessageLabel.swift index 7471f79d..64981f2e 100644 --- a/Sources/Views/MessageLabel.swift +++ b/Sources/Views/MessageLabel.swift @@ -57,7 +57,11 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate { open weak var delegate: MessageLabelDelegate? - open var enabledDetectors: [DetectorType] = [.phoneNumber, .address, .date, .url] + open var enabledDetectors: [DetectorType] = [] { + didSet { + setTextStorage(shouldParse: true) + } + } open override var attributedText: NSAttributedString? { didSet { From 2017404988d145e0cb233724f5fb3a4a2b732d01 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 18 Nov 2017 21:45:20 -0600 Subject: [PATCH 34/59] Disable MessageLabel detectors by default --- Sources/Protocols/MessagesDisplayDelegate.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Protocols/MessagesDisplayDelegate.swift b/Sources/Protocols/MessagesDisplayDelegate.swift index 6dfcccf3..ed314071 100644 --- a/Sources/Protocols/MessagesDisplayDelegate.swift +++ b/Sources/Protocols/MessagesDisplayDelegate.swift @@ -61,7 +61,7 @@ public extension TextMessageDisplayDelegate { } func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] { - return [.url, .address, .phoneNumber, .date] + return [] } } From fe89b4590ed806c58b711f2f1328545cb1024eac Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 23 Nov 2017 15:12:30 +0800 Subject: [PATCH 35/59] Fix MessageInputBar bottomAnchor issue --- Sources/Views/MessageInputBar.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 70418118..1b2f7c10 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -365,9 +365,7 @@ open class MessageInputBar: UIView { guard let window = window else { return } // bottomAnchor must be set to the window to avoid a memory leak issue - bottomStackViewLayoutSet?.bottom?.isActive = false - bottomStackViewLayoutSet?.bottom = bottomStackView.bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1) - bottomStackViewLayoutSet?.bottom?.isActive = true + bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1) } } From 5bb3e0988a0b4e37e66591de44ba3a563ba0b238 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 23 Nov 2017 16:31:10 +0800 Subject: [PATCH 36/59] Add bottomStackView --- Sources/Views/MessageInputBar.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 1b2f7c10..34529cb9 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -350,6 +350,7 @@ open class MessageInputBar: UIView { topStackViewLayoutSet?.right = topStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: topStackViewPadding.right) leftStackViewLayoutSet?.left = leftStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: padding.left) rightStackViewLayoutSet?.right = rightStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -padding.right) + bottomStackViewLayoutSet?.bottom = bottomStackView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -padding.bottom) bottomStackViewLayoutSet?.left = bottomStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: padding.left) bottomStackViewLayoutSet?.right = bottomStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -padding.right) } From fe3117367c6253764337819e5a68a8e947a7de02 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 23 Nov 2017 18:10:33 +0800 Subject: [PATCH 37/59] update --- Sources/Views/MessageInputBar.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 34529cb9..7f1bb2a6 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -350,7 +350,6 @@ open class MessageInputBar: UIView { topStackViewLayoutSet?.right = topStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: topStackViewPadding.right) leftStackViewLayoutSet?.left = leftStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: padding.left) rightStackViewLayoutSet?.right = rightStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -padding.right) - bottomStackViewLayoutSet?.bottom = bottomStackView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -padding.bottom) bottomStackViewLayoutSet?.left = bottomStackView.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: padding.left) bottomStackViewLayoutSet?.right = bottomStackView.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -padding.right) } @@ -366,7 +365,9 @@ open class MessageInputBar: UIView { guard let window = window else { return } // bottomAnchor must be set to the window to avoid a memory leak issue - bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1) + bottomStackViewLayoutSet?.bottom?.isActive = false + bottomStackViewLayoutSet?.bottom = bottomStackView.bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1) + bottomStackViewLayoutSet?.bottom?.isActive = true } } @@ -420,7 +421,7 @@ open class MessageInputBar: UIView { open func calculateIntrinsicContentSize() -> CGSize { let maxTextViewSize = CGSize(width: inputTextView.bounds.width, height: .greatestFiniteMagnitude) - var heightToFit = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() + var heightToFit = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() + padding.top + padding.bottom + textViewPadding.bottom + topStackView.bounds.height + bottomStackView.bounds.height if heightToFit >= maxHeight { if !isOverMaxTextViewHeight { textViewHeightAnchor?.isActive = true @@ -545,6 +546,7 @@ open class MessageInputBar: UIView { guard superview != nil else { return } topStackView.layoutIfNeeded() } + invalidateIntrinsicContentSize() } performLayout(animated) { From c212af8cee9ade5f1e0631e52ecbc56ddae13158 Mon Sep 17 00:00:00 2001 From: Wu Zhong Date: Thu, 23 Nov 2017 18:12:32 +0800 Subject: [PATCH 38/59] Revert "update" --- Sources/Views/MessageInputBar.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 7f1bb2a6..70418118 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -421,7 +421,7 @@ open class MessageInputBar: UIView { open func calculateIntrinsicContentSize() -> CGSize { let maxTextViewSize = CGSize(width: inputTextView.bounds.width, height: .greatestFiniteMagnitude) - var heightToFit = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() + padding.top + padding.bottom + textViewPadding.bottom + topStackView.bounds.height + bottomStackView.bounds.height + var heightToFit = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() if heightToFit >= maxHeight { if !isOverMaxTextViewHeight { textViewHeightAnchor?.isActive = true @@ -546,7 +546,6 @@ open class MessageInputBar: UIView { guard superview != nil else { return } topStackView.layoutIfNeeded() } - invalidateIntrinsicContentSize() } performLayout(animated) { From 12d177a2bcff4126da5c9cb4eb4451684ba1da25 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 27 Nov 2017 19:20:01 -0600 Subject: [PATCH 39/59] Fix failing tests --- .../MessageCollectionViewCellTests.swift | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Tests/ViewsTests/MessageCollectionViewCellTests.swift b/Tests/ViewsTests/MessageCollectionViewCellTests.swift index 5183f44b..1a77328b 100644 --- a/Tests/ViewsTests/MessageCollectionViewCellTests.swift +++ b/Tests/ViewsTests/MessageCollectionViewCellTests.swift @@ -51,19 +51,11 @@ class MessageCollectionViewCellTests: XCTestCase { XCTAssertTrue(cell.messageContainerView.layer.masksToBounds) } - func testCellTopLabelPropertySetup() { - XCTAssertEqual(cell.cellTopLabel.enabledDetectors, []) - } - func testMessageContentViewPropertiesSetup() { XCTAssertTrue(cell.messageContentView.clipsToBounds) XCTAssertTrue(cell.messageContentView.isUserInteractionEnabled) } - func testCellBottomLabelPropertiesSetup() { - XCTAssertEqual(cell.cellBottomLabel.enabledDetectors, []) - } - func testPrepareForReuse() { cell.prepareForReuse() XCTAssertNil(cell.cellTopLabel.text) @@ -79,10 +71,8 @@ class MessageCollectionViewCellTests: XCTestCase { XCTAssertEqual(cell.avatarView.frame, layoutAttributes.frame) XCTAssertEqual(cell.messageContainerView.frame, layoutAttributes.messageContainerFrame) XCTAssertEqual(cell.messageContentView.frame, cell.messageContainerView.frame) - XCTAssertEqual(cell.cellTopLabel.frame, layoutAttributes.cellTopLabelFrame) - XCTAssertEqual(cell.cellTopLabel.textInsets, layoutAttributes.cellTopLabelInsets) - XCTAssertEqual(cell.cellBottomLabel.frame, layoutAttributes.cellBottomLabelFrame) - XCTAssertEqual(cell.cellBottomLabel.textInsets, layoutAttributes.cellBottomLabelInsets) + XCTAssertEqual(cell.cellTopLabel.frame, layoutAttributes.topLabelFrame) + XCTAssertEqual(cell.cellBottomLabel.frame, layoutAttributes.bottomLabelFrame) } } From 8b461538adb07532a2511f4ac300985dfe3e6a7f Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 27 Nov 2017 19:24:42 -0600 Subject: [PATCH 40/59] Add CHANGELOG entry for #355. Closes #228, #327 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a3fbde..e8b6133e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa `AvatarView`'s vertical and horizontal position in a `MessageCollectionViewCell`. [#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). +### Changed + +- **Breaking Change** The `cellTopLabel` and `cellBottomLabel` properties of `MessageCollectionViewCell` are no longer +typed as `MessageLabel` and are now regular `UILabel`s. +[#355](https://github.com/MessageKit/MessageKit/pull/355) by [@SD10](https://github.com/sd10). + ### Fixed - **Breaking Change** Fixed all instances of misspelled `inital` property. `Avatar.inital` has changed to `Avatar.initial` From f6970f7d0c5d6c205a15ad469c1752906e638ee2 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 27 Nov 2017 19:33:29 -0600 Subject: [PATCH 41/59] Fix failing tests --- Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift b/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift index 95a599e4..4c32dbbd 100644 --- a/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift +++ b/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift @@ -226,11 +226,11 @@ class TextMessageDisplayDelegateTests: XCTestCase { XCTAssertEqual(textColor, .darkText) } - func testEnableDetectors_returnsUrlAddressPhoneNumberAndDateForDefault() { + func testEnableDetectors_returnsEmptyForDefault() { let detectors = sut.enabledDetectors(for: sut.dataProvider.messages[1], at: IndexPath(item: 0, section: 0), in: sut.messagesCollectionView) - let expectedDetectors: [DetectorType] = [.url, .address, .phoneNumber, .date] + let expectedDetectors: [DetectorType] = [] XCTAssertEqual(detectors, expectedDetectors) } From 819eb6547dcdbf75b6c384ed93c14cc22893e27c Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 27 Nov 2017 19:51:53 -0600 Subject: [PATCH 42/59] Add CHANGELOG entry for #356 --- CHANGELOG.md | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ec9587a..156f05d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,15 +15,32 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa `AvatarView`'s vertical and horizontal position in a `MessageCollectionViewCell`. [#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). +### Changed + +- All `DetectorType`s for `MessageLabel` are disabled by default. +[#356](https://github.com/MessageKit/MessageKit/pull/356) by [@SD10](https://github.com/sd10). + ### Fixed - **Breaking Change** Fixed all instances of misspelled `inital` property. `Avatar.inital` has changed to `Avatar.initial` and the initializer has changed from `public init(image: UIImage? = nil, initals: String = "?")` to `public init(image: UIImage? = nil, initials: String = "?")`. [#298](https://github.com/MessageKit/MessageKit/issues/298) by [@sidmclaughlin](https://github.com/sidmclaughlin). +- Fixed `MessageInputBar`'s `translucent` functionality. +[#348](https://github.com/MessageKit/MessageKit/pull/348) by [@zhongwuzw](https://github.com/zhongwuzw). + - Fixes infinite loop when dismissing keyboard on iPhone X. [#350](https://github.com/MessageKit/MessageKit/pull/350) by [@nathantannar4](https://github.com/nathantannar4). +### Removed + +- **Breaking Change** Removed `AvatarAlignment` and `avatarAlignment(for:at:in)` delegate method +in favor of new `AvatarPosition` representing both vertical and horizontal alignments. +[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). + +- **Breaking Change** Removed the `avatarAlwaysLeading` and `avatarAlwaysTrailing` properties of `MessagesCollectionViewFlow$ +[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). + ## [[Prerelease] 0.10.2](https://github.com/MessageKit/MessageKit/releases/tag/0.10.2) ### Fixed @@ -35,19 +52,6 @@ and the initializer has changed from `public init(image: UIImage? = nil, initals origin Y so that the `cellBottomLabel` is always under the `MessageContainerView`. [#326](https://github.com/MessageKit/MessageKit/pull/326) by [@SD10](https://github.com/sd10). -- Fixed `MessageInputBar`'s `translucent` functionality. -[#348](https://github.com/MessageKit/MessageKit/pull/348) by [@zhongwuzw](https://github.com/zhongwuzw). - -### Removed - -- **Breaking Change** Removed `AvatarAlignment` and `avatarAlignment(for:at:in)` delegate method -in favor of new `AvatarPosition` representing both vertical and horizontal alignments. -[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). - -- **Breaking Change** Removed the `avatarAlwaysLeading` and `avatarAlwaysTrailing` properties of `MessagesCollectionViewFlowLayout`. -[#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). - -======= - Fixed pixelation of `AvatarView`'s placeholder text initials. [#343](https://github.com/MessageKit/MessageKit/pull/343) by [@johnnyoin](https://github.com/johnnyoin). @@ -56,7 +60,6 @@ in favor of new `AvatarPosition` representing both vertical and horizontal align - Fixed crash for escaping block in `InputBarItem`’s `setSize(newValue:animated)` method. [#342](https://github.com/MessageKit/MessageKit/pull/342) by [@zhongwuzw](https://github.com/zhongwuzw). ->>>>>>> master ## [[Prerelease] 0.10.1](https://github.com/MessageKit/MessageKit/releases/tag/0.10.1) From b7f5f950de750bf9fb938265369b665c64e5d4e0 Mon Sep 17 00:00:00 2001 From: Wu Zhong Date: Thu, 30 Nov 2017 14:45:04 -0600 Subject: [PATCH 43/59] Fix #357 intrinsicContentSize --- Sources/Views/MessageInputBar.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 70418118..7404da58 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -216,6 +216,10 @@ open class MessageInputBar: UIView { } } + private var inputBarMinusTextViewHeight: CGFloat { + return topStackViewPadding.top + padding.top + textViewPadding.bottom + padding.bottom + topStackView.bounds.height + bottomStackView.bounds.height + } + /// The fixed widthAnchor constant of the leftStackView public private(set) var leftStackViewWidthConstant: CGFloat = 0 { didSet { @@ -421,14 +425,16 @@ open class MessageInputBar: UIView { open func calculateIntrinsicContentSize() -> CGSize { let maxTextViewSize = CGSize(width: inputTextView.bounds.width, height: .greatestFiniteMagnitude) - var heightToFit = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() - if heightToFit >= maxHeight { + let inputTextViewHeight = inputTextView.sizeThatFits(maxTextViewSize).height.rounded() + var heightToFit: CGFloat = inputBarMinusTextViewHeight + + if inputTextViewHeight >= maxHeight { if !isOverMaxTextViewHeight { textViewHeightAnchor?.isActive = true inputTextView.isScrollEnabled = true isOverMaxTextViewHeight = true } - heightToFit = maxHeight + heightToFit += maxHeight } else { if isOverMaxTextViewHeight { textViewHeightAnchor?.isActive = false @@ -436,6 +442,7 @@ open class MessageInputBar: UIView { isOverMaxTextViewHeight = false inputTextView.invalidateIntrinsicContentSize() } + heightToFit += inputTextViewHeight } return CGSize(width: bounds.width, height: heightToFit) } From 81de2180c9218137c31c85c81ece610ea866c867 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Thu, 30 Nov 2017 14:48:27 -0600 Subject: [PATCH 44/59] Remove additionalTopContentOffset from 0.10.0 --- Sources/Supporting/MessageKit+Availability.swift | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index cba8e441..d26ff3df 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -48,22 +48,6 @@ extension MessagesLayoutDelegate { } -// MARK: - MessagesViewController - -public extension MessagesViewController { - - @available(*, deprecated: 0.10.0, message: "Removed in MessageKit 0.10.0. Please use the messagesCollectionView.contentInsets.top property.") - public var additionalTopContentInset: CGFloat { - get { - return messagesCollectionView.contentInset.top - } - set { - messagesCollectionView.contentInset.top = newValue - } - } - -} - extension MessagesCollectionViewFlowLayout { /// A Boolean value that determines if the `AvatarView` is always on the leading From 3759610cfaed4ff1269cb85032b186c7563e57cb Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Thu, 30 Nov 2017 15:15:49 -0600 Subject: [PATCH 45/59] Remove different delegates for different message types --- Example/Podfile.lock | 4 +- .../Sources/ConversationViewController.swift | 61 ++++---- MessageKit.xcodeproj/project.pbxproj | 12 -- .../MessagesCollectionViewFlowLayout.swift | 14 +- .../LocationMessageDisplayDelegate.swift | 80 ----------- .../LocationMessageLayoutDelegate.swift | 59 -------- .../MediaMessageLayoutDelegate.swift | 72 ---------- .../Protocols/MessageInputBarDelegate.swift | 1 - .../Protocols/MessagesDisplayDelegate.swift | 134 ++++++++++++------ .../Protocols/MessagesLayoutDelegate.swift | 121 +++++++++++++--- .../Supporting/MessageKit+Availability.swift | 14 ++ Sources/Views/Cells/LocationMessageCell.swift | 2 +- Sources/Views/Cells/TextMessageCell.swift | 3 +- 13 files changed, 240 insertions(+), 337 deletions(-) delete mode 100644 Sources/Protocols/LocationMessageDisplayDelegate.swift delete mode 100644 Sources/Protocols/LocationMessageLayoutDelegate.swift delete mode 100644 Sources/Protocols/MediaMessageLayoutDelegate.swift diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 150ea00f..cbce23e4 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - MessageKit (0.10.1) + - MessageKit (0.10.2) DEPENDENCIES: - MessageKit (from `../MessageKit.podspec`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: ../MessageKit.podspec SPEC CHECKSUMS: - MessageKit: b0cd905fa1872952f25ff5ea8e67d7135fe7e176 + MessageKit: a1e8d0f1d7785891e4b37f6252898f23cd8f8b6a PODFILE CHECKSUM: 9ac65b8dedf0e1b63fea245b089b6645c4e66309 diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index c6a58c8c..894a1e31 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -234,7 +234,9 @@ extension ConversationViewController: MessagesDataSource { // MARK: - MessagesDisplayDelegate -extension ConversationViewController: MessagesDisplayDelegate, TextMessageDisplayDelegate { +extension ConversationViewController: MessagesDisplayDelegate { + + // MARK: - All Messages func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { return isFromCurrentSender(message: message) ? UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1) : UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1) @@ -251,6 +253,28 @@ extension ConversationViewController: MessagesDisplayDelegate, TextMessageDispla // return .custom(configurationClosure) } + // MARK: - Location Messages + + func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? { + let annotationView = MKAnnotationView(annotation: nil, reuseIdentifier: nil) + let pinImage = #imageLiteral(resourceName: "pin") + annotationView.image = pinImage + annotationView.centerOffset = CGPoint(x: 0, y: -pinImage.size.height / 2) + return annotationView + } + + func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? { + return { view in + view.layer.transform = CATransform3DMakeScale(0, 0, 0) + view.alpha = 0.0 + UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0, options: [], animations: { + view.layer.transform = CATransform3DIdentity + view.alpha = 1.0 + }, completion: nil) + } + } + + } // MARK: - MessagesLayoutDelegate @@ -286,11 +310,7 @@ extension ConversationViewController: MessagesLayoutDelegate { return CGSize(width: messagesCollectionView.bounds.width, height: 10) } -} - -// MARK: - LocationMessageLayoutDelegate - -extension ConversationViewController: LocationMessageLayoutDelegate { + // MARK: - Location Messages func heightForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { return 200 @@ -298,10 +318,6 @@ extension ConversationViewController: LocationMessageLayoutDelegate { } -// MARK: - MediaMessageLayoutDelegate - -extension ConversationViewController: MediaMessageLayoutDelegate {} - // MARK: - MessageCellDelegate extension ConversationViewController: MessageCellDelegate { @@ -346,31 +362,6 @@ extension ConversationViewController: MessageLabelDelegate { } -// MARK: - LocationMessageDisplayDelegate - -extension ConversationViewController: LocationMessageDisplayDelegate { - - func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? { - let annotationView = MKAnnotationView(annotation: nil, reuseIdentifier: nil) - let pinImage = #imageLiteral(resourceName: "pin") - annotationView.image = pinImage - annotationView.centerOffset = CGPoint(x: 0, y: -pinImage.size.height / 2) - return annotationView - } - - func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? { - return { view in - view.layer.transform = CATransform3DMakeScale(0, 0, 0) - view.alpha = 0.0 - UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0, options: [], animations: { - view.layer.transform = CATransform3DIdentity - view.alpha = 1.0 - }, completion: nil) - } - } - -} - // MARK: - MessageInputBarDelegate extension ConversationViewController: MessageInputBarDelegate { diff --git a/MessageKit.xcodeproj/project.pbxproj b/MessageKit.xcodeproj/project.pbxproj index 9eac4004..f97fd29a 100644 --- a/MessageKit.xcodeproj/project.pbxproj +++ b/MessageKit.xcodeproj/project.pbxproj @@ -55,16 +55,13 @@ B7A03F4C1F86694F006AEF79 /* PlayButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F441F86694F006AEF79 /* PlayButtonView.swift */; }; B7A03F4D1F86694F006AEF79 /* MessagesCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F451F86694F006AEF79 /* MessagesCollectionView.swift */; }; B7A03F4F1F86697C006AEF79 /* MessagesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F4E1F86697C006AEF79 /* MessagesViewController.swift */; }; - B7A03F5A1F8669CA006AEF79 /* MediaMessageLayoutDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F501F8669C9006AEF79 /* MediaMessageLayoutDelegate.swift */; }; B7A03F5B1F8669CA006AEF79 /* MessageType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F511F8669C9006AEF79 /* MessageType.swift */; }; B7A03F5C1F8669CA006AEF79 /* MessageCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F521F8669C9006AEF79 /* MessageCellDelegate.swift */; }; - B7A03F5D1F8669CA006AEF79 /* LocationMessageDisplayDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F531F8669C9006AEF79 /* LocationMessageDisplayDelegate.swift */; }; B7A03F5E1F8669CA006AEF79 /* MessagesLayoutDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F541F8669C9006AEF79 /* MessagesLayoutDelegate.swift */; }; B7A03F5F1F8669CA006AEF79 /* MessageLabelDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F551F8669C9006AEF79 /* MessageLabelDelegate.swift */; }; B7A03F601F8669CA006AEF79 /* MessagesDisplayDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F561F8669C9006AEF79 /* MessagesDisplayDelegate.swift */; }; B7A03F611F8669CA006AEF79 /* MessagesDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F571F8669CA006AEF79 /* MessagesDataSource.swift */; }; B7A03F621F8669CA006AEF79 /* MessageInputBarDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F581F8669CA006AEF79 /* MessageInputBarDelegate.swift */; }; - B7A03F631F8669CA006AEF79 /* LocationMessageLayoutDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F591F8669CA006AEF79 /* LocationMessageLayoutDelegate.swift */; }; B7A03F6A1F8669EB006AEF79 /* UICollectionView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F641F8669EB006AEF79 /* UICollectionView+Extensions.swift */; }; B7A03F6B1F8669EB006AEF79 /* UIColor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F651F8669EB006AEF79 /* UIColor+Extensions.swift */; }; B7A03F6C1F8669EB006AEF79 /* UIView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7A03F661F8669EB006AEF79 /* UIView+Extensions.swift */; }; @@ -137,16 +134,13 @@ B7A03F441F86694F006AEF79 /* PlayButtonView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayButtonView.swift; sourceTree = ""; }; B7A03F451F86694F006AEF79 /* MessagesCollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesCollectionView.swift; sourceTree = ""; }; B7A03F4E1F86697C006AEF79 /* MessagesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesViewController.swift; sourceTree = ""; }; - B7A03F501F8669C9006AEF79 /* MediaMessageLayoutDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MediaMessageLayoutDelegate.swift; sourceTree = ""; }; B7A03F511F8669C9006AEF79 /* MessageType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageType.swift; sourceTree = ""; }; B7A03F521F8669C9006AEF79 /* MessageCellDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageCellDelegate.swift; sourceTree = ""; }; - B7A03F531F8669C9006AEF79 /* LocationMessageDisplayDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationMessageDisplayDelegate.swift; sourceTree = ""; }; B7A03F541F8669C9006AEF79 /* MessagesLayoutDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesLayoutDelegate.swift; sourceTree = ""; }; B7A03F551F8669C9006AEF79 /* MessageLabelDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageLabelDelegate.swift; sourceTree = ""; }; B7A03F561F8669C9006AEF79 /* MessagesDisplayDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesDisplayDelegate.swift; sourceTree = ""; }; B7A03F571F8669CA006AEF79 /* MessagesDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesDataSource.swift; sourceTree = ""; }; B7A03F581F8669CA006AEF79 /* MessageInputBarDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageInputBarDelegate.swift; sourceTree = ""; }; - B7A03F591F8669CA006AEF79 /* LocationMessageLayoutDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationMessageLayoutDelegate.swift; sourceTree = ""; }; B7A03F641F8669EB006AEF79 /* UICollectionView+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UICollectionView+Extensions.swift"; sourceTree = ""; }; B7A03F651F8669EB006AEF79 /* UIColor+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+Extensions.swift"; sourceTree = ""; }; B7A03F661F8669EB006AEF79 /* UIView+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Extensions.swift"; sourceTree = ""; }; @@ -366,9 +360,6 @@ B096439B1F295D82004D0129 /* Protocols */ = { isa = PBXGroup; children = ( - B7A03F531F8669C9006AEF79 /* LocationMessageDisplayDelegate.swift */, - B7A03F591F8669CA006AEF79 /* LocationMessageLayoutDelegate.swift */, - B7A03F501F8669C9006AEF79 /* MediaMessageLayoutDelegate.swift */, B7A03F521F8669C9006AEF79 /* MessageCellDelegate.swift */, B7A03F581F8669CA006AEF79 /* MessageInputBarDelegate.swift */, B7A03F551F8669C9006AEF79 /* MessageLabelDelegate.swift */, @@ -546,10 +537,8 @@ B006FA021F99DE2100509C46 /* MessageIntermediateLayoutAttributes.swift in Sources */, B7A03F4B1F86694F006AEF79 /* MessageContainerView.swift in Sources */, B7A03F281F866895006AEF79 /* LocationMessageSnapshotOptions.swift in Sources */, - B7A03F5D1F8669CA006AEF79 /* LocationMessageDisplayDelegate.swift in Sources */, B7A03F481F86694F006AEF79 /* InputTextView.swift in Sources */, B7A03F6C1F8669EB006AEF79 /* UIView+Extensions.swift in Sources */, - B7A03F631F8669CA006AEF79 /* LocationMessageLayoutDelegate.swift in Sources */, B7A03F3A1F866946006AEF79 /* TextMessageCell.swift in Sources */, B7A03F191F86682C006AEF79 /* MessagesCollectionViewLayoutAttributes.swift in Sources */, B7A03F461F86694F006AEF79 /* AvatarView.swift in Sources */, @@ -569,7 +558,6 @@ B7A03F471F86694F006AEF79 /* MessageLabel.swift in Sources */, B7A03F5F1F8669CA006AEF79 /* MessageLabelDelegate.swift in Sources */, B7A03F4C1F86694F006AEF79 /* PlayButtonView.swift in Sources */, - B7A03F5A1F8669CA006AEF79 /* MediaMessageLayoutDelegate.swift in Sources */, B7A03F291F866895006AEF79 /* Sender.swift in Sources */, B7A03F4F1F86697C006AEF79 /* MessagesViewController.swift in Sources */, B7A03F251F866895006AEF79 /* NSConstraintLayoutSet.swift in Sources */, diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 07988470..f8a9371c 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -429,18 +429,12 @@ private extension MessagesCollectionViewFlowLayout { messageContainerSize.width += attributes.messageLabelHorizontalInsets messageContainerSize.height += attributes.messageLabelVerticalInsets case .photo, .video: - guard let layoutDelegate = messagesLayoutDelegate as? MediaMessageLayoutDelegate else { - fatalError("Layout object attempting to size media message type and MediaMessageLayoutDelegate is not set.") - } - let width = layoutDelegate.widthForMedia(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) - let height = layoutDelegate.heightForMedia(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) + let width = messagesLayoutDelegate.widthForMedia(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) + let height = messagesLayoutDelegate.heightForMedia(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) messageContainerSize = CGSize(width: width, height: height) case .location: - guard let layoutDelegate = messagesLayoutDelegate as? LocationMessageLayoutDelegate else { - fatalError("Layout object attempting to size location message type and LocationMessageLayoutDelegate is not set.") - } - let width = layoutDelegate.widthForLocation(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) - let height = layoutDelegate.heightForLocation(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) + let width = messagesLayoutDelegate.widthForLocation(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) + let height = messagesLayoutDelegate.heightForLocation(message: message, at: indexPath, with: maxWidth, in: messagesCollectionView) messageContainerSize = CGSize(width: width, height: height) } diff --git a/Sources/Protocols/LocationMessageDisplayDelegate.swift b/Sources/Protocols/LocationMessageDisplayDelegate.swift deleted file mode 100644 index 54cdb6a9..00000000 --- a/Sources/Protocols/LocationMessageDisplayDelegate.swift +++ /dev/null @@ -1,80 +0,0 @@ -/* - MIT License - - Copyright (c) 2017 MessageKit - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - */ - -import Foundation -import MapKit - -/// Conform to this protocol to customize location messages's style -public protocol LocationMessageDisplayDelegate: MessagesDisplayDelegate { - - /// Ask the delegate for a LocationMessageSnapshotOptions instance to customize the MapView on the given message - /// - /// - Parameters: - /// - message: The location message to be customized - /// - indexPath: Message's index path - /// - messagesCollectionView: The collection view requesting the information - /// - Returns: Your LocationMessageSnapshotOptions instance with the options to customize map style - func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions - - /// Ask the delegate for a custom MKAnnotationView to show on the given message. - /// You can return nil if you don't want to show any annotation. - /// - /// default: MKPinAnnotationView - /// - /// - Parameters: - /// - message: The location message with the annotation to customize - /// - indexPath: Message's index path - /// - messageCollectionView: The collection view requesting the information - /// - Returns: Your customized MKAnnotationView or nil to not show any. - func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? - - /// Ask the delegate for a custom animation block to run when whe map screenshot is ready to be displaied in the given location message - /// The animation block is called with the image view to be animated. You can animate it with CoreAnimation, UIView.animate or any library you prefer. - /// - /// default: nil - /// - /// - Parameters: - /// - message: The location message with the map to animate - /// - indexPath: Message's index path - /// - messagesCollectionView: The collection view requesting the information - /// - Returns: Your customized animation block. - func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? - -} - -public extension LocationMessageDisplayDelegate { - - func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions { - return LocationMessageSnapshotOptions() - } - - func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? { - return MKPinAnnotationView(annotation: nil, reuseIdentifier: nil) - } - - func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? { - return nil - } - -} diff --git a/Sources/Protocols/LocationMessageLayoutDelegate.swift b/Sources/Protocols/LocationMessageLayoutDelegate.swift deleted file mode 100644 index 8bd54b70..00000000 --- a/Sources/Protocols/LocationMessageLayoutDelegate.swift +++ /dev/null @@ -1,59 +0,0 @@ -/* - MIT License - - Copyright (c) 2017 MessageKit - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - */ - -import Foundation - -/// A protocol used by the `MessagesCollectionViewFlowLayout` object to determine -/// the size and layout of a `LocationMessageCell` and its contents. -public protocol LocationMessageLayoutDelegate: MessagesLayoutDelegate { - - /// Specifies the width for a `MessageContainerView`. - /// - /// - Parameters: - /// - message: The `MessageType` that will be displayed by this cell. - /// - indexPath: The `IndexPath` of the cell. - /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - /// - /// The default value returned by this method is the `maxWidth`. - func widthForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat - - /// Specifies the height for a `MessageContainerView`. - /// - /// - Parameters: - /// - message: The `MessageType` that will be displayed by this cell. - /// - indexPath: The `IndexPath` of the cell. - /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - func heightForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat - -} - -public extension LocationMessageLayoutDelegate { - - func widthForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { - return maxWidth - } - -} diff --git a/Sources/Protocols/MediaMessageLayoutDelegate.swift b/Sources/Protocols/MediaMessageLayoutDelegate.swift deleted file mode 100644 index 4ec18af6..00000000 --- a/Sources/Protocols/MediaMessageLayoutDelegate.swift +++ /dev/null @@ -1,72 +0,0 @@ -/* - MIT License - - Copyright (c) 2017 MessageKit - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - */ - -import AVFoundation - -/// A protocol used by the `MessagesCollectionViewFlowLayout` object to determine -/// the size and layout of a `MediaMessageCell`s and its contents. -public protocol MediaMessageLayoutDelegate: MessagesLayoutDelegate { - - /// Specifies the width for a `MessageContainerView`. - /// - /// - Parameters: - /// - message: The `MessageType` that will be displayed by this cell. - /// - indexPath: The `IndexPath` of the cell. - /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - /// - /// The default value returned by this method is the `maxWidth`. - func widthForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat - - /// Specifies the height for a `MessageContainerView`. - /// - /// - Parameters: - /// - message: The `MessageType` that will be displayed by this cell. - /// - indexPath: The `IndexPath` of the cell. - /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - /// - /// The default value returned by this method uses `AVMakeRect(aspectRatio:insideRect:)` with a bounding - /// rect using the `maxWidth` and `.greatestFiniteMagnitude` for the height. - func heightForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat - -} - -public extension MediaMessageLayoutDelegate { - - func widthForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { - return maxWidth - } - - func heightForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { - switch message.data { - case .photo(let image), .video(_, let image): - let boundingRect = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: .greatestFiniteMagnitude)) - return AVMakeRect(aspectRatio: image.size, insideRect: boundingRect).height - default: - return 0 - } - } - -} diff --git a/Sources/Protocols/MessageInputBarDelegate.swift b/Sources/Protocols/MessageInputBarDelegate.swift index 6c1e57a0..013846f6 100644 --- a/Sources/Protocols/MessageInputBarDelegate.swift +++ b/Sources/Protocols/MessageInputBarDelegate.swift @@ -23,7 +23,6 @@ */ import Foundation -import UIKit /// MessageInputBarDelegate is a protocol that can recieve notifications from the MessageInputBar public protocol MessageInputBarDelegate: AnyObject { diff --git a/Sources/Protocols/MessagesDisplayDelegate.swift b/Sources/Protocols/MessagesDisplayDelegate.swift index ed314071..e53ecb61 100644 --- a/Sources/Protocols/MessagesDisplayDelegate.swift +++ b/Sources/Protocols/MessagesDisplayDelegate.swift @@ -23,52 +23,13 @@ */ import Foundation - -/// A protocol used by the `MessagesViewController` to customize the appearance of a `TextMessageCell`. -public protocol TextMessageDisplayDelegate: AnyObject { - - /// Specifies the color of the text for a `TextMessageCell`. - /// - /// - Parameters: - /// - message: A `MessageType` with a `MessageData` case of `.text` or `.attributedText` to which the color will apply. - /// - indexPath: The `IndexPath` of the cell. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - /// - /// The default value returned by this method is determined by the messages `Sender`: - /// - /// Current Sender: UIColor.white - /// - /// All other Senders: UIColor.darkText - func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor - - /// Specifies the `DetectorType`s to check for the `MessageType`'s text against. - /// - /// - Parameters: - /// - message: A `MessageType` with a `MessageData` case of `.text` or `.attributedText` to which the detectors will apply. - /// - indexPath: The `IndexPath` of the cell. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - /// - /// The default value returned by this method is all available detector types. - func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] - -} - -public extension TextMessageDisplayDelegate { - - func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { - guard let dataSource = messagesCollectionView.messagesDataSource else { return .darkText } - return dataSource.isFromCurrentSender(message: message) ? .white : .darkText - } - - func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] { - return [] - } - -} +import MapKit /// A protocol used by the `MessagesViewController` to customize the appearance of a `MessagesCollectionViewCell`. public protocol MessagesDisplayDelegate: AnyObject { + // MARK: - All Messages + /// Specifies the `MessageStyle` to be used for a `MessageContainerView`. /// /// - Parameters: @@ -123,10 +84,74 @@ public protocol MessagesDisplayDelegate: AnyObject { /// The default value returned by this method is a `MessageFooterView`. func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView + // MARK: - Text Messages + + /// Specifies the color of the text for a `TextMessageCell`. + /// + /// - Parameters: + /// - message: A `MessageType` with a `MessageData` case of `.text` or `.attributedText` to which the color will apply. + /// - indexPath: The `IndexPath` of the cell. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method is determined by the messages `Sender`: + /// + /// Current Sender: UIColor.white + /// + /// All other Senders: UIColor.darkText + func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor + + /// Specifies the `DetectorType`s to check for the `MessageType`'s text against. + /// + /// - Parameters: + /// - message: A `MessageType` with a `MessageData` case of `.text` or `.attributedText` to which the detectors will apply. + /// - indexPath: The `IndexPath` of the cell. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method is all available detector types. + func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] + + // MARK: - Location Messages + + /// Ask the delegate for a LocationMessageSnapshotOptions instance to customize the MapView on the given message + /// + /// - Parameters: + /// - message: The location message to be customized + /// - indexPath: Message's index path + /// - messagesCollectionView: The collection view requesting the information + /// - Returns: Your LocationMessageSnapshotOptions instance with the options to customize map style + func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions + + /// Ask the delegate for a custom MKAnnotationView to show on the given message. + /// You can return nil if you don't want to show any annotation. + /// + /// default: MKPinAnnotationView + /// + /// - Parameters: + /// - message: The location message with the annotation to customize + /// - indexPath: Message's index path + /// - messageCollectionView: The collection view requesting the information + /// - Returns: Your customized MKAnnotationView or nil to not show any. + func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? + + /// Ask the delegate for a custom animation block to run when whe map screenshot is ready to be displaied in the given location message + /// The animation block is called with the image view to be animated. You can animate it with CoreAnimation, UIView.animate or any library you prefer. + /// + /// default: nil + /// + /// - Parameters: + /// - message: The location message with the map to animate + /// - indexPath: Message's index path + /// - messagesCollectionView: The collection view requesting the information + /// - Returns: Your customized animation block. + func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? + + } public extension MessagesDisplayDelegate { + // MARK: - All Messages Defaults + func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle { return .bubble } @@ -162,4 +187,29 @@ public extension MessagesDisplayDelegate { return messagesCollectionView.dequeueReusableFooterView(MessageFooterView.self, for: indexPath) } + // MARK: - Text Messages Defaults + + func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { + guard let dataSource = messagesCollectionView.messagesDataSource else { return .darkText } + return dataSource.isFromCurrentSender(message: message) ? .white : .darkText + } + + func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] { + return [] + } + + // MARK: - Location Messages Defaults + + func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions { + return LocationMessageSnapshotOptions() + } + + func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? { + return MKPinAnnotationView(annotation: nil, reuseIdentifier: nil) + } + + func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? { + return nil + } + } diff --git a/Sources/Protocols/MessagesLayoutDelegate.swift b/Sources/Protocols/MessagesLayoutDelegate.swift index 5c9ed957..ad3922a3 100644 --- a/Sources/Protocols/MessagesLayoutDelegate.swift +++ b/Sources/Protocols/MessagesLayoutDelegate.swift @@ -23,24 +23,13 @@ */ import Foundation +import AVFoundation /// A protocol used by the `MessagesCollectionViewFlowLayout` object to determine /// the size and layout of a `MessageCollectionViewCell` and its contents. public protocol MessagesLayoutDelegate: AnyObject { - /// Specifies the insets for the text rect of the `MessageLabel` in a `TextMessageCell`. - /// - /// - Parameters: - /// - message: A `MessageType` with a `MessageData` case of `.text` or `.attributedText` to which these insets will apply. - /// - indexPath: The `IndexPath` of the cell. - /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. - /// - /// The default value returned by this method is determined by the messages `Sender`: - /// - /// Current Sender: `UIEdgeInsets(top: 7, left: 14, bottom: 7, right: 18)` - /// - /// All other Senders: `UIEdgeInsets(top: 7, left: 18, bottom: 7, right: 14)` - func messageLabelInset(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets + // MARK: - All Messages /// Specifies the padding around the `MessageContainerView` in a `MessageCollectionViewCell`. /// @@ -125,18 +114,75 @@ public protocol MessagesLayoutDelegate: AnyObject { /// The default value returned by this method is a size of `GGSize.zero`. func footerViewSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize + // MARK: - Text Messages + + /// Specifies the insets for the text rect of the `MessageLabel` in a `TextMessageCell`. + /// + /// - Parameters: + /// - message: A `MessageType` with a `MessageData` case of `.text` or `.attributedText` to which these insets will apply. + /// - indexPath: The `IndexPath` of the cell. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method is determined by the messages `Sender`: + /// + /// Current Sender: `UIEdgeInsets(top: 7, left: 14, bottom: 7, right: 18)` + /// + /// All other Senders: `UIEdgeInsets(top: 7, left: 18, bottom: 7, right: 14)` + func messageLabelInset(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets + + // MARK: - Media Messages + + /// Specifies the width for a `MessageContainerView`. + /// + /// - Parameters: + /// - message: The `MessageType` that will be displayed by this cell. + /// - indexPath: The `IndexPath` of the cell. + /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method is the `maxWidth`. + func widthForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat + + /// Specifies the height for a `MessageContainerView`. + /// + /// - Parameters: + /// - message: The `MessageType` that will be displayed by this cell. + /// - indexPath: The `IndexPath` of the cell. + /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method uses `AVMakeRect(aspectRatio:insideRect:)` with a bounding + /// rect using the `maxWidth` and `.greatestFiniteMagnitude` for the height. + func heightForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat + + + // MARK: - Location Messages + + /// Specifies the width for a `MessageContainerView`. + /// + /// - Parameters: + /// - message: The `MessageType` that will be displayed by this cell. + /// - indexPath: The `IndexPath` of the cell. + /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + /// + /// The default value returned by this method is the `maxWidth`. + func widthForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat + + /// Specifies the height for a `MessageContainerView`. + /// + /// - Parameters: + /// - message: The `MessageType` that will be displayed by this cell. + /// - indexPath: The `IndexPath` of the cell. + /// - maxWidth: The max available width for the `MessageContainerView` respecting the cell's other content. + /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. + func heightForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat + } public extension MessagesLayoutDelegate { - func messageLabelInset(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets { - guard let dataSource = messagesCollectionView.messagesDataSource else { return .zero } - if dataSource.isFromCurrentSender(message: message) { - return UIEdgeInsets(top: 7, left: 14, bottom: 7, right: 18) - } else { - return UIEdgeInsets(top: 7, left: 18, bottom: 7, right: 14) - } - } + // MARK: - All Messages Defaults func messagePadding(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets { guard let dataSource = messagesCollectionView.messagesDataSource else { return .zero } @@ -175,6 +221,39 @@ public extension MessagesLayoutDelegate { return .zero } + // MARK: - Text Messages Defaults + + func messageLabelInset(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets { + guard let dataSource = messagesCollectionView.messagesDataSource else { return .zero } + if dataSource.isFromCurrentSender(message: message) { + return UIEdgeInsets(top: 7, left: 14, bottom: 7, right: 18) + } else { + return UIEdgeInsets(top: 7, left: 18, bottom: 7, right: 14) + } + } + + // MARK: - Media Messages Defaults + + func widthForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { + return maxWidth + } + + func heightForMedia(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { + switch message.data { + case .photo(let image), .video(_, let image): + let boundingRect = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: .greatestFiniteMagnitude)) + return AVMakeRect(aspectRatio: image.size, insideRect: boundingRect).height + default: + return 0 + } + } + + // MARK: - Location Messages Defaults + + func widthForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { + return maxWidth + } + } diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index d26ff3df..c1fba830 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -24,6 +24,20 @@ import Foundation +// MARK: - Deprecated Protocols + +@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesDisplayDelegate") +typealias LocationMessageDisplayDelegate = MessagesDisplayDelegate + +@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesDisplayDelegate") +typealias TextMessageDisplayDelegate = MessagesDisplayDelegate + +@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesLayoutDelegate") +typealias LocationMessageLayoutDelegate = MessagesLayoutDelegate + +@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesLayoutDelegate") +typealias MediaMessageLayoutDelegate = MessagesLayoutDelegate + // MARK: - AvatarAlignment @available(*, deprecated: 0.11.0, message: "Removed in MessageKit 0.11.0. Please use AvatarPosition instead.") diff --git a/Sources/Views/Cells/LocationMessageCell.swift b/Sources/Views/Cells/LocationMessageCell.swift index 401fbe56..ef1b52e4 100644 --- a/Sources/Views/Cells/LocationMessageCell.swift +++ b/Sources/Views/Cells/LocationMessageCell.swift @@ -37,7 +37,7 @@ open class LocationMessageCell: MessageCollectionViewCell { switch message.data { case .location(let location): - guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate as? LocationMessageDisplayDelegate else { return } + guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else { return } let options = displayDelegate.snapshotOptionsForLocation(message: message, at: indexPath, in: messagesCollectionView) let annotationView = displayDelegate.annotationViewForLocation(message: message, at: indexPath, in: messagesCollectionView) let animationBlock = displayDelegate.animationBlockForLocation(message: message, at: indexPath, in: messagesCollectionView) diff --git a/Sources/Views/Cells/TextMessageCell.swift b/Sources/Views/Cells/TextMessageCell.swift index 15a796c5..d1d6d3ec 100644 --- a/Sources/Views/Cells/TextMessageCell.swift +++ b/Sources/Views/Cells/TextMessageCell.swift @@ -64,7 +64,7 @@ open class TextMessageCell: MessageCollectionViewCell { open override func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) { super.configure(with: message, at: indexPath, and: messagesCollectionView) - if let displayDelegate = messagesCollectionView.messagesDisplayDelegate as? TextMessageDisplayDelegate { + if let displayDelegate = messagesCollectionView.messagesDisplayDelegate { let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView) let detectors = displayDelegate.enabledDetectors(for: message, at: indexPath, in: messagesCollectionView) @@ -72,7 +72,6 @@ open class TextMessageCell: MessageCollectionViewCell { messageContentView.textColor = textColor messageContentView.enabledDetectors = detectors } - } switch message.data { From 299de610cd0a808e23982042f643282cae6dbfc1 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Thu, 30 Nov 2017 15:22:47 -0600 Subject: [PATCH 46/59] Add CHANGELOG entry for #363 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f524be79..0b167020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,11 @@ in favor of new `AvatarPosition` representing both vertical and horizontal align - **Breaking Change** Removed the `avatarAlwaysLeading` and `avatarAlwaysTrailing` properties of `MessagesCollectionViewFlow$ [#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). +- **Breaking Change** Removed `LocationMessageDisplayDelegate` & `TextMessageDisplayDelegate` and moved their methods +into the `MessagesDisplayDelegate` protocol. Removed `LocationMessageLayoutDelegate` & `MediaMessageLayoutDelegate` and +moved their methods into the `MessagesLayoutDelegate` protocol. +[#363](https://github.com/MessageKit/MessageKit/pull/363) by [@SD10](https://github.com/sd10). + ## [[Prerelease] 0.10.2](https://github.com/MessageKit/MessageKit/releases/tag/0.10.2) ### Fixed From 9bc218514b7b18b227e739ce9124446dc1c1a035 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Thu, 30 Nov 2017 15:45:47 -0600 Subject: [PATCH 47/59] Add shouldCacheLayoutAttributes method --- .../Layout/MessagesCollectionViewFlowLayout.swift | 4 +++- Sources/Protocols/MessagesLayoutDelegate.swift | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index f8a9371c..e71d891e 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -213,8 +213,10 @@ fileprivate extension MessagesCollectionViewFlowLayout { return intermediateAttributes } else { let newAttributes = createMessageIntermediateLayoutAttributes(for: message, at: indexPath) + + let shouldCache = messagesLayoutDelegate.shouldCacheLayoutAttributes(for: message) && intermediateAttributesCache.count < attributesCacheMaxSize - if intermediateAttributesCache.count < attributesCacheMaxSize { + if shouldCache { intermediateAttributesCache[message.messageId] = newAttributes } return newAttributes diff --git a/Sources/Protocols/MessagesLayoutDelegate.swift b/Sources/Protocols/MessagesLayoutDelegate.swift index ad3922a3..39eba865 100644 --- a/Sources/Protocols/MessagesLayoutDelegate.swift +++ b/Sources/Protocols/MessagesLayoutDelegate.swift @@ -178,6 +178,14 @@ public protocol MessagesLayoutDelegate: AnyObject { /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed. func heightForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat + /// Specifies whether the layout attributes for a given `MessageType` + /// should be cached by the `MessagesCollectionViewFlowLayout`. + /// - Parameters: + /// - message: The `MessageType` whose attributes to cache. + /// + /// The default value returned by this method is `false`. + func shouldCacheLayoutAttributes(for message: MessageType) -> Bool + } public extension MessagesLayoutDelegate { @@ -221,6 +229,10 @@ public extension MessagesLayoutDelegate { return .zero } + func shouldCacheLayoutAttributes(for message: MessageType) -> Bool { + return false + } + // MARK: - Text Messages Defaults func messageLabelInset(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets { From 42b0f2806aaf3eef2e26e16cf3e87d24dd1b196e Mon Sep 17 00:00:00 2001 From: Bas Broek Date: Thu, 30 Nov 2017 23:02:21 +0100 Subject: [PATCH 48/59] Improve wording in deprecation messages --- Sources/Supporting/MessageKit+Availability.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Supporting/MessageKit+Availability.swift b/Sources/Supporting/MessageKit+Availability.swift index c1fba830..099e942a 100644 --- a/Sources/Supporting/MessageKit+Availability.swift +++ b/Sources/Supporting/MessageKit+Availability.swift @@ -26,16 +26,16 @@ import Foundation // MARK: - Deprecated Protocols -@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesDisplayDelegate") +@available(*, deprecated: 0.11.0, message: "LocationMessageDisplayDelegate has been deprecated in favor of MessagesDisplayDelegate") typealias LocationMessageDisplayDelegate = MessagesDisplayDelegate -@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesDisplayDelegate") +@available(*, deprecated: 0.11.0, message: "TextMessageDisplayDelegate has been deprecated in favor of MessagesDisplayDelegate") typealias TextMessageDisplayDelegate = MessagesDisplayDelegate -@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesLayoutDelegate") +@available(*, deprecated: 0.11.0, message: "LocationMessageLayoutDelegate has been deprecated in favor of MessagesDisplayDelegate") typealias LocationMessageLayoutDelegate = MessagesLayoutDelegate -@available(*, deprecated: 0.11.0, message: "Now typealias for MessagesLayoutDelegate") +@available(*, deprecated: 0.11.0, message: "MediaMessageLayoutDelegate has been deprecated in favor of MessagesDisplayDelegate") typealias MediaMessageLayoutDelegate = MessagesLayoutDelegate // MARK: - AvatarAlignment From 3862bbd73e452d7b124e80306e09d9439da00e7c Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 1 Dec 2017 12:10:24 +0800 Subject: [PATCH 49/59] Fix MessageInputBar bottomAnchor issue --- Sources/Views/MessageInputBar.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Views/MessageInputBar.swift b/Sources/Views/MessageInputBar.swift index 7404da58..e0c7ccc8 100644 --- a/Sources/Views/MessageInputBar.swift +++ b/Sources/Views/MessageInputBar.swift @@ -553,6 +553,7 @@ open class MessageInputBar: UIView { guard superview != nil else { return } topStackView.layoutIfNeeded() } + invalidateIntrinsicContentSize() } performLayout(animated) { From 248e6d9a06bee375303e4987b8a2d4b062b3f95c Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 1 Dec 2017 23:28:26 +0800 Subject: [PATCH 50/59] Optimize DateFormatter to static variable --- Example/Sources/ConversationViewController.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index 894a1e31..caf88b9e 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -220,14 +220,21 @@ extension ConversationViewController: MessagesDataSource { func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { let name = message.sender.displayName - return NSAttributedString(string: name, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1)]) + return NSAttributedString(string: name, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1)]) } func cellBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { - let formatter = DateFormatter() - formatter.dateStyle = .medium + + struct ConversationDateFormatter { + static let formatter: DateFormatter = { + let formatter = DateFormatter() + formatter.dateStyle = .medium + return formatter + }() + } + let formatter = ConversationDateFormatter.formatter let dateString = formatter.string(from: message.sentDate) - return NSAttributedString(string: dateString, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption2)]) + return NSAttributedString(string: dateString, attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption2)]) } } From 1f13555158a0afdaa4c8829fb070ff193cef55fe Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Fri, 1 Dec 2017 23:00:09 -0600 Subject: [PATCH 51/59] Fix failing unit tests from #363 --- Tests/ControllersTest/MessagesViewControllerTests.swift | 2 +- Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/ControllersTest/MessagesViewControllerTests.swift b/Tests/ControllersTest/MessagesViewControllerTests.swift index 1b27b316..61038193 100644 --- a/Tests/ControllersTest/MessagesViewControllerTests.swift +++ b/Tests/ControllersTest/MessagesViewControllerTests.swift @@ -244,7 +244,7 @@ class MessagesViewControllerTests: XCTestCase { } -private class MockLayoutDelegate: MessagesLayoutDelegate, LocationMessageLayoutDelegate, MediaMessageLayoutDelegate { +private class MockLayoutDelegate: MessagesLayoutDelegate { // MARK: - LocationMessageLayoutDelegate diff --git a/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift b/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift index 4c32dbbd..d1e5ee60 100644 --- a/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift +++ b/Tests/ProtocolsTests/MessagesDisplayDelegateTests.swift @@ -237,7 +237,12 @@ class TextMessageDisplayDelegateTests: XCTestCase { } -private class MockMessagesViewController: MessagesViewController, MessagesDisplayDelegate, TextMessageDisplayDelegate, MessagesLayoutDelegate { +private class MockMessagesViewController: MessagesViewController, MessagesDisplayDelegate, MessagesLayoutDelegate { + + func heightForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat { + return 200 + } + var dataProvider: MockMessagesDataSource! From 00b7fbf223e1261164cd2c25f59f96ec05959413 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 2 Dec 2017 15:47:55 -0600 Subject: [PATCH 52/59] Add CHANGELOG entry for #364 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b167020..c2e8d06f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa `AvatarView`'s vertical and horizontal position in a `MessageCollectionViewCell`. [#322](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). +- Added `shouldCacheLayoutAttributes(for:MessageType)-> Bool` method to `MessagesLayoutDelegate` +to manage whether a `MessageType`'s layout information is cached or not. +[#364](https://github.com/MessageKit/MessageKit/pull/322) by [@SD10](https://github.com/sd10). + ### Changed - **Breaking Change** The `cellTopLabel` and `cellBottomLabel` properties of `MessageCollectionViewCell` are no longer From 598d8cc8538ce2b293c565fd191d2f586aabddfc Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Sat, 2 Dec 2017 16:00:14 -0600 Subject: [PATCH 53/59] Update CHANGELOG and podspec for v0.11.0 --- CHANGELOG.md | 2 ++ MessageKit.podspec | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2e8d06f..72d40f41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ## Upcoming release +## [[Prerelease] 0.11.0](https://github.com/MessageKit/MessageKit/releases/tag/0.11.0) + ### Added - **Breaking Change** Added a top `InputStackView` to `MessageInputBar`. This adds the addition of the `.top` case to `InputStackView.Position`. diff --git a/MessageKit.podspec b/MessageKit.podspec index fb831f63..5e53f84f 100644 --- a/MessageKit.podspec +++ b/MessageKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'MessageKit' - s.version = '0.10.2' + s.version = '0.11.0' s.license = { :type => "MIT", :file => "LICENSE.md" } s.summary = 'An elegant messages UI library for iOS.' From 949796d68d175d2f2c8700ed0cb769935aee2184 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 4 Dec 2017 00:47:35 -0600 Subject: [PATCH 54/59] Fix cellTopLabel and cellBottomLabel sizes --- .../MessagesCollectionViewFlowLayout.swift | 99 ++++++++++--------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index e71d891e..03d5b516 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -472,31 +472,33 @@ private extension MessagesCollectionViewFlowLayout { let avatarHorizontal = attributes.avatarPosition.horizontal let avatarVertical = attributes.avatarPosition.vertical let avatarWidth = attributes.avatarSize.width - - switch (labelHorizontal, avatarHorizontal, avatarVertical) { - - case (.cellLeading, .cellTrailing, .cellBottom), (.cellTrailing, .cellLeading, .cellBottom): - return itemWidth - avatarWidth - attributes.bottomLabelHorizontalPadding - - case (.cellLeading, _, _), (.cellTrailing, _, _): - return itemWidth - attributes.bottomLabelHorizontalPadding - - case (.cellCenter, .cellLeading, .cellBottom), (.cellCenter, .cellTrailing, .cellBottom): - return itemWidth - (avatarWidth * 2) - attributes.bottomLabelHorizontalPadding - - case (.cellCenter, .cellLeading, _), (.cellCenter, .cellTrailing, _): - return itemWidth - attributes.bottomLabelHorizontalPadding - - case (.messageTrailing, .cellLeading, .cellBottom), (.messageLeading, .cellTrailing, .cellBottom): - return attributes.messageContainerSize.width - attributes.bottomLabelHorizontalPadding - - case (.messageLeading, .cellLeading, _), (.messageTrailing, .cellTrailing, _): - return itemWidth - avatarWidth - attributes.bottomLabelHorizontalPadding - - case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): - return attributes.messageContainerSize.width + avatarWidth - attributes.bottomLabelHorizontalPadding - - case (_, .natural, _): + + + switch (labelHorizontal, avatarHorizontal) { + + case (.cellLeading, _), (.cellTrailing, _): + let width = itemWidth - attributes.bottomLabelHorizontalPadding + return avatarVertical != .cellBottom ? width : width - avatarWidth + + case (.cellCenter, _): + let width = itemWidth - attributes.bottomLabelHorizontalPadding + return avatarVertical != .cellBottom ? width : width - (avatarWidth * 2) + + case (.messageTrailing, .cellLeading): + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.left + return avatarVertical != .cellBottom ? width : width + avatarWidth + + case (.messageLeading, .cellTrailing): + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.right + return avatarVertical != .cellBottom ? width : width + avatarWidth + + case (.messageLeading, .cellLeading): + return itemWidth - avatarWidth - attributes.messageContainerPadding.left - attributes.bottomLabelHorizontalPadding + + case (.messageTrailing, .cellTrailing): + return itemWidth - avatarWidth - attributes.messageContainerPadding.right - attributes.bottomLabelHorizontalPadding + + case (_, .natural): fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } @@ -547,30 +549,31 @@ private extension MessagesCollectionViewFlowLayout { let avatarVertical = attributes.avatarPosition.vertical let avatarWidth = attributes.avatarSize.width - switch (labelHorizontal, avatarHorizontal, avatarVertical) { - - case (.cellLeading, .cellTrailing, .cellTop), (.cellTrailing, .cellLeading, .cellTop): - return itemWidth - avatarWidth - attributes.topLabelHorizontalPadding - - case (.cellLeading, _, _), (.cellTrailing, _, _): - return itemWidth - attributes.topLabelHorizontalPadding - - case (.cellCenter, .cellLeading, .cellTop), (.cellCenter, .cellTrailing, .cellTop): - return itemWidth - (avatarWidth * 2) - attributes.topLabelHorizontalPadding - - case (.cellCenter, .cellLeading, _), (.cellCenter, .cellTrailing, _): - return itemWidth - attributes.topLabelHorizontalPadding - - case (.messageTrailing, .cellLeading, .cellTop), (.messageLeading, .cellTrailing, .cellTop): - return attributes.messageContainerSize.width - attributes.topLabelHorizontalPadding - - case (.messageLeading, .cellLeading, _), (.messageTrailing, .cellTrailing, _): - return itemWidth - avatarWidth - attributes.topLabelHorizontalPadding - - case (.messageLeading, .cellTrailing, _), (.messageTrailing, .cellLeading, _): - return attributes.messageContainerSize.width + avatarWidth - attributes.topLabelHorizontalPadding + switch (labelHorizontal, avatarHorizontal) { - case (_, .natural, _): + case (.cellLeading, _), (.cellTrailing, _): + let width = itemWidth - attributes.topLabelHorizontalPadding + return avatarVertical != .cellTop ? width : width - avatarWidth + + case (.cellCenter, _): + let width = itemWidth - attributes.topLabelHorizontalPadding + return avatarVertical != .cellTop ? width : width - (avatarWidth * 2) + + case (.messageTrailing, .cellLeading): + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.left + return avatarVertical != .cellTop ? width : width + avatarWidth + + case (.messageLeading, .cellTrailing): + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.right + return avatarVertical != .cellTop ? width : width + avatarWidth + + case (.messageLeading, .cellLeading): + return itemWidth - avatarWidth - attributes.messageContainerPadding.left - attributes.topLabelHorizontalPadding + + case (.messageTrailing, .cellTrailing): + return itemWidth - avatarWidth - attributes.messageContainerPadding.right - attributes.topLabelHorizontalPadding + + case (_, .natural): fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } From 5fdf3f69ae39bbea0407d55056ffbaf683ba0730 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 4 Dec 2017 00:48:55 -0600 Subject: [PATCH 55/59] Fix origin calculations using wrong coord system --- .../MessageIntermediateLayoutAttributes.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index d3712608..e2383ff1 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -51,7 +51,7 @@ final class MessageIntermediateLayoutAttributes { case .cellLeading: break case .cellTrailing: - origin.x = cellFrame.maxX - avatarSize.width + origin.x = cellFrame.width - avatarSize.width case .natural: fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } @@ -60,7 +60,7 @@ final class MessageIntermediateLayoutAttributes { case .cellTop: break case .cellBottom: - origin.y = cellFrame.maxY - avatarSize.height + origin.y = cellFrame.height - avatarSize.height case .messageTop: origin.y = messageContainerFrame.maxY case .messageBottom: @@ -90,7 +90,7 @@ final class MessageIntermediateLayoutAttributes { case .cellLeading: origin.x = avatarSize.width + messageContainerPadding.left case .cellTrailing: - origin.x = cellFrame.maxX - avatarSize.width - messageContainerSize.width - messageContainerPadding.right + origin.x = cellFrame.width - avatarSize.width - messageContainerSize.width - messageContainerPadding.right case .natural: fatalError("AvatarPosition Horizontal.natural needs to be resolved.") } @@ -116,9 +116,9 @@ final class MessageIntermediateLayoutAttributes { case .cellLeading: origin.x = topLabelPadding.left case .cellCenter: - origin.x = cellFrame.midX + topLabelPadding.left - topLabelPadding.right + origin.x = (cellFrame.width/2) + topLabelPadding.left - topLabelPadding.right case .cellTrailing: - origin.x = cellFrame.maxX - topLabelSize.width - topLabelPadding.right + origin.x = cellFrame.width - topLabelSize.width - topLabelPadding.right case .messageLeading: origin.x = messageContainerFrame.minX + topLabelPadding.left case .messageTrailing: @@ -146,9 +146,9 @@ final class MessageIntermediateLayoutAttributes { case .cellLeading: origin.x = bottomLabelPadding.left case .cellCenter: - origin.x = cellFrame.midX + bottomLabelPadding.left - bottomLabelPadding.right + origin.x = (cellFrame.width/2) + bottomLabelPadding.left - bottomLabelPadding.right case .cellTrailing: - origin.x = cellFrame.maxX - bottomLabelSize.width - bottomLabelPadding.right + origin.x = cellFrame.width - bottomLabelSize.width - bottomLabelPadding.right case .messageLeading: origin.x = messageContainerFrame.minX + bottomLabelPadding.left case .messageTrailing: From e150de6b2b51d14f3b9dbe273efdf1d9e15c9fab Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 4 Dec 2017 15:41:19 -0600 Subject: [PATCH 56/59] Add CHANGELOG entry for 949796d --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d40f41..94d7af9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,9 @@ and the initializer has changed from `public init(image: UIImage? = nil, initals - Fixes infinite loop when dismissing keyboard on iPhone X. [#350](https://github.com/MessageKit/MessageKit/pull/350) by [@nathantannar4](https://github.com/nathantannar4). +- Fixed incorrect sizing of `cellTopLabel` and `cellBottomLabel`. +[#371](https://github.com/MessageKit/MessageKit/pull/371/commits/949796d68d175d2f2c8700ed0cb769935aee2184) by [@SD10](https://github.com/sd10). + ### Removed - **Breaking Change** Removed `AvatarAlignment` and `avatarAlignment(for:at:in)` delegate method From 18b13857560ec9c373b507be7193a9b6de938bc4 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 4 Dec 2017 16:12:36 -0600 Subject: [PATCH 57/59] Add AvatarPosition init with default of .natural --- Sources/Models/AvatarPosition.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/Models/AvatarPosition.swift b/Sources/Models/AvatarPosition.swift index 576a275d..aaef1c38 100644 --- a/Sources/Models/AvatarPosition.swift +++ b/Sources/Models/AvatarPosition.swift @@ -75,7 +75,10 @@ public struct AvatarPosition { public init(horizontal: Horizontal, vertical: Vertical) { self.horizontal = horizontal self.vertical = vertical - + } + + public init(vertical: Vertical) { + self.init(horizontal: .natural, vertical: vertical) } } From cc09e541d6baf2167e44131a7b1704a6902e5b33 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 4 Dec 2017 17:25:53 -0600 Subject: [PATCH 58/59] Fix origin calcs for Avatar .messageCenter & .messageTop --- Sources/Layout/MessageIntermediateLayoutAttributes.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Layout/MessageIntermediateLayoutAttributes.swift b/Sources/Layout/MessageIntermediateLayoutAttributes.swift index e2383ff1..6599a6b1 100644 --- a/Sources/Layout/MessageIntermediateLayoutAttributes.swift +++ b/Sources/Layout/MessageIntermediateLayoutAttributes.swift @@ -62,11 +62,11 @@ final class MessageIntermediateLayoutAttributes { case .cellBottom: origin.y = cellFrame.height - avatarSize.height case .messageTop: - origin.y = messageContainerFrame.maxY + origin.y = messageContainerFrame.minY case .messageBottom: origin.y = messageContainerFrame.maxY - avatarSize.height case .messageCenter: - origin.y = messageContainerFrame.midY + origin.y = messageContainerFrame.midY - (avatarSize.height/2) } return CGRect(origin: origin, size: avatarSize) From 444fbb0f8e2386b76d01d1ec1a30be96deffd863 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Mon, 4 Dec 2017 17:30:23 -0600 Subject: [PATCH 59/59] cellTopLabel and cellBottomLabel size calculations --- .../Sources/ConversationViewController.swift | 4 ++++ .../MessagesCollectionViewFlowLayout.swift | 18 ++++++++---------- .../Cells/MessageCollectionViewCell.swift | 14 ++++++++++---- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index caf88b9e..87b103cd 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -288,6 +288,10 @@ extension ConversationViewController: MessagesDisplayDelegate { extension ConversationViewController: MessagesLayoutDelegate { + func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition { + return AvatarPosition(horizontal: .natural, vertical: .messageBottom) + } + func messagePadding(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIEdgeInsets { if isFromCurrentSender(message: message) { return UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 4) diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index 03d5b516..73294ce5 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -485,12 +485,12 @@ private extension MessagesCollectionViewFlowLayout { return avatarVertical != .cellBottom ? width : width - (avatarWidth * 2) case (.messageTrailing, .cellLeading): - let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.left - return avatarVertical != .cellBottom ? width : width + avatarWidth + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.left - attributes.bottomLabelHorizontalPadding + return avatarVertical == .cellBottom ? width : width + avatarWidth case (.messageLeading, .cellTrailing): - let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.right - return avatarVertical != .cellBottom ? width : width + avatarWidth + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.right - attributes.bottomLabelHorizontalPadding + return avatarVertical == .cellBottom ? width : width + avatarWidth case (.messageLeading, .cellLeading): return itemWidth - avatarWidth - attributes.messageContainerPadding.left - attributes.bottomLabelHorizontalPadding @@ -515,9 +515,7 @@ private extension MessagesCollectionViewFlowLayout { let text = messagesDataSource.cellBottomLabelAttributedText(for: attributes.message, at: attributes.indexPath) guard let bottomLabelText = text else { return .zero } - return labelSize(for: bottomLabelText, considering: attributes.bottomLabelMaxWidth) - } } @@ -560,12 +558,12 @@ private extension MessagesCollectionViewFlowLayout { return avatarVertical != .cellTop ? width : width - (avatarWidth * 2) case (.messageTrailing, .cellLeading): - let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.left - return avatarVertical != .cellTop ? width : width + avatarWidth + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.left - attributes.topLabelHorizontalPadding + return avatarVertical == .cellTop ? width : width + avatarWidth case (.messageLeading, .cellTrailing): - let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.right - return avatarVertical != .cellTop ? width : width + avatarWidth + let width = attributes.messageContainerSize.width + attributes.messageContainerPadding.right - attributes.topLabelHorizontalPadding + return avatarVertical == .cellTop ? width : width + avatarWidth case (.messageLeading, .cellLeading): return itemWidth - avatarWidth - attributes.messageContainerPadding.left - attributes.topLabelHorizontalPadding diff --git a/Sources/Views/Cells/MessageCollectionViewCell.swift b/Sources/Views/Cells/MessageCollectionViewCell.swift index d1f24c56..474d2eb0 100644 --- a/Sources/Views/Cells/MessageCollectionViewCell.swift +++ b/Sources/Views/Cells/MessageCollectionViewCell.swift @@ -38,7 +38,11 @@ open class MessageCollectionViewCell: UICollectionViewCell, open var avatarView: AvatarView = AvatarView() - open var cellTopLabel = UILabel() + open var cellTopLabel: UILabel = { + let label = UILabel() + label.numberOfLines = 0 + return label + }() open var messageContentView: ContentView = { let contentView = ContentView() @@ -47,7 +51,11 @@ open class MessageCollectionViewCell: UICollectionViewCell, return contentView }() - open var cellBottomLabel = UILabel() + open var cellBottomLabel: UILabel = { + let label = UILabel() + label.numberOfLines = 0 + return label + }() open weak var delegate: MessageCellDelegate? @@ -69,13 +77,11 @@ open class MessageCollectionViewCell: UICollectionViewCell, // MARK: - Methods internal func setupSubviews() { - contentView.addSubview(cellTopLabel) contentView.addSubview(messageContainerView) messageContainerView.addSubview(messageContentView) contentView.addSubview(avatarView) contentView.addSubview(cellBottomLabel) - } open override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {