Skip to content

Add priceFormatter to StoreProduct#1028

Merged
aboedo merged 8 commits into
mainfrom
priceFormatter
Dec 3, 2021
Merged

Add priceFormatter to StoreProduct#1028
aboedo merged 8 commits into
mainfrom
priceFormatter

Conversation

@aboedo

@aboedo aboedo commented Dec 3, 2021

Copy link
Copy Markdown
Member

Fixes #1013

Will follow up with another PR for the new methods that already format the price, figured smaller PRs was probably better.

Also does some small cleanup for outdated comments after the AvailabilityChecks PR

@aboedo aboedo requested a review from a team December 3, 2021 13:12
@aboedo aboedo self-assigned this Dec 3, 2021
Comment thread Purchases/Purchasing/StoreProduct.swift
Comment on lines -143 to +163
private lazy var formatter: NumberFormatter = {
@objc public override var priceFormatter: NumberFormatter? {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

had to remove lazy here because this is a computed var.
I think an optimal version of this would be to use a private static var, so we can check if it's initialized and only do it once, then listen for storefront changes.
the public var would be the same, it would just redirect internally to the static var.

configurePurchases()
var completionCalled = false
var receivedError: Error? = nil
var receivedOfferings: Offerings? = nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still don't like this at all 😅 in a strongly typed language we never put the type in variable names. This is like doing completionCalledBoolean. The fact that this is a "Maybe" (like Haskell calls it) is already encoded in the type.

@aboedo aboedo Dec 3, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah, I hear ya. I find it redundant often too.
I do find it useful for callbacks in particular, though, and as an ocasional reminder to consider nullability. It might still be the python coder in me that won't let go.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The compiler will remind you :P


@objc public override var localizedTitle: String { underlyingSK2Product.displayName }

@objc public override var priceFormatter: NumberFormatter? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe document that this creates a new formatter every time, which can be slow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good call. I'll add a TODO: as well as a reminder to tackle it before shipping ideally

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread Purchases/Purchasing/StoreProduct.swift
let priceFormatter = try XCTUnwrap(storeProduct.priceFormatter)
let productPrice = storeProduct.price as NSNumber

expect(priceFormatter.string(from: productPrice)) == "$4.99"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not for this PR
It would be nice to have tests that ensure this formats in other locales too. The new Test Plans in Xcode makes that a lot easier.
Otherwise if we changed the implementation to hardcode this to Dollars it would still pass.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think I can do that with StoreKitTest, I'll take a look

@aboedo aboedo requested review from a team and NachoSoto December 3, 2021 19:38
Comment thread Purchases/Purchasing/StoreProduct.swift Outdated
Co-authored-by: NachoSoto <NachoSoto@users.noreply.github.com>
@aboedo aboedo merged commit 494007a into main Dec 3, 2021
@aboedo aboedo deleted the priceFormatter branch December 3, 2021 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing ProductDetails.priceLocale

2 participants