Compare commits

..

2 Commits

Author SHA1 Message Date
Timur Kayumov c27fd5ef07
Merge branch 'maxsokolov:master' into expandable 2022-01-23 20:22:23 +02:00
Ivan Zinovyev 6f565376a8 Add Expandable protocol and ExpandableCellHeightCalculator 2019-01-10 14:09:19 +03:00
10 changed files with 66 additions and 179 deletions

View File

@ -1 +1 @@
5.7 5.0

View File

@ -1,4 +1,5 @@
// swift-tools-version:5.7 // swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription

View File

@ -1,96 +1,70 @@
import UIKit import UIKit
public extension TimeInterval {
static let defaultExpandableAnimationDuration: TimeInterval = 0.3
}
public protocol Expandable { public protocol Expandable {
associatedtype ViewModelType: ExpandableCellViewModel associatedtype ViewModelType: ExpandableCellViewModel
var viewModel: ViewModelType? { get } var viewModel: ViewModelType? { get }
func configure(state: ExpandableState) func configureAppearance(isCollapsed: Bool)
} }
extension Expandable where Self: UITableViewCell & ConfigurableCell { extension Expandable where Self: UITableViewCell & ConfigurableCell {
public func initState() { public func initState() {
guard let viewModel = viewModel else { guard let viewModel = viewModel else {
return return
} }
changeState(expandableState: viewModel.expandableState) changeState(isCollapsed: viewModel.isCollapsed)
} }
private func changeState(expandableState: ExpandableState) { private func changeState(isCollapsed: Bool) {
// layout to get right frames, frame of bottom subview can be used to get expanded height // layout to get right frames, frame of bottom subview can be used to get expanded height
setNeedsLayout()
layoutIfNeeded() layoutIfNeeded()
// apply changes // apply changes
configure(state: expandableState) configureAppearance(isCollapsed: isCollapsed)
layoutIfNeeded() layoutIfNeeded()
} }
public func toggleState(animated: Bool = true, public func toggleState(animated: Bool = true,
animationDuration: TimeInterval = .defaultExpandableAnimationDuration) { animationDuration: TimeInterval = 0.3) {
guard let viewModel = viewModel,
let stateIndex = viewModel.availableStates.firstIndex(where: { $0 == viewModel.expandableState }) else {
return
}
let targetState = stateIndex == viewModel.availableStates.count - 1
? viewModel.availableStates[0]
: viewModel.availableStates[stateIndex + 1]
transition(to: targetState,
animated: animated,
animationDuration: animationDuration)
}
public func transition(to state: ExpandableState,
animated: Bool = true,
animationDuration: TimeInterval = .defaultExpandableAnimationDuration) {
guard let tableView = tableView, guard let tableView = tableView,
let viewModel = viewModel, let viewModel = viewModel else {
viewModel.expandableState != state else {
return return
} }
let contentOffset = tableView.contentOffset let contentOffset = tableView.contentOffset
if animated { if animated {
UIView.animate(withDuration: animationDuration, UIView.animate(withDuration: animationDuration,
animations: { [weak self] in animations: { [weak self] in
self?.applyChanges(expandableState: state) self?.applyChanges(isCollapsed: !viewModel.isCollapsed)
}, completion: { _ in }, completion: { _ in
viewModel.expandableState = state viewModel.isCollapsed.toggle()
}) })
} else { } else {
applyChanges(expandableState: state) applyChanges(isCollapsed: !viewModel.isCollapsed)
viewModel.expandableState = state viewModel.isCollapsed.toggle()
} }
tableView.beginUpdates() tableView.beginUpdates()
tableView.endUpdates() tableView.endUpdates()
tableView.setContentOffset(contentOffset, animated: false) tableView.setContentOffset(contentOffset, animated: false)
} }
public func applyChanges(expandableState: ExpandableState) { private func applyChanges(isCollapsed: Bool) {
changeState(expandableState: expandableState) changeState(isCollapsed: isCollapsed)
if let indexPath = indexPath, if let indexPath = indexPath,
let tableDirector = (tableView?.delegate as? TableDirector), let tableDirector = (tableView?.delegate as? TableDirector),
let cellHeightCalculator = tableDirector.rowHeightCalculator as? ExpandableCellHeightCalculator { let cellHeightCalculator = tableDirector.rowHeightCalculator as? ExpandableCellHeightCalculator {
cellHeightCalculator.updateCached(height: expandableState.height ?? height(layoutType: Self.layoutType), for: indexPath) cellHeightCalculator.updateCached(height: height(layoutType: Self.layoutType), for: indexPath)
} }
} }
} }

View File

@ -1,15 +1,5 @@
public protocol ExpandableCellViewModel: AnyObject { public protocol ExpandableCellViewModel: class {
var expandableState: ExpandableState { get set } var isCollapsed: Bool { get set }
var availableStates: [ExpandableState] { get }
}
public extension ExpandableCellViewModel {
var availableStates: [ExpandableState] {
return [.collapsed, .expanded]
}
} }

View File

@ -1,41 +0,0 @@
import UIKit
public enum ExpandableState {
case collapsed
case expanded
case height(value: CGFloat)
}
extension ExpandableState: Equatable { }
extension ExpandableState {
public var isCollapsed: Bool {
guard case .collapsed = self else {
return false
}
return true
}
public var isExpanded: Bool {
guard case .expanded = self else {
return false
}
return true
}
public var height: CGFloat? {
guard case let .height(value: height) = self else {
return nil
}
return height
}
}

View File

@ -346,25 +346,9 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath) return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath)
} }
open func tableView(_ tableView: UITableView, open func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { return sections[indexPath.section].rows[indexPath.row].editingActions
let currentRow = sections[indexPath.section].rows[indexPath.row]
let configuration = UISwipeActionsConfiguration(actions: currentRow.leadingContextualActions)
configuration.performsFirstActionWithFullSwipe = currentRow.performsFirstActionWithFullSwipe
return configuration
}
open func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let currentRow = sections[indexPath.section].rows[indexPath.row]
let configuration = UISwipeActionsConfiguration(actions: currentRow.trailingContextualActions)
configuration.performsFirstActionWithFullSwipe = currentRow.performsFirstActionWithFullSwipe
return configuration
} }
open func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { open func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
@ -407,36 +391,36 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
extension TableDirector { extension TableDirector {
@discardableResult @discardableResult
public func append(section: TableSection) -> Self { open func append(section: TableSection) -> Self {
append(sections: [section]) append(sections: [section])
return self return self
} }
@discardableResult @discardableResult
public func append(sections: [TableSection]) -> Self { open func append(sections: [TableSection]) -> Self {
self.sections.append(contentsOf: sections) self.sections.append(contentsOf: sections)
return self return self
} }
@discardableResult @discardableResult
public func append(rows: [Row]) -> Self { open func append(rows: [Row]) -> Self {
append(section: TableSection(rows: rows)) append(section: TableSection(rows: rows))
return self return self
} }
@discardableResult @discardableResult
public func insert(section: TableSection, atIndex index: Int) -> Self { open func insert(section: TableSection, atIndex index: Int) -> Self {
sections.insert(section, at: index) sections.insert(section, at: index)
return self return self
} }
@discardableResult @discardableResult
public func replaceSection(at index: Int, with section: TableSection) -> Self { open func replaceSection(at index: Int, with section: TableSection) -> Self {
if index < sections.count { if index < sections.count {
sections[index] = section sections[index] = section
} }
@ -444,20 +428,20 @@ extension TableDirector {
} }
@discardableResult @discardableResult
public func delete(sectionAt index: Int) -> Self { open func delete(sectionAt index: Int) -> Self {
sections.remove(at: index) sections.remove(at: index)
return self return self
} }
@discardableResult @discardableResult
public func remove(sectionAt index: Int) -> Self { open func remove(sectionAt index: Int) -> Self {
return delete(sectionAt: index) return delete(sectionAt: index)
} }
@discardableResult @discardableResult
public func clear() -> Self { open func clear() -> Self {
rowHeightCalculator?.invalidate() rowHeightCalculator?.invalidate()
sections.removeAll() sections.removeAll()
@ -467,7 +451,7 @@ extension TableDirector {
// MARK: - deprecated methods // MARK: - deprecated methods
@available(*, deprecated, message: "Use 'delete(sectionAt:)' method instead") @available(*, deprecated, message: "Use 'delete(sectionAt:)' method instead")
@discardableResult @discardableResult
public func delete(index: Int) -> Self { open func delete(index: Int) -> Self {
sections.remove(at: index) sections.remove(at: index)
return self return self

View File

@ -37,10 +37,8 @@ public protocol RowConfigurable {
} }
public protocol RowActionable { public protocol RowActionable {
var leadingContextualActions: [UIContextualAction] { get }
var trailingContextualActions: [UIContextualAction] { get } var editingActions: [UITableViewRowAction]? { get }
var performsFirstActionWithFullSwipe: Bool { get }
func isEditingAllowed(forIndexPath indexPath: IndexPath) -> Bool func isEditingAllowed(forIndexPath indexPath: IndexPath) -> Bool
func invoke( func invoke(

View File

@ -21,21 +21,10 @@
import UIKit import UIKit
open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableViewCell { open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableViewCell {
public let item: CellType.CellData public let item: CellType.CellData
private lazy var actions = [String: [TableRowAction<CellType>]]() private lazy var actions = [String: [TableRowAction<CellType>]]()
private(set) open var editingActions: [UITableViewRowAction]?
open var leadingContextualActions: [UIContextualAction] {
[]
}
open var trailingContextualActions: [UIContextualAction] {
[]
}
open var performsFirstActionWithFullSwipe: Bool {
false
}
open var hashValue: Int { open var hashValue: Int {
return ObjectIdentifier(self).hashValue return ObjectIdentifier(self).hashValue
@ -61,11 +50,10 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
return CellType.self return CellType.self
} }
public init(item: CellType.CellData, public init(item: CellType.CellData, actions: [TableRowAction<CellType>]? = nil, editingActions: [UITableViewRowAction]? = nil) {
actions: [TableRowAction<CellType>]? = nil) {
self.item = item self.item = item
self.editingActions = editingActions
actions?.forEach { on($0) } actions?.forEach { on($0) }
} }
@ -93,10 +81,7 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
if actions[TableRowActionType.canEdit.key] != nil { if actions[TableRowActionType.canEdit.key] != nil {
return invoke(action: .canEdit, cell: nil, path: indexPath) as? Bool ?? false return invoke(action: .canEdit, cell: nil, path: indexPath) as? Bool ?? false
} }
return editingActions?.isEmpty == false || actions[TableRowActionType.clickDelete.key] != nil
return !leadingContextualActions.isEmpty
|| !trailingContextualActions.isEmpty
|| actions[TableRowActionType.clickDelete.key] != nil
} }
// MARK: - actions - // MARK: - actions -

View File

@ -2,16 +2,16 @@ Pod::Spec.new do |s|
s.name = 'TableKit' s.name = 'TableKit'
s.module_name = 'TableKit' s.module_name = 'TableKit'
s.version = '2.12' s.version = '2.11.0'
s.homepage = 'https://git.svc.touchin.ru/TouchInstinct/TableKit' s.homepage = 'https://github.com/maxsokolov/TableKit'
s.summary = 'Type-safe declarative table views with Swift.' s.summary = 'Type-safe declarative table views with Swift.'
s.author = { 'Max Sokolov' => 'i@maxsokolov.net' } s.author = { 'Max Sokolov' => 'i@maxsokolov.net' }
s.license = { :type => 'MIT', :file => 'LICENSE' } s.license = { :type => 'MIT', :file => 'LICENSE' }
s.platforms = { :ios => '12.0' } s.platforms = { :ios => '8.0' }
s.ios.deployment_target = '12.0' s.ios.deployment_target = '8.0'
s.source_files = 'Sources/*.swift' s.source_files = 'Sources/*.swift'
s.source = { :git => 'https://git.svc.touchin.ru/TouchInstinct/TableKit.git', :tag => s.version } s.source = { :git => 'https://github.com/maxsokolov/TableKit.git', :tag => s.version }
end end

View File

@ -7,7 +7,6 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
2CBFA2F521F692F100147B56 /* ExpandableState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CBFA2F421F692F100147B56 /* ExpandableState.swift */; };
3201E78421BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78321BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift */; }; 3201E78421BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78321BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift */; };
3201E78621BE9E25001DF9E7 /* UITableViewCell+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78521BE9E25001DF9E7 /* UITableViewCell+Extensions.swift */; }; 3201E78621BE9E25001DF9E7 /* UITableViewCell+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78521BE9E25001DF9E7 /* UITableViewCell+Extensions.swift */; };
3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78721BE9EB2001DF9E7 /* Expandable.swift */; }; 3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78721BE9EB2001DF9E7 /* Expandable.swift */; };
@ -38,7 +37,6 @@
/* End PBXContainerItemProxy section */ /* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
2CBFA2F421F692F100147B56 /* ExpandableState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandableState.swift; sourceTree = "<group>"; };
3201E78321BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandableCellHeightCalculator.swift; sourceTree = "<group>"; }; 3201E78321BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandableCellHeightCalculator.swift; sourceTree = "<group>"; };
3201E78521BE9E25001DF9E7 /* UITableViewCell+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+Extensions.swift"; sourceTree = "<group>"; }; 3201E78521BE9E25001DF9E7 /* UITableViewCell+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+Extensions.swift"; sourceTree = "<group>"; };
3201E78721BE9EB2001DF9E7 /* Expandable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Expandable.swift; sourceTree = "<group>"; }; 3201E78721BE9EB2001DF9E7 /* Expandable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Expandable.swift; sourceTree = "<group>"; };
@ -117,7 +115,6 @@
3201E78721BE9EB2001DF9E7 /* Expandable.swift */, 3201E78721BE9EB2001DF9E7 /* Expandable.swift */,
3201E78921BE9ED4001DF9E7 /* ExpandableCellViewModel.swift */, 3201E78921BE9ED4001DF9E7 /* ExpandableCellViewModel.swift */,
32BDFE9E21C167F400D0BBB4 /* LayoutType.swift */, 32BDFE9E21C167F400D0BBB4 /* LayoutType.swift */,
2CBFA2F421F692F100147B56 /* ExpandableState.swift */,
); );
path = Sources; path = Sources;
sourceTree = "<group>"; sourceTree = "<group>";
@ -254,7 +251,6 @@
DA9EA7AF1D0EC2C90021F650 /* ConfigurableCell.swift in Sources */, DA9EA7AF1D0EC2C90021F650 /* ConfigurableCell.swift in Sources */,
DA9EA7B31D0EC2C90021F650 /* TableDirector.swift in Sources */, DA9EA7B31D0EC2C90021F650 /* TableDirector.swift in Sources */,
3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */, 3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */,
2CBFA2F521F692F100147B56 /* ExpandableState.swift in Sources */,
DA9EA7B71D0EC2C90021F650 /* TableSection.swift in Sources */, DA9EA7B71D0EC2C90021F650 /* TableSection.swift in Sources */,
DA9EA7B01D0EC2C90021F650 /* TablePrototypeCellHeightCalculator.swift in Sources */, DA9EA7B01D0EC2C90021F650 /* TablePrototypeCellHeightCalculator.swift in Sources */,
3201E78421BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift in Sources */, 3201E78421BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift in Sources */,