Skip to content

Commit

Permalink
Ask for confirmation on operations
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoconti83 committed Mar 1, 2018
1 parent 20f5d35 commit af4676e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
39 changes: 35 additions & 4 deletions EasyTables/EasyTableSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class EasyTableSource<Object: Equatable> {
return self.dataSource.table
}

public typealias ConfirmationCallback = (Bool)->()

/// Creates a table configuration and applies it to a table
/// The configuration needs to be retained as long as the table
/// is retained
Expand All @@ -56,6 +58,7 @@ public class EasyTableSource<Object: Equatable> {
contextMenuOperations: [ObjectOperation<Object>],
table: NSTableView? = nil,
selectionModel: SelectionModel = .singleNative,
operationConfirmationCallback: @escaping (ConfirmationCallback, String)->() = ConfirmationAlert.ask,
selectionCallback: (([Object])->(Void))? = nil)
where Objects.Iterator.Element == Object
{
Expand All @@ -72,8 +75,10 @@ public class EasyTableSource<Object: Equatable> {

table.dataSource = self.dataSource
table.delegate = self.dataSource
self.setupTable(columns: columns, selectionModel: selectionModel)
self.setupMenu(operations: contextMenuOperations)
self.setupTable(columns: columns,
selectionModel: selectionModel)
self.setupMenu(operations: contextMenuOperations,
operationConfirmationCallback: operationConfirmationCallback)
}

/// Content of the table
Expand Down Expand Up @@ -119,7 +124,8 @@ extension EasyTableSource {

/// Sets up the contextual menu for the table
fileprivate func setupMenu(
operations: [ObjectOperation<Object>]
operations: [ObjectOperation<Object>],
operationConfirmationCallback: @escaping (ConfirmationCallback, String)->()
) {
guard !operations.isEmpty else {
self.table.menu = nil
Expand All @@ -131,7 +137,15 @@ extension EasyTableSource {
guard let `self` = self else { return }
let selectedObjects = self.targetObjectsForContextualOperation
guard !selectedObjects.isEmpty else { return }
operation.action(selectedObjects)
if operation.needsConfirmation {
operationConfirmationCallback({
if $0 {
operation.action(selectedObjects)
}
}, operation.label)
} else {
operation.action(selectedObjects)
}
}
menu.addItem(item)
item.isEnabled = true
Expand Down Expand Up @@ -184,3 +198,20 @@ extension ComparisonResult {
}
}
}


public struct ConfirmationAlert {
static func OKCancel(message: String) -> Bool {
let alert = NSAlert()
alert.messageText = message
alert.alertStyle = .warning
alert.addButton(withTitle: "Yes")
alert.addButton(withTitle: "Cancel")
return alert.runModal() == .alertFirstButtonReturn
}

public static func ask(callback: (Bool)->(), operation: String) {
let response = OKCancel(message: "\(operation)\nAre you sure?")
callback(response)
}
}
1 change: 1 addition & 0 deletions EasyTables/ObjectOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ public struct ObjectOperation<Object> {
self.needsConfirmation = needsConfirmation
}
}

6 changes: 3 additions & 3 deletions EasyTablesExample/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class TableViewController: NSViewController {

let (scroll, table) = NSTableView.inScrollView()
self.createLayoutConstraints(table: scroll, button1: selectButton, button2: filterButton)

self.createTableSource(for: table)

}
Expand All @@ -74,7 +73,7 @@ class TableViewController: NSViewController {
],
contextMenuOperations: [
// Remove object from the table
ObjectOperation(label: "Remove", action: {
ObjectOperation(label: "Remove", needsConfirmation: true, action: {
[weak self] (items: [String]) -> Void in
guard let `self` = self else { return }
items.forEach {
Expand Down Expand Up @@ -124,7 +123,6 @@ class TableViewController: NSViewController {
table.top == b1.bottom + space
}
}

}

extension String {
Expand All @@ -134,3 +132,5 @@ extension String {
return NSAttributedString(string: self, attributes: [NSAttributedStringKey.font: font])
}
}


0 comments on commit af4676e

Please sign in to comment.