From 958f93f4409220db769272afaedc6867d697dd64 Mon Sep 17 00:00:00 2001 From: Serg Date: Sun, 3 Jun 2018 00:49:48 +0700 Subject: [PATCH] handy method to open particular preset file, expose bug with wrong preset reload on group close --- MTMR/AppDelegate.swift | 21 ++++----------------- MTMR/TouchBarController.swift | 22 ++++++++++++++-------- TECHNICAL_DEBT.md | 1 + 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index bb6c3d9..f767fcf 100644 --- a/MTMR/AppDelegate.swift +++ b/MTMR/AppDelegate.swift @@ -71,17 +71,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { dialog.allowedFileTypes = ["json"] dialog.directoryURL = NSURL.fileURL(withPath: NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR"), isDirectory: true) - if (dialog.runModal() == NSApplication.ModalResponse.OK) { - let result = dialog.url - - if (result != nil) { - let path = result!.path - let jsonData = path.fileData - let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] - - TouchBarController.shared.touchbarNeedRefresh = true; - TouchBarController.shared.createAndUpdatePreset(newJsonItems: jsonItems) - } + if dialog.runModal() == .OK, let path = dialog.url?.path { + TouchBarController.shared.reloadPreset(path: path) } } @@ -123,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } func reloadOnDefaultConfigChanged() { - let file = NSURL.fileURL(withPath: NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR/items.json")) + let file = NSURL.fileURL(withPath: standardConfigPath) let fd = open(file.path, O_EVTONLY) @@ -132,11 +123,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.fileSystemSource?.setEventHandler(handler: { print("Config changed, reloading...") DispatchQueue.main.async { - let jsonData = file.path.fileData - let jsonItems = jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] - - TouchBarController.shared.touchbarNeedRefresh = true; - TouchBarController.shared.createAndUpdatePreset(newJsonItems: jsonItems) + TouchBarController.shared.reloadPreset(path: file.path) } }) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index cea56f0..bde2f65 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -13,6 +13,9 @@ struct ExactItem { let presetItem: BarItemDefinition } +let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR") +let standardConfigPath = appSupportDirectory.appending("/items.json") + extension ItemType { var identifierBase: String { @@ -93,7 +96,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in guard let `self` = self else { return } self.touchbarNeedRefresh = true - self.createAndUpdatePreset(newJsonItems: self.standardConfig() ?? []) + self.reloadStandardConfig() //fixme current config, not standard one! }), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: .stopProgressFreestandingTemplate))!)]) } @@ -105,7 +108,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil) - createAndUpdatePreset(newJsonItems: standardConfig() ?? []) + reloadPreset(path: standardConfigPath) } func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) { @@ -151,18 +154,21 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } } - func standardConfig() -> [BarItemDefinition]? { - let appSupportDirectory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!.appending("/MTMR") - let presetPath = appSupportDirectory.appending("/items.json") + func reloadStandardConfig() { + let presetPath = standardConfigPath if !FileManager.default.fileExists(atPath: presetPath), let defaultPreset = Bundle.main.path(forResource: "defaultPreset", ofType: "json") { try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil) try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath) } - let jsonData = presetPath.fileData - - return jsonData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] + reloadPreset(path: presetPath) + } + + func reloadPreset(path: String) { + let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] + touchbarNeedRefresh = true + createAndUpdatePreset(newJsonItems: items) } func loadItemDefinitions(jsonItems: [BarItemDefinition]) { diff --git a/TECHNICAL_DEBT.md b/TECHNICAL_DEBT.md index 8825331..94f77b3 100644 --- a/TECHNICAL_DEBT.md +++ b/TECHNICAL_DEBT.md @@ -3,3 +3,4 @@ * try view controllers on `NSCustomTouchBarItem` instead of subclassing item itself * try move away from enums when parse preset – enums are hard to extend * find better way to hide bar items +* extract bar items creating from TouchBarController to separate class, cover with tests