1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +00:00

Merge pull request #27 from Toxblh/ad-patch-weather

+ text and image sets for weather condition  ad committed
This commit is contained in:
Daniel Apatin 2018-04-20 11:19:52 +03:00 committed by GitHub
commit 4459a95691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 38 deletions

View File

@ -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"

View File

@ -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)
}

View File

@ -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<String, String>
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()
}
}