Skip to content

Fix/template editing undo#19488

Closed
7se7en72025 wants to merge 12 commits intoankidroid:mainfrom
7se7en72025:fix/template-editing-undo
Closed

Fix/template editing undo#19488
7se7en72025 wants to merge 12 commits intoankidroid:mainfrom
7se7en72025:fix/template-editing-undo

Conversation

@7se7en72025
Copy link
Contributor

Purpose / Description

This PR fixes an issue where editing card templates after deleting a deck would break the undo functionality. When users deleted a deck and then edited a template, they could no longer undo either operation. This happened because template editing operations were not wrapped in undoableOp, so they weren't being tracked in the undo history.

Fixes

Approach

The fix wraps the saveNoteType operation in undoableOp to ensure template editing changes are properly tracked in the undo history. Specifically:

  1. Made saveToDatabase a suspend function - This allows it to use undoableOp which is a suspend function
  2. Wrapped saveNoteType in undoableOp - This ensures all template editing operations (adding/deleting templates, updating template content) are tracked in the undo history
  3. Removed the col parameter - Since undoableOp provides the Collection context via withCol, the parameter is no longer needed
  4. Removed the TODO comment - The comment "TODO: make undoable" has been addressed

The changes ensure that template editing operations are properly integrated with Anki's undo system, matching the behavior of other operations like deck deletion.

How Has This Been Tested?

Manual Testing Steps:

  1. Delete a deck (this creates an undo entry)
  2. Edit a template of a note type (add/remove templates or modify template content)
  3. Save the template changes
  4. Attempt to undo - both the template editing and deck deletion should be undoable

Test Configuration:

  • Tested on Android emulator and physical device
  • Verified that undo history is preserved correctly
  • Confirmed that template changes can be undone independently of deck deletion

Expected Behavior:

  • After deleting a deck and editing templates, users can undo template editing
  • Users can also undo the deck deletion that occurred before template editing
  • Undo history is properly maintained for both operations

Learning (optional, can help others)

  • The undoableOp function is the standard way to wrap operations that should be undoable in AnkiDroid
  • undoableOp internally uses withCol to provide Collection context, so operations don't need to manually pass the Collection
  • Schema modifications (like template changes) need to be wrapped in undoableOp to preserve undo history
  • The undo system tracks operations at the backend level, so all operations that modify the database should use undoableOp

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

raiyyan and others added 12 commits November 15, 2025 04:30
Cache maximum scroll range to prevent thumb resizing when items
with different heights scroll in/out of view. This ensures the
thumb maintains a consistent size throughout scrolling.
Cache maximum scroll range to prevent thumb resizing when items
with different heights scroll in/out of view. This ensures the
thumb maintains a consistent size throughout scrolling.
Add logic to update cached maximum scroll range to maintain thumb size consistency.
Co-authored-by: Mike Hardy <github@mikehardy.net>
Fix the scrollbar thumb height calculation to prevent jittering during scroll.

Changes:
1. Cache the maximum scroll range to prevent thumb size changes when items
   with different heights scroll in/out of view
2. Fix handle height calculation to use computeVerticalScrollExtent() instead
   of barHeight in the numerator. The correct formula is:
   (visibleExtent / totalRange) * trackHeight

Previously used (barHeight / range) * barHeight which was mathematically
incorrect and didn't properly represent the proportion of visible content.

Fixes ankidroid#19224
Fix the scrollbar thumb height calculation to prevent jittering during scroll.

Changes:
1. Cache the maximum scroll range to prevent thumb size changes when items
   with different heights scroll in/out of view
2. Fix handle height calculation to use computeVerticalScrollExtent() instead
   of barHeight in the numerator. The correct formula is:
   (visibleExtent / totalRange) * trackHeight

Previously used (barHeight / range) * barHeight which was mathematically
incorrect and didn't properly represent the proportion of visible content.

Fixes ankidroid#19224
Cache computeVerticalScrollExtent() and only update when RecyclerView
is resized. The visible extent can vary during scroll, causing the
thumb height to change and create jitter.

Changes:
- Add cachedVisibleExtent and lastRecyclerViewHeight variables
- Only recalculate visible extent when RecyclerView height changes
- Use cached visible extent for handle height calculation
- Reset cache when adapter changes

This prevents the thumb size from changing during scroll, eliminating
the jitter issue.
Cache the calculated handle height and only update when there's a
significant change (> 2px difference). This prevents small calculation
fluctuations from causing jitter during scroll.

Changes:
- Add cachedHandleHeight and cachedBarHeight variables
- Only update handle height if barHeight changed or difference > 2px
- Use cached handle height to prevent jitter
- Reset cache when adapter changes

This provides additional stability on top of the visible extent caching.
- Wrap saveNoteType in undoableOp to track template edits in undo history
- Make saveToDatabase a suspend function to support undoableOp
- Remove TODO comment as the issue is now fixed

This fixes the issue where after deleting a deck and editing a template,
users couldn't undo the template editing operation. Now both operations
are properly tracked in the undo history.

Fixes: Template editing not being undoable after deck deletion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Editing templates breaks the undo feature

1 participant

Comments