Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of Kotlin wildcard imports on the project Codestyle file #11852

Closed
BrayanDSO opened this issue Jul 14, 2022 · 17 comments · Fixed by #16742
Closed

Remove usage of Kotlin wildcard imports on the project Codestyle file #11852

BrayanDSO opened this issue Jul 14, 2022 · 17 comments · Fixed by #16742

Comments

@BrayanDSO
Copy link
Member

I agree. Although this one was added automatically by the IDE.

Default for Kotlin is 3 imports to substitute to * (https://stackoverflow.com/questions/49870306/disable-wild-cart-import-in-intellij-android-studio-in-kotlin)

It can be a good first issue to change it on the project codestyle file (https://github.com/ankidroid/Anki-Android/blob/main/.idea/codeStyles/Project.xml)

Originally posted by @BrayanDSO in #11803 (comment)

@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Sep 12, 2022
@BrayanDSO

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Nov 12, 2022
@BrayanDSO BrayanDSO removed the Stale label Nov 14, 2022
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Jan 13, 2023
@BrayanDSO BrayanDSO removed the Stale label Jan 13, 2023
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Apr 13, 2023
@BrayanDSO BrayanDSO removed the Stale label Apr 13, 2023
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Jul 12, 2023
@BrayanDSO BrayanDSO removed the Stale label Jul 12, 2023
@github-actions

This comment has been minimized.

@github-actions github-actions bot added the Stale label Oct 10, 2023
@BrayanDSO BrayanDSO removed the Stale label Oct 11, 2023

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Jan 9, 2024
@BrayanDSO BrayanDSO removed the Stale label Jan 10, 2024

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Apr 9, 2024
@BrayanDSO BrayanDSO removed the Stale label Apr 9, 2024

This comment was marked as outdated.

@david-allison
Copy link
Member

This has begun to cause issues. My opinion has shifted and I'd be happy with avoiding * and seeing how this goes

Unusual mix of High Priority & Good First Issue: this should be simple to solve

  • one commit to change the setting
  • one commit to update all the files to remove * imports
  • (optional) one commit to add a lint rule

@andriusezerskis
Copy link
Contributor

Hi there @david-allison ! I'd like to work on this issue just to get a little familiar with the codebase ;)

@david-allison
Copy link
Member

Sounds good, cheers!

@andriusezerskis
Copy link
Contributor

Hey, me again @david-allison, I've made some progress on this but I've been stuck on FilteredDeckOptions.kt specifically, since it uses deprecated libraries of android and won't build. I noticed that it has already been discussed here #5019 and I'm not sure what to do about that problem (especially since I'm just starting out in this project).

@david-allison
Copy link
Member

david-allison commented Jul 15, 2024

@andriusezerskis could you push a branch so I can take a look

Or post a patch:

  • Android Studio -> Commit -> Right click the file(s) and select 'copy patch to clipboard'
  • Post on GitHub inside a code block. Set the language to patch

@andriusezerskis
Copy link
Contributor

andriusezerskis commented Jul 15, 2024

Index: AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt b/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt	(revision b4a3fc99c59a2afe8e80b8965aaf580ef8c09a24)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt	(revision ed6b0bccb9809bceffa8f4753f8a5a655f596740)
@@ -19,10 +19,13 @@
 
 import android.content.SharedPreferences
 import android.os.Bundle
-import android.preference.*
+import androidx.preference.CheckBoxPreference
+import androidx.preference.EditTextPreference
+import androidx.preference.ListPreference
+import androidx.preference.Preference
+import androidx.preference.PreferenceCategory
 import com.ichi2.anki.analytics.UsageAnalytics
 import com.ichi2.annotations.NeedsTest
-import com.ichi2.libanki.Collection
 import com.ichi2.preferences.StepsPreference.Companion.convertFromJSON
 import com.ichi2.preferences.StepsPreference.Companion.convertToJSON
 import com.ichi2.themes.Themes
@@ -216,7 +219,7 @@
         } else {
             pref = DeckPreferenceHack()
             pref.registerOnSharedPreferenceChangeListener(this)
-            addPreferences(col)
+            // addPreferences(col)
             buildLists()
             updateSummaries()
         }
@@ -238,32 +241,32 @@
         supportActionBar?.setDisplayHomeAsUpEnabled(true)
     }
 
-    @Suppress("deprecation") // Tracked as #5019 on github: addPreferencesFromResource
-    private fun addPreferences(col: Collection) {
-        addPreferencesFromResource(R.xml.cram_deck_options)
-        if (col.schedVer() != 1) {
-            Timber.d("sched v2: removing filtered deck custom study steps")
-            // getPreferenceScreen.removePreference didn't return true, so remove from the category
-            setupSecondFilterListener()
-            setupPreviewDelaysListener()
-            val category = findPreference("studyOptions") as PreferenceCategory
-            removePreference(category, "stepsOn")
-            removePreference(category, "steps")
-        }
-    }
+//    @Suppress("deprecation") // Tracked as #5019 on github: addPreferencesFromResource
+//    private fun addPreferences(col: Collection) {
+//        addPreferencesFromResource(R.xml.cram_deck_options)
+//        if (col.schedVer() != 1) {
+//            Timber.d("sched v2: removing filtered deck custom study steps")
+//            // getPreferenceScreen.removePreference didn't return true, so remove from the category
+//            setupSecondFilterListener()
+//            setupPreviewDelaysListener()
+//            val category = findPreference("studyOptions") as PreferenceCategory
+//            removePreference(category, "stepsOn")
+//            removePreference(category, "steps")
+//        }
+//    }
 
-    @Suppress("deprecation") // Tracked as #5019 on github: findPreference
-    private fun removePreference(category: PreferenceCategory?, key: String) {
-        val preference = findPreference(key)
-        if (category == null || preference == null) {
-            Timber.w("Failed to remove preference '%s'", key)
-            return
-        }
-        val result = category.removePreference(preference)
-        if (!result) {
-            Timber.w("Failed to remove preference '%s'", key)
-        }
-    }
+//    @Suppress("deprecation") // Tracked as #5019 on github: findPreference
+//    private fun removePreference(category: PreferenceCategory?, key: String) {
+//        val preference = findPreference(key)
+//        if (category == null || preference == null) {
+//            Timber.w("Failed to remove preference '%s'", key)
+//            return
+//        }
+//        val result = category.removePreference(preference)
+//        if (!result) {
+//            Timber.w("Failed to remove preference '%s'", key)
+//        }
+//    }
 
     override fun closeWithResult() {
         if (prefChanged) {

@david-allison
So, initially, the import was "import android.preference.*", which I had to change because of the wildcard. So I changed that import with this :
+import android.preference.CheckBoxPreference
+import android.preference.EditTextPreference
+import android.preference.ListPreference
+import android.preference.Preference
+import android.preference.PreferenceCategory,
which was not better, since these libraries were shown as deprecated during build, so it failed because of this. So I changed the "android" to "androidx" as suggested by the editor, but androidx didn't support some of the functions used in the code (multiple files concerned: FilteredDeckOptions.kt, NumberRangePreference.kt, IncrementerNumberRangePreference.kt)

I've commented the mentioned files, and here's the link to my fork if you need to take a look: https://github.com/andriusezerskis/Anki-Android/tree/remove-import-wildcard. The patch shown here is obviously just one of the files but the issue remains the same.

@david-allison
Copy link
Member

david-allison commented Jul 15, 2024

Index: AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt b/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt	(revision 2c959dcfdf444ffc62e0b8bc0bf4803889c2ec2c)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/FilteredDeckOptions.kt	(date 1721047190132)
@@ -1,4 +1,4 @@
-package com.ichi2.anki
+@file:Suppress("DEPRECATION") // #3625 android.preference deprecation
 
 /****************************************************************************************
  * Copyright (c) 2009 Casey Link <[email protected]>                             *
@@ -17,9 +17,15 @@
  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
  ****************************************************************************************/
 
+package com.ichi2.anki
+
 import android.content.SharedPreferences
 import android.os.Bundle
-import android.preference.*
+import android.preference.CheckBoxPreference
+import android.preference.EditTextPreference
+import android.preference.ListPreference
+import android.preference.Preference
+import android.preference.PreferenceCategory
 import com.ichi2.anki.analytics.UsageAnalytics
 import com.ichi2.annotations.NeedsTest
 import com.ichi2.libanki.Collection

You can apply this by copying the above code block double-tapping shift inside Android Studio and selecting 'apply patch from clipboard'.

This patch is based off main, so you'd want to revert your file changes before applying this

It's not a great option, but it's the best we have for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment