From daf41c811bdb3fc2b12e94b6add4754162a3540b Mon Sep 17 00:00:00 2001 From: Irfan Omur Date: Thu, 7 Dec 2023 15:37:15 +0300 Subject: [PATCH 1/2] Handle dead fragment case in LayoutPreviewFragment --- .../wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt index acc09f9a4f96..23b4a1ce6024 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt @@ -127,7 +127,7 @@ abstract class LayoutPreviewFragment : FullscreenBottomSheetDialogFragment() { binding?.webView?.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) - if (view == null) return + if (!isAdded || view == null) return val width = viewModel.selectedPreviewMode().previewWidth setWebViewWidth(view, width) val widthScript = context?.getString(R.string.web_preview_width_script, width) From 0e3924b5d1b030a2d7b292d51c85b74d817f2157 Mon Sep 17 00:00:00 2001 From: Irfan Omur Date: Thu, 7 Dec 2023 15:40:46 +0300 Subject: [PATCH 2/2] Move lambdas argument out of parentheses in LayoutPreviewFragment --- .../android/ui/layoutpicker/LayoutPreviewFragment.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt index 23b4a1ce6024..03292ed578f8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/layoutpicker/LayoutPreviewFragment.kt @@ -75,7 +75,7 @@ abstract class LayoutPreviewFragment : FullscreenBottomSheetDialogFragment() { private fun initViewModel() { this.viewModel = getViewModel() - viewModel.previewState.observe(viewLifecycleOwner, { state -> + viewModel.previewState.observe(viewLifecycleOwner) { state -> when (state) { is Loading -> { binding?.desktopPreviewHint?.setVisible(false) @@ -84,6 +84,7 @@ abstract class LayoutPreviewFragment : FullscreenBottomSheetDialogFragment() { binding?.errorView?.setVisible(false) binding?.webView?.loadUrl(state.url) } + is Loaded -> { binding?.progressBar?.setVisible(false) binding?.webView?.setVisible(true) @@ -97,6 +98,7 @@ abstract class LayoutPreviewFragment : FullscreenBottomSheetDialogFragment() { ) AniUtils.animateBottomBar(binding?.desktopPreviewHint, true) } + is Error -> { binding?.progressBar?.setVisible(false) binding?.webView?.setVisible(false) @@ -104,14 +106,14 @@ abstract class LayoutPreviewFragment : FullscreenBottomSheetDialogFragment() { state.toast?.let { ToastUtils.showToast(requireContext(), it) } } } - }) + } // We're skipping the first emitted value since it derives from the view model initialization (`start` method) - viewModel.previewMode.skip(1).observe(viewLifecycleOwner, { load() }) + viewModel.previewMode.skip(1).observe(viewLifecycleOwner) { load() } - viewModel.onPreviewModeButtonPressed.observe(viewLifecycleOwner, { + viewModel.onPreviewModeButtonPressed.observe(viewLifecycleOwner) { previewModeSelectorPopup.show(viewModel) - }) + } binding?.previewTypeSelectorButton?.let { previewModeSelectorPopup = PreviewModeSelectorPopup(requireActivity(), it)