mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
142 lines
5.4 KiB
Swift
142 lines
5.4 KiB
Swift
//
|
|
// WeatherBarItem.swift
|
|
// MTMR
|
|
//
|
|
// Created by Daniel Apatin on 18.04.2018.
|
|
// Copyright © 2018 Anton Palgunov. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
import CoreLocation
|
|
|
|
class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
|
|
private let activity: NSBackgroundActivityScheduler
|
|
private var units: String
|
|
private var api_key: String
|
|
private var units_str = "°F"
|
|
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, icon_type: String? = "text") {
|
|
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updatecheck")
|
|
activity.interval = interval
|
|
self.units = units
|
|
self.api_key = api_key
|
|
|
|
if self.units == "metric" {
|
|
units_str = "°C"
|
|
}
|
|
|
|
if self.units == "imperial" {
|
|
units_str = "°F"
|
|
}
|
|
|
|
if icon_type == "images" {
|
|
iconsSource = iconsImages
|
|
} else {
|
|
iconsSource = iconsText
|
|
}
|
|
|
|
super.init(identifier: identifier, title: "⏳")
|
|
|
|
self.view = button
|
|
|
|
let status = CLLocationManager.authorizationStatus()
|
|
if status == .restricted || status == .denied {
|
|
print("User permission not given")
|
|
return
|
|
}
|
|
|
|
if !CLLocationManager.locationServicesEnabled() {
|
|
print("Location services not enabled");
|
|
return
|
|
}
|
|
|
|
activity.repeats = true
|
|
activity.qualityOfService = .utility
|
|
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
|
|
self.updateWeather()
|
|
completion(NSBackgroundActivityScheduler.Result.finished)
|
|
}
|
|
updateWeather()
|
|
|
|
manager = CLLocationManager()
|
|
manager.delegate = self
|
|
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
|
|
manager.startUpdatingLocation()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
@objc func updateWeather() {
|
|
if self.location != nil {
|
|
let urlRequest = URLRequest(url: URL(string: "https://api.openweathermap.org/data/2.5/weather?lat=\(location.coordinate.latitude)&lon=\(location.coordinate.longitude)&units=\(self.units)&appid=\(self.api_key)")!)
|
|
|
|
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]
|
|
// print(json)
|
|
var temperature: Int!
|
|
var condition_icon = ""
|
|
|
|
if let main = json["main"] as? [String : AnyObject] {
|
|
if let temp = main["temp"] as? Double {
|
|
temperature = Int(temp)
|
|
}
|
|
}
|
|
|
|
if let weather = json["weather"] as? NSArray, let item = weather[0] as? NSDictionary {
|
|
let icon = item["icon"] as! String
|
|
if let test = self.iconsSource[icon] {
|
|
condition_icon = test
|
|
}
|
|
}
|
|
|
|
if temperature != nil {
|
|
DispatchQueue.main.async {
|
|
self.setWeather(text: "\(condition_icon) \(temperature!)\(self.units_str)")
|
|
}
|
|
}
|
|
} catch let jsonError {
|
|
print(jsonError.localizedDescription)
|
|
}
|
|
}
|
|
}
|
|
|
|
task.resume()
|
|
}
|
|
}
|
|
|
|
func setWeather(text: String) {
|
|
self.title = text
|
|
}
|
|
|
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
let lastLocation = locations.last!
|
|
self.location = lastLocation
|
|
if prev_location == nil {
|
|
updateWeather()
|
|
}
|
|
prev_location = lastLocation
|
|
}
|
|
|
|
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
|
print(error);
|
|
}
|
|
|
|
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
|
|
// print("inside didChangeAuthorization ");
|
|
updateWeather()
|
|
}
|
|
|
|
}
|