From 31db3f9983c6b3979d7f9e8ebbfc1228df2659fd Mon Sep 17 00:00:00 2001 From: subdiox Date: Mon, 18 Feb 2019 22:35:42 +0900 Subject: [PATCH] added AccessoryViewPosition parameter --- Example/ChatExample.xcodeproj/project.pbxproj | 9 ++-- Example/Podfile.lock | 8 ++-- .../AdvancedExampleViewController.swift | 4 +- Sources/Layout/MessageSizeCalculator.swift | 10 ++++ .../MessagesCollectionViewFlowLayout.swift | 16 +++++-- ...ssagesCollectionViewLayoutAttributes.swift | 5 +- Sources/Models/AccessoryPosition.swift | 48 +++++++++++++++++++ Sources/Views/Cells/MessageContentCell.swift | 21 ++++++-- 8 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 Sources/Models/AccessoryPosition.swift diff --git a/Example/ChatExample.xcodeproj/project.pbxproj b/Example/ChatExample.xcodeproj/project.pbxproj index 2b7dc8da..43b642b6 100644 --- a/Example/ChatExample.xcodeproj/project.pbxproj +++ b/Example/ChatExample.xcodeproj/project.pbxproj @@ -345,6 +345,7 @@ TargetAttributes = { 882B5E321CF7D4B900B6E160 = { CreatedOnToolsVersion = 7.3.1; + DevelopmentTeam = HY76D57XYN; LastSwiftMigration = 0800; }; 882B5E481CF7D4B900B6E160 = { @@ -447,7 +448,7 @@ files = ( ); inputPaths = ( - "${SRCROOT}/Pods/Target Support Files/Pods-ChatExample/Pods-ChatExample-frameworks.sh", + "${PODS_ROOT}/Target Support Files/Pods-ChatExample/Pods-ChatExample-frameworks.sh", "${BUILT_PRODUCTS_DIR}/MessageInputBar/MessageInputBar.framework", "${BUILT_PRODUCTS_DIR}/MessageKit/MessageKit.framework", ); @@ -458,7 +459,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-ChatExample/Pods-ChatExample-frameworks.sh\"\n"; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-ChatExample/Pods-ChatExample-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; 52AB594E03799821A66AFF18 /* [CP] Check Pods Manifest.lock */ = { @@ -676,7 +677,7 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = HY76D57XYN; INFOPLIST_FILE = "$(SRCROOT)/Sources/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.messagekit.ChatExample; @@ -691,7 +692,7 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = HY76D57XYN; INFOPLIST_FILE = "$(SRCROOT)/Sources/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.messagekit.ChatExample; diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 6d8fef54..14ba1653 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -2,7 +2,7 @@ PODS: - MessageInputBar (0.4.1): - MessageInputBar/Core (= 0.4.1) - MessageInputBar/Core (0.4.1) - - MessageKit (1.0.0): + - MessageKit (2.0.0): - MessageInputBar/Core DEPENDENCIES: @@ -18,13 +18,13 @@ EXTERNAL SOURCES: CHECKOUT OPTIONS: MessageInputBar: - :commit: faebe27f2dd8f39ea145e75b7296ef48133c099a + :commit: 117332dba2681cececc9ccacc0b759c161b9798e :git: https://github.com/MessageKit/MessageInputBar.git SPEC CHECKSUMS: MessageInputBar: e81c7535347f1f7b923de7080409a535a004b6e4 - MessageKit: 2bbd13dd6a7c06f42f2d13ed8871d1fe5383b477 + MessageKit: 29c1c87e5a396d2ca7a3f712e5171dc9aba42a1e PODFILE CHECKSUM: 04c1a805e1997e83bacab1a34787e71e9fe4432b -COCOAPODS: 1.5.3 +COCOAPODS: 1.6.0 diff --git a/Example/Sources/View Controllers/AdvancedExampleViewController.swift b/Example/Sources/View Controllers/AdvancedExampleViewController.swift index d55b07cd..2d24c9e9 100644 --- a/Example/Sources/View Controllers/AdvancedExampleViewController.swift +++ b/Example/Sources/View Controllers/AdvancedExampleViewController.swift @@ -102,7 +102,9 @@ final class AdvancedExampleViewController: ChatViewController { layout?.setMessageIncomingAccessoryViewPadding(HorizontalEdgeInsets(left: 8, right: 0)) layout?.setMessageOutgoingAccessoryViewSize(CGSize(width: 30, height: 30)) layout?.setMessageOutgoingAccessoryViewPadding(HorizontalEdgeInsets(left: 0, right: 8)) - + layout?.setMessageIncomingAccessoryViewPosition(.messageBottom) + layout?.setMessageOutgoingAccessoryViewPosition(.messageBottom) + messagesCollectionView.messagesLayoutDelegate = self messagesCollectionView.messagesDisplayDelegate = self } diff --git a/Sources/Layout/MessageSizeCalculator.swift b/Sources/Layout/MessageSizeCalculator.swift index f83f826e..0b0fee01 100644 --- a/Sources/Layout/MessageSizeCalculator.swift +++ b/Sources/Layout/MessageSizeCalculator.swift @@ -55,6 +55,9 @@ open class MessageSizeCalculator: CellSizeCalculator { public var incomingAccessoryViewPadding = HorizontalEdgeInsets.zero public var outgoingAccessoryViewPadding = HorizontalEdgeInsets.zero + + public var incomingAccessoryViewPosition: AccessoryPosition = .messageCenter + public var outgoingAccessoryViewPosition: AccessoryPosition = .messageCenter open override func configure(attributes: UICollectionViewLayoutAttributes) { guard let attributes = attributes as? MessagesCollectionViewLayoutAttributes else { return } @@ -77,6 +80,7 @@ open class MessageSizeCalculator: CellSizeCalculator { attributes.accessoryViewSize = accessoryViewSize(for: message) attributes.accessoryViewPadding = accessoryViewPadding(for: message) + attributes.accessoryViewPosition = accessoryViewPosition(for: message) } open override func sizeForItem(at indexPath: IndexPath) -> CGSize { @@ -210,6 +214,12 @@ open class MessageSizeCalculator: CellSizeCalculator { let isFromCurrentSender = dataSource.isFromCurrentSender(message: message) return isFromCurrentSender ? outgoingAccessoryViewPadding : incomingAccessoryViewPadding } + + public func accessoryViewPosition(for message: MessageType) -> AccessoryPosition { + let dataSource = messagesLayout.messagesDataSource + let isFromCurrentSender = dataSource.isFromCurrentSender(message: message) + return isFromCurrentSender ? outgoingAccessoryViewPosition : incomingAccessoryViewPosition + } // MARK: - MessageContainer diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index cd5fe5af..7a0ba1cf 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -239,20 +239,30 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { messageSizeCalculators().forEach { $0.incomingAccessoryViewSize = newSize } } - /// Set `outgoingAvatarSize` of all `MessageSizeCalculator`s + /// Set `outgoingAccessoryViewSize` of all `MessageSizeCalculator`s public func setMessageOutgoingAccessoryViewSize(_ newSize: CGSize) { messageSizeCalculators().forEach { $0.outgoingAccessoryViewSize = newSize } } - /// Set `incomingAccessoryViewSize` of all `MessageSizeCalculator`s + /// Set `incomingAccessoryViewPadding` of all `MessageSizeCalculator`s public func setMessageIncomingAccessoryViewPadding(_ newPadding: HorizontalEdgeInsets) { messageSizeCalculators().forEach { $0.incomingAccessoryViewPadding = newPadding } } - /// Set `outgoingAvatarSize` of all `MessageSizeCalculator`s + /// Set `outgoingAccessoryViewPadding` of all `MessageSizeCalculator`s public func setMessageOutgoingAccessoryViewPadding(_ newPadding: HorizontalEdgeInsets) { messageSizeCalculators().forEach { $0.outgoingAccessoryViewPadding = newPadding } } + + /// Set `incomingAccessoryViewPosition` of all `MessageSizeCalculator`s + public func setMessageIncomingAccessoryViewPosition(_ newPosition: AccessoryPosition) { + messageSizeCalculators().forEach { $0.incomingAccessoryViewPosition = newPosition } + } + + /// Set `outgoingAccessoryViewPosition` of all `MessageSizeCalculator`s + public func setMessageOutgoingAccessoryViewPosition(_ newPosition: AccessoryPosition) { + messageSizeCalculators().forEach { $0.outgoingAccessoryViewPosition = newPosition } + } /// Get all `MessageSizeCalculator`s open func messageSizeCalculators() -> [MessageSizeCalculator] { diff --git a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift index e055ac57..6ee95b5d 100644 --- a/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift +++ b/Sources/Layout/MessagesCollectionViewLayoutAttributes.swift @@ -48,7 +48,8 @@ open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttribu public var accessoryViewSize: CGSize = .zero public var accessoryViewPadding: HorizontalEdgeInsets = .zero - + public var accessoryViewPosition: AccessoryPosition = .messageCenter + // MARK: - Methods open override func copy(with zone: NSZone? = nil) -> Any { @@ -68,6 +69,7 @@ open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttribu copy.messageBottomLabelSize = messageBottomLabelSize copy.accessoryViewSize = accessoryViewSize copy.accessoryViewPadding = accessoryViewPadding + copy.accessoryViewPosition = accessoryViewPosition return copy // swiftlint:enable force_cast } @@ -89,6 +91,7 @@ open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttribu && attributes.messageBottomLabelSize == messageBottomLabelSize && attributes.accessoryViewSize == accessoryViewSize && attributes.accessoryViewPadding == accessoryViewPadding + && attributes.accessoryViewPosition == accessoryViewPosition } else { return false } diff --git a/Sources/Models/AccessoryPosition.swift b/Sources/Models/AccessoryPosition.swift new file mode 100644 index 00000000..d0496220 --- /dev/null +++ b/Sources/Models/AccessoryPosition.swift @@ -0,0 +1,48 @@ +/* + MIT License + + Copyright (c) 2017-2018 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 `AccessoryView` in a `MessageCollectionViewCell`. +public enum AccessoryPosition { + + /// Aligns the `AccessoryView`'s top edge to the cell's top edge. + case cellTop + + /// Aligns the `AccessoryView`'s top edge to the `messageTopLabel`'s top edge. + case messageLabelTop + + /// Aligns the `AccessoryView`'s top edge to the `MessageContainerView`'s top edge. + case messageTop + + /// Aligns the `AccessoryView` center to the `MessageContainerView` center. + case messageCenter + + /// Aligns the `AccessoryView`'s bottom edge to the `MessageContainerView`s bottom edge. + case messageBottom + + /// Aligns the `AccessoryView`'s bottom edge to the cell's bottom edge. + case cellBottom +} diff --git a/Sources/Views/Cells/MessageContentCell.swift b/Sources/Views/Cells/MessageContentCell.swift index 3a194a68..4abb2011 100644 --- a/Sources/Views/Cells/MessageContentCell.swift +++ b/Sources/Views/Cells/MessageContentCell.swift @@ -281,10 +281,23 @@ open class MessageContentCell: MessageCollectionViewCell { /// - attributes: The `MessagesCollectionViewLayoutAttributes` for the cell. open func layoutAccessoryView(with attributes: MessagesCollectionViewLayoutAttributes) { - // Accessory view aligned to the middle of the messageContainerView - let y = messageContainerView.frame.midY - (attributes.accessoryViewSize.height / 2) - - var origin = CGPoint(x: 0, y: y) + var origin: CGPoint = .zero + + // Accessory view is set at the side space of the messageContainerView + switch attributes.accessoryViewPosition { + case .messageLabelTop: + origin.y = messageTopLabel.frame.minY + case .messageTop: + origin.y = messageContainerView.frame.minY + case .messageBottom: + origin.y = messageContainerView.frame.maxY - attributes.accessoryViewSize.height + case .messageCenter: + origin.y = messageContainerView.frame.midY - (attributes.accessoryViewSize.height / 2) + case .cellBottom: + origin.y = attributes.frame.height - attributes.accessoryViewSize.height + default: + break + } // Accessory view is always on the opposite side of avatar switch attributes.avatarPosition.horizontal {