Skip to content

Commit

Permalink
fix: use default constructor / factory + Arguments init style
Browse files Browse the repository at this point in the history
this allows Fragment lifecycle save/restore to work seemlessly and
that fixes crashes on Activity restarts
  • Loading branch information
mikehardy authored and david-allison committed Nov 26, 2024
1 parent ed407bb commit c84fb10
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ 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
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<RadioGroup>(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<RadioGroup>(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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
)
}
}
}
}

0 comments on commit c84fb10

Please sign in to comment.