From 753cfd8a31ff1fd8dea988d68d3cf6cab7de71b6 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Tue, 10 Apr 2018 14:07:52 +0100 Subject: [PATCH] add buttons: battery and time with format --- MTMR/ItemsParsing.swift | 47 +++++++++++++++++++++++++---------- MTMR/TouchBarController.swift | 4 +++ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 95a77b0..a946090 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -1,26 +1,26 @@ import Foundation extension Data { - + func barItemDefinitions() -> [BarItemDefinition]? { return try? JSONDecoder().decode([BarItemDefinition].self, from: self) } - + } struct BarItemDefinition: Decodable { let type: ItemType let action: ActionType - + private enum CodingKeys: String, CodingKey { case type } - + init(type: ItemType, action: ActionType) { self.type = type self.action = action } - + init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let type = try container.decode(String.self, forKey: .type) @@ -54,20 +54,35 @@ class SupportedTypesHolder { let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0) return (item: item, action: .none) }, + "battery": { decoder in + enum CodingKeys: String, CodingKey { case refreshInterval } + let container = try decoder.container(keyedBy: CodingKeys.self) + let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) + let scriptURL = Bundle.main.url(forResource: "battery", withExtension: "scpt")! + let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0) + return (item: item, action: .none) + }, + "time": { decoder in + enum CodingKeys: String, CodingKey { case formatTemplate } + let container = try decoder.container(keyedBy: CodingKeys.self) + let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) + let item = ItemType.timeButton(formatTemplate: template ?? "HH:mm" ) + return (item: item, action: .none) + }, ] - + static let sharedInstance = SupportedTypesHolder() - + func lookup(by type: String) -> ParametersDecoder { return supportedTypes[type] ?? { decoder in return (item: try ItemType(from: decoder), action: try ActionType(from: decoder)) } } - + func register(typename: String, decoder: @escaping ParametersDecoder) { supportedTypes[typename] = decoder } - + func register(typename: String, item: ItemType, action: ActionType) { register(typename: typename) { _ in return (item: item, action: action) @@ -78,17 +93,20 @@ class SupportedTypesHolder { enum ItemType: Decodable { case staticButton(title: String) case appleScriptTitledButton(source: String, refreshInterval: Double) + case timeButton(formatTemplate: String) private enum CodingKeys: String, CodingKey { case type case title case titleAppleScript case refreshInterval + case formatTemplate } - + enum ItemTypeRaw: String, Decodable { case staticButton case appleScriptTitledButton + case timeButton } init(from decoder: Decoder) throws { @@ -102,6 +120,9 @@ enum ItemType: Decodable { case .staticButton: let title = try container.decode(String.self, forKey: .title) self = .staticButton(title: title) + case .timeButton: + let template = try container.decode(String.self, forKey: .formatTemplate) + self = .timeButton(formatTemplate: template) } } } @@ -112,19 +133,19 @@ enum ActionType: Decodable { case keyPress(keycode: Int) case appleSctipt(source: String) case custom(closure: ()->()) - + private enum CodingKeys: String, CodingKey { case action case keycode case actionAppleScript } - + private enum ActionTypeRaw: String, Decodable { case hidKey case keyPress case appleScript } - + init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let type = try container.decodeIfPresent(ActionTypeRaw.self, forKey: .action) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index e8c2760..8d9b225 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -21,6 +21,8 @@ extension ItemType { return "com.toxblh.mtmr.staticButton." case .appleScriptTitledButton(source: _): return "com.toxblh.mtmr.appleScriptButton." + case .timeButton(formatTemplate: _): + return "com.toxblh.mtmr.timeButton." } } @@ -103,6 +105,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action) case .appleScriptTitledButton(source: let source, refreshInterval: let interval): return AppleScriptTouchBarItem(identifier: identifier, appleScript: source, interval: interval) + case .timeButton(formatTemplate: let template): + return TimeTouchBarItem(identifier: identifier, formatTemplate: template) } }