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

[QUESTION] how to clear stack navigating to specific destination #13

Open
wakaztahir opened this issue Sep 19, 2023 · 6 comments
Open

Comments

@wakaztahir
Copy link

wakaztahir commented Sep 19, 2023

@chRyNaN

I want to navigate to a destination but have nothing in the back stack, so user can't go back !

I don't know how to do it at version 0.10.0, Maybe it can be done using context (i don't know how) but it would be hard compared to this

basically I would like to do resetTo(destination)

Use case

Account Screen -> User signs out (set initial destination to Sign in Screen)
Sign In Screen -> User signs in (set initial destination to Account Screen)
Sign Up Screen -> User signs up (set initial destination to Account Screen or Sign in Screen if he's not signed in)

Tries

router.push(SingleNavigationContext<AccountNavigation>(AccountNavigation.Account))

SingleNavigationContext's constructor is internal

@wakaztahir
Copy link
Author

@chRyNaN There's no way to replaceAll with a single

@wakaztahir
Copy link
Author

@chRyNaN For now I have resolved the issue by creating a enum class that implements NavigationContext

starting with different initial destination, I don't really need multiple contexts, I just need a way to replace the current destination, having a way to replace current context would also be great if that could be done

@chRyNaN
Copy link
Owner

chRyNaN commented Sep 21, 2023

@wakaztahir Thanks for bringing up the issue. A few things here:

  • You do not need to use the SingleNavigationContext constructor. The logic for that is encapsulated within the library. When creating a Navigator, if you do not explicitly provide an initialContext, then it uses the SingleNavigationContext.
  • Currently, the operations are that of a basic Stack: push/pop. There's no elegant solution yet for what you are trying to do unfortunately. You could continuously call the popDestination function until you reach the bottom of the stack and then push the new destination. But obviously this is not ideal.

I will try and add this feature in an upcoming release. Unfortunately, I don't have the time to immediately work on this. I'll keep this issue alive until it is implemented. Also, I'm considering moving from a Stack to a Tree approach and removing the NavigationContext concept. Thoughts on that are appreciated.

@wakaztahir
Copy link
Author

@chRyNaN I just want this library to meet my needs, which are

  • The API is very easy to use like google's compose navigation library for android
    • Currently The API is not complex, I don't have a complex navigation system anyway, simple account management user interface navigation is what I required, The library really fits my needs, I moved away from decompose navigation because it was complex to use, I definitely want this library to not be complex and remain easy to use.
  • The API allows me to do all sorts of operations
  • The library is fast & small

I don't use NavigationContext, only had to use for this issue, can't say the same for other people, I also don't really know how Tree data structure would impact this navigation library, I am perfectly fine with Stack navigation API as long as it allows all sorts of operations. For nested navigation I am also perfectly fine creating another navigator. Its your call whether tree data structure should be used or not.

@chRyNaN
Copy link
Owner

chRyNaN commented Sep 21, 2023

@wakaztahir Yes, a simple, easy to use API is one of the main goals of this library. And that is why I am considering removing the NavigationContext concept altogether as it seems to bring confusion and complexity.

@wakaztahir
Copy link
Author

wakaztahir commented Nov 30, 2023

This code snippet causes infinite loop

while(navigator.canPopContext()){
   navigator.popContext()
}

I have these functions currently

@OptIn(ExperimentalNavigationApi::class)
fun <D : NavigationDestination,C : NavigationContext<D>> Navigator<D, C>.replaceAllDestinations(destination : D) {
    while(canPopDestination()) {
        popDestination()
    }
    push(destination)
}

@OptIn(ExperimentalNavigationApi::class)
fun <D : NavigationDestination,C : NavigationContext<D>> Navigator<D, C>.replaceContext(context : C) {
    popContext()
    push(context)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants