@@ -16,95 +16,95 @@ class YandexWeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate
1616 private var location : CLLocation !
1717 private var prevLocation : CLLocation !
1818 private var manager : CLLocationManager !
19-
19+
2020 init ( identifier: NSTouchBarItem . Identifier , interval: TimeInterval ) {
2121 activity = NSBackgroundActivityScheduler ( identifier: " \( identifier. rawValue) .updatecheck " )
2222 activity. interval = interval
23-
23+
2424 super. init ( identifier: identifier, title: " ⏳ " )
25-
25+
2626 let status = CLLocationManager . authorizationStatus ( )
2727 if status == . restricted || status == . denied {
2828 print ( " User permission not given " )
2929 return
3030 }
31-
31+
3232 if !CLLocationManager. locationServicesEnabled ( ) {
3333 print ( " Location services not enabled " )
3434 return
3535 }
36-
36+
3737 activity. repeats = true
3838 activity. qualityOfService = . utility
3939 activity. schedule { ( completion: NSBackgroundActivityScheduler . CompletionHandler ) in
4040 self . updateWeather ( )
4141 completion ( NSBackgroundActivityScheduler . Result. finished)
4242 }
4343 updateWeather ( )
44-
44+
4545 manager = CLLocationManager ( )
4646 manager. delegate = self
4747 manager. desiredAccuracy = kCLLocationAccuracyHundredMeters
4848 manager. startUpdatingLocation ( )
49-
49+
5050 tapClosure = tapClosure ?? defaultTapAction
5151 }
52-
52+
5353 required init ? ( coder _: NSCoder ) {
5454 fatalError ( " init(coder:) has not been implemented " )
5555 }
56-
56+
5757 @objc func updateWeather( ) {
5858 var urlRequest = URLRequest ( url: URL ( string: getWeatherUrl ( ) ) !)
5959 urlRequest. addValue ( " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36 " , forHTTPHeaderField: " user-agent " ) // important for the right format
60-
60+
6161 let task = URLSession . shared. dataTask ( with: urlRequest) { data, _, error in
6262 guard error == nil , let response = data? . utf8string else {
6363 return
6464 }
6565// print(response)
66-
66+
6767 var matches : [ [ String ] ]
6868 var temperature : String ?
6969 matches = response. matchingStrings ( regex: " fact__temp.*?temp__value.*?>(.*?)< " )
7070 temperature = matches. first? . item ( at: 1 )
71-
71+
7272 var icon : String ?
7373 matches = response. matchingStrings ( regex: " link__condition.*?>(.*?)< " )
7474 icon = matches. first? . item ( at: 1 )
7575 if let _ = icon, let test = self . iconsSource [ icon!] {
7676 icon = test
7777 }
78-
78+
7979 if temperature != nil {
8080 DispatchQueue . main. async {
8181 self . setWeather ( text: " \( icon ?? " ? " ) \( temperature!) \( self . unitsStr) " )
8282 }
8383 }
8484 }
85-
85+
8686 task. resume ( )
8787 }
88-
88+
8989 func getWeatherUrl( ) -> String {
9090 if location != nil {
91- return " https://yandex.ru/pogoda/?lat= \( location. coordinate. latitude) &lon= \( location. coordinate. longitude) "
91+ return " https://yandex.ru/pogoda/?lat= \( location. coordinate. latitude) &lon= \( location. coordinate. longitude) ?lang=ru "
9292 } else {
93- return " https://yandex.ru/pogoda/ " // Yandex will try to determine your location by default
93+ return " https://yandex.ru/pogoda/?lang=ru " // Yandex will try to determine your location by default
9494 }
9595 }
96-
96+
9797 func setWeather( text: String ) {
9898 title = text
9999 }
100-
100+
101101 func defaultTapAction( ) {
102102 print ( getWeatherUrl ( ) )
103103 if let url = URL ( string: getWeatherUrl ( ) ) {
104104 NSWorkspace . shared. open ( url)
105105 }
106106 }
107-
107+
108108 func locationManager( _: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
109109 let lastLocation = locations. last!
110110 location = lastLocation
@@ -113,15 +113,15 @@ class YandexWeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate
113113 }
114114 prevLocation = lastLocation
115115 }
116-
116+
117117 func locationManager( _: CLLocationManager , didFailWithError error: Error ) {
118118 print ( error)
119119 }
120-
120+
121121 func locationManager( _: CLLocationManager , didChangeAuthorization _: CLAuthorizationStatus ) {
122122 updateWeather ( )
123123 }
124-
124+
125125 deinit {
126126 activity. invalidate ( )
127127 }
@@ -131,9 +131,9 @@ extension String {
131131 func matchingStrings( regex: String ) -> [ [ String ] ] {
132132 guard let regex = try ? NSRegularExpression ( pattern: regex, options: [ ] ) else { return [ ] }
133133 let nsString = self as NSString
134- let results = regex. matches ( in: self , options: [ ] , range: NSMakeRange ( 0 , nsString. length) )
134+ let results = regex. matches ( in: self , options: [ ] , range: NSMakeRange ( 0 , nsString. length) )
135135 return results. map { result in
136- ( 0 ..< result. numberOfRanges) . map {
136+ ( 0 ..< result. numberOfRanges) . map {
137137 result. range ( at: $0) . location != NSNotFound
138138 ? nsString. substring ( with: result. range ( at: $0) )
139139 : " "
0 commit comments