paescebu / CustomKeyboardKit

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

Add onDismiss callback when keyboard dismissed #8

Closed amoshsueh closed 1 year ago

amoshsueh commented 1 year ago

Thanks for your hard work on this library, it's really useful!

I wonder if you can add an onDismiss callback when custom keyboard dismissed?

When I click on other text fields (with default keyboard) while typing with custom keyboard, I need to handle something before dismissal and change the text.

Let me know if you need more details, thanks.

paescebu commented 1 year ago

Hi @amoshsueh! I think i understand what you mean! let me check if i can add something meaningful :) Good idea!, I'll give it a go.

paescebu commented 1 year ago

Question: What do you need in particular: A callback when keybords dismiss in general? or when your particular custom keyboard dismisses? Like, imagine you have multiple ones, would you want that all of those have separate callbacks?

amoshsueh commented 1 year ago

@paescebu Thanks for your quickly reply.

I want the particular custom keyboard which is in editing mode to have this dismiss callback. After all, only one text field can be edited at a time.

paescebu commented 1 year ago

Perfect, very clear! Let's get to work then.

amoshsueh commented 1 year ago

@paescebu By the way, is there any way to manually dismiss the custom keyboard?

paescebu commented 1 year ago

@amoshsueh could you check out the branchfeature/add-editing-ended-callback and tell me if this is what you imagined? it's used like: SwiftUI: using a new modifier called: onEditingEndedCustomKeyboard(action:) UIKit: assigning the CustomKeyboard the onEditingEnded closure

About:

@paescebu By the way, is there any way to manually dismiss the custom keyboard?

No there is nothing yet, but I'll tell you how you can solve this otherwise :)

But i'm having second thoughts about this feature, i'll give you more insights after i got your feedback :)

amoshsueh commented 1 year ago

@paescebu It doesn't seem to be working. I don't know if I'm missing something.

Code example:

let numberPad = CustomKeyboard.numberPad
numberPad.onEditingEnded = {
    print("onEditingEnded")  //  Nothing happened here
}

textField.inputView = numberPad.keyboardInputView
static var numberPad: CustomKeyboard {
    CustomKeyboardBuilder { textDocumentProxy, submit, playSystemFeedback in
         return VStack {
             // custom keyboard codes
         }
         .onEditingEndedCustomKeyboard {
             print("onEditingEnded") //  Nothing happened here
         }
    }
}

And I found textFieldDidEndEditing is not called when I switch to other text field.

paescebu commented 1 year ago

@amoshsueh, make sure you use the onEditingEndedCustomKeyboardmodifer the same way as the onSubmitCustomKeyboard modifier in the README.md. When in SwiftUI (from outside the Keyboard) and after the use of the customKeyboardmodifier.

For UIKit i actually haven't tested it, yet and i guess to make it work there i have to change it up a bit :D

But ANYWAY, about my second thoughts:

I like to be inspired by the default keyboards and APIs from Apple (on iOS), like in Messages, Contacts etc.

But I think FocusState is the way to go for both of your issues actually!

What do you think? Does it make sense what I'm saying or do you think this should actually be covered by the Keyboard, let me know! 😄

amoshsueh commented 1 year ago

@paescebu

I use my own custom text field created by UIViewRepresentable and apply onEditingEnded but it's not working. Then I use SwiftUI's TextField and it's working perfectly. Maybe something wrong with my codes but it's fine.

I've been doing some thinking on my own, I should use this library in a different way. The below API of TextField already meets my needs:

public init<S>(_ title: S, text: Binding<String>, onEditingChanged: @escaping (Bool) -> Void) where S : StringProtocol

So I think maybe there is no need for onEditingEndedCustomKeyboard. You can think about whether it is necessary to add this feature to the main branch.

I'm glad the discussion with you gave me some inspiration and thank you for being willing to share your thoughts with me.

Best wish.

paescebu commented 1 year ago

@amoshsueh True! Completely forgot that TextField also has a closure for that already!

Awesome to hear that my thoughts inspired to think the need through :)

Have a great one!