Skip to content

Commit

Permalink
Cleanup the DeckRenameException in libanki
Browse files Browse the repository at this point in the history
Changes:
- extract the DeckRenameException.getLocalizedName() method to a top
level function in the anki package where it was used. This enables
to further decouple the libanki package from android.* dependencies.
- various cleanups to fix code style/improve syntax
  • Loading branch information
lukstbit committed Sep 20, 2023
1 parent f17d3a2 commit ad22652
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Decks
import com.ichi2.libanki.backend.exception.DeckRenameException
import com.ichi2.libanki.getOrCreateFilteredDeck
import com.ichi2.utils.asLocalizedMessage
import com.ichi2.utils.displayKeyboard
import timber.log.Timber
import java.util.function.Consumer
Expand Down Expand Up @@ -130,7 +131,7 @@ class CreateDeckDialog(private val context: Context, private val title: Int, pri
val newDeckId = col.decks.newDyn(deckName)
mOnNewDeckCreated!!.accept(newDeckId)
} catch (ex: DeckRenameException) {
showThemedToast(context, ex.getLocalizedMessage(context.resources), false)
showThemedToast(context, ex.asLocalizedMessage(context), false)
return false
}
return true
Expand Down Expand Up @@ -187,7 +188,7 @@ class CreateDeckDialog(private val context: Context, private val title: Int, pri
} catch (e: DeckRenameException) {
Timber.w(e)
// We get a localized string from libanki to explain the error
showThemedToast(context, e.getLocalizedMessage(context.resources), false)
showThemedToast(context, e.asLocalizedMessage(context), false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.ichi2.libanki.DeckId
import com.ichi2.libanki.backend.exception.DeckRenameException
import com.ichi2.utils.HashUtil.HashMapInit
import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.asLocalizedMessage
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber
Expand Down Expand Up @@ -454,7 +455,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
dyn = try {
decks.get(decks.newDyn(customStudyDeck))!!
} catch (ex: DeckRenameException) {
showThemedToast(requireActivity(), ex.getLocalizedMessage(this.resources), true)
showThemedToast(requireActivity(), ex.asLocalizedMessage(requireContext()), true)
return
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
//noinspection MissingCopyrightHeader #8659
package com.ichi2.libanki.backend.exception

import android.content.res.Resources
import com.ichi2.anki.R

class DeckRenameException
(private val errorCode: Int) : Exception() {
class DeckRenameException(val errorCode: Int) : Exception() {
// region only if FILTERED_NOSUBDECKS
private var mFilteredAncestorName: String? = null
private var mDeckName: String? = null
private var filteredAncestorName: String? = null
private var deckName: String? = null
// endregion

override val message: String?
get() = if (errorCode == FILTERED_NOSUBDECKS) {
"Deck $mDeckName has filtered ancestor $mFilteredAncestorName"
"Deck $deckName has filtered ancestor $filteredAncestorName"
} else {
super.message
}

fun getLocalizedMessage(res: Resources): String {
return when (errorCode) {
ALREADY_EXISTS -> res.getString(R.string.decks_rename_exists)
FILTERED_NOSUBDECKS -> res.getString(R.string.decks_rename_filtered_nosubdecks)
else -> ""
}
}

companion object {
const val ALREADY_EXISTS = 0
private const val FILTERED_NOSUBDECKS = 1
const val FILTERED_NOSUBDECKS = 1

/** Generates a {@link com.ichi2.libanki.backend.exception.DeckRenameException} with additional information in the message */
fun filteredAncestor(deckName: String?, filteredAncestorName: String?): DeckRenameException {
val ex = DeckRenameException(FILTERED_NOSUBDECKS)
ex.mFilteredAncestorName = filteredAncestorName
ex.mDeckName = deckName
return ex
fun filteredAncestor(
deckName: String?,
filteredAncestorName: String?
): DeckRenameException = DeckRenameException(FILTERED_NOSUBDECKS).apply {
this.filteredAncestorName = filteredAncestorName
this.deckName = deckName
}
}
}
7 changes: 7 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/utils/ExceptionUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.annotation.CheckResult
import com.ichi2.anki.CrashReportService
import com.ichi2.anki.R
import com.ichi2.anki.UIUtils
import com.ichi2.libanki.backend.exception.DeckRenameException
import java.io.PrintWriter
import java.io.StringWriter

Expand Down Expand Up @@ -76,3 +77,9 @@ object ExceptionUtil {
}
}
}

fun DeckRenameException.asLocalizedMessage(context: Context): String = when (errorCode) {
DeckRenameException.ALREADY_EXISTS -> context.resources.getString(R.string.decks_rename_exists)
DeckRenameException.FILTERED_NOSUBDECKS -> context.resources.getString(R.string.decks_rename_filtered_nosubdecks)
else -> ""
}

0 comments on commit ad22652

Please sign in to comment.