Skip to content
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

Merged
merged 10 commits into from
Sep 13, 2022
Merged

Widgets: Data formatting #7708

merged 10 commits into from
Sep 13, 2022

Conversation

ealeksandrov
Copy link
Contributor

@ealeksandrov ealeksandrov commented Sep 12, 2022

Closes: #7565
Depends on #7707 (not logic, it's just to prevent merge conflicts in same files).

Description

We need store currency setting to correctly display amount field. This PR also fixes conversion field from overflow over 100%.

How

  • Updates conversion rate value to behave the same way as on dashboard.
  • Adds Codable conformance to CurrencySettings and shares it between app and extension.
  • Uses shared settings to correctly format Amount value.

Testing Steps

  • Set currency settings to be very different from normal. In wp-admin it's in WooCommerce -> Settings -> Currency Options.
  • Launch the app.
  • Compare amount field formatting on app dashboard and widget.

Screenshots

USD EUR
Simulator Screen Shot - 1 Simulator Screen Shot - 2

  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@ealeksandrov ealeksandrov added the feature: widgets iOS Homescreen widgets label Sep 12, 2022
@ealeksandrov ealeksandrov added this to the 10.4 milestone Sep 12, 2022
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Sep 12, 2022

You can test the changes from this Pull Request by:
  • Clicking here or scanning the QR code below to access App Center
  • Then installing the build number pr7708-22e4fe2 on your iPhone

If you need access to App Center, please ask a maintainer to add you.

@Ecarrion Ecarrion self-assigned this Sep 12, 2022
Copy link
Contributor

@Ecarrion Ecarrion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor issue in the conversion rate and then we should we good to go!

/// Redacted entry with sample data.
///
func placeholder(in context: Context) -> StoreInfoEntry {
let dependencies = Self.fetchDependencies()
return StoreInfoEntry.data(.init(range: Localization.today,
name: dependencies?.storeName ?? Localization.myShop,
revenue: "$132.234",
revenue: formattedAmountString(for: 132.234, with: dependencies?.storeCurrencySettings),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

Comment on lines +400 to +424
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually not custom, but required because of @Published property preventing auto-synthesized conformance.
Agree about a comment, added in 9abe891

}
}

private extension StoreInfoProvider {

func formattedAmountString(for amountValue: Decimal, with currencySettings: CurrencySettings?) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these could be static, right? Since they don't access self at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, updated in 37cca09

Comment on lines +162 to +164
let minimumFractionDigits = floor(conversionRate * 100.0) == conversionRate * 100.0 ? 0 : 1
numberFormatter.minimumFractionDigits = minimumFractionDigits
return numberFormatter.string(from: conversionRate as NSNumber) ?? Constants.valuePlaceholderText
Copy link
Contributor

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 as 10.000%
IMG_718F09EAB50E-1

In retrospective, I should have added unit tests to these types 🙈

Copy link
Contributor Author

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.

@Ecarrion Ecarrion linked an issue Sep 12, 2022 that may be closed by this pull request
2 tasks
@ealeksandrov
Copy link
Contributor Author

@Ecarrion thanks for useful feedback and catching conversion bug!
We can't add unit test target to an extension, so I'll experiment about using our extension target in app unit tests bundle.

Copy link
Contributor

@Ecarrion Ecarrion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

I merged the timezone branch into this one to be able to test both changes at once. Also it helped resolving conflicts!

Base automatically changed from issue/7564-store-tz-settings to trunk September 13, 2022 13:54
@ealeksandrov ealeksandrov merged commit 5323ed6 into trunk Sep 13, 2022
@ealeksandrov ealeksandrov deleted the issue/7565-store-formatting branch September 13, 2022 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: widgets iOS Homescreen widgets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Widgets] Render store info data
3 participants