https://kotlinlang.org logo
#compose
Title
# compose
a

Artur Matsehor

06/17/2020, 9:29 PM
Hey! I've got a simple form of two fields. Now I want to assign an IME action for a first one, so it will show an arrow on a soft keyboard, which will move focus to a second field. I tried doing this, but it doesn't work for unknown reason, email field stays focused
z

Zach Klippenstein (he/him) [MOD]

06/17/2020, 10:48 PM
any reason to not just put
passFocusModifier.requestFocus()
in the IME action handler?
👍 1
a

Artur Matsehor

06/18/2020, 5:54 AM
I can do it only by declaring this Modifier even before the first field, which is not that convenient in terms of code readability. Especially if the form contains like 10 fields, so I will need to declare them all at the top or use some Array. But still, it finally works, thank you!
z

Zach Klippenstein (he/him) [MOD]

06/18/2020, 2:11 PM
Why is it not readable to declare all your focusable modifiers upfront? The advantage of that over the string-based approach in your gist is that it's strongly typed - you can't have a bug where you make a typo in one of the string tags and focus breaks because the compiler checks that for you (can't reference a modifier that doesn't exist).
a

Artur Matsehor

06/18/2020, 2:19 PM
Sure, that is a strong point But here's what I mean in terms of readability: You open a @Composable function of some 9-fields form, and the first thing you see is 9 lines (or array/map/etc) of some modifiers that doesn't make sense for you yet. And then you go further in code, you see the fields these Modifier-s belong to. So basically, a FocusModifier that will be assigned even to a 9th field (which is declared at the very bottom of a page) appears right on top. Personally I'm not a big fan of such a code structure, so you need to scroll up and down just to figure out which modifier belongs to which field (of course, naming helps, but still).
z

Zach Klippenstein (he/him) [MOD]

06/18/2020, 2:26 PM
Not sure why you'd need to scroll any more than if you're using strings - just name your modifiers what you would pass in as strings. Once you scroll past the modifier list, it's exactly the same as your gist except the names are actual symbols instead of string literals. And defining all the modifiers up front is actually more maintainable imo because if someone wants to add focus logic, they don't have to scan the entire function to find the name of the string to pass in, they just go to the one place in the function where all focus modifiers are defined. It also improves maintainability. If you end up renaming one of the focus tags, you don't have to do a find-and-replace and hope you didn't forget any, you can just use the IDE to rename the var.
a

Artur Matsehor

06/18/2020, 2:27 PM
Agree
4 Views