Skip to content

Commit

Permalink
Fix background color update for TemplatePreviewerFragment in CardTemp…
Browse files Browse the repository at this point in the history
…lateEditor

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).
  • Loading branch information
lukstbit authored and mikehardy committed Sep 28, 2024
1 parent 3dca59d commit 3010482
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 2 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -65,14 +66,28 @@ class TemplatePreviewerFragment :
if (sharedPrefs().getBoolean("safeDisplay", false)) {
view.findViewById<MaterialCardView>(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
}
}
}
Expand Down

0 comments on commit 3010482

Please sign in to comment.