Skip to content

Commit

Permalink
add locale to timeButton
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxblh committed Sep 4, 2019
1 parent 29da70c commit aa73fd1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions MTMR/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.23</string>
<string>0.23.1</string>
<key>CFBundleVersion</key>
<string>275</string>
<string>278</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
6 changes: 4 additions & 2 deletions MTMR/ItemsParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ enum ItemType: Decodable {
case staticButton(title: String)
case appleScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
case shellScriptTitledButton(source: SourceProtocol, refreshInterval: Double)
case timeButton(formatTemplate: String, timeZone: String?)
case timeButton(formatTemplate: String, timeZone: String?, locale: String?)
case battery()
case dock(autoResize: Bool)
case volume()
Expand Down Expand Up @@ -374,6 +374,7 @@ enum ItemType: Decodable {
case api_key
case icon_type
case formatTemplate
case locale
case image
case url
case longUrl
Expand Down Expand Up @@ -428,7 +429,8 @@ enum ItemType: Decodable {
case .timeButton:
let template = try container.decodeIfPresent(String.self, forKey: .formatTemplate) ?? "HH:mm"
let timeZone = try container.decodeIfPresent(String.self, forKey: .timeZone) ?? nil
self = .timeButton(formatTemplate: template, timeZone: timeZone)
let locale = try container.decodeIfPresent(String.self, forKey: .locale) ?? nil
self = .timeButton(formatTemplate: template, timeZone: timeZone, locale: locale)

case .battery:
self = .battery()
Expand Down
6 changes: 3 additions & 3 deletions MTMR/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension ItemType {
return "com.toxblh.mtmr.appleScriptButton."
case .shellScriptTitledButton(source: _):
return "com.toxblh.mtmr.shellScriptButton."
case .timeButton(formatTemplate: _, timeZone: _):
case .timeButton(formatTemplate: _, timeZone: _, locale: _):
return "com.toxblh.mtmr.timeButton."
case .battery():
return "com.toxblh.mtmr.battery."
Expand Down Expand Up @@ -255,8 +255,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
case let .shellScriptTitledButton(source: source, refreshInterval: interval):
barItem = ShellScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
case let .timeButton(formatTemplate: template, timeZone: timeZone):
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, timeZone: timeZone)
case let .timeButton(formatTemplate: template, timeZone: timeZone, locale: locale):
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template, timeZone: timeZone, locale: locale)
case .battery():
barItem = BatteryBarItem(identifier: identifier)
case let .dock(autoResize: autoResize):
Expand Down
5 changes: 4 additions & 1 deletion MTMR/Widgets/TimeTouchBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ class TimeTouchBarItem: CustomButtonTouchBarItem {
private let dateFormatter = DateFormatter()
private var timer: Timer!

init(identifier: NSTouchBarItem.Identifier, formatTemplate: String, timeZone: String? = nil) {
init(identifier: NSTouchBarItem.Identifier, formatTemplate: String, timeZone: String? = nil, locale: String? = nil) {
dateFormatter.dateFormat = formatTemplate
if let locale = locale {
dateFormatter.locale = Locale(identifier: locale)
}
if let abbr = timeZone {
dateFormatter.timeZone = TimeZone(abbreviation: abbr)
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,14 @@ To close a group, use the button:
#### `timeButton`

> Attention! Works not all: https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
> formatTemplate examples: https://www.datetimeformatter.com/how-to-format-date-time-in-swift/
> locale examples: https://gist.github.com/jacobbubu/1836273
```js
{
"type": "timeButton",
"formatTemplate": "dd HH:mm",
"locale": "en_GB",
"timeZone": "UTC"
}
```
Expand Down

0 comments on commit aa73fd1

Please sign in to comment.