hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
431 stars 49 forks source link

Correct way to reload the current URL? #137

Closed ghiculescu closed 3 years ago

ghiculescu commented 3 years ago

turbo-ios provides session.reload() which just makes another request to whatever URL you're currently viewing. Example here. An example we're using it for is if you re-open the app after it's been asleep for more than N minutes, we call session.reload() to ensure data isn't stale.

Is there an equivalent in turbo-android?

I've got this in my main activity:

        val currentNav = delegate.currentNavDestination
        if (currentNav?.location != null) {
            delegate.navigate(currentNav.location)
        }

So want to confirm if that's the right way to do it, also pose the question if there should be a helper function for this?

ps. I noticed you can require a URL to "refresh" via a path configuration, as per this test, but I didn't find that very helpful. I may have missed the point of it, but it feels more likely you'd want to refresh a URL based on other criteria (eg. in our case, the app being closed for a while), rather than wanting to refresh based on the URL itself.

jayohms commented 3 years ago

While your approach will work, I agree that it should be nicer. I've added the ability to refresh a destination's contents in https://github.com/hotwired/turbo-android/pull/138 without performing a new navigation. The mechanics of this are identical to using pull-to-refresh in a TurboWebFragment.

From your Activity, now you'll just need to call:

delegate.currentNavDestination?.refresh()

I'm planning to release a new beta on Monday, which will include the new refresh() API.

jayohms commented 3 years ago

Note that if you ever would like to clear the navigation stack, reset all state, and reload the root destination, this would be the approach to use:

delegate.currentSessionNavHostFragment.reset()

It doesn't sound like that's the situation you're trying to handle here, so just an FYI.

ghiculescu commented 3 years ago

Thank you!

jayohms commented 3 years ago

7.0.0-beta05 is available, which addresses this: https://github.com/hotwired/turbo-android/releases/tag/7.0.0-beta05