From af4676ea9faa7264623d79682a16ea3f85c4b63a Mon Sep 17 00:00:00 2001 From: Marco Conti Date: Thu, 1 Mar 2018 09:31:58 +0100 Subject: [PATCH] Ask for confirmation on operations --- EasyTables/EasyTableSource.swift | 39 ++++++++++++++++++--- EasyTables/ObjectOperation.swift | 1 + EasyTablesExample/TableViewController.swift | 6 ++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/EasyTables/EasyTableSource.swift b/EasyTables/EasyTableSource.swift index 634d90c..4fe6bc7 100644 --- a/EasyTables/EasyTableSource.swift +++ b/EasyTables/EasyTableSource.swift @@ -38,6 +38,8 @@ public class EasyTableSource { 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 @@ -56,6 +58,7 @@ public class EasyTableSource { contextMenuOperations: [ObjectOperation], table: NSTableView? = nil, selectionModel: SelectionModel = .singleNative, + operationConfirmationCallback: @escaping (ConfirmationCallback, String)->() = ConfirmationAlert.ask, selectionCallback: (([Object])->(Void))? = nil) where Objects.Iterator.Element == Object { @@ -72,8 +75,10 @@ public class EasyTableSource { 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 @@ -119,7 +124,8 @@ extension EasyTableSource { /// Sets up the contextual menu for the table fileprivate func setupMenu( - operations: [ObjectOperation] + operations: [ObjectOperation], + operationConfirmationCallback: @escaping (ConfirmationCallback, String)->() ) { guard !operations.isEmpty else { self.table.menu = nil @@ -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 @@ -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) + } +} diff --git a/EasyTables/ObjectOperation.swift b/EasyTables/ObjectOperation.swift index dd96b84..9441d36 100644 --- a/EasyTables/ObjectOperation.swift +++ b/EasyTables/ObjectOperation.swift @@ -35,3 +35,4 @@ public struct ObjectOperation { self.needsConfirmation = needsConfirmation } } + diff --git a/EasyTablesExample/TableViewController.swift b/EasyTablesExample/TableViewController.swift index 9383076..c100e77 100644 --- a/EasyTablesExample/TableViewController.swift +++ b/EasyTablesExample/TableViewController.swift @@ -56,7 +56,6 @@ class TableViewController: NSViewController { let (scroll, table) = NSTableView.inScrollView() self.createLayoutConstraints(table: scroll, button1: selectButton, button2: filterButton) - self.createTableSource(for: table) } @@ -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 { @@ -124,7 +123,6 @@ class TableViewController: NSViewController { table.top == b1.bottom + space } } - } extension String { @@ -134,3 +132,5 @@ extension String { return NSAttributedString(string: self, attributes: [NSAttributedStringKey.font: font]) } } + +