From 114ef10f62f44094453f51c5f38b7cdec6ef745a Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Sun, 3 Nov 2024 17:03:05 +0100 Subject: [PATCH] NF: use enum and const in ImportDialog --- .../src/main/java/com/ichi2/anki/Import.kt | 2 +- .../com/ichi2/anki/dialogs/ImportDialog.kt | 34 +++++++++---------- .../main/java/com/ichi2/utils/ImportUtils.kt | 4 +-- .../com/ichi2/anki/DeckPickerImportTest.kt | 4 +-- .../anki/dialogs/AsyncDialogFragmentsTest.kt | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Import.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Import.kt index c61f26d3bd1f..d2675ddb686a 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/Import.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Import.kt @@ -71,7 +71,7 @@ fun Activity.onSelectedCsvForImport(data: Intent) { stackBuilder.startActivities() } -fun AnkiActivity.showImportDialog(id: Int, importPath: String) { +fun AnkiActivity.showImportDialog(id: ImportDialog.Type, importPath: String) { Timber.d("showImportDialog() delegating to ImportDialog") val newFragment: AsyncDialogFragment = ImportDialog.newInstance(id, importPath) showAsyncDialogFragment(newFragment) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ImportDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ImportDialog.kt index a1326eb3efa2..24c593b666ed 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ImportDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ImportDialog.kt @@ -18,15 +18,24 @@ package com.ichi2.anki.dialogs import android.os.Bundle import androidx.annotation.CheckResult -import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AlertDialog import com.ichi2.anki.R +import com.ichi2.anki.dialogs.ImportDialog.Type.Companion.toType import com.ichi2.utils.negativeButton import com.ichi2.utils.positiveButton import timber.log.Timber import java.net.URLDecoder +const val IMPORT_DIALOG_TYPE_KEY = "dialogType" +const val IMPORT_DIALOG_PACKAGE_PATH_KEY = "packagePath" + class ImportDialog : AsyncDialogFragment() { + enum class Type(val code: Int) { + DIALOG_IMPORT_ADD_CONFIRM(0), DIALOG_IMPORT_REPLACE_CONFIRM(1); + companion object { + fun Int.toType() = Type.entries.first { this == it.code } + } + } interface ImportDialogListener { fun importAdd(importPath: String) fun importReplace(importPath: String) @@ -35,14 +44,14 @@ class ImportDialog : AsyncDialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog { super.onCreate(savedInstanceState) - val type = requireArguments().getInt("dialogType") + val type = requireArguments().getInt(IMPORT_DIALOG_TYPE_KEY).toType() val dialog = AlertDialog.Builder(requireActivity()) dialog.setCancelable(true) - val packagePath = requireArguments().getString("packagePath")!! + val packagePath = requireArguments().getString(IMPORT_DIALOG_PACKAGE_PATH_KEY)!! val displayFileName = filenameFromPath(convertToDisplayName(packagePath)) return when (type) { - DIALOG_IMPORT_ADD_CONFIRM -> { + Type.DIALOG_IMPORT_ADD_CONFIRM -> { dialog.setTitle(R.string.import_title) .setMessage(res().getString(R.string.import_dialog_message_add, displayFileName)) .positiveButton(R.string.import_message_add) { @@ -52,7 +61,7 @@ class ImportDialog : AsyncDialogFragment() { .negativeButton(R.string.dialog_cancel) .create() } - DIALOG_IMPORT_REPLACE_CONFIRM -> { + Type.DIALOG_IMPORT_REPLACE_CONFIRM -> { dialog.setTitle(R.string.import_title) .setMessage(res().getString(R.string.import_message_replace_confirm, displayFileName)) .positiveButton(R.string.dialog_positive_replace) { @@ -62,7 +71,6 @@ class ImportDialog : AsyncDialogFragment() { .negativeButton(R.string.dialog_cancel) .create() } - else -> null!! } } @@ -92,14 +100,6 @@ class ImportDialog : AsyncDialogFragment() { } companion object { - const val DIALOG_IMPORT_ADD_CONFIRM = 2 - const val DIALOG_IMPORT_REPLACE_CONFIRM = 3 - - @VisibleForTesting - val dialogTypes = arrayOf( - DIALOG_IMPORT_ADD_CONFIRM, - DIALOG_IMPORT_REPLACE_CONFIRM - ) /** * A set of dialogs which deal with importing a file @@ -108,11 +108,11 @@ class ImportDialog : AsyncDialogFragment() { * @param packagePath the path of the package to import */ @CheckResult - fun newInstance(dialogType: Int, packagePath: String): ImportDialog { + fun newInstance(dialogType: Type, packagePath: String): ImportDialog { val f = ImportDialog() val args = Bundle() - args.putInt("dialogType", dialogType) - args.putString("packagePath", packagePath) + args.putInt(IMPORT_DIALOG_TYPE_KEY, dialogType.code) + args.putString(IMPORT_DIALOG_PACKAGE_PATH_KEY, packagePath) f.arguments = args return f } diff --git a/AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt b/AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt index 3240f5629361..a143e3f7b09a 100644 --- a/AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt +++ b/AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt @@ -408,7 +408,7 @@ object ImportUtils { ) { override fun handleAsyncMessage(activity: AnkiActivity) { // Handle import of collection package APKG - activity.showImportDialog(ImportDialog.DIALOG_IMPORT_REPLACE_CONFIRM, importPath) + activity.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_REPLACE_CONFIRM, importPath) } override fun toMessage(): Message = Message.obtain().apply { @@ -429,7 +429,7 @@ object ImportUtils { ) { override fun handleAsyncMessage(activity: AnkiActivity) { // Handle import of deck package APKG - activity.showImportDialog(ImportDialog.DIALOG_IMPORT_ADD_CONFIRM, importPath) + activity.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_ADD_CONFIRM, importPath) } override fun toMessage(): Message = Message.obtain().apply { diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerImportTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerImportTest.kt index a020bbd2d83a..cb74864dd1ba 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerImportTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerImportTest.kt @@ -31,7 +31,7 @@ class DeckPickerImportTest : RobolectricTest() { fun importAddShowsImportDialog() { val deckPicker = super.startActivityNormallyOpenCollectionWithIntent(DeckPickerImport::class.java, Intent()) - deckPicker.showImportDialog(ImportDialog.DIALOG_IMPORT_ADD_CONFIRM, "") + deckPicker.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_ADD_CONFIRM, "") assertThat(deckPicker.getAsyncDialogFragmentClass(), Matchers.typeCompatibleWith(ImportDialog::class.java)) } @@ -40,7 +40,7 @@ class DeckPickerImportTest : RobolectricTest() { fun replaceShowsImportDialog() { val deckPicker = super.startActivityNormallyOpenCollectionWithIntent(DeckPickerImport::class.java, Intent()) - deckPicker.showImportDialog(ImportDialog.DIALOG_IMPORT_REPLACE_CONFIRM, "") + deckPicker.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_REPLACE_CONFIRM, "") assertThat(deckPicker.getAsyncDialogFragmentClass(), Matchers.typeCompatibleWith(ImportDialog::class.java)) } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/AsyncDialogFragmentsTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/AsyncDialogFragmentsTest.kt index 4a205714fb27..72affccbe569 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/AsyncDialogFragmentsTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/AsyncDialogFragmentsTest.kt @@ -61,7 +61,7 @@ class AsyncDialogFragmentsTest { @Test fun `ImportDialog does not require context`() { - for (dialogType in ImportDialog.dialogTypes) { + for (dialogType in ImportDialog.Type.entries) { val instance = ImportDialog.newInstance(dialogType, "path") assertDoesNotThrow("$dialogType message required a context") { instance.notificationMessage } assertDoesNotThrow("$dialogType title required a context") { instance.notificationTitle }