Skip to content

feat(card-browser): field selection dialog for advanced searches [DEV ONLY]#20491

Open
david-allison wants to merge 4 commits intoankidroid:mainfrom
david-allison:select-field
Open

feat(card-browser): field selection dialog for advanced searches [DEV ONLY]#20491
david-allison wants to merge 4 commits intoankidroid:mainfrom
david-allison:select-field

Conversation

@david-allison
Copy link
Member

Purpose / Description

In #20071 I introduced an 'advanced search' feature in dev for the card browser. This PR improves this feature for the 'Fields' tab

Screenshot 2026-03-16 at 03 42 08

Tapping any but the last option opens up a FieldSelectionDialog, so we can replace field with the field name. This also allows us to remove the Search a field with a space in the name option, as we use buildSearchString to handle this case.

Change to the search text after tapping

- field:*text* 
+ "Add Reverse:*text*" 

Fixes

Approach

  1. Use OptionType to support extension
  2. Add FieldSelectionDialog, which uses the Fragment Result API to communicate
  3. Define a new OptionType, add most of the logic which handles this in AdvancedSearchFieldsTab, and have AdvancedSearchFragment coordinate between this and the FieldSelectionDialog

FieldSelectionDialog has a number of extension points which mean that it will differ from InsertFieldDialog

InsertFieldDialog

  • To be extended to handle special fields
  • To be extended to handle field filters
  • Typically used to list the fields of 1 note type

FieldSelectionDialog

  • For fields of all note types
  • To be extended to handle searching
  • To be extended to handle filtering by note type

How Has This Been Tested?

card_browser_fields.webm

Learning (optional, can help others)

Collection.buildSearchString

field = front; text = "*a*" returns front:\*a\* with no way to avoid escaping. Many of the strings which we are replacing use *, so this is hacked around


https://docs.ankiweb.net/searching.html#limiting-to-a-field

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

Supports Advanced Search performing different actions based
 on what is selected

Example: inserting fields

Prep for issue 18709
Allows selection of a single field, returning the name via the
 Fragment Result API

* This is a simple view over `InsertFieldDialog`, which I plan to
 heavily extend to handle special fields and field filters
   * PR 20041
* `ResultType` formalizes using the Fragment Result API bundle
 to switch on similar actions, without needing multiple listeners

Part of issue 18709

Assisted-by: GPT-5 Mini - `ResultType` docs
`FieldSelectionDialog` is used to obtain the field selection, this returns
 the field name and a `ResultType`, which is then used to obtain the
 search string building logic from `AdvancedSearchFieldsTab`
 and builds the search string.

Removed option: "Search a field with a space in the name" - "a field:text"
 `buildSearchString` handles adding quotes appropriately

Building the search string was much more complex than expected. The Anki
 backend 'buildSearchString' converts text = '*text*' to '\*text\*'
 and there is no way to unescape this

The fix I eventually settled on was to use placeholders in these special
 cases. This is mostly because field names with spaces wrapped the search
 in quotes, and it was risking a bug

'!!BEFORE!!' and '!!AFTER!!' are placeholders

The search logic for each tab was going to get complex, so I extracted
 the majority of the logic to a 'AdvancedSearchXTab' class

I'm happy that this has kept `AdvancedSearchFragment` relatively lean,
 whilst extending the functionality

Issue 18709
Builds on PR 20071
@david-allison david-allison changed the title feat(card-browser): field selection dialog for advanced searches feat(card-browser): field selection dialog for advanced searches [DEV ONLY] Mar 16, 2026
@david-allison
Copy link
Member Author

david-allison commented Mar 16, 2026

Optional: should I change the dialog title to match the option title?

Screenshot 2026-03-16 at 04 02 04

@ZornHadNoChoice
Copy link
Collaborator

Instead of "text", why not place the cursor there so the user can immediately type the actual text?

I prefer just "Select field" in the title since it makes it clearer that you're meant to select a field. Also, because of this dialog:

Screenshot_2026-03-17-07-41-32-282_com ichi2 anki a-edit

UI design is tough

I looked at the material 3 guidelines for dialogs and they actually state that "headlines" are optional, but the supporting text and the button text are not?!

To make it more confusing, Google Calendar has this dialog:

Screenshot_2026-03-17-07-25-49-244_com google android calendar-edit

and Gmail has this:

Screenshot_2026-03-17-07-32-15-713_com google android gm-edit

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.

2 participants