From e6057423399d71f7ec205721fc33a802ff85e1ca Mon Sep 17 00:00:00 2001 From: Robozinho <65715921+RobozinhoD@users.noreply.github.com> Date: Wed, 30 Nov 2022 09:17:35 -0300 Subject: [PATCH] Kotlin cleanups (#12902) --- .../main/java/com/ichi2/anki/CardBrowser.kt | 13 +++++-------- .../main/java/com/ichi2/anki/DeckPicker.kt | 1 - .../main/java/com/ichi2/anki/FieldEditText.kt | 3 +-- .../ichi2/anki/NavigationDrawerActivity.kt | 1 - .../anki/multimediacard/fields/ImageField.kt | 19 ++++++++----------- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt index 322872248621..eca8d035faf2 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt @@ -103,7 +103,6 @@ import kotlin.math.min @Suppress("LeakingThis") // The class is only 'open' due to testing @KotlinCleanup("scan through this class and add attributes - not started") -@KotlinCleanup("Add TextUtils.isNotNullOrEmpty accepting nulls and use it. Remove TextUtils import") open class CardBrowser : NavigationDrawerActivity(), SubtitleListener, @@ -555,7 +554,6 @@ open class CardBrowser : } // Finish initializing the activity after the collection has been correctly loaded - @KotlinCleanup("preferences.edit { }") override fun onCollectionLoaded(col: com.ichi2.libanki.Collection) { super.onCollectionLoaded(col) Timber.d("onCollectionLoaded()") @@ -584,7 +582,7 @@ open class CardBrowser : ) column1Adapter.setDropDownViewResource(R.layout.spinner_custom_layout) cardsColumn1Spinner.adapter = column1Adapter - mColumn1Index = AnkiDroidApp.getSharedPrefs(baseContext).getInt("cardBrowserColumn1", 0) + mColumn1Index = preferences.getInt("cardBrowserColumn1", 0) cardsColumn1Spinner.onItemSelectedListener = object : OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) { // If a new column was selected then change the key used to map from mCards to the column TextView @@ -604,7 +602,7 @@ open class CardBrowser : } } // Load default value for column2 selection - mColumn2Index = AnkiDroidApp.getSharedPrefs(baseContext).getInt("cardBrowserColumn2", 0) + mColumn2Index = preferences.getInt("cardBrowserColumn2", 0) // Setup the column 2 heading as a spinner so that users can easily change the column type val cardsColumn2Spinner = findViewById(R.id.browser_column2_spinner) val column2Adapter = ArrayAdapter.createFromResource( @@ -2238,9 +2236,8 @@ open class CardBrowser : /** * Reloads the data of the cards, taking on their current values from the database. */ - @KotlinCleanup("cards.isNullOrEmpty()") - protected fun reloadCards(cards: Array?) { - if (cards.isNullOrEmpty()) return + protected fun reloadCards(cards: Array) { + if (cards.isEmpty()) return val cardIds: MutableSet = HashSet() for (c in cards) { @@ -2715,7 +2712,7 @@ open class CardBrowser : private const val PERSISTENT_STATE_FILE = "DeckPickerState" private const val LAST_DECK_ID_KEY = "lastDeckId" const val CARD_NOT_AVAILABLE = -1 - @KotlinCleanup(".edit { }") + fun clearLastDeckId() { val context: Context = AnkiDroidApp.instance context.getSharedPreferences(PERSISTENT_STATE_FILE, 0).edit { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt b/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt index b8c4a8cb694e..3eed74f60f34 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt @@ -1377,7 +1377,6 @@ open class DeckPicker : * Show a simple snackbar message or notification if the activity is not in foreground * @param messageResource String resource for message */ - @KotlinCleanup("nullOrEmpty") fun showSyncLogMessage(@StringRes messageResource: Int, syncMessage: String?) { if (mActivityPaused) { val res = AnkiDroidApp.appResources diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/FieldEditText.kt b/AnkiDroid/src/main/java/com/ichi2/anki/FieldEditText.kt index 59b84323c424..fbfd800f403b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/FieldEditText.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/FieldEditText.kt @@ -257,9 +257,8 @@ class FieldEditText : FixedEditText, NoteService.NoteField { fun onSelectionChanged(selStart: Int, selEnd: Int) } - @KotlinCleanup("non-null") fun interface ImagePasteListener { - fun onImagePaste(editText: EditText?, uri: Uri?): Boolean + fun onImagePaste(editText: EditText, uri: Uri?): Boolean } companion object { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NavigationDrawerActivity.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NavigationDrawerActivity.kt index 655c229084b8..efcafda7a8b2 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NavigationDrawerActivity.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NavigationDrawerActivity.kt @@ -105,7 +105,6 @@ abstract class NavigationDrawerActivity : } // Navigation drawer initialisation - @KotlinCleanup("use .apply on enableToolbar") protected fun initNavigationDrawer(mainView: View) { // Create inherited navigation drawer layout here so that it can be used by parent class mDrawerLayout = mainView.findViewById(R.id.drawer_layout) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/multimediacard/fields/ImageField.kt b/AnkiDroid/src/main/java/com/ichi2/anki/multimediacard/fields/ImageField.kt index 6fa729bb8b3c..a3c5adfbc721 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/multimediacard/fields/ImageField.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/multimediacard/fields/ImageField.kt @@ -82,23 +82,20 @@ class ImageField : FieldBase(), IField { } @VisibleForTesting - @KotlinCleanup("remove ? from value") - fun getImageFullPath(col: Collection, value: String?): String { + fun getImageFullPath(col: Collection, value: String): String { val path = parseImageSrcFromHtml(value) - if ("" == path) { - return "" + + return if (path.isNotEmpty()) { + "${col.media.dir()}/$path" + } else { + "" } - val mediaDir = col.media.dir() + "/" - return mediaDir + path } @VisibleForTesting @CheckResult - @KotlinCleanup("remove ? from html") - fun parseImageSrcFromHtml(html: String?): String { - return if (html == null) { - "" - } else try { + fun parseImageSrcFromHtml(html: String): String { + return try { val doc = Jsoup.parseBodyFragment(html) val image = doc.selectFirst("img[src]") ?: return "" image.attr("src")