Merge pull request #340 from TouchInstinct/feature/view_containers

feat: container views
This commit is contained in:
Nikita Semenov 2023-02-14 11:39:28 +03:00 committed by GitHub
commit 05fa3dad31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 358 additions and 89 deletions

View File

@ -4,6 +4,9 @@
- **Added**: `ViewAppearance` and `ViewLayout` models for setting up Views' appearance and layout
- **Added**: `TableKit.Row` extension for configuration inner View's appearance and layout
- **Added**: `WrappableView` with typealiases for creating wrapped in the container views
- **Added**: `CollectionTableViewCell` and `ContainerView`
- **Update**: Separator appearance configureation for table views
### 1.32.0

View File

@ -0,0 +1,35 @@
//
// Copyright (c) 2023 Touch Instinct
//
// 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 TableKit
import TIUIElements
import TIUIKitCore
import UIKit
public extension WrappableView where Self: ConfigurableView {
typealias InTableRow = TableKit.TableRow<Self.InTableCell>
typealias InSeparatableRow = TableKit.TableRow<Self.InSeparatableTableCell>
}
public extension WrappableView where Self: UITableViewCell & ConfigurableCell {
typealias TableRow = TableKit.TableRow<Self>
}

View File

@ -31,23 +31,23 @@ public extension Array where Element == SeparatorRowBox {
}
/// Configure separators from SeparatorRowBox array
/// - parameter extreme: Configuration that will be used for extreme values, for first or last row
/// - parameter middle: Configuration for intermediate rows
func configureSeparators(extreme extremeSeparatorConfiguration: SeparatorConfiguration,
middle middleSeparatorConfiguration: SeparatorConfiguration) {
/// - parameter extreme: Appearance that will be used for extreme values, for first or last row
/// - parameter middle: Apearance for intermediate rows
func configureSeparators(extreme extremeSeparatorsAppearance: SeparatorAppearance,
middle middleSeparatorsAppearance: SeparatorAppearance) {
configureSeparators(first: extremeSeparatorConfiguration,
middle: middleSeparatorConfiguration,
last: extremeSeparatorConfiguration)
configureSeparators(first: extremeSeparatorsAppearance,
middle: middleSeparatorsAppearance,
last: extremeSeparatorsAppearance)
}
/// Configure separators from SeparatorRowBox array
/// - parameter first: Configuration of the top separator of the first row
/// - parameter middle: Configuration of the separators between the rows
/// - parameter last: Configuration of the bottom separator of the last row
func configureSeparators(first firstSeparatorConfiguration: SeparatorConfiguration,
middle middleSeparatorConfiguration: SeparatorConfiguration,
last lastSeparatorConfiguration: SeparatorConfiguration) {
/// - parameter first: Appearance of the top separator of the first row
/// - parameter middle: Appearance of the separators between the rows
/// - parameter last: Appearance of the bottom separator of the last row
func configureSeparators(first firstSeparatorsAppearance: SeparatorAppearance,
middle middleSeparatorsAppearance: SeparatorAppearance,
last lastSeparatorsAppearance: SeparatorAppearance) {
if isEmpty {
return
@ -55,12 +55,12 @@ public extension Array where Element == SeparatorRowBox {
switch count {
case 1:
first?.set(separatorType: .full(firstSeparatorConfiguration, lastSeparatorConfiguration))
first?.set(separatorType: .full(top: firstSeparatorsAppearance, bottom: lastSeparatorsAppearance))
default:
dropFirst().dropLast().forEach { $0.set(separatorType: .bottom(middleSeparatorConfiguration)) }
first?.set(separatorType: .full(firstSeparatorConfiguration, middleSeparatorConfiguration))
last?.set(separatorType: .bottom(lastSeparatorConfiguration))
dropFirst().dropLast().forEach { $0.set(separatorType: .bottom(middleSeparatorsAppearance)) }
first?.set(separatorType: .full(top: firstSeparatorsAppearance, bottom: middleSeparatorsAppearance))
last?.set(separatorType: .bottom(lastSeparatorsAppearance))
}
}
}

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2020 Touch Instinct
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
@ -20,17 +20,12 @@
// THE SOFTWARE.
//
import UIKit
import TableKit
import TIUIElements
import TIUIKitCore
public struct SeparatorConfiguration {
public let color: UIColor
public let insets: UIEdgeInsets
public let height: CGFloat
public init(color: UIColor, insets: UIEdgeInsets = .zero, height: CGFloat = 1) {
self.color = color
self.insets = insets
self.height = height
extension ContainerTableViewCell: ConfigurableCell where View: ConfigurableView {
public func configure(with viewModel: View.ViewModelType) {
wrappedView.configure(with: viewModel)
}
}

View File

@ -23,28 +23,29 @@
import TableKit
import TIUIElements
private let configureSeparatorActionId = "TableRowConfigureSeparatorActionId"
extension TableRow: SeparatorsConfigurable where CellType: SeparatorsConfigurable {
private static var configureSeparatorsActionId: String {
"TableRowConfigureSeparatorsActionId"
}
public extension TableRow where CellType: SeparatorConfigurable {
func with(separatorType: ViewSeparatorType) -> Self {
set(separatorType: separatorType)
public func with(separators: SeparatorsConfiguration) -> Self {
configureSeparators(with: separators)
return self
}
func set(separatorType: ViewSeparatorType) {
removeAction(forActionId: configureSeparatorActionId)
public func configureSeparators(with separatorsConfiguration: SeparatorsConfiguration) {
removeAction(forActionId: Self.configureSeparatorsActionId)
let action = TableRowAction<CellType>(.configure) {
$0.cell?.configureSeparators(with: separatorType)
let action = TableRowAction<CellType>(.configure) { options in
options.cell?.configureSeparators(with: separatorsConfiguration)
}
action.id = configureSeparatorActionId
action.id = Self.configureSeparatorsActionId
on(action)
}
}
public extension TableRow where CellType: SeparatorConfigurable {
public extension TableRow where CellType: SeparatorsConfigurable {
/// TableRow typed as SeparatorRowBox
var separatorRowBox: SeparatorRowBox {

View File

@ -21,21 +21,21 @@
//
import TableKit
import TIUIElements
import TISwiftUtils
import TIUIElements
/// Class that used to configure separators when multiply cells presented in one section
public final class SeparatorRowBox {
private let setSeparatorHandler: ParameterClosure<ViewSeparatorType>
private let setSeparatorHandler: ParameterClosure<SeparatorsConfiguration>
public func set(separatorType: ViewSeparatorType) {
public func set(separatorType: SeparatorsConfiguration) {
setSeparatorHandler(separatorType)
}
public let row: Row
public init<T>(row: TableRow<T>) where T: SeparatorConfigurable {
public init<T>(row: TableRow<T>) where T: SeparatorsConfigurable {
self.row = row
setSeparatorHandler = row.set
setSeparatorHandler = row.configureSeparators(with:)
}
}

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2020 Touch Instinct
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
@ -22,7 +22,8 @@
import UIKit
open class BaseSeparatorCell: BaseInitializableCell, SeparatorConfigurable {
open class ContainerSeparatorTableViewCell<View: UIView>: ContainerTableViewCell<View>, SeparatorsConfigurable {
private lazy var topSeparatorView = createTopSeparator()
private lazy var bottomSeparatorView = createBottomSeparator()
@ -52,23 +53,25 @@ open class BaseSeparatorCell: BaseInitializableCell, SeparatorConfigurable {
contentView.addSubview(bottomSeparatorView)
}
public func configureSeparators(with separatorType: ViewSeparatorType) {
topSeparatorView.isHidden = separatorType.topIsHidden
bottomSeparatorView.isHidden = separatorType.bottomIsHidden
// MARK: - SeparatorsConfigurable
switch separatorType {
public func configureSeparators(with separatorsConfiguration: SeparatorsConfiguration) {
topSeparatorView.isHidden = separatorsConfiguration.topIsHidden
bottomSeparatorView.isHidden = separatorsConfiguration.bottomIsHidden
switch separatorsConfiguration {
case .none:
break
case let .bottom(configuration):
updateBottomSeparator(with: configuration)
case let .bottom(appearance):
updateBottomSeparator(with: appearance)
case let .top(configuration):
updateTopSeparator(with: configuration)
case let .top(appearance):
updateTopSeparator(with: appearance)
case let .full(topConfiguration, bottomConfiguration):
updateTopSeparator(with: topConfiguration)
updateBottomSeparator(with: bottomConfiguration)
case let .full(topAppearance, bottomAppearance):
updateTopSeparator(with: topAppearance)
updateBottomSeparator(with: bottomAppearance)
}
}
@ -127,26 +130,26 @@ open class BaseSeparatorCell: BaseInitializableCell, SeparatorConfigurable {
$0.translatesAutoresizingMaskIntoConstraints = false
}
}
}
private extension BaseSeparatorCell {
func updateTopSeparator(with configuration: SeparatorConfiguration) {
topSeparatorView.backgroundColor = configuration.color
// MARK: - Private
topViewHeightConstraint?.constant = configuration.height
private func updateTopSeparator(with appearance: SeparatorAppearance) {
topSeparatorView.configureUIView(appearance: appearance)
topViewTopConstraint?.constant = configuration.insets.top
topViewLeftConstraint?.constant = configuration.insets.left
topViewRightConstraint?.constant = configuration.insets.right
topViewHeightConstraint?.constant = appearance.layout.size.height
topViewTopConstraint?.constant = appearance.layout.insets.top
topViewLeftConstraint?.constant = appearance.layout.insets.left
topViewRightConstraint?.constant = appearance.layout.insets.right
}
func updateBottomSeparator(with configuration: SeparatorConfiguration) {
bottomSeparatorView.backgroundColor = configuration.color
private func updateBottomSeparator(with appearance: SeparatorAppearance) {
bottomSeparatorView.configureUIView(appearance: appearance)
bottomViewHeightConstraint?.constant = configuration.height
bottomViewHeightConstraint?.constant = appearance.layout.size.height
bottomViewBottomConstraint?.constant = configuration.insets.bottom
bottomViewLeftConstraint?.constant = configuration.insets.left
bottomViewRightConstraint?.constant = configuration.insets.right
bottomViewBottomConstraint?.constant = appearance.layout.insets.bottom
bottomViewLeftConstraint?.constant = appearance.layout.insets.left
bottomViewRightConstraint?.constant = appearance.layout.insets.right
}
}

View File

@ -0,0 +1,44 @@
//
// Copyright (c) 2023 Touch Instinct
//
// 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 TIUIKitCore
import UIKit
public final class SeparatorLayout: UIView.BaseWrappedLayout, WrappedViewLayout {
public static var defaultLayout: Self {
Self()
}
public init(insets: UIEdgeInsets = .zero,
size: CGSize = .fixedHeight(0.5)) {
super.init(insets: insets,
size: size,
centerOffset: .nan)
}
}
public final class SeparatorAppearance: UIView.BaseAppearance<SeparatorLayout>, ViewAppearance {
public static var defaultAppearance: Self {
Self(backgroundColor: .lightGray)
}
}

View File

@ -20,6 +20,6 @@
// THE SOFTWARE.
//
public protocol SeparatorConfigurable {
func configureSeparators(with separatorType: ViewSeparatorType)
public protocol SeparatorsConfigurable {
func configureSeparators(with separatorsConfiguration: SeparatorsConfiguration)
}

View File

@ -20,22 +20,14 @@
// THE SOFTWARE.
//
public enum ViewSeparatorType {
/// All separators for view is hidden
public enum SeparatorsConfiguration {
case none
/// Show only top separator
case top(SeparatorConfiguration)
/// Show only bottom separator
case bottom(SeparatorConfiguration)
/// First configuration for top, second for bottom
case full(SeparatorConfiguration, SeparatorConfiguration)
case top(SeparatorAppearance)
case bottom(SeparatorAppearance)
case full(top: SeparatorAppearance, bottom: SeparatorAppearance)
}
public extension ViewSeparatorType {
public extension SeparatorsConfiguration {
/// Determine if bottom separator is hidden.
var bottomIsHidden: Bool {
@ -48,7 +40,7 @@ public extension ViewSeparatorType {
}
/// Returns top configuration if type is top or full.
var topConfiguration: SeparatorConfiguration? {
var topConfiguration: SeparatorAppearance? {
switch self {
case let .top(configuration), let .full(configuration, _):
return configuration
@ -59,7 +51,7 @@ public extension ViewSeparatorType {
}
/// Returns bottom configuration if type is bottom or full.
var bottomConfiguration: SeparatorConfiguration? {
var bottomConfiguration: SeparatorAppearance? {
switch self {
case let .bottom(configuration), let .full(_, configuration):
return configuration

View File

@ -0,0 +1,50 @@
//
// Copyright (c) 2023 Touch Instinct
//
// 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
open class CollectionTableViewCell<CollectionView: UICollectionView>: ContainerTableViewCell<CollectionView> {
// MARK: - UIView Overrides
open override func systemLayoutSizeFitting(_ targetSize: CGSize,
withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority,
verticalFittingPriority: UILayoutPriority) -> CGSize {
let cachedCollectionFrame = wrappedView.frame
wrappedView.frame.size.width = targetSize.width - contentInsets.left - contentInsets.right
let collectionContentHeight = wrappedView.collectionViewLayout.collectionViewContentSize.height
wrappedView.frame = cachedCollectionFrame
return CGSize(width: targetSize.width,
height: collectionContentHeight + contentInsets.top + contentInsets.bottom)
}
// MARK: - Open methods
open func createCollectionLayout() -> UICollectionViewLayout {
UICollectionViewFlowLayout()
}
open override func createView() -> CollectionView {
CollectionView(frame: .zero, collectionViewLayout: createCollectionLayout())
}
}

View File

@ -53,4 +53,22 @@ open class ContainerTableViewCell<View: UIView>: BaseInitializableCell, WrappedV
open func createView() -> View {
return View()
}
// MARK: - Open methods
public func configureContainerTableViewCell(appearance: BaseWrappedViewHolderAppearance<some WrappedViewAppearance, some ViewLayout>) {
contentInsets = appearance.subviewAppearance.layout.insets
configureUIView(appearance: appearance)
}
}
// MARK: - AppearanceConfigurable
extension ContainerTableViewCell: AppearanceConfigurable where View: AppearanceConfigurable,
View.Appearance: WrappedViewAppearance {
public func configure(appearance: DefaultWrappedViewHolderAppearance<View.Appearance, UIView.NoLayout>) {
configureContainerTableViewCell(appearance: appearance)
wrappedView.configure(appearance: appearance.subviewAppearance)
}
}

View File

@ -0,0 +1,74 @@
//
// Copyright (c) 2023 Touch Instinct
//
// 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 TIUIKitCore
import UIKit
public final class ContainerView<View: UIView>: BaseInitializableView, WrappedViewHolder {
public var wrappedView = View()
public var contentInsets: UIEdgeInsets = .zero {
didSet {
contentEdgeConstraints?.update(from: contentInsets)
}
}
private var contentEdgeConstraints: EdgeConstraints?
// MARK: - InitializableView
public override func addViews() {
super.addViews()
addSubview(wrappedView)
}
public override func configureLayout() {
super.configureLayout()
contentEdgeConstraints = configureWrappedViewLayout()
}
}
// MARK: - ConfigurableView
extension ContainerView: ConfigurableView where View: ConfigurableView {
public func configure(with viewModel: View.ViewModelType) {
wrappedView.configure(with: viewModel)
}
}
// MARK: - AppearanceConfigurable
extension ContainerView: AppearanceConfigurable where View: AppearanceConfigurable,
View.Appearance: WrappedViewAppearance {
public typealias Appearance = UIView.DefaultWrappedViewHolderAppearance<View.Appearance, UIView.DefaultWrappedLayout>
public func configure(appearance: Appearance) {
wrappedView.configure(appearance: appearance.subviewAppearance)
configureUIView(appearance: appearance)
contentInsets = appearance.subviewAppearance.layout.insets
}
}

View File

@ -0,0 +1,27 @@
//
// Copyright (c) 2023 Touch Instinct
//
// 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 extension WrappableView {
typealias InContainerView = ContainerView<Self>
typealias InTableCell = ContainerTableViewCell<Self>
typealias InSeparatableTableCell = ContainerSeparatorTableViewCell<Self>
}

View File

@ -0,0 +1,27 @@
//
// Copyright (c) 2023 Touch Instinct
//
// 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 protocol WrappableView: UIView {}
extension UIView: WrappableView {}