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

Only calls one 'onSubmitCustomKeyboard' #2

Closed
SPMen opened this issue Mar 31, 2023 · 6 comments · Fixed by #3 or #26
Closed

Only calls one 'onSubmitCustomKeyboard' #2

SPMen opened this issue Mar 31, 2023 · 6 comments · Fixed by #3 or #26
Labels
bug Something isn't working

Comments

@SPMen
Copy link

SPMen commented Mar 31, 2023

This is such a help....thank you so much for posting it. You're absolutely right to say it is the easiest way to add a custom keyboard to SwiftUI.

I've been having one issue: only one onSubmitCustomKeyboard is called, even if I have multiple instances with a few different text fields. I thought at first it might be something in my code, so here's what I did to check: I started a new project in Xcode, installed the package, and copied the code of your sample yesnt keyboard. Then I only slightly modified your sample implementation:

struct ContentView: View {

    @State var text1: String = ""
    @State var text2: String = ""
    @State var text3: String = ""

        var body: some View {
            VStack {
                Text("Textfield #1:")
                TextField("", text: $text1)
                    .customKeyboard(.yesnt)
                    .onSubmitCustomKeyboard {
                        print("Textfield #1 submit")
                    }
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Text("Textfield #2")
                TextField("", text: $text2)
                    .customKeyboard(.yesnt)
                    .onSubmitCustomKeyboard {
                        print("Textfield #2 submit")
                    }
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Text("Textfield #3")
                TextField("", text: $text3)
                    .customKeyboard(.yesnt)
                    .onSubmitCustomKeyboard {
                        print("Textfield #3 submit")
                    }
                    .textFieldStyle(RoundedBorderTextFieldStyle())
            }
        }
    }

When run, only the first onSubmitCustomKeyboard is ever called ("Textfield #1 submit"), even when submit?() is called in Textfield #2 or Textfield #3.

Is that how it is supposed to work? In my own actual project, for some reason it is oddly calling the third instance of onSubmitCustomKeyboard every time, and I can't make sense of why.

@paescebu
Copy link
Owner

paescebu commented Apr 1, 2023

Hey! Thanks for your kind words! And your very detailed lookinto it already!
Very good find!
I agree that it should not work like this!

Im pretty confident it has to do with the fact that we define one static keyboard in my example. And every assignment of the "onSubmit" closure reassigns it and therefore you lose the previous closure.

I think to fix this I have to redesign this API so that every textfield can have its own keyboard instance with its own onSubmit closure.

Will get to work as soon as possible! :)

@paescebu
Copy link
Owner

paescebu commented Apr 1, 2023

Hey @SPMen,
Good news, I think i managed to find a solution that keeps the API the same, just how the CustomKeyboard needs to be declared changes slightly (a static computed property (or method) instead of a static stored property), see the examples in the adjusted README :)
Can you try out the implementation of the linked branch and tell me if it fixes the issue for you?
I tested it on my side and it seems to work fine now.

I will merge upon your approval if its fixed for you :)

Kind regards
Pascal

@paescebu paescebu added the bug Something isn't working label Apr 1, 2023
@SPMen
Copy link
Author

SPMen commented Apr 1, 2023

Hello Pascal,

Wow...yes, this works great!!! I tried the changes on the test code I posted and on my own project. It worked perfectly for both.

Thank you for sharing CustomKeyboardKit...it really is so helpful, so simple, and a gift to those of us who use it (and aren't quite as impressive in programming as you are). You are kind to share!

Take care,
Stephen

@paescebu
Copy link
Owner

paescebu commented Apr 1, 2023

Omg thank you for your kind words!

But Kudos to you! Your first analysis and great systematic feedback helped me to nail down the issue efficiently!

I will merge it to master directly so that you'll have the changes when you directly fetch the Swift Package next time :-)

Have a great weekend!
Pascal

@bwalton
Copy link

bwalton commented Apr 20, 2023

@paescebu anywhere I can buy you a coffee or show some support?

@paescebu
Copy link
Owner

paescebu commented Apr 20, 2023

@paescebu anywhere I can buy you a coffee or show some support?

@bwalton haha it's really not necessary to pay me anything.
But i quickly and dirty set up a PayPal donation link and a buy me a coffee link. It's in the updated README.

Thanks for appreciating my tiny package anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants