voyager icon indicating copy to clipboard operation
voyager copied to clipboard

Double click causes crack

Open funyin opened this issue 2 years ago • 7 comments

There is a bit of a delay between clicking on an item for navigation and the navigation actually occurring. So when I double-click on a button for navigation, the app crashes.

funyin avatar Nov 02 '22 18:11 funyin

This is by design - the library is designed to only allow unique screen keys on the stack, so to avoid the crash you'll either need to make your screen keys unique (instead of hard coded), or don't allow double clicks on your navigation actions

jollygreenegiant avatar Nov 26 '22 03:11 jollygreenegiant

Hmm, this makes sense but the issue came up because there was some delay in navigation.

I think a good fix would be to reject any new requests to navigate if Voyager is currently processing a navigation request.

funyin avatar Jan 22 '23 02:01 funyin

Thats not really possible, its not Voyager thats processing the navigation request, its Compose. Voyager just tells Compose the last screen in the stack and Compose has the responsibility to display it. If you want to protect from this crash I would suggest doing something like this

val navigator = LocalNavigator.currentOrThrow
lamba = {
    if (YourScreen in navigator.items) return@lamba
    navigator push YourScreen
}

Syer10 avatar Jan 22 '23 03:01 Syer10

This seems like a great idea but it did not work. I also tried checking if any item in the list contains the key of the screen I am about to push but that also did not work.

Also not that I was doing a right-to-left navigation animation

funyin avatar Mar 04 '23 00:03 funyin

I have made this function to handle this in voyager navigator, it works fine but occasionally breaks for some reason, haven't been able to find the perfect fix for this but looking for something better than this @DevSrSouza @adrielcafe can better comment on this

  public fun pushX(screen: Screen) {
        if (items.last().key != screen.key && items.last().uniqueScreenKey != screen.uniqueScreenKey) {
            push(screen)
        }
    }

Kashif-E avatar Jun 23 '23 07:06 Kashif-E

uniqueScreenKey is a key generator, it doesn't actually reference anything, you're better off removing it from the if statement. My suggestion would basically use a debounce function, like https://github.com/mmolosay/debounce/ to make sure that clicks only happen once.

Syer10 avatar Jun 23 '23 13:06 Syer10

Thanks for elaborating @Syer10

Kashif-E avatar Jun 23 '23 13:06 Kashif-E