From edca2a0e6dd8f5d26408aec2e9f07bf106039dea Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Thu, 28 Mar 2024 21:13:44 +0100 Subject: [PATCH] feat: added buttons for import/export application settings (#1837) --- Kit/plugins/Store.swift | 15 +++++++++++++ Stats/Views/AppSettings.swift | 40 ++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Kit/plugins/Store.swift b/Kit/plugins/Store.swift index c9816027..781e5c0d 100644 --- a/Kit/plugins/Store.swift +++ b/Kit/plugins/Store.swift @@ -62,4 +62,19 @@ public class Store { self.defaults.removeObject(forKey: key) } } + + public func export(to url: URL) { + guard let id = Bundle.main.bundleIdentifier, + let dicitionary = self.defaults.persistentDomain(forName: id) else { return } + NSDictionary(dictionary: dicitionary).write(to: url, atomically: true) + } + + public func `import`(from url: URL) { + guard let id = Bundle.main.bundleIdentifier, let dict = NSDictionary(contentsOf: url) as? [String: Any] else { return } + self.defaults.setPersistentDomain(dict, forName: id) + if let path = Bundle.main.resourceURL?.deletingLastPathComponent().deletingLastPathComponent().absoluteString { + asyncShell("/usr/bin/open \(path)") + NSApp.terminate(self) + } + } } diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index 8a05fd75..5337455c 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -298,6 +298,18 @@ class ApplicationSettings: NSStackView { grid.setContentHuggingPriority(.defaultHigh, for: .horizontal) grid.setContentHuggingPriority(.defaultHigh, for: .vertical) + let importBtn: NSButton = NSButton() + importBtn.title = localizedString("Import") + importBtn.bezelStyle = .rounded + importBtn.target = self + importBtn.action = #selector(self.importSettings) + + let exportBtn: NSButton = NSButton() + exportBtn.title = localizedString("Export") + exportBtn.bezelStyle = .rounded + exportBtn.target = self + exportBtn.action = #selector(self.exportSettings) + let resetBtn: NSButton = NSButton() resetBtn.title = localizedString("Reset") resetBtn.bezelStyle = .rounded @@ -307,8 +319,10 @@ class ApplicationSettings: NSStackView { grid.addRow(with: [ self.titleView(localizedString("Settings")), - resetBtn + importBtn ]) + grid.addRow(with: [NSGridCell.emptyContentView, exportBtn]) + grid.addRow(with: [NSGridCell.emptyContentView, resetBtn]) view.addArrangedSubview(grid) @@ -434,6 +448,30 @@ class ApplicationSettings: NSStackView { } } + @objc private func importSettings(_ sender: NSObject) { + let panel = NSOpenPanel() + panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow))) + panel.begin { (result) in + guard result.rawValue == NSApplication.ModalResponse.OK.rawValue else { return } + if let url = panel.url { + Store.shared.import(from: url) + } + } + } + + @objc private func exportSettings(_ sender: NSObject) { + let panel = NSSavePanel() + panel.nameFieldStringValue = "Stats.plist" + panel.showsTagField = false + panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow))) + panel.begin { (result) in + guard result.rawValue == NSApplication.ModalResponse.OK.rawValue else { return } + if let url = panel.url { + Store.shared.export(to: url) + } + } + } + @objc private func resetSettings(_ sender: NSObject) { let alert = NSAlert() alert.messageText = localizedString("Reset settings")