Skip to content

feat(profiles): populate the switch profile list - #21811

Open
criticalAY wants to merge 5 commits into
ankidroid:mainfrom
criticalAY:profiles/list-read
Open

criticalAY wants to merge 5 commits into
ankidroid:mainfrom
criticalAY:profiles/list-read

Conversation

@criticalAY

Copy link
Copy Markdown
Contributor

Note

Assisted-by: Claude Opus 5 (commit 1 of this PR)

Purpose / Description

Fixes

Approach

See commits

How Has This Been Tested?

Unit test

Learning (optional, can help others)

NA

Checklist

Please, go through these checks before submitting the PR.

  • 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

Loads the active profile without throwing, so a caller can fall back to
the base context. Throwing during attachBaseContext would stop the app
from starting at all.

Timber is not planted that early, so the failure is recorded in
attachError for the caller to log once logging is available.

No caller yet.
Users with only the Default profile are unaffected: the wrapper
delegates every override to the base context for that profile.

Logging is not available this early, so any attach failure is logged
once Timber is planted.
createOrNull built a ProfileManager that attachBaseContext used only for
its context and then dropped, so callers had no way to reach the active
profile.

The manager cannot live in a static field: lint rejects that with
StaticFieldLeak because ProfileManager holds a Context. An instance
field on the Application is process scoped and allowed.
@criticalAY criticalAY added the Multi Profile Anything that is related to AnkiDroid multiple profiles label Sep 10, 2026
The list was a hardcoded empty flow. It now maps
ProfileManager.getAllProfiles() into the UI model, so the screen shows
the real profiles.

The registry is backed by SharedPreferences, which has no defined
iteration order, so profiles are sorted by name to keep the list stable
across launches.

Assisted-by: Claude Opus 5
@criticalAY criticalAY changed the title Profiles/list read feat(profiles): populate the switch profile list Sep 10, 2026
@criticalAY criticalAY added the Blocked by dependency Currently blocked by some other dependent / related change label Sep 10, 2026
@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Snapshot diff report vs main. Open screenshot-diff for diffs.

  • PreferencesScreenshotTest: 1 change
All 1 changed screenshots

PreferencesScreenshotTest

  • SwitchProfilesFragment_compare.png

Comment on lines +28 to +30
checkNotNull(AnkiDroidApp.instance.profileManager) {
"the profile environment failed to load, see ProfileManager.attachError"
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we want to handle this error without crashing, or is it a developer-level assertion?

@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
prefs.edit(commit = true) { clear() }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should do this in After as well

private fun ProfileManager.profileItems(): List<ProfileItem> =
getAllProfiles()
.map { (id, metadata) -> ProfileItem(id = id, name = metadata.displayName.value) }
.sortedBy { it.name }

@david-allison david-allison Sep 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This matches Anki Desktop: lowercase profile names are at the bottom of the list

We should encode this in tests, with a quick discussion on whether this makes sense (I prefer case in


Question: DisplayName does not seem to have Anki Desktop's uniqueness check - could you link it in the docs if I missed the reference, or could we consider implementing it if possible.

2 profiles in Anki Desktop which differ in case are not allowed on my MacBook (likely due to a potential directory conflict on Windows)

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

Labels

Blocked by dependency Currently blocked by some other dependent / related change Has Conflicts Multi Profile Anything that is related to AnkiDroid multiple profiles Needs Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants