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

Improve decks adapter code #17755

Merged
merged 4 commits into from
Jan 9, 2025
Merged

Conversation

lukstbit
Copy link
Member

@lukstbit lukstbit commented Jan 7, 2025

Purpose / Description

Refactors the code for DeckAdapter to improve it and fix a kotlin cleanup + rust cleanup. More info on the commits messages. If the last commit seem too bold I can remove it.

How Has This Been Tested?

Ran the tests, manually verified the decks screen.

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All great stuff!

Comment on lines 2289 to 2290
kotlin
.runCatching {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why kotlin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an extension runCatching in CoroutineHelpers that takes precedence. That method isn't used outside of that file but it felt out of place to make it private so I can access the platform runCatching directly. Made an import alias instead for runCatchingKotlin.

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt Outdated Show resolved Hide resolved
AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt Outdated Show resolved Hide resolved
Fixes IDE issues and kotlin cleanups.

The setBackgroundAlpha() method was inlined, there was only one usage
and this also avoids a warning about one of its parameters having the
same value.

Used the getDrawableOrThrow ktx extension method to improve the
nullability of some of properties.

Slightly improves adapter performance by extracting the fetching of
some resources values from adapter methods that can be called a lot.
The callbacks and the hasBackground property where only set when the
adapter was initialized so it didn't make sense to have setters for them.
Also added a bit of documentation. The callbacks were modified to pass
the deck id which was previously extracted from the OnClickListener's
view that was passed in.

LayoutInflater was removed as a constructor property because we pass
the context which can be used to obtain it.
DeckPicker was modified to calculate itself the due count because it
has the required data, there's no need to go through the adapter.

I also refactored a bit the code around setting the subtitle to the due
count, I think this looks and reads better.
The suspended code was removed and a similar implementation was done
in DeckPicker which will now do the necessary work and then will pass the
calculated data to the adapter.

The adapter was changed to extend ListAdapter which uses diff-ing to
observe changes so we get free animations. The filtering was removed
from the adapter, DeckPicker computes the data as it has all info needed,
the query and the deck tree. The adapter becomes dumb and just reacts to
the changes. A bit wasteful to start a coroutine for each change in the
query text, but I wanted to keep the change count small.

Note: the methods findDeckPosition() and getNodeByDid() were moved in
DeckPicker, they are not needed by the adapter and the DeckPicker already
has a reference to the deck tree that it can use in the methods.
@lukstbit lukstbit force-pushed the refactor_decksAdapter branch from e1c40cb to e06a890 Compare January 9, 2025 07:18
@lukstbit lukstbit closed this Jan 9, 2025
@lukstbit lukstbit reopened this Jan 9, 2025
@david-allison david-allison added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Jan 9, 2025
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy. CI is having general issues

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice set of refactors

@mikehardy mikehardy added this pull request to the merge queue Jan 9, 2025
@mikehardy mikehardy added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Second Approval Has one approval, one more approval to merge labels Jan 9, 2025
Merged via the queue into ankidroid:main with commit 5f1bd43 Jan 9, 2025
22 checks passed
@github-actions github-actions bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Jan 9, 2025
@github-actions github-actions bot added this to the 2.21 release milestone Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants