-
Notifications
You must be signed in to change notification settings - Fork 594
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
Composable messages #764
Merged
Merged
Composable messages #764
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
98be300
Extract fallbackItemPresenterFactory
wiruzx 61f4b03
Add toggle for switching between old and new messages
wiruzx 04e78d4
Override fallback item presenter factory
wiruzx b38dd7e
Add composable message
wiruzx 19a5783
Run pod install
wiruzx 230847a
Add missing UIKit imports
wiruzx 6d86079
Fix method names in ContainerViewProtocols
wiruzx 4c326e1
Fix typo in CachingLayoutProvider name
wiruzx a1c6b49
Group if statemets
wiruzx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
ChattoAdditions/ChattoAdditions.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
ChattoAdditions/sources/Chat Items/Composable/BaseViewComposition.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
public protocol BaseViewComposition { | ||
associatedtype View | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
ChattoAdditions/sources/Chat Items/Composable/Binder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
// MARK: - Protocols | ||
|
||
public protocol BinderProtocol { | ||
typealias Key = AnyBindingKey | ||
typealias Subviews = IndexedSubviews<Key> | ||
typealias ViewModels = [Key: Any] | ||
|
||
func bind(subviews: Subviews, viewModels: ViewModels) throws | ||
} | ||
|
||
public protocol BinderRegistryProtocol { | ||
mutating func register<Binding: ViewModelBinding, Key: BindingKeyProtocol>( | ||
binding: Binding, | ||
for key: Key | ||
) where Binding.View == Key.View, Binding.ViewModel == Key.ViewModel | ||
} | ||
|
||
// MARK: - Implementation | ||
|
||
public struct Binder: BinderProtocol, BinderRegistryProtocol { | ||
|
||
// MARK: - Type declarations | ||
|
||
public enum Error: Swift.Error { | ||
case keysMismatch | ||
case noView(key: String) | ||
case noViewModel(key: String) | ||
} | ||
|
||
// MARK: - Private properties | ||
|
||
private var bindings: [AnyBindingKey: AnyViewModelBinding] = [:] | ||
|
||
// MARK: - Instantiation | ||
|
||
public init() {} | ||
|
||
// MARK: - BinderRegistryProtocol | ||
|
||
public mutating func register<Binding: ViewModelBinding, Key: BindingKeyProtocol>(binding: Binding, for key: Key) | ||
where Binding.View == Key.View, Binding.ViewModel == Key.ViewModel { | ||
self.bindings[.init(key)] = AnyViewModelBinding(binding) | ||
} | ||
|
||
// MARK: - BinderProtocol | ||
|
||
public func bind(subviews: Subviews, viewModels: ViewModels) throws { | ||
try self.checkKeys(subviews: subviews, viewModels: viewModels) | ||
|
||
for (key, binding) in self.bindings { | ||
guard let view = subviews.subviews[key] else { | ||
throw Error.noView(key: key.description) | ||
} | ||
guard let viewModel = viewModels[key] else { | ||
throw Error.noViewModel(key: key.description) | ||
} | ||
try binding.bind(view: view, to: viewModel) | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func checkKeys(subviews: Subviews, viewModels: ViewModels) throws { | ||
let bindingKeys = Set(bindings.keys) | ||
let subviewsKeys = Set(subviews.subviews.keys) | ||
let viewModelsKeys = Set(viewModels.keys) | ||
guard bindingKeys == subviewsKeys && subviewsKeys == viewModelsKeys else { | ||
throw Error.keysMismatch | ||
} | ||
} | ||
} | ||
|
64 changes: 64 additions & 0 deletions
64
ChattoAdditions/sources/Chat Items/Composable/BindingKey.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import Foundation | ||
|
||
// MARK: - Protocol | ||
|
||
public protocol BindingKeyProtocol: Hashable, CustomStringConvertible { | ||
associatedtype View | ||
associatedtype ViewModel | ||
associatedtype LayoutProvider: LayoutProviderProtocol | ||
var uuid: UUID { get } | ||
} | ||
|
||
// MARK: - Implementation | ||
|
||
public struct BindingKey<View, ViewModel, LayoutProvider: LayoutProviderProtocol>: BindingKeyProtocol { | ||
public let uuid = UUID() | ||
public var description: String { "BindingKey<\(View.self),\(ViewModel.self),\(LayoutProvider.self)>" } | ||
} | ||
|
||
// MARK: - Type erasure | ||
|
||
public struct AnyBindingKey: Hashable, CustomStringConvertible { | ||
|
||
let uuid: UUID | ||
|
||
// MARK: - Private properties | ||
|
||
private let viewType: Any.Type | ||
private let viewModelType: Any.Type | ||
private let layoutProviderType: Any.Type | ||
private let _description: () -> String | ||
|
||
// MARK: - Instantiation | ||
|
||
public init<Base: BindingKeyProtocol>(_ base: Base) { | ||
self.viewType = Base.View.self | ||
self.viewModelType = Base.ViewModel.self | ||
self.layoutProviderType = Base.LayoutProvider.self | ||
self.uuid = base.uuid | ||
self._description = { base.description } | ||
} | ||
|
||
// MARK: - Hashable | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(self.uuid) | ||
hasher.combine(ObjectIdentifier(self.viewType)) | ||
hasher.combine(ObjectIdentifier(self.viewModelType)) | ||
hasher.combine(ObjectIdentifier(self.layoutProviderType)) | ||
} | ||
|
||
// MARK: - Equatable | ||
|
||
public static func == (lhs: AnyBindingKey, rhs: AnyBindingKey) -> Bool { | ||
return lhs.viewType == rhs.viewType | ||
&& lhs.viewModelType == rhs.viewModelType | ||
&& lhs.layoutProviderType == rhs.layoutProviderType | ||
&& lhs.uuid == rhs.uuid | ||
} | ||
|
||
// MARK: - CustomStringConvertible | ||
|
||
public var description: String { self._description() } | ||
} |
47 changes: 47 additions & 0 deletions
47
ChattoAdditions/sources/Chat Items/Composable/ChatItemCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// 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 | ||
|
||
public final class ChatItemCell: UICollectionViewCell { | ||
|
||
public var indexed: AnyIndexedSubviews? { | ||
didSet { | ||
guard oldValue == nil else { return } | ||
guard let subviews = self.indexed else { fatalError() } | ||
self.setup(subviews: subviews) | ||
} | ||
} | ||
|
||
private func setup(subviews: AnyIndexedSubviews) { | ||
let subview = subviews.root | ||
subview.translatesAutoresizingMaskIntoConstraints = false | ||
self.contentView.addSubview(subview) | ||
NSLayoutConstraint.activate([ | ||
subview.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), | ||
subview.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), | ||
subview.topAnchor.constraint(equalTo: self.contentView.topAnchor), | ||
subview.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor) | ||
]) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ChattoAdditions/sources/Chat Items/Composable/ChatItemContentView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
public protocol ChatItemContentView: AnyObject { | ||
associatedtype ViewModel | ||
func subscribe(for viewModel: ViewModel) | ||
} | ||
|
||
// MARK: - Automatic binding | ||
|
||
private struct ChatItemContentViewModelBinding<View: ChatItemContentView>: ViewModelBinding { | ||
func bind(view: View, to viewModel: View.ViewModel) { | ||
view.subscribe(for: viewModel) | ||
} | ||
} | ||
|
||
public extension Binder { | ||
mutating func registerBinding<Key: BindingKeyProtocol>(for key: Key) where Key.View: ChatItemContentView, Key.ViewModel == Key.View.ViewModel { | ||
self.register(binding: ChatItemContentViewModelBinding(), for: key) | ||
} | ||
} | ||
|
||
|
26 changes: 26 additions & 0 deletions
26
ChattoAdditions/sources/Chat Items/Composable/ChatItemLifecycleViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// 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. | ||
|
||
public protocol ChatItemLifecycleViewModel { | ||
func willShow() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about keeping consistency with the above methods and rename this method to
configure(with collectionView: UICollectionView)
?(Or change the other one. This also applies to the above method)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is part of public protocol, so not sure if it's a great idea to do renaming here.
I could rename the other one(
configure(with collectionView: UICollectionView)
), to match this function (configure(withCollectionView collectionView: UICollectionView)
), but I'm going to remove the protocol completely: #761