-
Notifications
You must be signed in to change notification settings - Fork 79
Add environment-configurable formatter support #884
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
Sources/OpenSwiftUICore/View/Text/Util/EnvironmentConfigurableFormatter.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // | ||
| // EnvironmentConfigurableFormatter.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
|
|
||
| public import Foundation | ||
|
|
||
| // MARK: - EnvironmentConfigurableFormatter | ||
|
|
||
| /// A formatter that can apply text-related environment values before use. | ||
| /// | ||
| /// OpenSwiftUI calls this hook for Foundation formatters whose output depends | ||
| /// on environment-provided formatting state, such as locale, calendar, or time | ||
| /// zone. Conforming formatters update only the environment-dependent properties | ||
| /// they support. | ||
| /// | ||
| /// - Note: Some Foundation formatter types are only available on Apple | ||
| /// platforms. | ||
| protocol EnvironmentConfigurableFormatter: AnyObject { | ||
| /// Updates the formatter with values from the current environment. | ||
| /// | ||
| /// - Parameter environment: The environment values that should influence | ||
| /// the formatter's output. | ||
| func configure(in environment: EnvironmentValues) | ||
| } | ||
|
|
||
| // MARK: - DateFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension DateFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| locale = environment.locale | ||
| calendar = environment.calendar | ||
| timeZone = environment.timeZone | ||
| } | ||
| } | ||
|
|
||
| // MARK: - ISO8601DateFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension ISO8601DateFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| timeZone = environment.timeZone | ||
| } | ||
| } | ||
|
|
||
| // MARK: - DateComponentsFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| #if canImport(Darwin) | ||
| extension DateComponentsFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| calendar = environment.calendar | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| // MARK: - DateIntervalFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension DateIntervalFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| calendar = environment.calendar | ||
| locale = environment.locale | ||
| timeZone = environment.timeZone | ||
| } | ||
| } | ||
|
|
||
| // MARK: - NumberFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension NumberFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| locale = environment.locale | ||
| } | ||
| } | ||
|
|
||
| // MARK: - MeasurementFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| #if canImport(Darwin) | ||
| extension MeasurementFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| locale = environment.locale | ||
| numberFormatter.locale = environment.locale | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| // MARK: - MassFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension MassFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| numberFormatter.locale = environment.locale | ||
| } | ||
| } | ||
|
|
||
| // MARK: - LengthFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension LengthFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| numberFormatter.locale = environment.locale | ||
| } | ||
| } | ||
|
|
||
| // MARK: - EnergyFormatter + EnvironmentConfigurableFormatter | ||
|
|
||
| extension EnergyFormatter: EnvironmentConfigurableFormatter { | ||
| func configure(in environment: EnvironmentValues) { | ||
| numberFormatter.locale = environment.locale | ||
| } | ||
| } | ||
112 changes: 112 additions & 0 deletions
112
Tests/OpenSwiftUICoreTests/View/Text/Util/EnvironmentConfigurableFormatterTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // | ||
| // EnvironmentConfigurableFormatterTests.swift | ||
| // OpenSwiftUICoreTests | ||
|
|
||
| import Foundation | ||
| @testable import OpenSwiftUICore | ||
| import Testing | ||
|
|
||
| struct EnvironmentConfigurableFormatterTests { | ||
| @Test | ||
| func dateFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = DateFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.locale == env.locale) | ||
| #expect(formatter.calendar == env.calendar) | ||
| #expect(formatter.timeZone == env.timeZone) | ||
| } | ||
|
|
||
| @Test | ||
| func iso8601DateFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = ISO8601DateFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.timeZone == env.timeZone) | ||
| } | ||
|
|
||
| #if canImport(Darwin) | ||
| @Test | ||
| func dateComponentsFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = DateComponentsFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.calendar == env.calendar) | ||
| } | ||
| #endif | ||
|
|
||
| @Test | ||
| func dateIntervalFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = DateIntervalFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.calendar == env.calendar) | ||
| #expect(formatter.locale == env.locale) | ||
| #expect(formatter.timeZone == env.timeZone) | ||
| } | ||
|
|
||
| @Test | ||
| func numberFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = NumberFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.locale == env.locale) | ||
| } | ||
|
|
||
| #if canImport(Darwin) | ||
| @Test | ||
| func measurementFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = MeasurementFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.locale == env.locale) | ||
| #expect(formatter.numberFormatter.locale == env.locale) | ||
| } | ||
| #endif | ||
|
|
||
| @Test | ||
| func massFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = MassFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.numberFormatter.locale == env.locale) | ||
| } | ||
|
|
||
| @Test | ||
| func lengthFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = LengthFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.numberFormatter.locale == env.locale) | ||
| } | ||
|
|
||
| @Test | ||
| func energyFormatterConfigureAppliesEnvironmentValues() { | ||
| let env = makeEnvironmentValues() | ||
|
|
||
| let formatter = EnergyFormatter() | ||
| formatter.configure(in: env) | ||
| #expect(formatter.numberFormatter.locale == env.locale) | ||
| } | ||
|
|
||
| private func makeEnvironmentValues() -> EnvironmentValues { | ||
| let locale = Locale(identifier: "fr_FR") | ||
| var calendar = Calendar(identifier: .buddhist) | ||
| calendar.timeZone = TimeZone(secondsFromGMT: 9 * 3600)! | ||
| let timeZone = TimeZone(secondsFromGMT: 5 * 3600)! | ||
|
|
||
| var environment = EnvironmentValues() | ||
| environment.locale = locale | ||
| environment.calendar = calendar | ||
| environment.timeZone = timeZone | ||
| return environment | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.