Skip to content

Commit

Permalink
Merge pull request #138 from hotwired/destination-refresh
Browse files Browse the repository at this point in the history
Allow a destination's contents to be refreshed
  • Loading branch information
jayohms committed Jan 30, 2021
2 parents b62782a + f3c1cdf commit 828090e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ internal class TurboWebFragmentDelegate(
}
}

/**
* Should be called by the implementing Fragment during
* [dev.hotwire.turbo.nav.TurboNavDestination.refresh]
*/
fun refresh(displayProgress: Boolean) {
turboView?.webViewRefresh?.apply {
if (displayProgress && !isRefreshing) {
isRefreshing = true
}
}

isWebViewAttachedToNewDestination = false
visit(location, restoreWithCachedSnapshot = false, reload = true)
}

/**
* Retrieves the Turbo session from the destination.
*/
Expand Down Expand Up @@ -358,17 +373,15 @@ internal class TurboWebFragmentDelegate(
turboView.webViewRefresh?.apply {
isEnabled = navDestination.pathProperties.pullToRefreshEnabled
setOnRefreshListener {
isWebViewAttachedToNewDestination = false
visit(location, restoreWithCachedSnapshot = false, reload = true)
refresh(displayProgress = true)
}
}
}

private fun initializeErrorPullToRefresh(turboView: TurboView) {
turboView.errorRefresh?.apply {
setOnRefreshListener {
isWebViewAttachedToNewDestination = false
visit(location, restoreWithCachedSnapshot = false, reload = true)
refresh(displayProgress = true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ abstract class TurboBottomSheetDialogFragment : BottomSheetDialogFragment(),

override fun onBeforeNavigation() {}

override fun refresh(displayProgress: Boolean) {}

/**
* Gets the Toolbar instance in your Fragment's view for use with
* navigation. The title in the Toolbar will automatically be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ abstract class TurboFragment : Fragment(), TurboNavDestination {

override fun onBeforeNavigation() {}

override fun refresh(displayProgress: Boolean) {}

/**
* Gets the Toolbar instance in your Fragment's view for use with
* navigation. The title in the Toolbar will automatically be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ abstract class TurboWebBottomSheetDialogFragment : TurboBottomSheetDialogFragmen
super.onDismiss(dialog)
}

override fun refresh(displayProgress: Boolean) {
webDelegate.refresh(displayProgress)
}

// ----------------------------------------------------------------------------
// TurboWebFragmentCallback interface
// ----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ abstract class TurboWebFragment : TurboFragment(), TurboWebFragmentCallback {
}
}

override fun refresh(displayProgress: Boolean) {
webDelegate.refresh(displayProgress)
}

// ----------------------------------------------------------------------------
// TurboWebFragmentCallback interface
// ----------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavDestination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import dev.hotwire.turbo.config.TurboPathConfiguration
import dev.hotwire.turbo.config.TurboPathConfigurationProperties
import dev.hotwire.turbo.delegates.TurboFragmentDelegate
import dev.hotwire.turbo.delegates.TurboNestedFragmentDelegate
import dev.hotwire.turbo.fragments.TurboFragment
import dev.hotwire.turbo.fragments.TurboFragmentViewModel
import dev.hotwire.turbo.fragments.TurboWebFragment
import dev.hotwire.turbo.session.TurboSession
import dev.hotwire.turbo.session.TurboSessionNavHostFragment
import dev.hotwire.turbo.visit.TurboVisitOptions
Expand Down Expand Up @@ -100,6 +102,15 @@ interface TurboNavDestination {
*/
fun onBeforeNavigation()

/**
* Refresh the destination's contents. In a [TurboWebFragment], this will perform
* a cold boot reload of the WebView location. In an all-native [TurboFragment]
* each subclass is responsible for implementing how to refresh its contents.
*
* @param displayProgress Whether progress should be displayed while refreshing.
*/
fun refresh(displayProgress: Boolean = true)

/**
* Gets the nav host fragment that will be used for navigating to `newLocation`. You should
* not have to override this, unless you're using a [TurboNestedFragmentDelegate] to provide
Expand Down

0 comments on commit 828090e

Please sign in to comment.