marinofelipe / CurrencyText

Currency text field formatter available for UIKit and SwiftUI 💶✏️

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftUI view not updated when currency value changed

naoisegolden opened this issue · comments

What problems I want to solve?

If the CurrencyFormatter currency value is changed and re-rendered, the CurrencyTextField does not update with the new currency value.

Example:

struct SomeView: View {
    @State var currency: String
    @State var text: String
    @State var unformatted: String?
    @State var input: Double?
    
    var body: some View {
        Picker("Currency", selection: $currency) {
            ForEach(["EUR", "GBP", "USD"], id: \.self) { currency in
                Text(currency)
            }
        }
        CurrencyTextField(
            configuration: .init(
                placeholder: "0",
                text: $text,
                unformattedText: $unformatted,
                inputAmount: $input,
                formatter: CurrencyFormatter {
                    // HERE the View is not updated when `$currency` changes
                    $0.currency = Currency(rawValue: currency)!
                },
                textFieldConfiguration: { uiTextField in
                    // …
                }
            )
        )
    }
}

change-currency-dynamically

What value it delivers?

It would allow to dynamically change the currency of the CurrencyTextField

Tasks

TBD

Dependecies / References

No dependencies and/or references by now.

Hi @naoisegolden, thanks for the issue :).

I was able to reproduce it here.
This is a good finding, and I can say it's mostly because on UIKit, updating a formatter as a reference type would do the work, while on SwiftUI, via UIViewRepresentable, the updateUIView function should be ensuring such update is propagated.

I'll work on a patch and will get back to you whenever it's ready to be tested and hopefully released.

Cheers!

@marinofelipe looking forward to the patch because I'm stuck at the same point as @naoisegolden 🤷🏼‍♂️

Hey @leafarenuk and @naoisegolden,

Could you please help validate if 2.3.1 has the issue fixed?
Thank you!

@marinofelipe I have to revert my comment. As you can see in the video, the fields which already have values inside are not being updated..

thanks for the fast fix! It's working in my use case!

CleanShot.2022-06-04.at.15.39.52.mp4

Hey @leafarenuk , thanks for the report.
I will work on making it fully functional and will hopefully have it ready soon.

Here is a sneak peak on the update/fix I'm doing:

recording.mp4

I'll run few more tests before making it an official version, but it's looking promising 🤞 .
The only con is that it will require a breaking change, though it will be a very small and worth one - making the formatter a binding so it propagates the updates.

Hey @leafarenuk and @naoisegolden.

I finally found some time to wrap it up. Could you please try out the draft release 3.0.0?

Thanks :), and hopefully it will be fully working this time ;). Let me know in case you face any issues.

Hey @leafarenuk and @naoisegolden.

I finally found some time to wrap it up. Could you please try out the draft release 3.0.0?

Thanks :), and hopefully it will be fully working this time ;). Let me know in case you face any issues.

thanks @marinofelipe. its working in my use case now including the switch between currencies and the auto update of the currency field 💪🏼

Thank you @leafarenuk !!