paescebu / CustomKeyboardKit

Creating custom In App Keyboards with SwiftUI has never been easier!
GNU General Public License v3.0
203 stars 20 forks source link

Only calls one 'onSubmitCustomKeyboard' #2

Closed SPMen closed 1 year ago

SPMen commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

SPMen commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

paescebu commented 1 year ago

@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!