From d83f055ad4dfd336c834d42c53ca82c6a4adddfb Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Sat, 30 Jan 2021 00:53:19 -0500 Subject: [PATCH 1/2] Allow a destination's contents to be refreshed. Progress can optionally be displayed while refreshing. --- .../delegates/TurboWebFragmentDelegate.kt | 21 +++++++++++++++---- .../TurboBottomSheetDialogFragment.kt | 2 ++ .../hotwire/turbo/fragments/TurboFragment.kt | 2 ++ .../TurboWebBottomSheetDialogFragment.kt | 4 ++++ .../turbo/fragments/TurboWebFragment.kt | 4 ++++ .../hotwire/turbo/nav/TurboNavDestination.kt | 11 ++++++++++ 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt index 97a42acb..3610f5dd 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt @@ -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?.let { + if (displayProgress && !it.isRefreshing) { + it.isRefreshing = true + } + } + + isWebViewAttachedToNewDestination = false + visit(location, restoreWithCachedSnapshot = false, reload = true) + } + /** * Retrieves the Turbo session from the destination. */ @@ -358,8 +373,7 @@ internal class TurboWebFragmentDelegate( turboView.webViewRefresh?.apply { isEnabled = navDestination.pathProperties.pullToRefreshEnabled setOnRefreshListener { - isWebViewAttachedToNewDestination = false - visit(location, restoreWithCachedSnapshot = false, reload = true) + refresh(true) } } } @@ -367,8 +381,7 @@ internal class TurboWebFragmentDelegate( private fun initializeErrorPullToRefresh(turboView: TurboView) { turboView.errorRefresh?.apply { setOnRefreshListener { - isWebViewAttachedToNewDestination = false - visit(location, restoreWithCachedSnapshot = false, reload = true) + refresh(true) } } } diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboBottomSheetDialogFragment.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboBottomSheetDialogFragment.kt index d452994f..a821e3a4 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboBottomSheetDialogFragment.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboBottomSheetDialogFragment.kt @@ -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 diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboFragment.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboFragment.kt index cdf7a8a3..59b139ec 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboFragment.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboFragment.kt @@ -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 diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebBottomSheetDialogFragment.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebBottomSheetDialogFragment.kt index c01a7b09..c845005c 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebBottomSheetDialogFragment.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebBottomSheetDialogFragment.kt @@ -56,6 +56,10 @@ abstract class TurboWebBottomSheetDialogFragment : TurboBottomSheetDialogFragmen super.onDismiss(dialog) } + override fun refresh(displayProgress: Boolean) { + webDelegate.refresh(displayProgress) + } + // ---------------------------------------------------------------------------- // TurboWebFragmentCallback interface // ---------------------------------------------------------------------------- diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt index 53e5b85a..ae27a327 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt @@ -68,6 +68,10 @@ abstract class TurboWebFragment : TurboFragment(), TurboWebFragmentCallback { } } + override fun refresh(displayProgress: Boolean) { + webDelegate.refresh(displayProgress) + } + // ---------------------------------------------------------------------------- // TurboWebFragmentCallback interface // ---------------------------------------------------------------------------- diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavDestination.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavDestination.kt index 0097d989..c694a3e9 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavDestination.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavDestination.kt @@ -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 @@ -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 From f3c1cdf6506f02399392ed35e1198013df64a650 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Sat, 30 Jan 2021 07:07:10 -0500 Subject: [PATCH 2/2] Code cleanup --- .../turbo/delegates/TurboWebFragmentDelegate.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt index 3610f5dd..dbf30f0b 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt @@ -130,9 +130,9 @@ internal class TurboWebFragmentDelegate( * [dev.hotwire.turbo.nav.TurboNavDestination.refresh] */ fun refresh(displayProgress: Boolean) { - turboView?.webViewRefresh?.let { - if (displayProgress && !it.isRefreshing) { - it.isRefreshing = true + turboView?.webViewRefresh?.apply { + if (displayProgress && !isRefreshing) { + isRefreshing = true } } @@ -373,7 +373,7 @@ internal class TurboWebFragmentDelegate( turboView.webViewRefresh?.apply { isEnabled = navDestination.pathProperties.pullToRefreshEnabled setOnRefreshListener { - refresh(true) + refresh(displayProgress = true) } } } @@ -381,7 +381,7 @@ internal class TurboWebFragmentDelegate( private fun initializeErrorPullToRefresh(turboView: TurboView) { turboView.errorRefresh?.apply { setOnRefreshListener { - refresh(true) + refresh(displayProgress = true) } } }