From ee25ca777a98fc61255152b9f15c86bb49b2b9c1 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 27 Dec 2020 09:50:23 +0300 Subject: [PATCH 1/6] Replace UITableViewRowAction with UIContextualAction --- Sources/TableDirector.swift | 30 +++++++++++++++++++++++++--- Sources/TableKit.swift | 13 ++++++++++++- Sources/TableRow.swift | 39 ++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 2ffc99d..13015e8 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -343,6 +343,8 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { } // MARK: - Row editing + + @available(iOS, deprecated: 11, message: "Use leadingSwipeActionsConfigurationForRowAt(:_) and trailingSwipeActionsConfigurationForRowAt(:_) instead") open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath) } @@ -351,6 +353,28 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { return sections[indexPath.section].rows[indexPath.row].editingActions } + @available(iOS 11, *) + 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 + } + + @available(iOS 11, *) + 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 { if invoke(action: .canDelete, cell: tableView.cellForRow(at: indexPath), indexPath: indexPath) as? Bool ?? false { return UITableViewCell.EditingStyle.delete @@ -380,10 +404,10 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { open func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { invoke(action: .move, cell: tableView.cellForRow(at: sourceIndexPath), indexPath: sourceIndexPath, userInfo: [TableKitUserInfoKeys.CellMoveDestinationIndexPath: destinationIndexPath]) } - + open func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { - let cell = tableView.cellForRow(at: indexPath) - invoke(action: .accessoryButtonTap, cell: cell, indexPath: indexPath) + let cell = tableView.cellForRow(at: indexPath) + invoke(action: .accessoryButtonTap, cell: cell, indexPath: indexPath) } } diff --git a/Sources/TableKit.swift b/Sources/TableKit.swift index 526b378..fe5e0e8 100644 --- a/Sources/TableKit.swift +++ b/Sources/TableKit.swift @@ -36,8 +36,19 @@ public protocol RowConfigurable { } public protocol RowActionable { - + + @available(iOS 11, *) + var leadingContextualActions: [UIContextualAction] { get } + + @available(iOS 11, *) + var trailingContextualActions: [UIContextualAction] { get } + + @available(iOS 11, *) + var performsFirstActionWithFullSwipe: Bool { get } + + @available(iOS, deprecated: 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 b04382d..7a95cc9 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -24,7 +24,24 @@ open class TableRow: Row where CellType: UITableView public let item: CellType.CellData private lazy var actions = [String: [TableRowAction]]() - private(set) open var editingActions: [UITableViewRowAction]? + + @available(iOS, deprecated: 11, message: "Use leadingContextualActions, trailingContextualActions instead") + open private(set) var editingActions: [UITableViewRowAction]? + + @available(iOS 11, *) + open var leadingContextualActions: [UIContextualAction] { + [] + } + + @available(iOS 11, *) + open var trailingContextualActions: [UIContextualAction] { + [] + } + + @available(iOS 11, *) + open var performsFirstActionWithFullSwipe: Bool { + false + } open var hashValue: Int { return ObjectIdentifier(self).hashValue @@ -46,13 +63,22 @@ open class TableRow: Row where CellType: UITableView return CellType.self } + @available(iOS, deprecated: 11, message: "Use leadingContextualActions, trailingContextualActions instead") public init(item: CellType.CellData, actions: [TableRowAction]? = nil, 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 + + actions?.forEach { on($0) } + } + // MARK: - RowConfigurable - open func configure(_ cell: UITableViewCell) { @@ -77,7 +103,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 editingActions?.isEmpty == false || 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 - From 87dd391975382393adc0336681577f947198185c Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 3 Jan 2021 11:24:58 +0300 Subject: [PATCH 2/6] Replace UITableViewRowAction with UIContextualAction --- Sources/TableDirector.swift | 8 ++++++-- Sources/TableRow.swift | 7 +++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 13015e8..43d6861 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -343,8 +343,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { } // MARK: - Row editing - - @available(iOS, deprecated: 11, message: "Use leadingSwipeActionsConfigurationForRowAt(:_) and trailingSwipeActionsConfigurationForRowAt(:_) instead") + open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath) } @@ -367,6 +366,11 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { @available(iOS 11, *) open func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + guard let editingActions = sections[indexPath.section].rows[indexPath.row].editingActions, + editingActions.isEmpty else { + return nil + } + let currentRow = sections[indexPath.section].rows[indexPath.row] let configuration = UISwipeActionsConfiguration(actions: currentRow.trailingContextualActions) diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 7a95cc9..8096002 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -63,7 +63,7 @@ open class TableRow: Row where CellType: UITableView return CellType.self } - @available(iOS, deprecated: 11, message: "Use leadingContextualActions, trailingContextualActions instead") + @available(iOS, deprecated: 11, message: "Use init(item:_, actions:_) instead") public init(item: CellType.CellData, actions: [TableRowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { self.item = item @@ -71,11 +71,9 @@ open class TableRow: Row where CellType: UITableView actions?.forEach { on($0) } } - @available(iOS 11, *) public init(item: CellType.CellData, actions: [TableRowAction]? = nil) { self.item = item - actions?.forEach { on($0) } } @@ -105,7 +103,8 @@ open class TableRow: Row where CellType: UITableView } if #available(iOS 11, *) { - return !leadingContextualActions.isEmpty + return editingActions?.isEmpty == false + || !leadingContextualActions.isEmpty || !trailingContextualActions.isEmpty || actions[TableRowActionType.clickDelete.key] != nil } else { From cbc079a833d0ca1847b9a98b6b1704f015490532 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 3 Jan 2021 11:25:17 +0300 Subject: [PATCH 3/6] Up IPHONEOS_DEPLOYMENT_TARGET to 9.0 --- Demo/TableKitDemo.xcodeproj/project.pbxproj | 8 ++++---- Package.swift | 8 ++++++-- TableKit.podspec | 4 ++-- TableKit.xcodeproj/project.pbxproj | 8 ++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Demo/TableKitDemo.xcodeproj/project.pbxproj b/Demo/TableKitDemo.xcodeproj/project.pbxproj index eef710e..81999da 100644 --- a/Demo/TableKitDemo.xcodeproj/project.pbxproj +++ b/Demo/TableKitDemo.xcodeproj/project.pbxproj @@ -373,7 +373,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -422,7 +422,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; @@ -439,7 +439,7 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = Resources/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.tablekit.demo; PRODUCT_NAME = TableKitDemo; @@ -456,7 +456,7 @@ CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = Resources/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.tablekit.demo; PRODUCT_NAME = TableKitDemo; diff --git a/Package.swift b/Package.swift index 5ce7c3d..0b5fafd 100644 --- a/Package.swift +++ b/Package.swift @@ -5,13 +5,17 @@ import PackageDescription let package = Package( name: "TableKit", - + + platforms: [ + .iOS(.v9) + ], + products: [ .library( name: "TableKit", targets: ["TableKit"]), ], - + targets: [ .target( name: "TableKit", diff --git a/TableKit.podspec b/TableKit.podspec index 2db6237..5dbb70f 100644 --- a/TableKit.podspec +++ b/TableKit.podspec @@ -9,8 +9,8 @@ Pod::Spec.new do |s| 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 => '9.0' } + s.ios.deployment_target = '9.0' s.source_files = 'Sources/*.swift' s.source = { :git => 'https://github.com/maxsokolov/TableKit.git', :tag => s.version } diff --git a/TableKit.xcodeproj/project.pbxproj b/TableKit.xcodeproj/project.pbxproj index d20f8ba..0a12164 100644 --- a/TableKit.xcodeproj/project.pbxproj +++ b/TableKit.xcodeproj/project.pbxproj @@ -311,7 +311,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -365,7 +365,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; @@ -388,7 +388,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "$(SRCROOT)/Configs/TableKit.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.tablekit.TableKit; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -409,7 +409,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "$(SRCROOT)/Configs/TableKit.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.tablekit.TableKit; PRODUCT_NAME = "$(TARGET_NAME)"; From 457cee574dd0cf31ef6683bd054538a23b718085 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 3 Jan 2021 11:25:25 +0300 Subject: [PATCH 4/6] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 222e8ec..7754794 100755 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ playground.xcworkspace # Swift Package Manager .build/ +.swiftpm/ # Carthage Carthage/Build From 8a3561b564502961af67e50321f4e4229ea93c34 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 3 Jan 2021 11:36:28 +0300 Subject: [PATCH 5/6] Code correction --- Package.swift | 2 +- Sources/TableDirector.swift | 11 ++++------- Sources/TableKit.swift | 1 - 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Package.swift b/Package.swift index 0b5fafd..d5b2d0f 100644 --- a/Package.swift +++ b/Package.swift @@ -15,7 +15,7 @@ let package = Package( name: "TableKit", targets: ["TableKit"]), ], - + targets: [ .target( name: "TableKit", diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 43d6861..bdf9851 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -353,8 +353,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { } @available(iOS 11, *) - open func tableView(_ tableView: UITableView, - leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + open func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let currentRow = sections[indexPath.section].rows[indexPath.row] let configuration = UISwipeActionsConfiguration(actions: currentRow.leadingContextualActions) @@ -364,10 +363,8 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { } @available(iOS 11, *) - open func tableView(_ tableView: UITableView, - trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { - guard let editingActions = sections[indexPath.section].rows[indexPath.row].editingActions, - editingActions.isEmpty else { + open func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + guard sections[indexPath.section].rows[indexPath.row].editingActions?.isEmpty ?? true else { return nil } @@ -408,7 +405,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { open func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { invoke(action: .move, cell: tableView.cellForRow(at: sourceIndexPath), indexPath: sourceIndexPath, userInfo: [TableKitUserInfoKeys.CellMoveDestinationIndexPath: destinationIndexPath]) } - + open func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) invoke(action: .accessoryButtonTap, cell: cell, indexPath: indexPath) diff --git a/Sources/TableKit.swift b/Sources/TableKit.swift index fe5e0e8..8f8ff2e 100644 --- a/Sources/TableKit.swift +++ b/Sources/TableKit.swift @@ -46,7 +46,6 @@ public protocol RowActionable { @available(iOS 11, *) var performsFirstActionWithFullSwipe: Bool { get } - @available(iOS, deprecated: 11, message: "Use leadingContextualActions, trailingContextualActions instead") var editingActions: [UITableViewRowAction]? { get } func isEditingAllowed(forIndexPath indexPath: IndexPath) -> Bool From 979c0f655e0c51714f085ca64773c6d4380dc918 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 3 Jan 2021 11:40:08 +0300 Subject: [PATCH 6/6] Update error message --- Sources/TableRow.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 8096002..76ffee3 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -63,7 +63,7 @@ open class TableRow: Row where CellType: UITableView return CellType.self } - @available(iOS, deprecated: 11, message: "Use init(item:_, actions:_) instead") + @available(iOS, deprecated: 11, message: "Use init(item:_, actions:_) with leadingContextualActions, trailingContextualActions instead") public init(item: CellType.CellData, actions: [TableRowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { self.item = item