Skip to content

Commit 4d27c75

Browse files
committed
NumericTextStyle/locale(\_:) now only transforms the style if locales are different. This skips one NSCache lookup per update, in most cases (and always when the user cares enough to set locale on init). Cleanup.
1 parent 3544a73 commit 4d27c75

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

Examples/iOS/App/Screens/Numeric/NumericScreenExamples.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,21 @@ struct NumericScreenExamples: View {
5757

5858
@ViewBuilder var examples: some View {
5959
switch kind.content {
60-
case .number: example(.number)
61-
case .currency: example(.currency(code: currency.content))
62-
case .percent: example(.percent)
60+
case .currency: currencyExample
61+
case .number: numberExample
62+
case .percent: percentExample
6363
}
6464
}
6565

66-
func example<F: NumericTextFormat>(_ base: _NumericTextStyle<F>) -> some View where F.FormatInput == Value {
67-
NumericScreenExample(context, base: base)
66+
@inlinable var currencyExample: some View {
67+
NumericScreenExample(context, base: _NumericTextStyle<Currency>(code: currency.content, locale: locale.content))
68+
}
69+
70+
@inlinable var numberExample: some View {
71+
NumericScreenExample(context, base: _NumericTextStyle<Number>(locale: locale.content))
72+
}
73+
74+
@inlinable var percentExample: some View {
75+
NumericScreenExample(context, base: _NumericTextStyle<Percent>(locale: locale.content))
6876
}
6977
}

Sources/DiffableTextStylesXNumeric/Helpers/Adapter.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ import Foundation
3333
self.scheme = format.scheme()
3434
}
3535

36+
//=------------------------------------------------------------------------=
37+
// MARK: Accessors
38+
//=------------------------------------------------------------------------=
39+
40+
@inlinable var locale: Locale {
41+
format.locale
42+
}
43+
3644
//=------------------------------------------------------------------------=
3745
// MARK: Preferences
3846
//=------------------------------------------------------------------------=
@@ -49,8 +57,8 @@ import Foundation
4957
// MARK: Transformations
5058
//=------------------------------------------------------------------------=
5159

52-
@inlinable func locale(_ locale: Locale) -> Self {
53-
Self(format.locale(locale))
60+
@inlinable mutating func update(_ locale: Locale) {
61+
self = Self(format.locale(locale))
5462
}
5563

5664
//=------------------------------------------------------------------------=
@@ -61,3 +69,14 @@ import Foundation
6169
lhs.format == rhs.format
6270
}
6371
}
72+
73+
extension Adapter where Format: Formats.Currency {
74+
75+
//=------------------------------------------------------------------------=
76+
// MARK: Accessors
77+
//=------------------------------------------------------------------------=
78+
79+
@inlinable var currencyCode: String {
80+
format.currencyCode
81+
}
82+
}

Sources/DiffableTextStylesXNumeric/Style+Currency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension NumericTextStyle where Format: NumericTextFormatXCurrency {
2929
//=------------------------------------------------------------------------=
3030

3131
@inlinable public var currencyCode: String {
32-
adapter.format.currencyCode
32+
adapter.currencyCode
3333
}
3434
}
3535

Sources/DiffableTextStylesXNumeric/Style.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ public struct _NumericTextStyle<Format: NumericTextFormat>: DiffableTextStyle {
6060
//=------------------------------------------------------------------------=
6161

6262
@inlinable public var locale: Locale {
63-
adapter.format.locale
63+
adapter.locale
6464
}
65-
65+
6666
@inlinable public func locale(_ locale: Locale) -> Self {
67-
var result = self; result.adapter = result.adapter.locale(locale); return result
67+
guard self.locale != locale else { return self }
68+
var result = self; result.adapter.update(locale); return result
6869
}
6970

7071
//=------------------------------------------------------------------------=

0 commit comments

Comments
 (0)