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

Add callback to allow/deny navigating when browser forward/back button is used #713

Merged
merged 12 commits into from
Jun 13, 2024

Conversation

gsteckman
Copy link
Contributor

@gsteckman gsteckman commented May 13, 2024

This PR adds the capability to provide a callback to DefaultWebHistoryController.attach that will be invoked when the popstate event listener is called by the browser, e.g. when the back or forward buttons are used. The callback can be used to indicate to the DefaultWebHistoryController that the navigation should not be allowed, and it will reverse the back/forward action the browser performed to keep the navigation state at the current point in the stack. This should address #42 and can also be used for more general use cases, for example to prevent a user from navigating away from a screen with unsaved form data by using the forward/back buttons.

Summary by CodeRabbit

  • New Features

    • Added forward() and back() methods to DefaultWebHistoryControllerWindow for browser history navigation.
    • Introduced onWebNavigation callback parameter to control web navigation in WebHistoryController.
  • Bug Fixes

    • Improved navigation control in DefaultWebHistoryController by handling onWebNavigation callback.
  • Documentation

    • Added CHANGELOG.md to track versions starting from 0.1.0 (Alpha snapshot).
  • Chores

    • Updated decompose version to 3.0.0-gs in deps.versions.toml.

Copy link

coderabbitai bot commented May 13, 2024

Walkthrough

The recent updates across multiple files introduce an additional Function1 parameter to the attach method in the WebHistoryController interface, enhancing web navigation control. This change is reflected in various platforms, including Android, JVM, Kotlin, JS, and WebAssembly. New methods for navigating browser history (forward and back) have been added, and test cases have been updated to validate these enhancements. Additionally, a new DefaultWebHistoryController class is introduced, and the versioning in deps.versions.toml has been updated.

Changes

Files/Paths Change Summary
decompose/api/android/decompose.api Added an additional parameter to the attach method in WebHistoryController and a new synthetic method attach$default.
decompose/api/jvm/decompose.api Added an additional Function1 parameter to the attach method and a new attach$default method in WebHistoryController$DefaultImpls class.
decompose/src/commonMain/kotlin/.../webhistory/WebHistoryController.kt Added an optional onWebNavigation callback parameter to the attach function in WebHistoryController.
decompose/src/jsMain/kotlin/.../webhistory/DefaultWebHistoryControllerWindow.kt Implemented forward() and back() methods in DefaultWebHistoryControllerWindow for browser history navigation.
decompose/src/wasmJsMain/kotlin/.../webhistory/DefaultWebHistoryControllerWindow.kt Implemented forward() and back() methods in DefaultWebHistoryControllerWindow for browser history navigation.
decompose/src/webMain/kotlin/.../webhistory/DefaultWebHistoryController.kt Added onWebNavigation parameter to the constructor and updated onPopState method to handle this callback for web navigation control.
decompose/src/webTest/kotlin/.../webhistory/DefaultWebHistoryControllerTest.kt Modified attach function to accept an additional callback parameter and added three new test functions.
deps.versions.toml Updated the version of decompose from "3.0.0" to "3.0.0-gs".
CHANGELOG.md Introduced version 0.1.0 with the tag "Alpha snapshot".
decompose/api/decompose.klib.api Added an additional parameter to the attach function in WebHistoryController and introduced DefaultWebHistoryController class with the updated method.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant WebHistoryController
    participant DefaultWebHistoryController
    User->>WebHistoryController: attach(StackNavigator, Value, KSerializer, Function1, Function1, Function1)
    WebHistoryController->>DefaultWebHistoryController: handle attach with onWebNavigation callback
    DefaultWebHistoryController->>WebHistoryController: invoke onWebNavigation callback
    WebHistoryController->>User: navigation allowed/denied based on callback
Loading

Poem

In the code where history we weave,
New paths and callbacks now perceive.
Forward and back, the browser's dance,
Enhanced control at every glance.
Tests to guide, ensure we're right,
Version bumped, all set for flight.
With changes vast, our code's delight!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 78de501 and e380100.
Files selected for processing (9)
  • decompose/api/android/decompose.api (1 hunks)
  • decompose/api/jvm/decompose.api (1 hunks)
  • decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt (1 hunks)
  • decompose/src/jsMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerWindow.kt (1 hunks)
  • decompose/src/wasmJsMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerWindow.kt (1 hunks)
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (4 hunks)
  • decompose/src/webTest/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerTest.kt (1 hunks)
  • decompose/src/webTest/kotlin/com/arkivanov/decompose/router/stack/webhistory/TestHistory.kt (1 hunks)
  • deps.versions.toml (1 hunks)
Additional comments not posted (14)
decompose/src/jsMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerWindow.kt (2)

27-29: The implementation of the forward method correctly uses the browser's history API to navigate forward.


31-33: The implementation of the back method correctly uses the browser's history API to navigate backward.

decompose/src/wasmJsMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerWindow.kt (2)

27-29: The implementation of the forward method correctly uses the browser's history API to navigate forward.


31-33: The implementation of the back method correctly uses the browser's history API to navigate backward.

decompose/src/webTest/kotlin/com/arkivanov/decompose/router/stack/webhistory/TestHistory.kt (2)

35-39: The implementation of the forward method correctly simulates navigating forward in the history stack.


41-45: The implementation of the back method correctly simulates navigating backward in the history stack.

decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt (1)

39-39: The addition of the allowWebNavigationCallback parameter to the attach method aligns with the PR's objectives to provide more control over navigation.

deps.versions.toml (1)

3-3: The update of the decompose version to "3.0.0-gs" is appropriate, indicating preparation for future stable releases following significant changes.

decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (2)

50-52: The implementation of the attach method in DefaultWebHistoryController correctly integrates the allowWebNavigationCallback parameter, providing dynamic control over navigation.


166-196: The doOnPopState method effectively handles navigation decisions based on the allowWebNavigationCallback, aligning with the PR's objectives to provide dynamic navigation control.

decompose/src/webTest/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerTest.kt (2)

423-438: The test correctly simulates and verifies the behavior when back navigation is denied using the callback, ensuring that the history remains unchanged.


440-455: The test correctly simulates and verifies the behavior when back navigation is allowed using the callback, ensuring that the history is updated correctly.

decompose/api/jvm/decompose.api (1)

329-329: The addition of attach$default method supports backward compatibility by handling default parameters. Ensure it is correctly used wherever attach is called.

decompose/api/android/decompose.api (1)

349-349: Verify the implementation of attach$default for backward compatibility.

Ensure that the new attach$default method in WebHistoryController$DefaultImpls correctly supports default arguments without breaking existing usages. This method is critical for maintaining backward compatibility when new parameters are added to methods in Kotlin.

decompose/api/android/decompose.api Outdated Show resolved Hide resolved
decompose/api/jvm/decompose.api Outdated Show resolved Hide resolved
@arkivanov
Copy link
Owner

Thanks! Please allow me some time to review, as I'm on vacation currently.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between e380100 and fc0ad13.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

@@ -220,5 +246,7 @@ class DefaultWebHistoryController internal constructor(
fun go(delta: Int)
fun pushState(data: String, url: String?)
fun replaceState(data: String, url: String?)
fun forward()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two methods actually used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

back is used in the doOnPopState method, forward is not used.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use go(-1) instead of back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point. That should be equivalent, per the documentation here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between fc0ad13 and a5d8c45.
Files selected for processing (2)
  • decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt (1 hunks)
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (4 hunks)
Additional comments not posted (2)
decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt (1)

32-41: The addition of allowWebNavigationCallback enhances navigation control. Consider adding detailed documentation for this new parameter to help developers understand its usage and impact.

decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (1)

Line range hint 50-166: The implementation of allowWebNavigationCallback in the attach method and its usage in onPopState are well-integrated. Ensure comprehensive unit tests cover these changes to validate the behavior under various scenarios.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between a5d8c45 and fe8608d.
Files selected for processing (1)
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fe8608d and 29c491e.

Files selected for processing (2)
  • decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt (1 hunks)
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 29c491e and 49a5cc9.

Files selected for processing (5)
  • decompose/api/android/decompose.api (1 hunks)
  • decompose/api/jvm/decompose.api (1 hunks)
  • decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt (1 hunks)
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt (3 hunks)
  • decompose/src/webTest/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerTest.kt (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • decompose/src/commonMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/WebHistoryController.kt
  • decompose/src/webMain/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryController.kt
Additional comments not posted (6)
decompose/src/webTest/kotlin/com/arkivanov/decompose/router/stack/webhistory/DefaultWebHistoryControllerTest.kt (3)

423-437: The test correctly simulates the scenario where back navigation is denied by the callback. It ensures that the history stack remains unchanged, which aligns with the expected behavior.


440-455: The test correctly simulates the scenario where back navigation is allowed by the callback. It ensures that the history stack is updated correctly, which aligns with the expected behavior.


458-475: The test correctly simulates the scenario where forward navigation is denied by the callback after a back navigation. It ensures that the history stack remains unchanged, which aligns with the expected behavior.

decompose/api/jvm/decompose.api (1)

329-329: The addition of a default implementation for the attach method in the WebHistoryController$DefaultImpls class ensures backward compatibility and follows good practices.

decompose/api/android/decompose.api (2)

344-344: The addition of the Function1 parameter to the attach method is a good enhancement for handling navigation control.

Please ensure to document this new parameter to help developers understand its usage and impact.


349-349: The addition of the attach$default method supports backward compatibility by handling default values for the new parameter.

Please verify that this integrates seamlessly with existing implementations that use the attach method.

decompose/api/jvm/decompose.api Show resolved Hide resolved
@arkivanov
Copy link
Owner

Looks good, thanks! Do you mind to resolve the conflict? I'm happy to approve and merge after that.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 49a5cc9 and 3c8a955.

Files selected for processing (2)
  • decompose/api/android/decompose.api (1 hunks)
  • decompose/api/jvm/decompose.api (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • decompose/api/android/decompose.api
  • decompose/api/jvm/decompose.api

@arkivanov
Copy link
Owner

Oh, I think we forgot to update the API files. Could you please run ./gradlew :decompose:apiDump and commit all the changed files?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3c8a955 and b027fd4.

Files selected for processing (1)
  • decompose/api/decompose.klib.api (3 hunks)
Additional comments not posted (2)
decompose/api/decompose.klib.api (2)

93-93: The updated attach method signature correctly includes the new callback parameter for navigation control. Ensure all implementations of this interface are updated accordingly.


170-175: The DefaultWebHistoryController class correctly implements the updated attach method and includes the new historyPaths property. Ensure this class's usage reflects the new method signature and property.

@arkivanov arkivanov merged commit 291842d into arkivanov:master Jun 13, 2024
2 checks passed
@arkivanov
Copy link
Owner

Thank you for working on this!

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.

2 participants