Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compound bubble layout: dynamic content insets #685

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@
import UIKit
import Chatto

public struct LayoutInfo {

public let size: CGSize
public let contentInsets: UIEdgeInsets

public init(size: CGSize, contentInsets: UIEdgeInsets) {
self.size = size
self.contentInsets = contentInsets
}
}

public protocol MessageManualLayoutProviderProtocol: HashableRepresentible {
func sizeThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> CGSize
func layoutThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> LayoutInfo
}

// MARK: - Text

public struct TextMessageLayout {
public let frame: CGRect
public let size: CGSize
public let contentInsets: UIEdgeInsets

public init(frame: CGRect, size: CGSize) {
public init(frame: CGRect, size: CGSize, contentInsets: UIEdgeInsets) {
self.frame = frame
self.size = size
self.contentInsets = contentInsets
}
}

Expand All @@ -45,8 +58,9 @@ public protocol TextMessageLayoutProviderProtocol: MessageManualLayoutProviderPr
}

extension TextMessageLayoutProviderProtocol {
public func sizeThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> CGSize {
self.layout(for: size, safeAreaInsets: safeAreaInsets).size
public func layoutThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> LayoutInfo {
let layout = self.layout(for: size, safeAreaInsets: safeAreaInsets)
return LayoutInfo(size: layout.size, contentInsets: layout.contentInsets)
}
}

Expand Down Expand Up @@ -98,7 +112,8 @@ public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProt
origin: combinedInsets.origin,
size: textSize
),
size: resultSize
size: resultSize,
contentInsets: self.textInsets
)
}

Expand All @@ -123,9 +138,10 @@ public struct ImageMessageLayoutProvider: Hashable, MessageManualLayoutProviderP
self.imageSize = imageSize
}

public func sizeThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> CGSize {
public func layoutThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> LayoutInfo {
let ratio = self.imageSize.width / self.imageSize.height
return CGSize(width: size.width, height: size.width / ratio).bma_round()
let size = CGSize(width: size.width, height: size.width / ratio).bma_round()
return LayoutInfo(size: size, contentInsets: .zero)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ public struct CompoundBubbleLayoutProvider {

var resultWidth: CGFloat = 0
var maxY: CGFloat = 0
var previousItemBottomInset: CGFloat = 0
self.configuration.layoutProviders.forEach { layoutProvider in
let frame: CGRect
let size = layoutProvider.sizeThatFits(size: CGSize(width: width, height: .greatestFiniteMagnitude), safeAreaInsets: safeAreaInsets)
let viewWidth = max(size.width, resultWidth)
let layoutInfo = layoutProvider.layoutThatFits(size: CGSize(width: width, height: .greatestFiniteMagnitude), safeAreaInsets: safeAreaInsets)
let viewWidth = max(layoutInfo.size.width, resultWidth)
resultWidth = min(viewWidth, width)
frame = CGRect(x: 0, y: maxY, width: viewWidth, height: size.height)
let currentItemTopInset = layoutInfo.contentInsets.top
let y = maxY - min(previousItemBottomInset, currentItemTopInset)
frame = CGRect(x: 0, y: y, width: viewWidth, height: layoutInfo.size.height)
subviewsFramesWithProviders.append((frame, layoutProvider))
maxY = frame.maxY
previousItemBottomInset = layoutInfo.contentInsets.bottom
}

subviewsFramesWithProviders = subviewsFramesWithProviders.map { frameWithProvider in
Expand Down
8 changes: 8 additions & 0 deletions ChattoApp/ChattoApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
DECD501A21AEEAF100988729 /* ContentAwareInputItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = DECD501921AEEAF100988729 /* ContentAwareInputItem.swift */; };
DECD501C21AEEC5A00988729 /* CustomInput.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DECD501B21AEEC5A00988729 /* CustomInput.xcassets */; };
EAE6345722EB0CAD0053E21A /* TestItemsReloadingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAE6345622EB0CAD0053E21A /* TestItemsReloadingViewController.swift */; };
EAEC841E253066C500F149CD /* DemoText2MessageContentFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAEC841D253066C500F149CD /* DemoText2MessageContentFactory.swift */; };
EAEC84202530737900F149CD /* DemoInvisibleSplitterFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAEC841F2530737900F149CD /* DemoInvisibleSplitterFactory.swift */; };
FE2D050B1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -125,6 +127,8 @@
DECD501921AEEAF100988729 /* ContentAwareInputItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentAwareInputItem.swift; sourceTree = "<group>"; };
DECD501B21AEEC5A00988729 /* CustomInput.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = CustomInput.xcassets; sourceTree = "<group>"; };
EAE6345622EB0CAD0053E21A /* TestItemsReloadingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestItemsReloadingViewController.swift; sourceTree = "<group>"; };
EAEC841D253066C500F149CD /* DemoText2MessageContentFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoText2MessageContentFactory.swift; sourceTree = "<group>"; };
EAEC841F2530737900F149CD /* DemoInvisibleSplitterFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoInvisibleSplitterFactory.swift; sourceTree = "<group>"; };
FE2D050A1C915ADB006F902B /* BaseMessageCollectionViewCellAvatarStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseMessageCollectionViewCellAvatarStyle.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -162,9 +166,11 @@
268CD56C21FA3C4C00DEE2C2 /* DemoCompoundMessageModel.swift */,
268CD57021FB704500DEE2C2 /* DemoCompoundMessageViewModel.swift */,
268CD577220336EB00DEE2C2 /* DemoTextMessageContentFactory.swift */,
EAEC841D253066C500F149CD /* DemoText2MessageContentFactory.swift */,
268CD57922034A9900DEE2C2 /* DemoImageMessageContentFactory.swift */,
268CD57D220377D500DEE2C2 /* DemoDateMessageContentFactory.swift */,
266C7AE024B2AE2E00DB0F61 /* DemoEmojiDecorationViewFactory.swift */,
EAEC841F2530737900F149CD /* DemoInvisibleSplitterFactory.swift */,
);
path = "Compound Messages";
sourceTree = "<group>";
Expand Down Expand Up @@ -487,6 +493,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
EAEC84202530737900F149CD /* DemoInvisibleSplitterFactory.swift in Sources */,
268CD57E220377D500DEE2C2 /* DemoDateMessageContentFactory.swift in Sources */,
268CD56F21FA485500DEE2C2 /* DemoMessageInteractionHandler.swift in Sources */,
55A77B4A1FCC5EE70040C77E /* MessagesSelectorProtocol.swift in Sources */,
Expand All @@ -496,6 +503,7 @@
268CD57121FB704500DEE2C2 /* DemoCompoundMessageViewModel.swift in Sources */,
C3F91DB81C75EF9E00D461D2 /* ChatExamplesViewController.swift in Sources */,
C3F91DC31C75EF9E00D461D2 /* SlidingDatasSource.swift in Sources */,
EAEC841E253066C500F149CD /* DemoText2MessageContentFactory.swift in Sources */,
55A77B4C1FCC5FFB0040C77E /* BaseMessagesSelector.swift in Sources */,
C3F91DC61C75EF9E00D461D2 /* DemoTextMessageViewModel.swift in Sources */,
C341D42E1C9635DF00FD3463 /* TimeSeparatorModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ final class DemoCompoundMessageModel: Equatable, DecoratedMessageModelProtocol,

// MARK: - Instantiation

init(text: String, image: UIImage?, emoji: String?, messageModel: MessageModelProtocol) {
init(text: String?, image: UIImage?, showInvisibleSplitter: Bool, text2: String?, emoji: String?, messageModel: MessageModelProtocol) {
self.text = text
self.image = image
self.text2 = text2
self.emoji = emoji
self.messageModel = messageModel
self.status = messageModel.status
self.showInvisibleSplitter = showInvisibleSplitter
}

// MARK: - Public properties

let text: String
let text: String?
let text2: String?
let image: UIImage?
let emoji: String?
let showInvisibleSplitter: Bool

// MARK: - DecoratedMessageModelProtocol

Expand All @@ -64,6 +68,8 @@ final class DemoCompoundMessageModel: Equatable, DecoratedMessageModelProtocol,
guard let anotherModel = anotherItem as? DemoCompoundMessageModel else { return false }
return self.text == anotherModel.text
&& self.image == anotherModel.image
&& self.text2 == anotherModel.text2
&& self.emoji == anotherModel.emoji
&& self.showInvisibleSplitter == anotherModel.showInvisibleSplitter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ private final class DemoEmojiDecorationViewLayoutProvider: Hashable, MessageDeco
let textLayoutProvider = TextMessageLayoutProvider(text: self.emoji,
font: self.font,
textInsets: .zero)
return textLayoutProvider.sizeThatFits(size: UIView.layoutFittingExpandedSize, safeAreaInsets: .zero)
let layoutInfo = textLayoutProvider.layoutThatFits(size: UIView.layoutFittingExpandedSize,
safeAreaInsets: .zero)
return layoutInfo.size
}()

static func == (lhs: DemoEmojiDecorationViewLayoutProvider, rhs: DemoEmojiDecorationViewLayoutProvider) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015-present Badoo Trading Limited.
//
// 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 UIKit
import Chatto
import ChattoAdditions

struct DemoInvisibleSplitterFactory: MessageContentFactoryProtocol {

func canCreateMessageContent(forModel model: DemoCompoundMessageModel) -> Bool {
model.showInvisibleSplitter
}

func createContentView() -> UIView {
UIStackView()
}

func createContentPresenter(forModel model: DemoCompoundMessageModel) -> MessageContentPresenterProtocol {
return DefaultMessageContentPresenter<DemoCompoundMessageModel, UIStackView>(
message: model,
showBorder: false,
onBinding: nil
)
}

func createLayoutProvider(forModel model: DemoCompoundMessageModel) -> MessageManualLayoutProviderProtocol {
InvisibleSplitterLayoutProvider()
}

func createMenuPresenter(forModel model: DemoCompoundMessageModel) -> ChatItemMenuPresenterProtocol? {
return nil
}
}

private final class InvisibleSplitterLayoutProvider: Hashable, MessageManualLayoutProviderProtocol {
static func == (lhs: InvisibleSplitterLayoutProvider, rhs: InvisibleSplitterLayoutProvider) -> Bool { true }
func layoutThatFits(size: CGSize, safeAreaInsets: UIEdgeInsets) -> LayoutInfo { LayoutInfo(size: .zero, contentInsets: .zero) }
func hash(into hasher: inout Hasher) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015-present Badoo Trading Limited.
//
// 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 UIKit
import Chatto
import ChattoAdditions

struct DemoText2MessageContentFactory: MessageContentFactoryProtocol {

private let font = UIFont.systemFont(ofSize: 17)
private let textInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)

func canCreateMessageContent(forModel model: DemoCompoundMessageModel) -> Bool {
return model.text2 != nil
}

func createContentView() -> UIView {
let textView = TextView()
textView.label.numberOfLines = 0
textView.label.font = self.font
return textView
}

func createContentPresenter(forModel model: DemoCompoundMessageModel) -> MessageContentPresenterProtocol {
let layoutProvider = self.createTextLayoutProvider(forModel: model)
return DefaultMessageContentPresenter<DemoCompoundMessageModel, TextView>(
message: model,
showBorder: false,
onBinding: { message, textView in
guard let textView = textView else { return }
textView.label.text = message.text2
textView.label.textColor = message.isIncoming ? .black : .white
textView.layoutProvider = layoutProvider
}
)
}

func createLayoutProvider(forModel model: DemoCompoundMessageModel) -> MessageManualLayoutProviderProtocol {
self.createTextLayoutProvider(forModel: model)
}

func createMenuPresenter(forModel model: DemoCompoundMessageModel) -> ChatItemMenuPresenterProtocol? {
return nil
}

private func createTextLayoutProvider(forModel model: DemoCompoundMessageModel) -> TextMessageLayoutProviderProtocol {
TextMessageLayoutProvider(text: model.text2!,
font: self.font,
textInsets: self.textInsets)
}
}

private final class TextView: UIView {

let label = UILabel()
var layoutProvider: TextMessageLayoutProviderProtocol? {
didSet {
guard self.layoutProvider != nil else { return }
self.setNeedsLayout()
}
}

init() {
super.init(frame: .zero)
self.addSubview(label)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()
guard let layoutProvider = self.layoutProvider else { return }
self.label.frame = layoutProvider.layout(for: self.bounds.size, safeAreaInsets: self.safeAreaInsets).frame
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct DemoTextMessageContentFactory: MessageContentFactoryProtocol {
private let textInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)

func canCreateMessageContent(forModel model: DemoCompoundMessageModel) -> Bool {
return true
return model.text != nil
}

func createContentView() -> UIView {
Expand Down Expand Up @@ -64,7 +64,7 @@ struct DemoTextMessageContentFactory: MessageContentFactoryProtocol {
}

private func createTextLayoutProvider(forModel model: DemoCompoundMessageModel) -> TextMessageLayoutProviderProtocol {
TextMessageLayoutProvider(text: model.text,
TextMessageLayoutProvider(text: model.text!,
font: self.font,
textInsets: self.textInsets)
}
Expand Down
Loading