From 30104828bf25818a19d1b208d586308ae0159e83 Mon Sep 17 00:00:00 2001 From: lukstbit <52494258+lukstbit@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:29:38 +0300 Subject: [PATCH] Fix background color update for TemplatePreviewerFragment in CardTemplateEditor The fragment's view was accessed to set the background on it but it wasn't available at that moment resulting in an IllegalStateException. The fragment's view wasn't available because although the transaction that adds the fragment is done synchronously it only takes the fragment to the parent's CURRENT lifecycle state(still too early in the lifecycle). --- .../java/com/ichi2/anki/CardTemplateEditor.kt | 5 ++--- .../previewer/TemplatePreviewerFragment.kt | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt index c43bdf9a8038..aa7c8489d6ce 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt @@ -203,12 +203,11 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener { tags = note.tags, fillEmpty = true ) - val fragment = TemplatePreviewerFragment.newInstance(args) + val backgroundColor = ThemeUtils.getThemeAttrColor(this@CardTemplateEditor, R.attr.alternativeBackgroundColor) + val fragment = TemplatePreviewerFragment.newInstance(args, backgroundColor) supportFragmentManager.commitNow { replace(R.id.fragment_container, fragment) } - val backgroundColor = ThemeUtils.getThemeAttrColor(this@CardTemplateEditor, R.attr.alternativeBackgroundColor) - fragment.requireView().setBackgroundColor(backgroundColor) } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt index dbf185a6847c..ee58f788af81 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt @@ -29,6 +29,7 @@ import com.ichi2.anki.cardviewer.CardMediaPlayer import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider import com.ichi2.anki.snackbar.SnackbarBuilder import com.ichi2.anki.utils.ext.sharedPrefs +import com.ichi2.utils.BundleUtils.getNullableInt import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -65,14 +66,28 @@ class TemplatePreviewerFragment : if (sharedPrefs().getBoolean("safeDisplay", false)) { view.findViewById(R.id.webview_container).elevation = 0F } + + arguments?.getNullableInt(ARG_BACKGROUND_OVERRIDE_COLOR)?.let { color -> + view.setBackgroundColor(color) + } } companion object { const val ARGS_KEY = "templatePreviewerArgs" + private const val ARG_BACKGROUND_OVERRIDE_COLOR = "arg_background_override_color" - fun newInstance(arguments: TemplatePreviewerArguments): TemplatePreviewerFragment { + /** + * @param backgroundOverrideColor optional color to be used as background on the root view + * of this fragment + */ + fun newInstance( + arguments: TemplatePreviewerArguments, + backgroundOverrideColor: Int? = null + ): TemplatePreviewerFragment { return TemplatePreviewerFragment().apply { - this.arguments = bundleOf(ARGS_KEY to arguments) + val args = bundleOf(ARGS_KEY to arguments) + backgroundOverrideColor?.let { args.putInt(ARG_BACKGROUND_OVERRIDE_COLOR, backgroundOverrideColor) } + this.arguments = args } } }