mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
commit
b3500684d4
@ -19,6 +19,7 @@
|
||||
6042B6A72083E03A00C525C8 /* AppScrubberTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6042B6A62083E03A00C525C8 /* AppScrubberTouchBarItem.swift */; };
|
||||
6042B6AA2083E27000C525C8 /* DeprecatedCarbonAPI.c in Sources */ = {isa = PBXBuildFile; fileRef = 6042B6A92083E27000C525C8 /* DeprecatedCarbonAPI.c */; };
|
||||
607EEA4B2087835F009DA5F0 /* WeatherBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */; };
|
||||
607EEA4D2087A8DA009DA5F0 /* CurrencyBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */; };
|
||||
B0008E552080286C003AD4DD /* SupportHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0008E542080286C003AD4DD /* SupportHelpers.swift */; };
|
||||
B05600D32083E9BB00EB218D /* CustomSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = B05600D22083E9BB00EB218D /* CustomSlider.swift */; };
|
||||
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; };
|
||||
@ -68,6 +69,7 @@
|
||||
6042B6A82083E1F500C525C8 /* DeprecatedCarbonAPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeprecatedCarbonAPI.h; sourceTree = "<group>"; };
|
||||
6042B6A92083E27000C525C8 /* DeprecatedCarbonAPI.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = DeprecatedCarbonAPI.c; sourceTree = "<group>"; };
|
||||
607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherBarItem.swift; sourceTree = "<group>"; };
|
||||
607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyBarItem.swift; sourceTree = "<group>"; };
|
||||
B0008E542080286C003AD4DD /* SupportHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportHelpers.swift; sourceTree = "<group>"; };
|
||||
B05600D22083E9BB00EB218D /* CustomSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSlider.swift; sourceTree = "<group>"; };
|
||||
B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = "<group>"; };
|
||||
@ -175,6 +177,7 @@
|
||||
6042B6A62083E03A00C525C8 /* AppScrubberTouchBarItem.swift */,
|
||||
B05600D22083E9BB00EB218D /* CustomSlider.swift */,
|
||||
607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */,
|
||||
607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */,
|
||||
);
|
||||
path = MTMR;
|
||||
sourceTree = "<group>";
|
||||
@ -349,6 +352,7 @@
|
||||
36C2ECDB207C3FE7003CDA33 /* ItemsParsing.swift in Sources */,
|
||||
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */,
|
||||
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */,
|
||||
607EEA4D2087A8DA009DA5F0 /* CurrencyBarItem.swift in Sources */,
|
||||
6027D1B92080E52A004FFDC7 /* BrightnessViewController.swift in Sources */,
|
||||
368EDDE720812A1D00E10953 /* ScrollViewItem.swift in Sources */,
|
||||
);
|
||||
|
||||
73
MTMR/CurrencyBarItem.swift
Normal file
73
MTMR/CurrencyBarItem.swift
Normal file
@ -0,0 +1,73 @@
|
||||
//
|
||||
// CurrencyBarItem.swift
|
||||
// MTMR
|
||||
//
|
||||
// Created by Daniel Apatin on 18.04.2018.
|
||||
// Copyright © 2018 Anton Palgunov. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import CoreLocation
|
||||
|
||||
class CurrencyBarItem: NSCustomTouchBarItem {
|
||||
private var timer: Timer!
|
||||
private var interval: TimeInterval!
|
||||
private var from: String
|
||||
private var to: String
|
||||
private let button = NSButton(title: "", target: nil, action: nil)
|
||||
|
||||
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String) {
|
||||
self.interval = interval
|
||||
self.from = from
|
||||
self.to = to
|
||||
|
||||
super.init(identifier: identifier)
|
||||
|
||||
button.bezelColor = .clear
|
||||
button.title = "⏳"
|
||||
self.view = button
|
||||
|
||||
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateCurrency), userInfo: nil, repeats: true)
|
||||
|
||||
updateCurrency()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
@objc func updateCurrency() {
|
||||
let urlRequest = URLRequest(url: URL(string: "https://api.coinbase.com/v2/exchange-rates?currency=\(from)")!)
|
||||
|
||||
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]
|
||||
|
||||
var value: String!
|
||||
|
||||
if let data_array = json["data"] as? [String : AnyObject] {
|
||||
if let rates = data_array["rates"] as? [String : AnyObject] {
|
||||
if let item = rates["\(self.to)"] as? String {
|
||||
value = item
|
||||
}
|
||||
}
|
||||
}
|
||||
if value != nil {
|
||||
DispatchQueue.main.async {
|
||||
self.setCurrency(text: "\(self.from)\(value!)")
|
||||
}
|
||||
}
|
||||
} catch let jsonError {
|
||||
print(jsonError.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task.resume()
|
||||
}
|
||||
|
||||
func setCurrency(text: String) {
|
||||
button.title = text
|
||||
}
|
||||
}
|
||||
@ -80,6 +80,14 @@ class SupportedTypesHolder {
|
||||
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: [:])
|
||||
},
|
||||
"currency": { decoder in
|
||||
enum CodingKeys: String, CodingKey { case refreshInterval; case from; case to }
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||
let from = try container.decodeIfPresent(String.self, forKey: .from)
|
||||
let to = try container.decodeIfPresent(String.self, forKey: .to)
|
||||
return (item: .currency(interval: interval ?? 600.00, from: from ?? "RUB", to: to ?? "USD"), action: .none, parameters: [:])
|
||||
},
|
||||
"dock": { decoder in
|
||||
return (item: .dock(), action: .none, parameters: [:])
|
||||
},
|
||||
@ -141,12 +149,15 @@ enum ItemType: Decodable {
|
||||
case volume()
|
||||
case brightness(refreshInterval: Double)
|
||||
case weather(interval: Double, units: String, api_key: String)
|
||||
case currency(interval: Double, from: String, to: String)
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case type
|
||||
case title
|
||||
case source
|
||||
case refreshInterval
|
||||
case from
|
||||
case to
|
||||
case units
|
||||
case api_key
|
||||
case formatTemplate
|
||||
@ -161,6 +172,7 @@ enum ItemType: Decodable {
|
||||
case volume
|
||||
case brightness
|
||||
case weather
|
||||
case currency
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
@ -189,6 +201,11 @@ enum ItemType: Decodable {
|
||||
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)
|
||||
case .currency:
|
||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0
|
||||
let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB"
|
||||
let to = try container.decodeIfPresent(String.self, forKey: .to) ?? "USD"
|
||||
self = .currency(interval: interval, from: from, to: to)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,6 +31,8 @@ extension ItemType {
|
||||
return "com.toxblh.mtmr.brightness"
|
||||
case .weather(interval: _, units: _, api_key: _):
|
||||
return "com.toxblh.mtmr.weather"
|
||||
case .currency(interval: _, from: _, to: _):
|
||||
return "com.toxblh.mtmr.currency"
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,6 +172,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
}
|
||||
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 .currency(interval: let interval, from: let from, to: let to):
|
||||
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to)
|
||||
}
|
||||
|
||||
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user