From c5a7a3142b4cdf6b14951d2e0880a39143c70ef1 Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Thu, 19 Apr 2018 21:54:12 +0300 Subject: [PATCH 1/9] Fixed currency formatting (trailing zero), added colours --- MTMR/CurrencyBarItem.swift | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/MTMR/CurrencyBarItem.swift b/MTMR/CurrencyBarItem.swift index 584f9b9..af05bbd 100644 --- a/MTMR/CurrencyBarItem.swift +++ b/MTMR/CurrencyBarItem.swift @@ -15,6 +15,7 @@ class CurrencyBarItem: NSCustomTouchBarItem { private var prefix: String private var from: String private var to: String + private var oldValue: Float32! private let button = NSButton(title: "", target: nil, action: nil) private let currencies = [ @@ -68,18 +69,18 @@ class CurrencyBarItem: NSCustomTouchBarItem { do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] - var value: String! + var value: Float32! if let data_array = json["data"] as? [String : AnyObject] { if let rates = data_array["rates"] as? [String : AnyObject] { if let item = rates["\(self.to)"] as? String { - value = item + value = Float32(item) } } } if value != nil { DispatchQueue.main.async { - self.setCurrency(text: "\(value!)") + self.setCurrency(value: value!) } } } catch let jsonError { @@ -91,7 +92,26 @@ class CurrencyBarItem: NSCustomTouchBarItem { task.resume() } - func setCurrency(text: String) { - button.title = "\(self.prefix)\(text)" + func setCurrency(value: Float32) { + var color = NSColor.white + + if let oldValue = self.oldValue { + if oldValue < value { + color = NSColor(red: 95.0/255.0, green: 185.0/255.0, blue: 50.0/255.0, alpha: 1.0) + } else if oldValue > value { + color = NSColor(red: 185.0/255.0, green: 95.0/255.0, blue: 50.0/255.0, alpha: 1.0) + } + } + self.oldValue = value + + button.title = String(format: "%@%.2f", self.prefix, value) + + let textRange = NSRange(location: 0, length: button.title.count) + let newTitle = NSMutableAttributedString(string: button.title) + newTitle.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: textRange) + newTitle.addAttribute(NSAttributedStringKey.font, value: button.font!, range: textRange) + newTitle.setAlignment(.center, range: textRange) + + button.attributedTitle = newTitle } } From 4eb14ea16634404f216fec2f7be1140c4362d10f Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 11:16:50 +0300 Subject: [PATCH 2/9] Update TouchBarController.swift --- MTMR/TouchBarController.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 2ff16a4..11bae48 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -29,7 +29,7 @@ extension ItemType { return "com.toxblh.mtmr.volume" case .brightness(refreshInterval: _): return "com.toxblh.mtmr.brightness" - case .weather(interval: _, units: _, api_key: _): + case .weather(interval: _, units: _, api_key: _, icon_type: _): return "com.toxblh.mtmr.weather" case .currency(interval: _, from: _, to: _): return "com.toxblh.mtmr.currency" @@ -178,8 +178,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } else { barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval) } - case .weather(interval: let interval, units: let units, api_key: let api_key): - barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key) + case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type): + barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type) case .currency(interval: let interval, from: let from, to: let to): barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to) } From bfc4dcf22c9e656002fe5c488f4b66ffff2beb60 Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 11:17:29 +0300 Subject: [PATCH 3/9] Update ItemsParsing.swift --- MTMR/ItemsParsing.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 39db97c..055fb09 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -73,12 +73,13 @@ class SupportedTypesHolder { return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT), parameters: [.image: imageParameter]) }, "weather": { decoder in - enum CodingKeys: String, CodingKey { case refreshInterval; case units; case api_key } + enum CodingKeys: String, CodingKey { case refreshInterval; case units; case api_key ; case icon_type } let container = try decoder.container(keyedBy: CodingKeys.self) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) let units = try container.decodeIfPresent(String.self, forKey: .units) let api_key = try container.decodeIfPresent(String.self, forKey: .api_key) - return (item: .weather(interval: interval ?? 1800.00, units: units ?? "metric", api_key: api_key ?? "32c4256d09a4c52b38aecddba7a078f6"), action: .none, parameters: [:]) + let icon_type = try container.decodeIfPresent(String.self, forKey: .icon_type) + return (item: .weather(interval: interval ?? 1800.00, units: units ?? "metric", api_key: api_key ?? "32c4256d09a4c52b38aecddba7a078f6", icon_type: icon_type ?? "text"), action: .none, parameters: [:]) }, "currency": { decoder in enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to } @@ -148,7 +149,7 @@ enum ItemType: Decodable { case dock() case volume() case brightness(refreshInterval: Double) - case weather(interval: Double, units: String, api_key: String) + case weather(interval: Double, units: String, api_key: String, icon_type: String) case currency(interval: Double, from: String, to: String) private enum CodingKeys: String, CodingKey { @@ -160,6 +161,7 @@ enum ItemType: Decodable { case to case units case api_key + case icon_type case formatTemplate case image } @@ -200,7 +202,8 @@ enum ItemType: Decodable { let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0 let units = try container.decodeIfPresent(String.self, forKey: .units) ?? "metric" let api_key = try container.decodeIfPresent(String.self, forKey: .api_key) ?? "32c4256d09a4c52b38aecddba7a078f6" - self = .weather(interval: interval, units: units, api_key: api_key) + let icon_type = try container.decodeIfPresent(String.self, forKey: .icon_type) ?? "text" + self = .weather(interval: interval, units: units, api_key: api_key, icon_type: icon_type) case .currency: let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0 let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB" From 7bc8458d8ba8a20bef819547fc6d51c97daf0b31 Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 11:18:11 +0300 Subject: [PATCH 4/9] Update WeatherBarItem.swift --- MTMR/WeatherBarItem.swift | 45 ++++++++++++--------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/MTMR/WeatherBarItem.swift b/MTMR/WeatherBarItem.swift index b902375..87ad8be 100644 --- a/MTMR/WeatherBarItem.swift +++ b/MTMR/WeatherBarItem.swift @@ -19,10 +19,13 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { private let button = NSButton(title: "", target: nil, action: nil) private var prev_location: CLLocation! private var location: CLLocation! + private let iconsImages = ["01d": "☀️", "01n": "☀️", "02d": "⛅️", "02n": "⛅️", "03d": "☁️", "03n": "☁️", "04d": "☁️", "04n": "☁️", "09d": "⛅️", "09n": "⛅️", "10d": "🌦", "10n": "🌦", "11d": "🌩", "11n": "🌩", "13d": "❄️", "13n": "❄️", "50d": "🌫", "50n": "🌫"] + private let iconsText = ["01d": "☀", "01n": "☀", "02d": "☁", "02n": "☁", "03d": "☁", "03n": "☁", "04d": "☁", "04n": "☁", "09d": "☂", "09n": "☂", "10d": "☂", "10n": "☂", "11d": "☈", "11n": "☈", "13d": "☃", "13n": "☃", "50d": "♨", "50n": "♨"] + private var iconsSource: Dictionary private var manager:CLLocationManager! - init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String) { + init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text") { self.interval = interval self.units = units self.api_key = api_key @@ -31,6 +34,12 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { units_str = "°C" } + if icon_type == "images" { + iconsSource = iconsImages + } else { + iconsSource = iconsText + } + super.init(identifier: identifier) button.bezelColor = .clear @@ -44,7 +53,7 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { } if !CLLocationManager.locationServicesEnabled() { - print("not enabled"); + print("Location services not enabled"); return } @@ -81,33 +90,8 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { if let weather = json["weather"] as? NSArray, let item = weather[0] as? NSDictionary { let icon = item["icon"] as! String - switch (icon) { - case "01d", "01n": - condition_icon = "☀️" - break - case "02d", "02n": - condition_icon = "⛅️" - break - case "03d", "03n", "04d", "04n": - condition_icon = "☁️" - break - case "09d", "09n": - condition_icon = "⛅️" - break - case "10d", "10n": - condition_icon = "🌦" - break - case "11d", "11n": - condition_icon = "🌩" - break - case "13d", "13n": - condition_icon = "❄️" - break - case "50d", "50n": - condition_icon = "🌫" - break - default: - condition_icon = "" + if let test = self.iconsSource[icon] { + condition_icon = test } } @@ -144,9 +128,8 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { } func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { - print("inside didChangeAuthorization "); +// print("inside didChangeAuthorization "); updateWeather() } } - From 73411368976ecfd7f3834de829d59e4f0160694c Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 13:34:56 +0300 Subject: [PATCH 5/9] Update TouchBarController.swift --- MTMR/TouchBarController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 11bae48..4cf4086 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -179,9 +179,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval) } case .weather(interval: let interval, units: let units, api_key: let api_key, icon_type: let icon_type): - barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type) + barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type, onTap: action) case .currency(interval: let interval, from: let from, to: let to): - barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to) + barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, onTap: action) } if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth { From 94d519cd65a52d9658977fea641064e9865b0120 Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 13:35:21 +0300 Subject: [PATCH 6/9] Update CurrencyBarItem.swift --- MTMR/CurrencyBarItem.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/MTMR/CurrencyBarItem.swift b/MTMR/CurrencyBarItem.swift index af05bbd..8d2ed32 100644 --- a/MTMR/CurrencyBarItem.swift +++ b/MTMR/CurrencyBarItem.swift @@ -9,14 +9,13 @@ import Cocoa import CoreLocation -class CurrencyBarItem: NSCustomTouchBarItem { +class CurrencyBarItem: CustomButtonTouchBarItem { private var timer: Timer! private var interval: TimeInterval! private var prefix: String private var from: String private var to: String private var oldValue: Float32! - private let button = NSButton(title: "", target: nil, action: nil) private let currencies = [ "USD": "$", @@ -35,7 +34,7 @@ class CurrencyBarItem: NSCustomTouchBarItem { "CHF": "Fr." ] - init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String) { + init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> ()) { self.interval = interval self.from = from self.to = to @@ -46,10 +45,9 @@ class CurrencyBarItem: NSCustomTouchBarItem { self.prefix = from } - super.init(identifier: identifier) + super.init(identifier: identifier, title: "⏳", onTap: onTap) button.bezelColor = .clear - button.title = "⏳" self.view = button timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateCurrency), userInfo: nil, repeats: true) From 92c4eed4edcdb4d9eacd3ee70579ccac06541d57 Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 13:35:41 +0300 Subject: [PATCH 7/9] Update WeatherBarItem.swift --- MTMR/WeatherBarItem.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/MTMR/WeatherBarItem.swift b/MTMR/WeatherBarItem.swift index 87ad8be..fb120e9 100644 --- a/MTMR/WeatherBarItem.swift +++ b/MTMR/WeatherBarItem.swift @@ -9,14 +9,13 @@ import Cocoa import CoreLocation -class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { +class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { private let dateFormatter = DateFormatter() private var timer: Timer! private var interval: TimeInterval! private var units: String private var api_key: String private var units_str = "°F" - private let button = NSButton(title: "", target: nil, action: nil) private var prev_location: CLLocation! private var location: CLLocation! private let iconsImages = ["01d": "☀️", "01n": "☀️", "02d": "⛅️", "02n": "⛅️", "03d": "☁️", "03n": "☁️", "04d": "☁️", "04n": "☁️", "09d": "⛅️", "09n": "⛅️", "10d": "🌦", "10n": "🌦", "11d": "🌩", "11n": "🌩", "13d": "❄️", "13n": "❄️", "50d": "🌫", "50n": "🌫"] @@ -25,7 +24,7 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { private var manager:CLLocationManager! - init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text") { + init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text", onTap: @escaping () -> ()) { self.interval = interval self.units = units self.api_key = api_key @@ -40,10 +39,9 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate { iconsSource = iconsText } - super.init(identifier: identifier) + super.init(identifier: identifier, title: "⏳", onTap: onTap) button.bezelColor = .clear - button.title = "⏳" self.view = button let status = CLLocationManager.authorizationStatus() From 3e35e03bfb379c7b233e7f41210052f756791efd Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 14:25:31 +0300 Subject: [PATCH 8/9] + action for currency, weather, battery It will allow to process custom action on click by widget --- MTMR/ItemsParsing.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 055fb09..f7965e9 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -79,15 +79,17 @@ class SupportedTypesHolder { let units = try container.decodeIfPresent(String.self, forKey: .units) let api_key = try container.decodeIfPresent(String.self, forKey: .api_key) let icon_type = try container.decodeIfPresent(String.self, forKey: .icon_type) - return (item: .weather(interval: interval ?? 1800.00, units: units ?? "metric", api_key: api_key ?? "32c4256d09a4c52b38aecddba7a078f6", icon_type: icon_type ?? "text"), action: .none, parameters: [:]) + let action = try ActionType(from: decoder) + return (item: .weather(interval: interval ?? 1800.00, units: units ?? "metric", api_key: api_key ?? "32c4256d09a4c52b38aecddba7a078f6", icon_type: icon_type ?? "text"), action: action, parameters: [:]) }, "currency": { decoder in - enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to } + enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to; case action } let container = try decoder.container(keyedBy: CodingKeys.self) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) let from = try container.decodeIfPresent(String.self, forKey: .from) let to = try container.decodeIfPresent(String.self, forKey: .to) - return (item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD"), action: .none, parameters: [:]) + let action = try ActionType(from: decoder) + return (item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD"), action: action, parameters: [:]) }, "dock": { decoder in return (item: .dock(), action: .none, parameters: [:]) @@ -117,7 +119,8 @@ class SupportedTypesHolder { let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) let scriptPath = Bundle.main.path(forResource: "Battery", ofType: "scpt")! let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0) - return (item: item, action: .none, parameters: [:]) + let action = try ActionType(from: decoder) + return (item: item, action: action, parameters: [:]) }, "sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]), parameters: [:]) }, "displaySleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["displaysleepnow"]), parameters: [:]) }, From ac234aa4bfa8919df00b08c9553c63901c850724 Mon Sep 17 00:00:00 2001 From: Daniel Apatin Date: Fri, 20 Apr 2018 16:24:00 +0300 Subject: [PATCH 9/9] Fix for temperature value with decimals --- MTMR/WeatherBarItem.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MTMR/WeatherBarItem.swift b/MTMR/WeatherBarItem.swift index fb120e9..8951853 100644 --- a/MTMR/WeatherBarItem.swift +++ b/MTMR/WeatherBarItem.swift @@ -81,8 +81,8 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { var condition_icon = "" if let main = json["main"] as? [String : AnyObject] { - if let temp = main["temp"] as? Int { - temperature = temp + if let temp = main["temp"] as? Double { + temperature = Int(temp) } }