@@ -18,7 +18,6 @@ package com.ichi2.anki.dialogs
18
18
19
19
import android.os.Bundle
20
20
import androidx.annotation.CheckResult
21
- import androidx.annotation.VisibleForTesting
22
21
import androidx.appcompat.app.AlertDialog
23
22
import com.ichi2.anki.R
24
23
import com.ichi2.anki.utils.ext.dismissAllDialogFragments
@@ -28,6 +27,18 @@ import timber.log.Timber
28
27
import java.net.URLDecoder
29
28
30
29
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
+
31
42
interface ImportDialogListener {
32
43
fun importAdd (importPath : String )
33
44
@@ -36,14 +47,14 @@ class ImportDialog : AsyncDialogFragment() {
36
47
37
48
override fun onCreateDialog (savedInstanceState : Bundle ? ): AlertDialog {
38
49
super .onCreate(savedInstanceState)
39
- val type = requireArguments().getInt(" dialogType " )
50
+ val type = Type .fromCode( requireArguments().getInt(IMPORT_DIALOG_TYPE_KEY ) )
40
51
val dialog = AlertDialog .Builder (requireActivity())
41
52
dialog.setCancelable(true )
42
- val packagePath = requireArguments().getString(" packagePath " )!!
53
+ val packagePath = requireArguments().getString(IMPORT_DIALOG_PACKAGE_PATH_KEY )!!
43
54
val displayFileName = filenameFromPath(convertToDisplayName(packagePath))
44
55
45
56
return when (type) {
46
- DIALOG_IMPORT_ADD_CONFIRM -> {
57
+ Type . DIALOG_IMPORT_ADD_CONFIRM -> {
47
58
dialog
48
59
.setTitle(R .string.import_title)
49
60
.setMessage(res().getString(R .string.import_dialog_message_add, displayFileName))
@@ -53,7 +64,7 @@ class ImportDialog : AsyncDialogFragment() {
53
64
}.negativeButton(R .string.dialog_cancel)
54
65
.create()
55
66
}
56
- DIALOG_IMPORT_REPLACE_CONFIRM -> {
67
+ Type . DIALOG_IMPORT_REPLACE_CONFIRM -> {
57
68
dialog
58
69
.setTitle(R .string.import_title)
59
70
.setMessage(res().getString(R .string.import_message_replace_confirm, displayFileName))
@@ -63,7 +74,6 @@ class ImportDialog : AsyncDialogFragment() {
63
74
}.negativeButton(R .string.dialog_cancel)
64
75
.create()
65
76
}
66
- else -> null !!
67
77
}
68
78
}
69
79
@@ -89,15 +99,8 @@ class ImportDialog : AsyncDialogFragment() {
89
99
}
90
100
91
101
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"
101
104
102
105
/* *
103
106
* A set of dialogs which deal with importing a file
@@ -107,13 +110,13 @@ class ImportDialog : AsyncDialogFragment() {
107
110
*/
108
111
@CheckResult
109
112
fun newInstance (
110
- dialogType : Int ,
113
+ dialogType : Type ,
111
114
packagePath : String ,
112
115
): ImportDialog {
113
116
val f = ImportDialog ()
114
117
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)
117
120
f.arguments = args
118
121
return f
119
122
}
0 commit comments