Skip to content

Commit

Permalink
replace activity? by requireActivity
Browse files Browse the repository at this point in the history
Given that we already required the deck picker that ensures the
activity is non null, this can't cause more crash, except in
KEYCODE_BACK in `onCreateDialog` which is a place where the activity
should exists anyway.
  • Loading branch information
Arthur-Milchior authored and mikehardy committed Dec 8, 2024
1 parent 79ddf78 commit f023dea
Showing 1 changed file with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import com.ichi2.anki.dialogs.DatabaseErrorDialog.DatabaseErrorDialogType.INCOMP
import com.ichi2.anki.dialogs.ImportFileSelectionFragment.ImportOptions
import com.ichi2.anki.isLoggedIn
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.requireAnkiActivity
import com.ichi2.anki.servicelayer.DebugInfoService
import com.ichi2.anki.showImportDialog
import com.ichi2.anki.utils.ext.dismissAllDialogFragments
Expand Down Expand Up @@ -112,7 +113,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
message(text = message)
setIcon(R.drawable.ic_warning)
positiveButton(R.string.error_handling_options) {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_ERROR_HANDLING, exceptionData)
showDatabaseErrorDialog(DIALOG_ERROR_HANDLING)
}
negativeButton(R.string.close) {
closeCollectionAndFinish()
Expand All @@ -128,11 +129,11 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
message(text = message)
setIcon(R.drawable.ic_warning)
positiveButton(R.string.error_handling_options) {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_ERROR_HANDLING, exceptionData)
showDatabaseErrorDialog(DIALOG_ERROR_HANDLING)
}
negativeButton(R.string.answering_error_report) {
requireDeckPicker().sendErrorReport()
activity?.dismissAllDialogFragments()
requireActivity().dismissAllDialogFragments()
}
neutralButton(R.string.close) {
closeCollectionAndFinish()
Expand Down Expand Up @@ -179,23 +180,23 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
listItems(items = options) { _, index ->
when (values[index]) {
ErrorHandlingEntries.RETRY -> {
ActivityCompat.recreate(requireDeckPicker())
ActivityCompat.recreate(requireActivity())
return@listItems
}
ErrorHandlingEntries.REPAIR -> {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_REPAIR_COLLECTION, exceptionData)
showDatabaseErrorDialog(DIALOG_REPAIR_COLLECTION)
return@listItems
}
ErrorHandlingEntries.RESTORE -> {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_RESTORE_BACKUP, exceptionData)
showDatabaseErrorDialog(DIALOG_RESTORE_BACKUP)
return@listItems
}
ErrorHandlingEntries.ONE_WAY -> {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_ONE_WAY_SYNC_FROM_SERVER, exceptionData)
showDatabaseErrorDialog(DIALOG_ONE_WAY_SYNC_FROM_SERVER)
return@listItems
}
ErrorHandlingEntries.NEW -> {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_NEW_COLLECTION, exceptionData)
showDatabaseErrorDialog(DIALOG_NEW_COLLECTION)
return@listItems
}

Expand All @@ -220,7 +221,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
setIcon(R.drawable.ic_warning)
positiveButton(R.string.dialog_positive_repair) {
requireDeckPicker().repairCollection()
activity?.dismissAllDialogFragments()
requireActivity().dismissAllDialogFragments()
}
negativeButton(R.string.dialog_cancel)
}
Expand All @@ -233,7 +234,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
alertDialog.title(R.string.backup_restore)
.title(text = message)
.positiveButton(R.string.dialog_ok) {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_ERROR_HANDLING, exceptionData)
showDatabaseErrorDialog(DIALOG_ERROR_HANDLING)
}
} else {
// Show backups sorted with latest on top
Expand All @@ -254,7 +255,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
requireDeckPicker().restoreFromBackup(
backups[index].path
)
activity?.dismissAllDialogFragments()
requireActivity().dismissAllDialogFragments()
} else {
// otherwise show an error dialog
AlertDialog.Builder(requireActivity()).show {
Expand All @@ -268,7 +269,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
alertDialog.setOnKeyListener { _: DialogInterface?, keyCode: Int, _: KeyEvent? ->
if (keyCode == KeyEvent.KEYCODE_BACK) {
Timber.i("DIALOG_RESTORE_BACKUP caught hardware back button")
activity?.dismissAllDialogFragments()
requireActivity().dismissAllDialogFragments()
return@setOnKeyListener true
}
false
Expand All @@ -290,7 +291,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
if (BackupManager.moveDatabaseToBrokenDirectory(path1, false, time)) {
ActivityCompat.recreate(requireDeckPicker())
} else {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_LOAD_FAILED, exceptionData)
showDatabaseErrorDialog(DIALOG_LOAD_FAILED)
}
}
negativeButton(R.string.dialog_cancel)
Expand All @@ -303,7 +304,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
message(text = message)
positiveButton(R.string.dialog_ok) {
requireDeckPicker().integrityCheck()
activity?.dismissAllDialogFragments()
requireActivity().dismissAllDialogFragments()
}
negativeButton(R.string.dialog_cancel)
}
Expand All @@ -314,7 +315,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
title(R.string.restore_backup_title)
message(text = message)
positiveButton(R.string.dialog_continue) {
requireDeckPicker().showDatabaseErrorDialog(DIALOG_RESTORE_BACKUP, exceptionData)
showDatabaseErrorDialog(DIALOG_RESTORE_BACKUP)
}
negativeButton(R.string.dialog_cancel)
}
Expand All @@ -326,7 +327,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
message(text = message)
positiveButton(R.string.dialog_positive_overwrite) {
requireDeckPicker().sync(ConflictResolution.FULL_DOWNLOAD)
activity?.dismissAllDialogFragments()
requireActivity().dismissAllDialogFragments()
}
negativeButton(R.string.dialog_cancel)
}
Expand Down Expand Up @@ -360,13 +361,11 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
}
listItemsAndMessage(message = message, items = options) { _, index: Int ->
when (values[index]) {
IncompatibleDbVersionEntries.RESTORE -> requireDeckPicker().showDatabaseErrorDialog(
DIALOG_RESTORE_BACKUP,
exceptionData
IncompatibleDbVersionEntries.RESTORE -> showDatabaseErrorDialog(
DIALOG_RESTORE_BACKUP
)
IncompatibleDbVersionEntries.ONE_WAY -> requireDeckPicker().showDatabaseErrorDialog(
DIALOG_ONE_WAY_SYNC_FROM_SERVER,
exceptionData
IncompatibleDbVersionEntries.ONE_WAY -> showDatabaseErrorDialog(
DIALOG_ONE_WAY_SYNC_FROM_SERVER
)
}
}
Expand All @@ -387,7 +386,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
title(R.string.directory_inaccessible_after_uninstall)
listItemsAndMessage(message = message, items = listItems.map { getString(it.stringRes) }) { _, index: Int ->
val listItem = listItems[index]
listItem.onClick(requireDeckPicker())
listItem.onClick(requireAnkiActivity())
if (listItem.dismissesDialog) {
dismiss()
}
Expand All @@ -398,6 +397,11 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
}
}

/** @see DeckPicker.showDatabaseErrorDialog */
private fun showDatabaseErrorDialog(errorDialogType: DatabaseErrorDialogType) {
requireDeckPicker().showDatabaseErrorDialog(errorDialogType, exceptionData)
}

/**
* Collects the current exception's stack trace and debug information,
* and copies the combines information to the Android clipboard.
Expand Down Expand Up @@ -521,7 +525,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
}

private fun closeCollectionAndFinish() {
(activity as AnkiActivity).closeCollectionAndFinish()
(requireActivity() as AnkiActivity).closeCollectionAndFinish()
} // Generic message shown when a libanki task failed

// The sqlite database has been corrupted (DatabaseErrorHandler.onCorrupt() was called)
Expand Down

0 comments on commit f023dea

Please sign in to comment.