Skip to content

Commit

Permalink
Merge pull request #19753 from wordpress-mobile/issue/19336-fix-crash…
Browse files Browse the repository at this point in the history
…-on-design-preview-screen

Fix a crash on the design preview screen
  • Loading branch information
ravishanker authored Dec 7, 2023
2 parents 9779ce2 + 0e3924b commit 12baac7
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -97,21 +98,22 @@ abstract class LayoutPreviewFragment : FullscreenBottomSheetDialogFragment() {
)
AniUtils.animateBottomBar(binding?.desktopPreviewHint, true)
}

is Error -> {
binding?.progressBar?.setVisible(false)
binding?.webView?.setVisible(false)
binding?.errorView?.setVisible(true)
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)
Expand All @@ -127,7 +129,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)
Expand Down

0 comments on commit 12baac7

Please sign in to comment.