Compare commits
35 Commits
expandable
...
master
| Author | SHA1 | Date |
|---|---|---|
|
|
fec9537745 | |
|
|
246c0d06c0 | |
|
|
54bf141aff | |
|
|
01134b83b4 | |
|
|
3fefa09c3a | |
|
|
4886e43d20 | |
|
|
41826e18db | |
|
|
caec2dd10e | |
|
|
efc03d7c22 | |
|
|
94c40faa63 | |
|
|
d87a23587e | |
|
|
49b3f868f3 | |
|
|
37482d3b69 | |
|
|
2cc161f0c0 | |
|
|
1b4a988b35 | |
|
|
f8f2ca6852 | |
|
|
ff18a4d8b8 | |
|
|
d098691621 | |
|
|
7f349c6944 | |
|
|
a768352b47 | |
|
|
65cf31717b | |
|
|
9d10bc18bf | |
|
|
497b4e009c | |
|
|
6ef5ad504e | |
|
|
e22ec03990 | |
|
|
5982d5db3a | |
|
|
6eaf2cf3a2 | |
|
|
921e2b42b3 | |
|
|
70e6addcd0 | |
|
|
86f07b8e7c | |
|
|
1c92c14a1a | |
|
|
7416271076 | |
|
|
0812293813 | |
|
|
b62dea8702 | |
|
|
279fdd4854 |
|
|
@ -1 +1 @@
|
|||
5.0
|
||||
5.7
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
// swift-tools-version:5.0
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
// swift-tools-version:5.7
|
||||
|
||||
import PackageDescription
|
||||
|
||||
|
|
|
|||
|
|
@ -1,70 +1,96 @@
|
|||
import UIKit
|
||||
|
||||
public extension TimeInterval {
|
||||
|
||||
static let defaultExpandableAnimationDuration: TimeInterval = 0.3
|
||||
|
||||
}
|
||||
|
||||
public protocol Expandable {
|
||||
|
||||
|
||||
associatedtype ViewModelType: ExpandableCellViewModel
|
||||
|
||||
|
||||
var viewModel: ViewModelType? { get }
|
||||
|
||||
func configureAppearance(isCollapsed: Bool)
|
||||
|
||||
|
||||
func configure(state: ExpandableState)
|
||||
|
||||
}
|
||||
|
||||
extension Expandable where Self: UITableViewCell & ConfigurableCell {
|
||||
|
||||
|
||||
public func initState() {
|
||||
guard let viewModel = viewModel else {
|
||||
return
|
||||
}
|
||||
|
||||
changeState(isCollapsed: viewModel.isCollapsed)
|
||||
|
||||
changeState(expandableState: viewModel.expandableState)
|
||||
}
|
||||
|
||||
private func changeState(isCollapsed: Bool) {
|
||||
|
||||
private func changeState(expandableState: ExpandableState) {
|
||||
// layout to get right frames, frame of bottom subview can be used to get expanded height
|
||||
setNeedsLayout()
|
||||
layoutIfNeeded()
|
||||
|
||||
|
||||
// apply changes
|
||||
configureAppearance(isCollapsed: isCollapsed)
|
||||
configure(state: expandableState)
|
||||
layoutIfNeeded()
|
||||
}
|
||||
|
||||
|
||||
public func toggleState(animated: Bool = true,
|
||||
animationDuration: TimeInterval = 0.3) {
|
||||
|
||||
guard let tableView = tableView,
|
||||
let viewModel = viewModel else {
|
||||
animationDuration: TimeInterval = .defaultExpandableAnimationDuration) {
|
||||
|
||||
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,
|
||||
let viewModel = viewModel,
|
||||
viewModel.expandableState != state else {
|
||||
return
|
||||
}
|
||||
|
||||
let contentOffset = tableView.contentOffset
|
||||
|
||||
|
||||
if animated {
|
||||
UIView.animate(withDuration: animationDuration,
|
||||
animations: { [weak self] in
|
||||
self?.applyChanges(isCollapsed: !viewModel.isCollapsed)
|
||||
self?.applyChanges(expandableState: state)
|
||||
}, completion: { _ in
|
||||
viewModel.isCollapsed.toggle()
|
||||
viewModel.expandableState = state
|
||||
})
|
||||
} else {
|
||||
applyChanges(isCollapsed: !viewModel.isCollapsed)
|
||||
viewModel.isCollapsed.toggle()
|
||||
applyChanges(expandableState: state)
|
||||
viewModel.expandableState = state
|
||||
}
|
||||
|
||||
|
||||
tableView.beginUpdates()
|
||||
tableView.endUpdates()
|
||||
|
||||
|
||||
tableView.setContentOffset(contentOffset, animated: false)
|
||||
}
|
||||
|
||||
private func applyChanges(isCollapsed: Bool) {
|
||||
changeState(isCollapsed: isCollapsed)
|
||||
|
||||
|
||||
public func applyChanges(expandableState: ExpandableState) {
|
||||
changeState(expandableState: expandableState)
|
||||
|
||||
if let indexPath = indexPath,
|
||||
let tableDirector = (tableView?.delegate as? TableDirector),
|
||||
let cellHeightCalculator = tableDirector.rowHeightCalculator as? ExpandableCellHeightCalculator {
|
||||
cellHeightCalculator.updateCached(height: height(layoutType: Self.layoutType), for: indexPath)
|
||||
cellHeightCalculator.updateCached(height: expandableState.height ?? height(layoutType: Self.layoutType), for: indexPath)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,15 @@
|
|||
public protocol ExpandableCellViewModel: class {
|
||||
|
||||
var isCollapsed: Bool { get set }
|
||||
|
||||
public protocol ExpandableCellViewModel: AnyObject {
|
||||
|
||||
var expandableState: ExpandableState { get set }
|
||||
|
||||
var availableStates: [ExpandableState] { get }
|
||||
|
||||
}
|
||||
|
||||
public extension ExpandableCellViewModel {
|
||||
|
||||
var availableStates: [ExpandableState] {
|
||||
return [.collapsed, .expanded]
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -346,9 +346,25 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
|
|||
open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
||||
return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath)
|
||||
}
|
||||
|
||||
open func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
|
||||
return sections[indexPath.section].rows[indexPath.row].editingActions
|
||||
|
||||
open func tableView(_ tableView: UITableView,
|
||||
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
||||
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 {
|
||||
|
|
@ -391,36 +407,36 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
|
|||
extension TableDirector {
|
||||
|
||||
@discardableResult
|
||||
open func append(section: TableSection) -> Self {
|
||||
public func append(section: TableSection) -> Self {
|
||||
|
||||
append(sections: [section])
|
||||
return self
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func append(sections: [TableSection]) -> Self {
|
||||
|
||||
public func append(sections: [TableSection]) -> Self {
|
||||
|
||||
self.sections.append(contentsOf: sections)
|
||||
return self
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func append(rows: [Row]) -> Self {
|
||||
|
||||
public func append(rows: [Row]) -> Self {
|
||||
|
||||
append(section: TableSection(rows: rows))
|
||||
return self
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func insert(section: TableSection, atIndex index: Int) -> Self {
|
||||
|
||||
public func insert(section: TableSection, atIndex index: Int) -> Self {
|
||||
|
||||
sections.insert(section, at: index)
|
||||
return self
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func replaceSection(at index: Int, with section: TableSection) -> Self {
|
||||
|
||||
public func replaceSection(at index: Int, with section: TableSection) -> Self {
|
||||
|
||||
if index < sections.count {
|
||||
sections[index] = section
|
||||
}
|
||||
|
|
@ -428,20 +444,20 @@ extension TableDirector {
|
|||
}
|
||||
|
||||
@discardableResult
|
||||
open func delete(sectionAt index: Int) -> Self {
|
||||
|
||||
public func delete(sectionAt index: Int) -> Self {
|
||||
|
||||
sections.remove(at: index)
|
||||
return self
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func remove(sectionAt index: Int) -> Self {
|
||||
public func remove(sectionAt index: Int) -> Self {
|
||||
return delete(sectionAt: index)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func clear() -> Self {
|
||||
|
||||
public func clear() -> Self {
|
||||
|
||||
rowHeightCalculator?.invalidate()
|
||||
sections.removeAll()
|
||||
|
||||
|
|
@ -451,7 +467,7 @@ extension TableDirector {
|
|||
// MARK: - deprecated methods
|
||||
@available(*, deprecated, message: "Use 'delete(sectionAt:)' method instead")
|
||||
@discardableResult
|
||||
open func delete(index: Int) -> Self {
|
||||
public func delete(index: Int) -> Self {
|
||||
|
||||
sections.remove(at: index)
|
||||
return self
|
||||
|
|
|
|||
|
|
@ -37,8 +37,10 @@ public protocol RowConfigurable {
|
|||
}
|
||||
|
||||
public protocol RowActionable {
|
||||
|
||||
var editingActions: [UITableViewRowAction]? { get }
|
||||
var leadingContextualActions: [UIContextualAction] { get }
|
||||
var trailingContextualActions: [UIContextualAction] { get }
|
||||
var performsFirstActionWithFullSwipe: Bool { get }
|
||||
|
||||
func isEditingAllowed(forIndexPath indexPath: IndexPath) -> Bool
|
||||
|
||||
func invoke(
|
||||
|
|
|
|||
|
|
@ -21,10 +21,21 @@
|
|||
import UIKit
|
||||
|
||||
open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableViewCell {
|
||||
|
||||
|
||||
public let item: CellType.CellData
|
||||
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 {
|
||||
return ObjectIdentifier(self).hashValue
|
||||
|
|
@ -50,10 +61,11 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
|
|||
return CellType.self
|
||||
}
|
||||
|
||||
public init(item: CellType.CellData, actions: [TableRowAction<CellType>]? = nil, editingActions: [UITableViewRowAction]? = nil) {
|
||||
public init(item: CellType.CellData,
|
||||
actions: [TableRowAction<CellType>]? = nil) {
|
||||
|
||||
self.item = item
|
||||
self.editingActions = editingActions
|
||||
|
||||
actions?.forEach { on($0) }
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +93,10 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
|
|||
if actions[TableRowActionType.canEdit.key] != nil {
|
||||
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 -
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@ Pod::Spec.new do |s|
|
|||
s.name = 'TableKit'
|
||||
s.module_name = 'TableKit'
|
||||
|
||||
s.version = '2.11.0'
|
||||
s.version = '2.12'
|
||||
|
||||
s.homepage = 'https://github.com/maxsokolov/TableKit'
|
||||
s.homepage = 'https://git.svc.touchin.ru/TouchInstinct/TableKit'
|
||||
s.summary = 'Type-safe declarative table views with Swift.'
|
||||
|
||||
s.author = { 'Max Sokolov' => 'i@maxsokolov.net' }
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
s.platforms = { :ios => '8.0' }
|
||||
s.ios.deployment_target = '8.0'
|
||||
s.platforms = { :ios => '12.0' }
|
||||
s.ios.deployment_target = '12.0'
|
||||
|
||||
s.source_files = 'Sources/*.swift'
|
||||
s.source = { :git => 'https://github.com/maxsokolov/TableKit.git', :tag => s.version }
|
||||
s.source = { :git => 'https://git.svc.touchin.ru/TouchInstinct/TableKit.git', :tag => s.version }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
objects = {
|
||||
|
||||
/* 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 */; };
|
||||
3201E78621BE9E25001DF9E7 /* UITableViewCell+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78521BE9E25001DF9E7 /* UITableViewCell+Extensions.swift */; };
|
||||
3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78721BE9EB2001DF9E7 /* Expandable.swift */; };
|
||||
|
|
@ -37,6 +38,7 @@
|
|||
/* End PBXContainerItemProxy 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>"; };
|
||||
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>"; };
|
||||
|
|
@ -115,6 +117,7 @@
|
|||
3201E78721BE9EB2001DF9E7 /* Expandable.swift */,
|
||||
3201E78921BE9ED4001DF9E7 /* ExpandableCellViewModel.swift */,
|
||||
32BDFE9E21C167F400D0BBB4 /* LayoutType.swift */,
|
||||
2CBFA2F421F692F100147B56 /* ExpandableState.swift */,
|
||||
);
|
||||
path = Sources;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -251,6 +254,7 @@
|
|||
DA9EA7AF1D0EC2C90021F650 /* ConfigurableCell.swift in Sources */,
|
||||
DA9EA7B31D0EC2C90021F650 /* TableDirector.swift in Sources */,
|
||||
3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */,
|
||||
2CBFA2F521F692F100147B56 /* ExpandableState.swift in Sources */,
|
||||
DA9EA7B71D0EC2C90021F650 /* TableSection.swift in Sources */,
|
||||
DA9EA7B01D0EC2C90021F650 /* TablePrototypeCellHeightCalculator.swift in Sources */,
|
||||
3201E78421BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift in Sources */,
|
||||
|
|
|
|||
Loading…
Reference in New Issue