From 4b39b000e0be826ebfe44cd7f402d46a4b132a04 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Sat, 21 Apr 2018 01:43:37 +0100 Subject: [PATCH] Upd default. Readme. Fixed reload for central scroller. --- MTMR/CurrencyBarItem.swift | 36 +++++++++++++++++------------------ MTMR/ItemsParsing.swift | 2 +- MTMR/TouchBarController.swift | 15 ++++++++++----- MTMR/WeatherBarItem.swift | 4 ++++ MTMR/defaultPreset.json | 4 +++- README.md | 2 ++ 6 files changed, 38 insertions(+), 25 deletions(-) diff --git a/MTMR/CurrencyBarItem.swift b/MTMR/CurrencyBarItem.swift index b9ef51e..440d3b1 100644 --- a/MTMR/CurrencyBarItem.swift +++ b/MTMR/CurrencyBarItem.swift @@ -16,7 +16,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem { private var from: String private var to: String private var oldValue: Float32! - + private let currencies = [ "USD": "$", "EUR": "€", @@ -36,42 +36,42 @@ class CurrencyBarItem: CustomButtonTouchBarItem { "LTC": "Ł", "ETH": "Ξ", ] - + init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> ()) { self.interval = interval self.from = from self.to = to - + if let prefix = currencies[from] { self.prefix = prefix } else { self.prefix = from } - + super.init(identifier: identifier, title: "⏳", onTap: onTap) - + button.bezelColor = .clear self.view = button - + timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateCurrency), userInfo: nil, repeats: true) - + updateCurrency() } - + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + @objc func updateCurrency() { let urlRequest = URLRequest(url: URL(string: "https://api.coinbase.com/v2/exchange-rates?currency=\(from)")!) - + let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in if error == nil { do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] - + 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 { @@ -92,21 +92,21 @@ class CurrencyBarItem: CustomButtonTouchBarItem { task.resume() } - + 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) + color = NSColor.green } else if oldValue > value { - color = NSColor(red: 185.0/255.0, green: 95.0/255.0, blue: 50.0/255.0, alpha: 1.0) + color = NSColor.red } } 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) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index dedd461..2a4b1eb 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -208,7 +208,7 @@ enum ItemType: Decodable { 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 interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 600.0 let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB" let to = try container.decodeIfPresent(String.self, forKey: .to) ?? "USD" self = .currency(interval: interval, from: from, to: to) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 50d68b7..92f74fa 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -42,7 +42,6 @@ extension ItemType { extension NSTouchBarItem.Identifier { static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip") - static let centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea") } class TouchBarController: NSObject, NSTouchBarDelegate { @@ -56,6 +55,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { var leftIdentifiers: [NSTouchBarItem.Identifier] = [] var centerItems: [NSTouchBarItem] = [] var rightIdentifiers: [NSTouchBarItem.Identifier] = [] + var scrollArea: NSCustomTouchBarItem? + var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) private override init() { super.init() @@ -83,8 +84,12 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return definition.align == .center ? items[identifier] : nil } + self.centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) + self.scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems) + touchBar.delegate = self - touchBar.defaultItemIdentifiers = self.leftIdentifiers + [.centerScrollArea] + self.rightIdentifiers + touchBar.defaultItemIdentifiers = [] + touchBar.defaultItemIdentifiers = self.leftIdentifiers + [centerScrollArea] + self.rightIdentifiers self.presentTouchBar() } @@ -143,8 +148,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? { - if identifier == .centerScrollArea { - return ScrollViewItem(identifier: identifier, items: centerItems) + if identifier == centerScrollArea { + return self.scrollArea } guard let item = self.items[identifier], @@ -230,7 +235,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return { if let url = URL(string: url), NSWorkspace.shared.open(url) { #if DEBUG - print("URL was successfully opened") + print("URL was successfully opened") #endif } else { print("error", url) diff --git a/MTMR/WeatherBarItem.swift b/MTMR/WeatherBarItem.swift index 8951853..7140bd8 100644 --- a/MTMR/WeatherBarItem.swift +++ b/MTMR/WeatherBarItem.swift @@ -33,6 +33,10 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { units_str = "°C" } + if self.units == "imperial" { + units_str = "°F" + } + if icon_type == "images" { iconsSource = iconsImages } else { diff --git a/MTMR/defaultPreset.json b/MTMR/defaultPreset.json index cac4fa2..f041f37 100644 --- a/MTMR/defaultPreset.json +++ b/MTMR/defaultPreset.json @@ -66,8 +66,10 @@ { "type": "previous", "width": 44 }, { "type": "play", "width": 44 }, { "type": "next", "width": 44 }, - { "type": "weather", "refreshInterval": 1800 }, + { "type": "weather", "icon_type": "images", "units": "metric" }, + { "type": "currency", "from": "BTC", "to": "USD" }, { "type": "sleep", "width": 44 }, + { "type": "mute", "width": 40, "align": "right" }, { "type": "volumeDown", "width": 34, "align": "right" }, { "type": "volume", "width": 60, "align": "right" }, { "type": "volumeUp", "width": 34, "align": "right" }, diff --git a/README.md b/README.md index 0de328c..4f406c5 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ File for customize your preset for MTMR: `open ~/Library/Application\ Support/MT "type": "weather", "refreshInterval": 600, "units": "metric", // or imperial + "icon_type": "text" // or images + "api_key": "" // you can get the key on openweather ``` - `currency`