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

Weather and Currency timers replaced with NSBackgroundActivityScheduler (more energy efficiency)

This commit is contained in:
ad 2018-04-22 13:38:16 +03:00
parent 3c171b449a
commit 683cc4c9f4
2 changed files with 20 additions and 13 deletions

View File

@ -10,8 +10,7 @@ import Cocoa
import CoreLocation import CoreLocation
class CurrencyBarItem: CustomButtonTouchBarItem { class CurrencyBarItem: CustomButtonTouchBarItem {
private var timer: Timer! private let activity = NSBackgroundActivityScheduler(identifier: "com.toxblh.mtmr.currency.updatecheck")
private var interval: TimeInterval!
private var prefix: String private var prefix: String
private var from: String private var from: String
private var to: String private var to: String
@ -38,7 +37,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
] ]
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
self.interval = interval activity.interval = interval
self.from = from self.from = from
self.to = to self.to = to
@ -52,8 +51,12 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
self.view = button self.view = button
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateCurrency), userInfo: nil, repeats: true) activity.repeats = true
activity.qualityOfService = .utility
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
self.updateCurrency()
completion(NSBackgroundActivityScheduler.Result.finished)
}
updateCurrency() updateCurrency()
} }
@ -68,7 +71,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
if error == nil { if error == nil {
do { do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
// print(json)
var value: Float32! var value: Float32!
if let data_array = json["data"] as? [String : AnyObject] { if let data_array = json["data"] as? [String : AnyObject] {

View File

@ -10,9 +10,7 @@ import Cocoa
import CoreLocation import CoreLocation
class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
private let dateFormatter = DateFormatter() private let activity = NSBackgroundActivityScheduler(identifier: "com.toxblh.mtmr.weather.updatecheck")
private var timer: Timer!
private var interval: TimeInterval!
private var units: String private var units: String
private var api_key: String private var api_key: String
private var units_str = "°F" private var units_str = "°F"
@ -25,7 +23,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
private var manager:CLLocationManager! private var manager:CLLocationManager!
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text", onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text", onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
self.interval = interval activity.interval = interval
self.units = units self.units = units
self.api_key = api_key self.api_key = api_key
@ -58,7 +56,13 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
return return
} }
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateWeather), userInfo: nil, repeats: true) activity.repeats = true
activity.qualityOfService = .utility
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
self.updateWeather()
completion(NSBackgroundActivityScheduler.Result.finished)
}
updateWeather()
manager = CLLocationManager() manager = CLLocationManager()
manager.delegate = self manager.delegate = self
@ -79,7 +83,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
if error == nil { if error == nil {
do { do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
// print(json)
var temperature: Int! var temperature: Int!
var condition_icon = "" var condition_icon = ""