Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Inconsistencies when using SwipeAction #1504

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions Source/Core/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -947,11 +947,9 @@ extension FormViewController : UITableViewDelegate {
return form[indexPath].trailingSwipe.contextualConfiguration
}

open func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?{
guard let actions = form[indexPath].trailingSwipe.contextualActions as? [UITableViewRowAction], !actions.isEmpty else {
return nil
}
return actions
public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let actions = form[indexPath].trailingSwipe.tableViewRowActions
return !actions.isEmpty ? actions : nil
}
}

Expand Down
71 changes: 42 additions & 29 deletions Source/Core/SwipeActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,40 @@ public class SwipeAction: ContextualAction {
}

func contextualAction(forRow: BaseRow) -> ContextualAction {
var action: ContextualAction
if #available(iOS 11, *){
action = UIContextualAction(style: style.contextualStyle as! UIContextualAction.Style, title: title){ [weak self] action, view, completion -> Void in
let action = UIContextualAction(style: style.contextualStyle as! UIContextualAction.Style, title: title){ [weak self] action, view, completion -> Void in
guard let strongSelf = self else{ return }
strongSelf.handler(strongSelf, forRow, completion)
}
} else {
action = UITableViewRowAction(style: style.contextualStyle as! UITableViewRowAction.Style,title: title){ [weak self] (action, indexPath) -> Void in
guard let strongSelf = self else{ return }
strongSelf.handler(strongSelf, forRow) { _ in
DispatchQueue.main.async {
guard action.style == .destructive else {
forRow.baseCell?.formViewController()?.tableView?.setEditing(false, animated: true)
return
}
forRow.section?.remove(at: indexPath.row)
}
}
}

action.backgroundColor = self.backgroundColor ?? action.backgroundColor
action.image = self.image ?? action.image

return action
}
if let color = self.backgroundColor {
action.backgroundColor = color
}
if let image = self.image {
action.image = image
}
return action

return tableViewRowAction(forRow: forRow)
}

func tableViewRowAction(forRow: BaseRow) -> UITableViewRowAction {
let action = UITableViewRowAction(style: style.tableViewRowStyle, title: title) { [weak self] (action, indexPath) -> Void in
guard let strongSelf = self else { return }
strongSelf.handler(strongSelf, forRow) { _ in
DispatchQueue.main.async {
guard action.style == .destructive else {
forRow.baseCell?.formViewController()?.tableView?.setEditing(false, animated: true)
return
}
forRow.section?.remove(at: indexPath.row)
}
}
}
action.backgroundColor = self.backgroundColor ?? action.backgroundColor
action.image = self.image ?? action.image

return action
}

public enum Style{
case normal
case destructive
Expand All @@ -68,14 +73,18 @@ public class SwipeAction: ContextualAction {
return UIContextualAction.Style.destructive
}
} else {
switch self{
case .normal:
return UITableViewRowAction.Style.normal
case .destructive:
return UITableViewRowAction.Style.destructive
}
return tableViewRowStyle
}
}

var tableViewRowStyle: UITableViewRowAction.Style {
switch self {
case .normal:
return UITableViewRowAction.Style.normal
case .destructive:
return UITableViewRowAction.Style.destructive
}
}
}
}

Expand All @@ -99,9 +108,13 @@ extension SwipeConfiguration {
return contextualConfiguration
}

var contextualActions: [ContextualAction]{
var contextualActions: [ContextualAction] {
return self.actions.map { $0.contextualAction(forRow: self.row) }
}

var tableViewRowActions: [UITableViewRowAction] {
return self.actions.map { $0.tableViewRowAction(forRow: self.row) }
}
}

protocol ContextualAction {
Expand Down