0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 12:02:16 +02:00

Use fragments resources on AsyncDialogFragment

To avoid bugs like #12448
This commit is contained in:
RobozinhoD 2022-12-09 19:37:56 -03:00 committed by lukstbit
parent 8f602ec6a8
commit 6ea36860a8
6 changed files with 49 additions and 61 deletions

View File

@ -15,11 +15,8 @@
****************************************************************************************/
package com.ichi2.anki.dialogs
import android.content.res.Resources
import android.os.Message
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.analytics.AnalyticsDialogFragment
import timber.log.Timber
abstract class AsyncDialogFragment : AnalyticsDialogFragment() {
/* provide methods for text to show in notification bar when the DialogFragment
@ -29,13 +26,4 @@ abstract class AsyncDialogFragment : AnalyticsDialogFragment() {
abstract val notificationMessage: String?
abstract val notificationTitle: String
open val dialogHandlerMessage: Message? get() = null
protected fun res(): Resources {
return try {
AnkiDroidApp.appResources
} catch (e: Exception) {
Timber.w(e, "AnkiDroidApp.getAppResources failure. Returning Fragment resources as fallback.")
resources
}
}
}

View File

@ -352,19 +352,19 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
DIALOG_LOAD_FAILED -> if (databaseCorruptFlag) {
// The sqlite database has been corrupted (DatabaseErrorHandler.onCorrupt() was called)
// Show a specific message appropriate for the situation
res().getString(R.string.corrupt_db_message, res().getString(R.string.repair_deck))
resources.getString(R.string.corrupt_db_message, resources.getString(R.string.repair_deck))
} else {
// Generic message shown when a libanki task failed
res().getString(R.string.access_collection_failed_message, res().getString(R.string.link_help))
resources.getString(R.string.access_collection_failed_message, resources.getString(R.string.link_help))
}
DIALOG_DB_ERROR -> res().getString(R.string.answering_error_message)
DIALOG_REPAIR_COLLECTION -> res().getString(R.string.repair_deck_dialog, BackupManager.BROKEN_COLLECTIONS_SUFFIX)
DIALOG_RESTORE_BACKUP -> res().getString(R.string.backup_restore_no_backups)
DIALOG_NEW_COLLECTION -> res().getString(R.string.backup_del_collection_question)
DIALOG_CONFIRM_DATABASE_CHECK -> res().getString(R.string.check_db_warning)
DIALOG_CONFIRM_RESTORE_BACKUP -> res().getString(R.string.restore_backup)
DIALOG_FULL_SYNC_FROM_SERVER -> res().getString(R.string.backup_full_sync_from_server_question)
DIALOG_DB_LOCKED -> res().getString(R.string.database_locked_summary)
DIALOG_DB_ERROR -> resources.getString(R.string.answering_error_message)
DIALOG_REPAIR_COLLECTION -> resources.getString(R.string.repair_deck_dialog, BackupManager.BROKEN_COLLECTIONS_SUFFIX)
DIALOG_RESTORE_BACKUP -> resources.getString(R.string.backup_restore_no_backups)
DIALOG_NEW_COLLECTION -> resources.getString(R.string.backup_del_collection_question)
DIALOG_CONFIRM_DATABASE_CHECK -> resources.getString(R.string.check_db_warning)
DIALOG_CONFIRM_RESTORE_BACKUP -> resources.getString(R.string.restore_backup)
DIALOG_FULL_SYNC_FROM_SERVER -> resources.getString(R.string.backup_full_sync_from_server_question)
DIALOG_DB_LOCKED -> resources.getString(R.string.database_locked_summary)
INCOMPATIBLE_DB_VERSION -> {
var databaseVersion = -1
try {
@ -377,7 +377,7 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
} else {
Consts.BACKEND_SCHEMA_VERSION
}
res().getString(
resources.getString(
R.string.incompatible_database_version_summary,
schemaVersion,
databaseVersion
@ -387,22 +387,22 @@ class DatabaseErrorDialog : AsyncDialogFragment() {
}
private val title: String
get() = when (requireArguments().getInt("dialogType")) {
DIALOG_LOAD_FAILED -> res().getString(R.string.open_collection_failed_title)
DIALOG_ERROR_HANDLING -> res().getString(R.string.error_handling_title)
DIALOG_REPAIR_COLLECTION -> res().getString(R.string.dialog_positive_repair)
DIALOG_RESTORE_BACKUP -> res().getString(R.string.backup_restore)
DIALOG_NEW_COLLECTION -> res().getString(R.string.backup_new_collection)
DIALOG_CONFIRM_DATABASE_CHECK -> res().getString(R.string.check_db_title)
DIALOG_CONFIRM_RESTORE_BACKUP -> res().getString(R.string.restore_backup_title)
DIALOG_FULL_SYNC_FROM_SERVER -> res().getString(R.string.backup_full_sync_from_server)
DIALOG_DB_LOCKED -> res().getString(R.string.database_locked_title)
INCOMPATIBLE_DB_VERSION -> res().getString(R.string.incompatible_database_version_title)
DIALOG_DB_ERROR -> res().getString(R.string.answering_error_title)
else -> res().getString(R.string.answering_error_title)
DIALOG_LOAD_FAILED -> resources.getString(R.string.open_collection_failed_title)
DIALOG_ERROR_HANDLING -> resources.getString(R.string.error_handling_title)
DIALOG_REPAIR_COLLECTION -> resources.getString(R.string.dialog_positive_repair)
DIALOG_RESTORE_BACKUP -> resources.getString(R.string.backup_restore)
DIALOG_NEW_COLLECTION -> resources.getString(R.string.backup_new_collection)
DIALOG_CONFIRM_DATABASE_CHECK -> resources.getString(R.string.check_db_title)
DIALOG_CONFIRM_RESTORE_BACKUP -> resources.getString(R.string.restore_backup_title)
DIALOG_FULL_SYNC_FROM_SERVER -> resources.getString(R.string.backup_full_sync_from_server)
DIALOG_DB_LOCKED -> resources.getString(R.string.database_locked_title)
INCOMPATIBLE_DB_VERSION -> resources.getString(R.string.incompatible_database_version_title)
DIALOG_DB_ERROR -> resources.getString(R.string.answering_error_title)
else -> resources.getString(R.string.answering_error_title)
}
override val notificationMessage: String? get() = message
override val notificationTitle: String get() = res().getString(R.string.answering_error_title)
override val notificationTitle: String get() = resources.getString(R.string.answering_error_title)
override val dialogHandlerMessage: Message
get() {

View File

@ -59,8 +59,8 @@ class ExportCompleteDialog(private val listener: ExportCompleteDialogListener) :
}
override val notificationTitle: String
get() = res().getString(R.string.export_success_title)
get() = resources.getString(R.string.export_success_title)
override val notificationMessage: String
get() = res().getString(R.string.export_success_message)
get() = resources.getString(R.string.export_success_message)
}

View File

@ -78,12 +78,12 @@ class ImportDialog : AsyncDialogFragment() {
override val notificationMessage: String
get() {
return res().getString(R.string.import_interrupted)
return resources.getString(R.string.import_interrupted)
}
override val notificationTitle: String
get() {
return res().getString(R.string.import_title)
return resources.getString(R.string.import_title)
}
fun dismissAllDialogFragments() {

View File

@ -51,28 +51,28 @@ class MediaCheckDialog : AsyncDialogFragment() {
// Generate report
val report = StringBuilder()
if (invalid!!.isNotEmpty()) {
report.append(String.format(res().getString(R.string.check_media_invalid), invalid.size))
report.append(String.format(resources.getString(R.string.check_media_invalid), invalid.size))
}
if (unused!!.isNotEmpty()) {
if (report.isNotEmpty()) {
report.append("\n")
}
report.append(String.format(res().getString(R.string.check_media_unused), unused.size))
report.append(String.format(resources.getString(R.string.check_media_unused), unused.size))
}
if (nohave!!.isNotEmpty()) {
if (report.isNotEmpty()) {
report.append("\n")
}
report.append(String.format(res().getString(R.string.check_media_nohave), nohave.size))
report.append(String.format(resources.getString(R.string.check_media_nohave), nohave.size))
}
if (report.isEmpty()) {
report.append(res().getString(R.string.check_media_no_unused_missing))
report.append(resources.getString(R.string.check_media_no_unused_missing))
}
// We also prefix the report with a message about the media db being rebuilt, since
// we do a full media scan and update the db on each media check on AnkiDroid.
val reportStr = """
${res().getString(R.string.check_media_db_updated)}
${resources.getString(R.string.check_media_db_updated)}
$report
""".trimIndent()
@ -115,17 +115,17 @@ class MediaCheckDialog : AsyncDialogFragment() {
override val notificationMessage: String
get() {
return when (requireArguments().getInt("dialogType")) {
DIALOG_CONFIRM_MEDIA_CHECK -> res().getString(R.string.check_media_warning)
DIALOG_MEDIA_CHECK_RESULTS -> res().getString(R.string.check_media_acknowledge)
else -> res().getString(R.string.app_name)
DIALOG_CONFIRM_MEDIA_CHECK -> resources.getString(R.string.check_media_warning)
DIALOG_MEDIA_CHECK_RESULTS -> resources.getString(R.string.check_media_acknowledge)
else -> resources.getString(R.string.app_name)
}
}
override val notificationTitle: String
get() {
return if (requireArguments().getInt("dialogType") == DIALOG_CONFIRM_MEDIA_CHECK) {
res().getString(R.string.check_media_title)
} else res().getString(R.string.app_name)
resources.getString(R.string.check_media_title)
} else resources.getString(R.string.app_name)
}
override val dialogHandlerMessage: Message

View File

@ -187,10 +187,10 @@ class SyncErrorDialog : AsyncDialogFragment() {
private val title: String
get() = when (requireArguments().getInt("dialogType")) {
DIALOG_USER_NOT_LOGGED_IN_SYNC -> res().getString(R.string.not_logged_in_title)
DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_LOCAL, DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_REMOTE -> res().getString(R.string.sync_conflict_replace_title)
DIALOG_SYNC_CONFLICT_RESOLUTION -> res().getString(R.string.sync_conflict_title_new)
else -> res().getString(R.string.sync_error)
DIALOG_USER_NOT_LOGGED_IN_SYNC -> resources.getString(R.string.not_logged_in_title)
DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_LOCAL, DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_REMOTE -> resources.getString(R.string.sync_conflict_replace_title)
DIALOG_SYNC_CONFLICT_RESOLUTION -> resources.getString(R.string.sync_conflict_title_new)
else -> resources.getString(R.string.sync_error)
}
/**
@ -201,17 +201,17 @@ class SyncErrorDialog : AsyncDialogFragment() {
override val notificationTitle: String
get() {
return if (requireArguments().getInt("dialogType") == DIALOG_USER_NOT_LOGGED_IN_SYNC) {
res().getString(R.string.sync_error)
resources.getString(R.string.sync_error)
} else title
}
private val message: String?
get() = when (requireArguments().getInt("dialogType")) {
DIALOG_USER_NOT_LOGGED_IN_SYNC -> res().getString(R.string.login_create_account_message)
DIALOG_CONNECTION_ERROR -> res().getString(R.string.connection_error_message)
DIALOG_SYNC_CONFLICT_RESOLUTION -> res().getString(R.string.sync_conflict_message_new)
DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_LOCAL, DIALOG_SYNC_SANITY_ERROR_CONFIRM_KEEP_LOCAL -> res().getString(R.string.sync_conflict_local_confirm_new)
DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_REMOTE, DIALOG_SYNC_SANITY_ERROR_CONFIRM_KEEP_REMOTE -> res().getString(R.string.sync_conflict_remote_confirm_new)
DIALOG_USER_NOT_LOGGED_IN_SYNC -> resources.getString(R.string.login_create_account_message)
DIALOG_CONNECTION_ERROR -> resources.getString(R.string.connection_error_message)
DIALOG_SYNC_CONFLICT_RESOLUTION -> resources.getString(R.string.sync_conflict_message_new)
DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_LOCAL, DIALOG_SYNC_SANITY_ERROR_CONFIRM_KEEP_LOCAL -> resources.getString(R.string.sync_conflict_local_confirm_new)
DIALOG_SYNC_CONFLICT_CONFIRM_KEEP_REMOTE, DIALOG_SYNC_SANITY_ERROR_CONFIRM_KEEP_REMOTE -> resources.getString(R.string.sync_conflict_remote_confirm_new)
DIALOG_SYNC_CORRUPT_COLLECTION -> {
val syncMessage = requireArguments().getString("dialogMessage")
val repairUrl = getString(R.string.repair_deck)
@ -229,7 +229,7 @@ class SyncErrorDialog : AsyncDialogFragment() {
override val notificationMessage: String?
get() {
return if (requireArguments().getInt("dialogType") == DIALOG_USER_NOT_LOGGED_IN_SYNC) {
res().getString(R.string.not_logged_in_title)
resources.getString(R.string.not_logged_in_title)
} else message
}