-
Notifications
You must be signed in to change notification settings - Fork 112
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
Widgets: Data formatting #7708
Widgets: Data formatting #7708
Changes from all commits
14e9863
46bd16d
7a5fac3
9b1250d
4324e25
dcc8180
9abe891
37cca09
72e118c
22e4fe2
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 |
---|---|---|
|
@@ -2,13 +2,13 @@ import Foundation | |
|
||
/// Site-wide settings for displaying prices/money | ||
/// | ||
public class CurrencySettings { | ||
public class CurrencySettings: Codable { | ||
|
||
// MARK: - Enums | ||
|
||
/// Designates where the currency symbol is located on a formatted price | ||
/// | ||
public enum CurrencyPosition: String { | ||
public enum CurrencyPosition: String, Codable { | ||
case left = "left" | ||
case right = "right" | ||
case leftSpace = "left_space" | ||
|
@@ -394,4 +394,36 @@ public class CurrencySettings { | |
return "ZK" | ||
} | ||
} | ||
|
||
// MARK: - Codable implementation | ||
// Used for serialization in UserDefaults to share settings between app and widgets extension | ||
// | ||
// No custom logic, but it is required because `@Published` property prevents automatic Codable synthesis | ||
// (currencyCode type is Published<CurrencyCode> instead of CurrencyCode) | ||
|
||
enum CodingKeys: CodingKey { | ||
case currencyCode | ||
case currencyPosition | ||
case groupingSeparator | ||
case decimalSeparator | ||
case fractionDigits | ||
} | ||
|
||
public required init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
currencyCode = try container.decode(CurrencyCode.self, forKey: .currencyCode) | ||
currencyPosition = try container.decode(CurrencyPosition.self, forKey: .currencyPosition) | ||
groupingSeparator = try container.decode(String.self, forKey: .groupingSeparator) | ||
decimalSeparator = try container.decode(String.self, forKey: .decimalSeparator) | ||
fractionDigits = try container.decode(Int.self, forKey: .fractionDigits) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(currencyCode, forKey: .currencyCode) | ||
try container.encode(currencyPosition, forKey: .currencyPosition) | ||
try container.encode(groupingSeparator, forKey: .groupingSeparator) | ||
try container.encode(decimalSeparator, forKey: .decimalSeparator) | ||
try container.encode(fractionDigits, forKey: .fractionDigits) | ||
} | ||
Comment on lines
+404
to
+428
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. Since we have a custom encoding here, maybe its worth noting that it was added for the widget extension. So future devs will be more informed. 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. It's actually not custom, but required because of |
||
} |
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.
I think there is a bit of an edge case here or in the conversion formula as because
100%
is showing as10.000%
In retrospective, I should have added unit tests to these types 🙈
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.
Yeah, very obvious error, converted rate value to percentage twice 😄
Fixed in 72e118c.