1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 09:28:38 +00:00

+ text and image sets for weather condition

This commit is contained in:
ad 2018-04-20 11:13:03 +03:00
parent 5b08861dec
commit c15488b61f
3 changed files with 24 additions and 37 deletions

View File

@ -73,12 +73,13 @@ class SupportedTypesHolder {
return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT), parameters: [.image: imageParameter]) return (item: .staticButton(title: ""), action: .hidKey(keycode: NX_KEYTYPE_NEXT), parameters: [.image: imageParameter])
}, },
"weather": { decoder in "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 container = try decoder.container(keyedBy: CodingKeys.self)
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
let units = try container.decodeIfPresent(String.self, forKey: .units) let units = try container.decodeIfPresent(String.self, forKey: .units)
let api_key = try container.decodeIfPresent(String.self, forKey: .api_key) 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 "currency": { decoder in
enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to } enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to }
@ -148,7 +149,7 @@ enum ItemType: Decodable {
case dock() case dock()
case volume() case volume()
case brightness(refreshInterval: Double) 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) case currency(interval: Double, from: String, to: String)
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
@ -160,6 +161,7 @@ enum ItemType: Decodable {
case to case to
case units case units
case api_key case api_key
case icon_type
case formatTemplate case formatTemplate
case image case image
} }
@ -200,7 +202,8 @@ enum ItemType: Decodable {
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0 let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
let units = try container.decodeIfPresent(String.self, forKey: .units) ?? "metric" let units = try container.decodeIfPresent(String.self, forKey: .units) ?? "metric"
let api_key = try container.decodeIfPresent(String.self, forKey: .api_key) ?? "32c4256d09a4c52b38aecddba7a078f6" 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: case .currency:
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0 let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB" let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB"

View File

@ -29,7 +29,7 @@ extension ItemType {
return "com.toxblh.mtmr.volume" return "com.toxblh.mtmr.volume"
case .brightness(refreshInterval: _): case .brightness(refreshInterval: _):
return "com.toxblh.mtmr.brightness" return "com.toxblh.mtmr.brightness"
case .weather(interval: _, units: _, api_key: _): case .weather(interval: _, units: _, api_key: _, icon_type: _):
return "com.toxblh.mtmr.weather" return "com.toxblh.mtmr.weather"
case .currency(interval: _, from: _, to: _): case .currency(interval: _, from: _, to: _):
return "com.toxblh.mtmr.currency" return "com.toxblh.mtmr.currency"
@ -178,8 +178,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} else { } else {
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval) barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval)
} }
case .weather(interval: let interval, units: let units, api_key: let 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) 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): 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)
} }

View File

@ -19,10 +19,13 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate {
private let button = NSButton(title: "", target: nil, action: nil) private let button = NSButton(title: "", target: nil, action: nil)
private var prev_location: CLLocation! private var prev_location: CLLocation!
private var 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<String, String>
private var manager:CLLocationManager! 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.interval = interval
self.units = units self.units = units
self.api_key = api_key self.api_key = api_key
@ -31,6 +34,12 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate {
units_str = "°C" units_str = "°C"
} }
if icon_type == "images" {
iconsSource = iconsImages
} else {
iconsSource = iconsText
}
super.init(identifier: identifier) super.init(identifier: identifier)
button.bezelColor = .clear button.bezelColor = .clear
@ -44,7 +53,7 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate {
} }
if !CLLocationManager.locationServicesEnabled() { if !CLLocationManager.locationServicesEnabled() {
print("not enabled"); print("Location services not enabled");
return return
} }
@ -81,33 +90,8 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate {
if let weather = json["weather"] as? NSArray, let item = weather[0] as? NSDictionary { if let weather = json["weather"] as? NSArray, let item = weather[0] as? NSDictionary {
let icon = item["icon"] as! String let icon = item["icon"] as! String
switch (icon) { if let test = self.iconsSource[icon] {
case "01d", "01n": condition_icon = test
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 = ""
} }
} }
@ -144,7 +128,7 @@ class WeatherBarItem: NSCustomTouchBarItem, CLLocationManagerDelegate {
} }
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("inside didChangeAuthorization "); // print("inside didChangeAuthorization ");
updateWeather() updateWeather()
} }