Repository navigation
feat(weather): add configurable iOS weather widget locations #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>com.apple.security.application-groups</key> | ||
| <array> | ||
| <string>group.com.exptech.dpip.dpip.widgets</string> | ||
| </array> | ||
| </dict> | ||
| </plist> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>NSExtension</key> | ||
| <dict> | ||
| <key>NSExtensionAttributes</key> | ||
| <dict> | ||
| <key>IntentsRestrictedWhileLocked</key> | ||
| <array/> | ||
| <key>IntentsSupported</key> | ||
| <array> | ||
| <string>WeatherWidgetConfigurationIntent</string> | ||
| </array> | ||
| </dict> | ||
| <key>NSExtensionPointIdentifier</key> | ||
| <string>com.apple.intents-service</string> | ||
| <key>NSExtensionPrincipalClass</key> | ||
| <string>$(PRODUCT_MODULE_NAME).IntentHandler</string> | ||
| </dict> | ||
| </dict> | ||
| </plist> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import Intents | ||
|
|
||
| final class IntentHandler: INExtension, | ||
| WeatherWidgetConfigurationIntentHandling | ||
| { | ||
| override func handler(for intent: INIntent) -> Any { | ||
| return self | ||
| } | ||
|
|
||
| func provideLocationOptionsCollection( | ||
| for intent: WeatherWidgetConfigurationIntent, | ||
| with completion: @escaping ( | ||
| INObjectCollection<WidgetLocation>?, | ||
| Error? | ||
| ) -> Void | ||
| ) { | ||
| let catalog = WidgetLocationCatalogStore().load() | ||
| let currentLocationDisplayString = String( | ||
| localized: "intent.current_location", | ||
| bundle: .main, | ||
| comment: "Current-location option in weather widget configuration." | ||
| ) | ||
|
|
||
| let locations = makeWidgetLocationOptions( | ||
| from: catalog, | ||
| currentLocationDisplayString: currentLocationDisplayString | ||
| ).map { | ||
| WidgetLocation( | ||
| identifier: $0.identifier, | ||
| display: $0.displayString | ||
| ) | ||
| } | ||
|
|
||
| completion( | ||
| INObjectCollection(items: locations), | ||
| nil | ||
| ) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "sourceLanguage" : "en", | ||
| "strings" : { | ||
| "intent.current_location" : { | ||
| "comment" : "Current-location option in weather widget configuration.", | ||
| "localizations" : { | ||
| "en" : { | ||
| "stringUnit" : { | ||
| "state" : "translated", | ||
| "value" : "Current Location" | ||
| } | ||
| }, | ||
| "zh-Hant" : { | ||
| "stringUnit" : { | ||
| "state" : "translated", | ||
| "value" : "所在地" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "version" : "1.0" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import Foundation | ||
|
|
||
| struct WidgetLocationCatalog: Decodable { | ||
| let schemaVersion: Int | ||
| let locations: [WidgetLocationCatalogLocation] | ||
| } | ||
|
|
||
| struct WidgetLocationCatalogLocation: Decodable { | ||
| let regionCode: String | ||
| let displayName: String | ||
| let administrativeAreaName: String | ||
| let latitude: Double | ||
| let longitude: Double | ||
| } | ||
|
|
||
| struct WidgetLocationCatalogStore { | ||
| private let appGroupIdentifier = | ||
| "group.com.exptech.dpip.dpip.widgets" | ||
|
|
||
| func load() -> WidgetLocationCatalog? { | ||
| guard let containerURL = | ||
| FileManager.default.containerURL( | ||
| forSecurityApplicationGroupIdentifier: appGroupIdentifier | ||
| ) | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| let url = containerURL | ||
| .appendingPathComponent("WidgetSnapshots") | ||
| .appendingPathComponent("location-catalog.json") | ||
|
|
||
| guard let data = try? Data(contentsOf: url) else { | ||
| return nil | ||
| } | ||
|
|
||
| return Self.decode(data) | ||
| } | ||
|
|
||
| static func decode(_ data: Data) -> WidgetLocationCatalog? { | ||
| guard let catalog = try? JSONDecoder().decode( | ||
| WidgetLocationCatalog.self, | ||
| from: data | ||
| ) else { | ||
| return nil | ||
| } | ||
|
|
||
| guard catalog.schemaVersion == 1 else { | ||
| return nil | ||
| } | ||
|
|
||
| return catalog | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||
| import Foundation | ||||||||||
|
|
||||||||||
| struct WidgetLocationOption: Equatable { | ||||||||||
| let identifier: String | ||||||||||
| let displayString: String | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func makeWidgetLocationOptions( | ||||||||||
| from catalog: WidgetLocationCatalog?, | ||||||||||
| currentLocationDisplayString: String | ||||||||||
| ) -> [WidgetLocationOption] { | ||||||||||
| var options = [ | ||||||||||
| WidgetLocationOption( | ||||||||||
| identifier: "current-location", | ||||||||||
| displayString: currentLocationDisplayString | ||||||||||
| ) | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| guard let catalog else { | ||||||||||
| return options | ||||||||||
| } | ||||||||||
|
|
||||||||||
| options.append( | ||||||||||
| contentsOf: catalog.locations.map { location in | ||||||||||
| WidgetLocationOption( | ||||||||||
| identifier: "region:\(location.regionCode)", | ||||||||||
| displayString: | ||||||||||
| "\(location.displayName) — " | ||||||||||
| + location.administrativeAreaName | ||||||||||
|
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||
| ) | ||||||||||
| } | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| return options | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WidgetLocationCatalogStore的load()與decode(_:)方法使用了try?來處理檔案讀取與 JSON 解碼,這會導致錯誤被靜默忽略。雖然在 Intent Extension 中,當載入失敗時退回預設選項(僅顯示「目前位置」)是一種穩健的 UX 決策,但這會增加除錯難度,使開發者難以區分是檔案不存在、權限問題還是 JSON 格式不正確。建議改用do-catch並在錯誤發生時進行記錄(例如使用os_log或Logger)。