Skip to content

Commit

Permalink
NF: use enum and const in ImportDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur-Milchior committed Dec 4, 2024
1 parent a18eae6 commit 114ef10
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/Import.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 17 additions & 17 deletions AnkiDroid/src/main/java/com/ichi2/anki/dialogs/ImportDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -62,7 +71,6 @@ class ImportDialog : AsyncDialogFragment() {
.negativeButton(R.string.dialog_cancel)
.create()
}
else -> null!!
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/utils/ImportUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 114ef10

Please sign in to comment.