diff --git a/Sources/Expandable.swift b/Sources/Expandable.swift index 6553e9c..e3cb3a1 100644 --- a/Sources/Expandable.swift +++ b/Sources/Expandable.swift @@ -40,7 +40,7 @@ extension Expandable where Self: UITableViewCell & ConfigurableCell { animationDuration: TimeInterval = .defaultExpandableAnimationDuration) { guard let viewModel = viewModel, - let stateIndex = viewModel.availableStates.index(where: { $0 == viewModel.expandableState }) else { + let stateIndex = viewModel.availableStates.firstIndex(where: { $0 == viewModel.expandableState }) else { return } diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 10e6390..cc99f16 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -347,15 +347,18 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath) } + @available(iOS, obsoleted: 11, message: "Use leadingSwipeActionsConfigurationForRowAt(:_) and trailingSwipeActionsConfigurationForRowAt(:_) instead") open func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { - return nil + return sections[indexPath.section].rows[indexPath.row].editingActions } + @available(iOS 11, *) open func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { return .init(actions: sections[indexPath.section].rows[indexPath.row].leadingContextualActions) } + @available(iOS 11, *) open func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { return .init(actions: sections[indexPath.section].rows[indexPath.row].trailingContextualActions) diff --git a/Sources/TableKit.swift b/Sources/TableKit.swift index 97b48ec..e3f25f4 100644 --- a/Sources/TableKit.swift +++ b/Sources/TableKit.swift @@ -38,9 +38,15 @@ public protocol RowConfigurable { public protocol RowActionable { + @available(iOS 11, *) var leadingContextualActions: [UIContextualAction] { get } + + @available(iOS 11, *) var trailingContextualActions: [UIContextualAction] { get } + @available(iOS, obsoleted: 11, message: "Use leadingContextualActions, trailingContextualActions instead") + var editingActions: [UITableViewRowAction]? { get } + func isEditingAllowed(forIndexPath indexPath: IndexPath) -> Bool func invoke( diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 467dfec..bf44619 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -21,12 +21,21 @@ import UIKit open class TableRow: Row where CellType: UITableViewCell { - + public let item: CellType.CellData private lazy var actions = [String: [TableRowAction]]() - private(set) open var leadingContextualActions: [UIContextualAction] = [] - private(set) open var trailingContextualActions: [UIContextualAction] = [] + open private(set) var editingActions: [UITableViewRowAction]? + + @available(iOS 11, *) + public var leadingContextualActions: [UIContextualAction] { + [] + } + + @available(iOS 11, *) + public var trailingContextualActions: [UIContextualAction] { + [] + } open var hashValue: Int { return ObjectIdentifier(self).hashValue @@ -52,14 +61,22 @@ open class TableRow: Row where CellType: UITableView return CellType.self } + @available(iOS, obsoleted: 11, message: "Use leadingContextualActions, trailingContextualActions instead") public init(item: CellType.CellData, actions: [TableRowAction]? = nil, - leadingContextualActions: [UIContextualAction] = [], - trailingContextualActions: [UIContextualAction] = []) { + editingActions: [UITableViewRowAction]? = nil) { + + self.item = item + self.editingActions = editingActions + + actions?.forEach { on($0) } + } + + @available(iOS 11, *) + public init(item: CellType.CellData, + actions: [TableRowAction]? = nil) { self.item = item - self.leadingContextualActions = leadingContextualActions - self.trailingContextualActions = trailingContextualActions actions?.forEach { on($0) } } @@ -88,10 +105,14 @@ open class TableRow: Row where CellType: UITableView if actions[TableRowActionType.canEdit.key] != nil { return invoke(action: .canEdit, cell: nil, path: indexPath) as? Bool ?? false } - - return !leadingContextualActions.isEmpty - || !trailingContextualActions.isEmpty - || actions[TableRowActionType.clickDelete.key] != nil + + if #available(iOS 11, *) { + return !leadingContextualActions.isEmpty + || !trailingContextualActions.isEmpty + || actions[TableRowActionType.clickDelete.key] != nil + } else { + return editingActions?.isEmpty == false || actions[TableRowActionType.clickDelete.key] != nil + } } // MARK: - actions -