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

feat: add onComplete param to popWhile function in RouterExt.kt #104

Merged

Conversation

Tommyten
Copy link
Contributor

@Tommyten Tommyten commented May 13, 2022

Closes #98

@arkivanov
Copy link
Owner

Ahh, and could you please run ./gradlew apiDump and commit the changes?

@Tommyten
Copy link
Contributor Author

Thank you for the suggestions, I've updated my branch

@arkivanov
Copy link
Owner

Sorry, the sample master-detail doesn't compile. It's better to also allow the most common use case without onComplete callback: router.popWhile {}.

Perhaps we need two variants - the existing one as it is now, and the new one with all required arguments:

router.popWhile(predicate) and router.popWhile(predicate, onComplete).

Do you want to update? Or alternatively I can do it in your branch. If you will update, please don't forget to rerun apiDump task at the end.

@arkivanov
Copy link
Owner

So the existing function will delegate to the new one, similar to Router.navigate -

navigate(transformer = transformer, onComplete = { _, _ -> })

@Tommyten
Copy link
Contributor Author

I will do it as soon as I get to my PC.
Sorry for the hassle 😅
I'm still getting used to the project but hoping to be involved more soon!

@arkivanov
Copy link
Owner

Thanks for your contributions!

Sorry for the hassle

I should have noticed this at the very beginning. :-)

@Tommyten
Copy link
Contributor Author

How about we just swap the parameters to be

inline fun <C : Any> Router<C, *>.popWhile(
    crossinline onComplete: (isSuccess: Boolean) -> Unit = {},
    crossinline predicate: (C) -> Boolean
) {
    navigate(
        transformer = { stack -> stack.dropLastWhile(predicate) },
        onComplete = { newStack, oldStack -> onComplete(newStack.size < oldStack.size) }
    )
}

This way the API would be backwards compatible. Although having the first parameter being optional is not always best practice, we could prevent having to overload popWhile. You call.

@arkivanov
Copy link
Owner

How about we just swap the parameters to be

inline fun <C : Any> Router<C, *>.popWhile(
    crossinline onComplete: (isSuccess: Boolean) -> Unit = {},
    crossinline predicate: (C) -> Boolean
) {
    navigate(
        transformer = { stack -> stack.dropLastWhile(predicate) },
        onComplete = { newStack, oldStack -> onComplete(newStack.size < oldStack.size) }
    )
}

This way the API would be backwards compatible. Although having the first parameter being optional is not always best practice, we could prevent having to overload popWhile. You call.

There are a few points:

  • I believe this change is still binary incompatible, it's only source compatible
  • When using both arguments, it's looks intuitive to handle completion in the lambda outside parentheses
  • There is already Navigator.navigate(transformer, onComplete) method, and an extension Navigator.navigate(transformer). This was done like this for the reason mentioned above. Would be good to keep popWhile aligned.

@arkivanov
Copy link
Owner

There is a build error:

API check failed for project decompose.

Could you please run ./gradlew :decompose:apiDump and commit the changes?

@arkivanov arkivanov merged commit 653aa65 into arkivanov:master May 15, 2022
@arkivanov
Copy link
Owner

Thanks for the contribution!

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

Successfully merging this pull request may close these issues.

Add onComplete callback to Router.popWhile
2 participants