1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +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
class CurrencyBarItem: CustomButtonTouchBarItem {
private var timer: Timer!
private var interval: TimeInterval!
private let activity = NSBackgroundActivityScheduler(identifier: "com.toxblh.mtmr.currency.updatecheck")
private var prefix: String
private var from: 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 () -> ()) {
self.interval = interval
activity.interval = interval
self.from = from
self.to = to
@ -52,8 +51,12 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
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()
}
@ -68,7 +71,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
// print(json)
var value: Float32!
if let data_array = json["data"] as? [String : AnyObject] {

View File

@ -10,9 +10,7 @@ import Cocoa
import CoreLocation
class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
private let dateFormatter = DateFormatter()
private var timer: Timer!
private var interval: TimeInterval!
private let activity = NSBackgroundActivityScheduler(identifier: "com.toxblh.mtmr.weather.updatecheck")
private var units: String
private var api_key: String
private var units_str = "°F"
@ -25,7 +23,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
private var manager:CLLocationManager!
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.api_key = api_key
@ -57,8 +55,14 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
print("Location services not enabled");
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.delegate = self
@ -79,7 +83,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
// print(json)
var temperature: Int!
var condition_icon = ""