From 45adb3f6a1c1712368ff487a7d63d834a24fe655 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Sat, 23 Nov 2024 16:21:08 -0500 Subject: [PATCH] fix: use default constructor / factory + Arguments init style this allows Fragment lifecycle save/restore to work seemlessly and that fixes crashes on Activity restarts (cherry picked from commit c84fb10627232817764b43b55379d945ba67ca17) --- .../main/java/com/ichi2/anki/CardBrowser.kt | 2 +- .../anki/dialogs/BrowserOptionsDialog.kt | 43 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt index cc909000b568..bb135392c617 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt @@ -1563,7 +1563,7 @@ open class CardBrowser : } private fun showOptionsDialog() { - val dialog = BrowserOptionsDialog(viewModel.cardsOrNotes, viewModel.isTruncated) + val dialog = BrowserOptionsDialog.newInstance(viewModel.cardsOrNotes, viewModel.isTruncated) dialog.show(supportFragmentManager, "browserOptionsDialog") } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/BrowserOptionsDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/BrowserOptionsDialog.kt index 924c9c743b3e..c1077d8593ef 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/BrowserOptionsDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/BrowserOptionsDialog.kt @@ -26,6 +26,7 @@ import android.widget.RadioButton import android.widget.RadioGroup import androidx.annotation.IdRes import androidx.appcompat.app.AppCompatDialogFragment +import androidx.core.os.bundleOf import androidx.fragment.app.activityViewModels import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.ichi2.anki.R @@ -33,14 +34,16 @@ import com.ichi2.anki.browser.CardBrowserViewModel import com.ichi2.anki.model.CardsOrNotes import timber.log.Timber -class BrowserOptionsDialog(private val cardsOrNotes: CardsOrNotes, private val isTruncated: Boolean) : AppCompatDialogFragment() { +class BrowserOptionsDialog : AppCompatDialogFragment() { private lateinit var dialogView: View private val viewModel: CardBrowserViewModel by activityViewModels() private val positiveButtonClick = { _: DialogInterface, _: Int -> - @IdRes val selectedButtonId = dialogView.findViewById(R.id.select_browser_mode).checkedRadioButtonId - val newCardsOrNotes = if (selectedButtonId == R.id.select_cards_mode) CardsOrNotes.CARDS else CardsOrNotes.NOTES + @IdRes val selectedButtonId = + dialogView.findViewById(R.id.select_browser_mode).checkedRadioButtonId + val newCardsOrNotes = + if (selectedButtonId == R.id.select_cards_mode) CardsOrNotes.CARDS else CardsOrNotes.NOTES if (cardsOrNotes != newCardsOrNotes) { viewModel.setCardsOrNotes(newCardsOrNotes) } @@ -51,6 +54,25 @@ class BrowserOptionsDialog(private val cardsOrNotes: CardsOrNotes, private val i } } + private val cardsOrNotes by lazy { + when (arguments?.getBoolean(CARDS_OR_NOTES_KEY)) { + true -> CardsOrNotes.CARDS + false -> CardsOrNotes.NOTES + null -> { + // Default case, and what we'll do if there were no arguments supplied + Timber.w("BrowserOptionsDialog instantiated without configuration.") + CardsOrNotes.CARDS + } + } + } + + private val isTruncated by lazy { + arguments?.getBoolean(IS_TRUNCATED_KEY) ?: run { + Timber.w("BrowserOptionsDialog instantiated without configuration.") + false + } + } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val layoutInflater = requireActivity().layoutInflater dialogView = layoutInflater.inflate(R.layout.browser_options_dialog, null) @@ -80,4 +102,19 @@ class BrowserOptionsDialog(private val cardsOrNotes: CardsOrNotes, private val i this.create() } } + + companion object { + private const val CARDS_OR_NOTES_KEY = "cardsOrNotes" + private const val IS_TRUNCATED_KEY = "isTruncated" + + fun newInstance(cardsOrNotes: CardsOrNotes, isTruncated: Boolean): BrowserOptionsDialog { + Timber.i("BrowserOptionsDialog::newInstance") + return BrowserOptionsDialog().apply { + arguments = bundleOf( + CARDS_OR_NOTES_KEY to (cardsOrNotes == CardsOrNotes.CARDS), + IS_TRUNCATED_KEY to isTruncated + ) + } + } + } }