diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt index 5874909218a6..2fe3ff4956b4 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt @@ -1562,7 +1562,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..efbc5e95a501 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,7 +34,7 @@ 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() @@ -41,27 +42,48 @@ class BrowserOptionsDialog(private val cardsOrNotes: CardsOrNotes, private val i 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 - if (cardsOrNotes != newCardsOrNotes) { + if (getCardsOrNotes() != newCardsOrNotes) { viewModel.setCardsOrNotes(newCardsOrNotes) } val newTruncate = dialogView.findViewById(R.id.truncate_checkbox).isChecked - if (newTruncate != isTruncated) { + if (newTruncate != getIsTruncated()) { viewModel.setTruncated(newTruncate) } } + private fun getCardsOrNotes(): CardsOrNotes { + val cardsOrNotesBoolean = arguments?.getBoolean("cardsOrNotes") + if (cardsOrNotesBoolean == null) { + Timber.w("BrowserOptionsDialog instantiated without configuration.") + } + if (cardsOrNotesBoolean == false) { + return CardsOrNotes.NOTES + } + // Default case, and what we'll do if there were no arguments supplied + return CardsOrNotes.CARDS + } + + private fun getIsTruncated(): Boolean { + var isTruncatedBoolean = arguments?.getBoolean("isTruncated") + if (isTruncatedBoolean == null) { + Timber.w("BrowserOptionsDialog instantiated without configuration.") + isTruncatedBoolean = false + } + return isTruncatedBoolean + } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val layoutInflater = requireActivity().layoutInflater dialogView = layoutInflater.inflate(R.layout.browser_options_dialog, null) - if (cardsOrNotes == CardsOrNotes.CARDS) { + if (getCardsOrNotes() == CardsOrNotes.CARDS) { dialogView.findViewById(R.id.select_cards_mode).isChecked = true } else { dialogView.findViewById(R.id.select_notes_mode).isChecked = true } - dialogView.findViewById(R.id.truncate_checkbox).isChecked = isTruncated + dialogView.findViewById(R.id.truncate_checkbox).isChecked = getIsTruncated() dialogView.findViewById(R.id.action_rename_flag).setOnClickListener { Timber.d("Rename flag clicked") @@ -80,4 +102,16 @@ class BrowserOptionsDialog(private val cardsOrNotes: CardsOrNotes, private val i this.create() } } + + companion object { + fun newInstance(cardsOrNotes: CardsOrNotes, isTruncated: Boolean): BrowserOptionsDialog { + val args = bundleOf( + "cardsOrNote" to (cardsOrNotes == CardsOrNotes.CARDS), + "isTruncated" to isTruncated + ) + return BrowserOptionsDialog().apply { + arguments = args + } + } + } }