Skip to content

Commit

Permalink
Fixed issues based upon feedback from xmartlabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Betschart authored and Martin Barreto committed Dec 19, 2017
1 parent bc46e05 commit 9ba30a7
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 103 deletions.
118 changes: 64 additions & 54 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1761,60 +1761,70 @@ class MultivaluedOnlyDeleteController: FormViewController {

class SwipeActionsController: FormViewController {

override func viewDidLoad() {
super.viewDidLoad()

form +++ Section(footer: "Eureka sets table.isEditing = false for SwipeActions.\n\nMultivaluedSections need table.isEditing = true, therefore both can't be used on the same view.")
<<< LabelRow("Actions Right: iOS >= 7"){
$0.title = $0.tag

$0.trailingSwipeConfiguration = SwipeConfiguration(){
let moreAction = SwipeAction(style: .normal, title: "More", handler: { (action, path, completionHandler) in
print("More")
completionHandler?(true)
})

let deleteAction = SwipeAction(style: .destructive, title: "Delete", handler: { (action, path, completionHandler) in
print("Delete")
completionHandler?(true)
})

$0.actions = [deleteAction,moreAction]
}
}

<<< LabelRow("Actions Left & Right: iOS >= 11"){
$0.title = $0.tag

$0.trailingSwipeConfiguration = SwipeConfiguration(){
let moreAction = SwipeAction(style: .normal, title: "More", handler: { (action, path, completionHandler) in
print("More")
completionHandler?(true)
})

let deleteAction = SwipeAction(style: .destructive, title: "Delete", handler: { (action, path, completionHandler) in
print("Delete")
completionHandler?(true)
})

$0.performsFirstActionWithFullSwipe = true
$0.actions = [deleteAction,moreAction]
}

if #available(iOS 11,*){
$0.leadingSwipeConfiguration = SwipeConfiguration(){
let infoAction = SwipeAction(style: .normal, title: "Info", handler: { (action, path, completionHandler) in
print("Info")
completionHandler?(true)
})
infoAction.backgroundColor = .blue

$0.performsFirstActionWithFullSwipe = true
$0.actions = [infoAction]
}
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

form +++ Section(footer: "Eureka sets table.isEditing = false for SwipeActions.\n\nMultivaluedSections need table.isEditing = true, therefore both can't be used on the same view.")
<<< LabelRow("Actions Right: iOS >= 7") {
$0.title = $0.tag

$0.trailingSwipeConfiguration = SwipeConfiguration() {
let moreAction = SwipeAction(style: .normal, title: "More", handler: { (action, source, completionHandler) in
print("More")
completionHandler?(true)
})

let deleteAction = SwipeAction(style: .destructive, title: "Delete", handler: { (action, source, completionHandler) in
print("Delete")
completionHandler?(true)
})

$0.actions = [deleteAction,moreAction]
}
}

<<< LabelRow("Actions Left & Right: iOS >= 11") {
$0.title = $0.tag

$0.trailingSwipeConfiguration = SwipeConfiguration() {
let moreAction = SwipeAction(style: .normal, title: "More", handler: { (action, source, completionHandler) in
print("More")
completionHandler?(true)
})

let deleteAction = SwipeAction(style: .destructive, title: "Delete", handler: { (action, source, completionHandler) in
print("Delete")
completionHandler?(true)
})

$0.performsFirstActionWithFullSwipe = true
$0.actions = [deleteAction,moreAction]
}

if #available(iOS 11,*) {
$0.leadingSwipeConfiguration = SwipeConfiguration() {
let infoAction = SwipeAction(style: .normal, title: "Info", handler: { (action, source, completionHandler) in
print("Info")
completionHandler?(true)
})
infoAction.backgroundColor = .blue

$0.performsFirstActionWithFullSwipe = true
$0.actions = [infoAction]
}
}
}
}
}








//MARK: Plain Table View Style View Example

class PlainTableViewStyleController : FormViewController {
Expand Down
22 changes: 11 additions & 11 deletions Source/Core/BaseRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ open class BaseRow: BaseRowType {

/// The section to which this row belongs.
open weak var section: Section?

public var trailingSwipeConfiguration: SwipeConfiguration?

//needs the accessor because if marked directly this throws "Stored properties cannot be marked potentially unavailable with '@available'"
private var _leadingSwipeConfiguration: SwipeConfiguration?

@available(iOS 11,*)
public var leadingSwipeConfiguration: SwipeConfiguration?{
get{ return self._leadingSwipeConfiguration }
set{ self._leadingSwipeConfiguration = newValue }
}
public var trailingSwipeConfiguration: SwipeConfiguration?
//needs the accessor because if marked directly this throws "Stored properties cannot be marked potentially unavailable with '@available'"
private var _leadingSwipeConfiguration: SwipeConfiguration?
@available(iOS 11,*)
public var leadingSwipeConfiguration: SwipeConfiguration?{
get{ return self._leadingSwipeConfiguration }
set{ self._leadingSwipeConfiguration = newValue }
}

public required init(tag: String? = nil) {
self.tag = tag
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -929,16 +929,16 @@ extension FormViewController : UITableViewDelegate {

@available(iOS 11,*)
public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return form[indexPath].leadingSwipeConfiguration?.platformValue as? UISwipeActionsConfiguration
return form[indexPath].leadingSwipeConfiguration?.contextualConfiguration as? UISwipeActionsConfiguration
}

@available(iOS 11,*)
public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return form[indexPath].trailingSwipeConfiguration?.platformValue as? UISwipeActionsConfiguration
return form[indexPath].trailingSwipeConfiguration?.contextualConfiguration as? UISwipeActionsConfiguration
}

public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?{
return form[indexPath].trailingSwipeConfiguration?.platformActions as? [UITableViewRowAction]
return form[indexPath].trailingSwipeConfiguration?.contextualActions as? [UITableViewRowAction]
}
}

Expand Down
104 changes: 69 additions & 35 deletions Source/Core/SwipeActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,51 @@

import Foundation

public typealias SwipeActionHandler = (Any, Any, ((Bool) -> Void)?) -> Void

public typealias SwipeActionHandler = (ContextualAction, ContextualActionSource, ((Bool) -> Void)?) -> Void

public class SwipeAction{
public let contextualAction: ContextualAction

public let platformValue: Any

@available(iOS 11, *)
public var backgroundColor: UIColor?{
didSet{
if #available(iOS 11, *), let platformValue = self.platformValue as? UIContextualAction{
platformValue.backgroundColor = self.backgroundColor
}
get{
guard let contextualAction = self.contextualAction as? UIContextualAction else{ return nil }
return contextualAction.backgroundColor
}
set{
guard let contextualAction = self.contextualAction as? UIContextualAction else{ return }
contextualAction.backgroundColor = newValue
}
}

@available(iOS 11, *)
public var image: UIImage?{
didSet{
if #available(iOS 11, *), let platformValue = self.platformValue as? UIContextualAction{
platformValue.image = self.image
}
get{
guard let contextualAction = self.contextualAction as? UIContextualAction else{ return nil }
return contextualAction.image
}
set{
guard let contextualAction = self.contextualAction as? UIContextualAction else{ return }
return contextualAction.image = newValue
}
}

public var title: String?{
didSet{
if #available(iOS 11, *), let platformValue = self.platformValue as? UIContextualAction{
platformValue.title = self.title
get{
if #available(iOS 11, *), let contextualAction = self.contextualAction as? UIContextualAction{
return contextualAction.title

} else if let contextualAction = self.contextualAction as? UITableViewRowAction{
return contextualAction.title
}
return nil
}
set{
if #available(iOS 11, *), let contextualAction = self.contextualAction as? UIContextualAction{
contextualAction.title = newValue

} else if let contextualAction = self.contextualAction as? UITableViewRowAction{
contextualAction.title = newValue
}
}
}
Expand All @@ -45,15 +63,14 @@ public class SwipeAction{
public init(style: Style, title: String?, handler: @escaping SwipeActionHandler){
self.style = style
self.handler = handler
self.title = title

if #available(iOS 11, *){
self.platformValue = UIContextualAction(style: style.platformValue as! UIContextualAction.Style, title: title){ action, view, completion -> Void in
self.contextualAction = UIContextualAction(style: style.contextualStyle as! UIContextualAction.Style, title: title){ action, view, completion -> Void in
handler(action,view,completion)
}

} else {
self.platformValue = UITableViewRowAction(style: style.platformValue as! UITableViewRowActionStyle,title: title){ (action, indexPath) -> Void in
self.contextualAction = UITableViewRowAction(style: style.contextualStyle as! UITableViewRowActionStyle,title: title){ (action, indexPath) -> Void in
handler(action,indexPath,nil)
}
}
Expand All @@ -63,7 +80,7 @@ public class SwipeAction{
case normal = 0
case destructive = 1

var platformValue: Any{
var contextualStyle: ContextualStyle{
if #available(iOS 11, *){
switch self{
case .normal:
Expand All @@ -82,27 +99,27 @@ public class SwipeAction{
}
}

public init(platformValue: Any){
if #available(iOS 11, *), let platformValue = platformValue as? UIContextualAction.Style{
switch platformValue{
public init(contextualStyle: ContextualStyle){
if #available(iOS 11, *), let contextualStyle = contextualStyle as? UIContextualAction.Style{
switch contextualStyle{
case .normal:
self = .normal
case .destructive:
self = .destructive
}

} else if let platformValue = platformValue as? UITableViewRowActionStyle{
switch platformValue{
} else if let contextualStyle = contextualStyle as? UITableViewRowActionStyle{
switch contextualStyle{
case .normal:
self = .normal
case .destructive:
self = .destructive
case .default:
fatalError("Unmapped UITableViewRowActionStyle for SwipeActionStyle: \(platformValue)")
fatalError("Unmapped UITableViewRowActionStyle for SwipeActionStyle: \(contextualStyle)")
}

} else {
fatalError("Invalid platformValue for SwipeActionStyle: \(platformValue)")
fatalError("Invalid platformValue for SwipeActionStyle: \(contextualStyle)")
}
}
}
Expand All @@ -118,17 +135,34 @@ public class SwipeConfiguration{
public var performsFirstActionWithFullSwipe: Bool = false
public var actions: [SwipeAction] = []

public var platformValue: Any?{
guard #available(iOS 11, *) else{
return nil
}
let platformValue = UISwipeActionsConfiguration(actions: self.platformActions as! [UIContextualAction])
platformValue.performsFirstActionWithFullSwipe = self.performsFirstActionWithFullSwipe
@available(iOSApplicationExtension 11.0, *)
public var contextualConfiguration: UISwipeActionsConfiguration?{
let contextualConfiguration = UISwipeActionsConfiguration(actions: self.contextualActions as! [UIContextualAction])
contextualConfiguration.performsFirstActionWithFullSwipe = self.performsFirstActionWithFullSwipe

return platformValue
return contextualConfiguration
}

public var platformActions: [Any]{
return self.actions.map{ $0.platformValue }
public var contextualActions: [ContextualAction]{
return self.actions.map{ $0.contextualAction }
}
}


public protocol ContextualAction{}
extension UITableViewRowAction: ContextualAction{}

@available(iOSApplicationExtension 11.0, *)
extension UIContextualAction: ContextualAction{}

public protocol ContextualStyle{}
extension UITableViewRowActionStyle: ContextualStyle{}

@available(iOSApplicationExtension 11.0, *)
extension UIContextualAction.Style: ContextualStyle{}

public protocol ContextualActionSource{}
extension IndexPath: ContextualActionSource{}

@available(iOSApplicationExtension 11.0, *)
extension UIView: ContextualActionSource{}

0 comments on commit 9ba30a7

Please sign in to comment.