Add backward capability to editingActions

This commit is contained in:
Vlad 2020-12-26 10:52:06 +03:00
parent 49b3f868f3
commit d87a23587e
4 changed files with 43 additions and 13 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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(

View File

@ -21,12 +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 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<CellType: ConfigurableCell>: Row where CellType: UITableView
return CellType.self
}
@available(iOS, obsoleted: 11, message: "Use leadingContextualActions, trailingContextualActions instead")
public init(item: CellType.CellData,
actions: [TableRowAction<CellType>]? = 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<CellType>]? = nil) {
self.item = item
self.leadingContextualActions = leadingContextualActions
self.trailingContextualActions = trailingContextualActions
actions?.forEach { on($0) }
}
@ -88,10 +105,14 @@ 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 !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 -