Skip to content

Commit 3df546a

Browse files
Arthur-Milchiordavid-allison
authored andcommitted
NF: use enum and const in ImportDialog
1 parent b653c81 commit 3df546a

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/Import.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fun Activity.onSelectedCsvForImport(data: Intent) {
7474
}
7575

7676
fun AnkiActivity.showImportDialog(
77-
id: Int,
77+
id: ImportDialog.Type,
7878
importPath: String,
7979
) {
8080
Timber.d("showImportDialog() delegating to ImportDialog")

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ImportDialog.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.ichi2.anki.dialogs
1818

1919
import android.os.Bundle
2020
import androidx.annotation.CheckResult
21-
import androidx.annotation.VisibleForTesting
2221
import androidx.appcompat.app.AlertDialog
2322
import com.ichi2.anki.R
2423
import com.ichi2.anki.utils.ext.dismissAllDialogFragments
@@ -28,6 +27,18 @@ import timber.log.Timber
2827
import java.net.URLDecoder
2928

3029
class ImportDialog : AsyncDialogFragment() {
30+
enum class Type(
31+
val code: Int,
32+
) {
33+
DIALOG_IMPORT_ADD_CONFIRM(0),
34+
DIALOG_IMPORT_REPLACE_CONFIRM(1),
35+
;
36+
37+
companion object {
38+
fun fromCode(code: Int) = Type.entries.first { code == it.code }
39+
}
40+
}
41+
3142
interface ImportDialogListener {
3243
fun importAdd(importPath: String)
3344

@@ -36,14 +47,14 @@ class ImportDialog : AsyncDialogFragment() {
3647

3748
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog {
3849
super.onCreate(savedInstanceState)
39-
val type = requireArguments().getInt("dialogType")
50+
val type = Type.fromCode(requireArguments().getInt(IMPORT_DIALOG_TYPE_KEY))
4051
val dialog = AlertDialog.Builder(requireActivity())
4152
dialog.setCancelable(true)
42-
val packagePath = requireArguments().getString("packagePath")!!
53+
val packagePath = requireArguments().getString(IMPORT_DIALOG_PACKAGE_PATH_KEY)!!
4354
val displayFileName = filenameFromPath(convertToDisplayName(packagePath))
4455

4556
return when (type) {
46-
DIALOG_IMPORT_ADD_CONFIRM -> {
57+
Type.DIALOG_IMPORT_ADD_CONFIRM -> {
4758
dialog
4859
.setTitle(R.string.import_title)
4960
.setMessage(res().getString(R.string.import_dialog_message_add, displayFileName))
@@ -53,7 +64,7 @@ class ImportDialog : AsyncDialogFragment() {
5364
}.negativeButton(R.string.dialog_cancel)
5465
.create()
5566
}
56-
DIALOG_IMPORT_REPLACE_CONFIRM -> {
67+
Type.DIALOG_IMPORT_REPLACE_CONFIRM -> {
5768
dialog
5869
.setTitle(R.string.import_title)
5970
.setMessage(res().getString(R.string.import_message_replace_confirm, displayFileName))
@@ -63,7 +74,6 @@ class ImportDialog : AsyncDialogFragment() {
6374
}.negativeButton(R.string.dialog_cancel)
6475
.create()
6576
}
66-
else -> null!!
6777
}
6878
}
6979

@@ -89,15 +99,8 @@ class ImportDialog : AsyncDialogFragment() {
8999
}
90100

91101
companion object {
92-
const val DIALOG_IMPORT_ADD_CONFIRM = 2
93-
const val DIALOG_IMPORT_REPLACE_CONFIRM = 3
94-
95-
@VisibleForTesting
96-
val dialogTypes =
97-
arrayOf(
98-
DIALOG_IMPORT_ADD_CONFIRM,
99-
DIALOG_IMPORT_REPLACE_CONFIRM,
100-
)
102+
const val IMPORT_DIALOG_TYPE_KEY = "dialogType"
103+
const val IMPORT_DIALOG_PACKAGE_PATH_KEY = "packagePath"
101104

102105
/**
103106
* A set of dialogs which deal with importing a file
@@ -107,13 +110,13 @@ class ImportDialog : AsyncDialogFragment() {
107110
*/
108111
@CheckResult
109112
fun newInstance(
110-
dialogType: Int,
113+
dialogType: Type,
111114
packagePath: String,
112115
): ImportDialog {
113116
val f = ImportDialog()
114117
val args = Bundle()
115-
args.putInt("dialogType", dialogType)
116-
args.putString("packagePath", packagePath)
118+
args.putInt(IMPORT_DIALOG_TYPE_KEY, dialogType.code)
119+
args.putString(IMPORT_DIALOG_PACKAGE_PATH_KEY, packagePath)
117120
f.arguments = args
118121
return f
119122
}

AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ object ImportUtils {
439439
) {
440440
override fun handleAsyncMessage(activity: AnkiActivity) {
441441
// Handle import of collection package APKG
442-
activity.showImportDialog(ImportDialog.DIALOG_IMPORT_REPLACE_CONFIRM, importPath)
442+
activity.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_REPLACE_CONFIRM, importPath)
443443
}
444444

445445
override fun toMessage(): Message =
@@ -462,7 +462,7 @@ object ImportUtils {
462462
) {
463463
override fun handleAsyncMessage(activity: AnkiActivity) {
464464
// Handle import of deck package APKG
465-
activity.showImportDialog(ImportDialog.DIALOG_IMPORT_ADD_CONFIRM, importPath)
465+
activity.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_ADD_CONFIRM, importPath)
466466
}
467467

468468
override fun toMessage(): Message =

AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerImportTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DeckPickerImportTest : RobolectricTest() {
3131
fun importAddShowsImportDialog() {
3232
val deckPicker = super.startActivityNormallyOpenCollectionWithIntent(DeckPickerImport::class.java, Intent())
3333

34-
deckPicker.showImportDialog(ImportDialog.DIALOG_IMPORT_ADD_CONFIRM, "")
34+
deckPicker.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_ADD_CONFIRM, "")
3535

3636
assertThat(deckPicker.getAsyncDialogFragmentClass(), Matchers.typeCompatibleWith(ImportDialog::class.java))
3737
}
@@ -40,7 +40,7 @@ class DeckPickerImportTest : RobolectricTest() {
4040
fun replaceShowsImportDialog() {
4141
val deckPicker = super.startActivityNormallyOpenCollectionWithIntent(DeckPickerImport::class.java, Intent())
4242

43-
deckPicker.showImportDialog(ImportDialog.DIALOG_IMPORT_REPLACE_CONFIRM, "")
43+
deckPicker.showImportDialog(ImportDialog.Type.DIALOG_IMPORT_REPLACE_CONFIRM, "")
4444

4545
assertThat(deckPicker.getAsyncDialogFragmentClass(), Matchers.typeCompatibleWith(ImportDialog::class.java))
4646
}

AnkiDroid/src/test/java/com/ichi2/anki/dialogs/AsyncDialogFragmentsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class AsyncDialogFragmentsTest {
6161

6262
@Test
6363
fun `ImportDialog does not require context`() {
64-
for (dialogType in ImportDialog.dialogTypes) {
64+
for (dialogType in ImportDialog.Type.entries) {
6565
val instance = ImportDialog.newInstance(dialogType, "path")
6666
assertDoesNotThrow("$dialogType message required a context") { instance.notificationMessage }
6767
assertDoesNotThrow("$dialogType title required a context") { instance.notificationTitle }

0 commit comments

Comments
 (0)