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 committed Nov 23, 2024
1 parent c5899d9 commit b517320
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 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,42 +26,64 @@ 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
if (cardsOrNotes != newCardsOrNotes) {
if (getCardsOrNotes() != newCardsOrNotes) {
viewModel.setCardsOrNotes(newCardsOrNotes)
}
val newTruncate = dialogView.findViewById<CheckBox>(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<RadioButton>(R.id.select_cards_mode).isChecked = true
} else {
dialogView.findViewById<RadioButton>(R.id.select_notes_mode).isChecked = true
}

dialogView.findViewById<CheckBox>(R.id.truncate_checkbox).isChecked = isTruncated
dialogView.findViewById<CheckBox>(R.id.truncate_checkbox).isChecked = getIsTruncated()

dialogView.findViewById<LinearLayout>(R.id.action_rename_flag).setOnClickListener {
Timber.d("Rename flag clicked")
Expand All @@ -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
}
}
}
}

0 comments on commit b517320

Please sign in to comment.