Skip to content

Conversation

@Kota-Jagadeesh
Copy link
Contributor

Purpose / Description

This PR refactors CardBrowser.kt to remove the redundant reloadRequired boolean flag. The goal is to move away from manual intent-based signaling between activities and instead utilize the centralized OpChanges system via ChangeManager. This modernizes the codebase and ensures that UI refreshes are driven by actual database events.

Fixes

  • Fixes one of the internal TODOs in CardBrowser.kt regarding OpChanges migration.

Approach

  • Removed the private var reloadRequired local variable.
  • Cleaned up setResult calls in onCreate, onCardsUpdated, and Activity Result listeners that were manually passing RELOAD_REQUIRED_EXTRA_KEY.
  • Updated opExecuted to listen for OpChanges (browserSidebar, browserTable, noteText, card, and collection).
  • Shifted the responsibility of detecting changes to the caller (e.g., the Reviewer), which now relies on its own ChangeManager subscription rather than an explicit signal from the Browser.

How Has This Been Tested?

Manual Testing on Physical Device:

  1. Open a card in the Reviewer.
  2. Navigate to the Card Browser via the overflow menu.
  3. Edit the same card (e.g., change the "Front" text).
  4. Save the edit and press the back button to return to the Reviewer.
  5. Result: Verified that the Reviewer automatically updated to show the modified text without needing a manual intent flag.

Configuration:

  • Device: Physical Android Phone
  • Build Tools: JDK 21
  • Verification: Successful synchronization between Browser edits and Reviewer state.

Learning

Researched the AnkiDroid ChangeManager architecture to understand how OpChanges broadcasts database modifications globally. This pattern reduces coupling between activities by using the observer pattern.

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: No visual changes (refactor only).

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.

Thanks!! I have a few questions

A unit test here would be extremely useful

Comment on lines +287 to +289
// Set a simple RESULT_OK. The Reviewer or calling activity will now
// rely on ChangeManager/OpChanges to detect if a refresh is needed.
setResult(RESULT_OK)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem relevant, should it be in another commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here this change is actually the core part of the refactor. the previous code was explicitly putting the reloadRequired boolean into the result Intent. Since,, that variable has been removed in favor of OpChanges, keeping the Intent().apply { ... } block would result in a compiler error or a redundant empty Intent.for that i simplified it to setResult(RESULT_OK) to complete the removal of manual signaling for this activity's result.

private var onEditCardActivityResult =
registerForActivityResult(StartActivityForResult()) { result: ActivityResult ->
Timber.i("onEditCardActivityResult: resultCode=%d", result.resultCode)
if (result.resultCode == DeckPicker.RESULT_DB_ERROR) {
Copy link
Member

Choose a reason for hiding this comment

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

Why was this removed?

@Kota-Jagadeesh
Copy link
Contributor Author

A unit test here would be extremely useful

I agree that a unit test would be valuable here. here i am looking at how to mock OpChanges to verify that opExecuted triggers the UI refresh. if you have a specific existing test in the codebase you'd recommend as a reference for ChangeManager subscribers, i'd be happy to implement a similar test for this change. thank you :)

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 wouldn't worry for now. First contribution, let's get it in.

Thanks so much!!

@david-allison david-allison added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Jan 30, 2026
@Kota-Jagadeesh
Copy link
Contributor Author

@david-allison updated the code to rename the refresh method and restored the error handling checks.

@david-allison david-allison added the squash-merge A squash & force push is required. The PR author may do this to speed up the merge process. label Jan 30, 2026
Copy link
Member

@lukstbit lukstbit left a comment

Choose a reason for hiding this comment

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

Thanks and welcome!

@lukstbit lukstbit 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 Feb 4, 2026
@lukstbit lukstbit merged commit fd8c4cb into ankidroid:main Feb 4, 2026
15 checks passed
@github-actions github-actions bot removed Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) squash-merge A squash & force push is required. The PR author may do this to speed up the merge process. labels Feb 4, 2026
@github-actions github-actions bot added this to the 2.24 release milestone Feb 4, 2026
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