Skip to content

Commit

Permalink
Kotlin cleanups (ankidroid#12902)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobozinhoD authored Nov 30, 2022
1 parent c15cc96 commit e605742
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
13 changes: 5 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()")
Expand Down Expand Up @@ -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
Expand All @@ -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<Spinner>(R.id.browser_column2_spinner)
val column2Adapter = ArrayAdapter.createFromResource(
Expand Down Expand Up @@ -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<Card>?) {
if (cards.isNullOrEmpty()) return
protected fun reloadCards(cards: Array<Card>) {
if (cards.isEmpty()) return

val cardIds: MutableSet<Long> = HashSet()
for (c in cards) {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/FieldEditText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e605742

Please sign in to comment.