.../Source/Components/DialogFactory.swift
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // ConfirmationDialog.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason Milkins on 15/4/18. |
6 | | // Copyright © 2018-2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Cocoa |
10 | | |
11 | | enum SuppressibleDialog: String, Equatable, CaseIterable { |
12 | | case clearHistoryActionClicked |
13 | | case destructiveLimitChangeWarning |
14 | | case clearHistoryWarning |
15 | | |
16 | 14 | var defaultSuppressionKey: String { |
17 | 14 | return "\(self)_CutBoxSuppressed" |
18 | 14 | } |
19 | | |
20 | 7 | var defaultChoiceKey: String { |
21 | 7 | return "\(defaultSuppressionKey)Choice" |
22 | 7 | } |
23 | | |
24 | 2 | static var all: [SuppressibleDialog] { |
25 | 2 | return Self.allCases |
26 | 2 | } |
27 | | } |
28 | | |
29 | | class DialogAlert: NSAlert {} |
30 | | |
31 | | class DialogFactory { |
32 | | static var testing: Bool = false |
33 | | static var testResponse: Bool = false |
34 | | static var madeDialog: String = "" |
35 | | |
36 | | private func makeDialog(messageText: String, |
37 | | informativeText: String, |
38 | | ok: String, |
39 | | cancel: String, |
40 | 8 | alert: DialogAlert = DialogAlert()) -> DialogAlert { |
41 | 8 | if Self.testing { Self.madeDialog = messageText } |
42 | 8 | alert.messageText = messageText |
43 | 8 | alert.informativeText = informativeText |
44 | 8 | alert.addButton(withTitle: ok) |
45 | 8 | alert.addButton(withTitle: cancel) |
46 | 8 | return alert |
47 | 8 | } |
48 | | |
49 | | func suppressibleConfirmationDialog(messageText: String, |
50 | | informativeText: String, |
51 | | dialogName: SuppressibleDialog, |
52 | | ok: String = "ok".l7n, |
53 | | cancel: String = "cancel".l7n, |
54 | | defaults: UserDefaults = UserDefaults.standard, |
55 | 9 | alert: DialogAlert = DialogAlert()) -> Bool { |
56 | 9 | |
57 | 9 | let suppressionKey = "\(dialogName)_CutBoxSuppressed" |
58 | 9 | let suppressionChoiceKey = "\(dialogName)_CutBoxSuppressedChoice" |
59 | 9 | |
60 | 9 | if defaults.bool(forKey: suppressionKey) { |
61 | 2 | return defaults.bool(forKey: suppressionChoiceKey) |
62 | 7 | } |
63 | 7 | |
64 | 7 | let alert: DialogAlert = makeDialog(messageText: messageText, |
65 | 7 | informativeText: informativeText, |
66 | 7 | ok: ok, |
67 | 7 | cancel: cancel, |
68 | 7 | alert: alert) |
69 | 7 | |
70 | 7 | alert.showsSuppressionButton = true |
71 | 7 | |
72 | 7 | let alertResponse = Self.testing ? Self.testResponse : alert.runModal() == .alertFirstButtonReturn |
73 | 7 | |
74 | 7 | if alert.suppressionButton?.state == .on { |
75 | 2 | defaults.set(true, forKey: suppressionKey) |
76 | 2 | defaults.set(alertResponse, forKey: suppressionChoiceKey) |
77 | 7 | } |
78 | 7 | |
79 | 7 | return alertResponse |
80 | 7 | } |
81 | | |
82 | | func confirmationDialog(messageText: String, |
83 | | informativeText: String, |
84 | | ok: String = "ok".l7n, |
85 | 1 | cancel: String = "cancel".l7n) -> Bool { |
86 | 1 | |
87 | 1 | let alert = makeDialog(messageText: messageText, |
88 | 1 | informativeText: informativeText, |
89 | 1 | ok: ok, cancel: cancel) |
90 | 1 | |
91 | 1 | let alertResponse = Self.testing ? Self.testResponse : alert.runModal() == .alertFirstButtonReturn |
92 | 1 | |
93 | 1 | return alertResponse |
94 | 1 | } |
95 | | } |