Anki-Android icon indicating copy to clipboard operation
Anki-Android copied to clipboard

API: "add" should use `ChangeManager`

Open david-allison opened this issue 1 year ago • 22 comments

Edit: I was incorrect here

Implementation: use undoableOp in CardContentProvider.kt when adding a note

Also add a unit test. We have a much stronger testing guarantee on our API

Extension: document or fix other similar issues in this class, there will be a lot


Don't know if this is related, but was frustrated by this behaviour I noticed for the first time today: Was reviewing a card and decided in the moment that one part of it deserved it's own card. I created that card using another app with an 'add to anki' feature (Kanji Study App, in case someone wants to attempt a repro), then went back to reviewing. I answered with an 'again' and a couple cards later decided I wanted to undo and change it to a 'hard', so I hit the undo button a few times button. I was shocked and frustrated to see 'Add card undone'. I definitely don't want that to be part of the same undo queue as my card reviews. If I hadn't noticed the small toast message, I would have continued on my way not even noticing that the card wasn't in my deck and wasn't getting learned as I intended.

Originally posted by @joeholley in https://github.com/ankidroid/Anki-Android/issues/14793#issuecomment-2777449338

david-allison avatar Apr 04 '25 09:04 david-allison

Hi @david-allison Can I work on this? I will go through the codes and get back with an approach.

tanmayChakrawarty avatar Apr 04 '25 13:04 tanmayChakrawarty

You can work but consider the priority

criticalAY avatar Apr 04 '25 15:04 criticalAY

For now, we don't have suspend in CardContentProvider.

I'd experiment with something along the lines of this: package it into a method/extension method, maybe: col.undoableOpSync { addNote(newNote, newNote.notetype.did) }

Index: AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt b/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt	(revision a4fe400114641a7ca9a971054705c47aa65536f0)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt	(date 1743727377594)
@@ -37,6 +37,7 @@
 import com.ichi2.anki.utils.ext.description
 import com.ichi2.libanki.Card
 import com.ichi2.libanki.CardTemplate
+import com.ichi2.libanki.ChangeManager
 import com.ichi2.libanki.Collection
 import com.ichi2.libanki.Consts
 import com.ichi2.libanki.Deck
@@ -53,6 +54,7 @@
 import com.ichi2.libanki.exception.ConfirmModSchemaException
 import com.ichi2.libanki.exception.EmptyMediaException
 import com.ichi2.libanki.sched.DeckNode
+import com.ichi2.libanki.undoableOp
 import com.ichi2.libanki.utils.TimeManager
 import com.ichi2.utils.FileUtil
 import com.ichi2.utils.FileUtil.internalizeUri
@@ -820,7 +822,7 @@
                     newNote.setTagsFromStr(col, tags)
                 }
                 // Add to collection
-                col.addNote(newNote, newNote.notetype.did)
+                ChangeManager.notifySubscribers(col.addNote(newNote, newNote.notetype.did), this)
 
                 Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI, newNote.id.toString())
             }

david-allison avatar Apr 05 '25 00:04 david-allison

Hi @david-allison , I'm a little bit of a tough time understanding how to replicate the error, could I get some insight so that i can validate my fix? I think I see the fix to the issue, but i need clarity on how to reciprocate the exact error that Joe was facing, and what the fix is supposed to look like...

Apologies for the ping.

aywhoosh avatar Apr 05 '25 15:04 aywhoosh

You should be able to add a card with some of:

  • https://github.com/ankidroid/Anki-Android/wiki/Third-Party-Apps

I personally use Pleco, but setup is a little complicated in the settings. Some of the Japanese Dictionary apps are simple to get started with.

From there, see if the action is undoable

david-allison avatar Apr 05 '25 16:04 david-allison

Just to reiterate for my own reference, only thing to do here is to allow the externally added cards to be undo-able right? Do the "again", "hard" labels factor into the issue too? (the labels joe mentioned in their comment)

aywhoosh avatar Apr 05 '25 16:04 aywhoosh

@joeh

I believe the problem is that the externally added card is not registered as being redoable.

I may be mistaken

I suspect the problem is that the user had some standard undoable actions (answering cards), and then undid too far, with no option to redo

But you'll need to confirm, either by pinging the reporter or producing your own repro steps

david-allison avatar Apr 05 '25 16:04 david-allison

I followed what Joe said, and from what I can see, the error isn't the exported card, added using the "add to anki" feature, being removed on multiple undo-s, rather the "Add note undone" prompt is popping up because the last card of the deck is being removed if i try to undo again after reaching the first card in the deck. (video attached)

Working towards a fix, as per my current understanding of the problem. 🫔🫔

https://github.com/user-attachments/assets/026121a4-2dfb-4815-afe4-a0751d645cc2

aywhoosh avatar Apr 05 '25 17:04 aywhoosh

UPDATE: This issue persists even on a deck with no external cards added; if I undo beyond the first card of the deck, it is removing the last card from that deck (add card undone), although it is retrievable through a redo operation then and there, the card removed, will be lost/permanently deleted if we exit the review session and start a new one, as I think AnkiDroid only maintains a undo/redo stack on a review-session basis.

Is this intended functionality, or a bug?

If it's a bug, could i get assigned this issue? I can get to a fix pretty quickly.

aywhoosh avatar Apr 05 '25 17:04 aywhoosh

@aywhoosh Did you ping the reporter and confirm that this was their issue? The review stack is not removed after a review session, it's global

@joeholley Could you restate the error report, since it feels there's some ambiguity here

david-allison avatar Apr 22 '25 08:04 david-allison

The suggestion to use undoableOp I provided is definitely an improvement and is likely a good first issue.

Changing the way which undo works (local vs global), as reported in the error description is not feasible, but we may be able to work with the error reporter and add a warning about undoing actions performed by the API

I would not consider this part of the issue to be a Good First Issue

david-allison avatar Apr 22 '25 08:04 david-allison

Hello šŸ‘‹, this issue has been opened for more than 3 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

github-actions[bot] avatar Sep 20 '25 12:09 github-actions[bot]

Hi! I’m new to open source and would love to work on this issue as my first contribution. Could you please assign it to me?

SahityaGupta25 avatar Nov 10 '25 14:11 SahityaGupta25

I'd recommend something else.

I didn't realise at the time. This will be the first usage of coroutines in CardContentProvider, and likely isn't a suitable architectural change for a Good First Issue

david-allison avatar Nov 10 '25 17:11 david-allison

@david-allison Thank you for the clarification! 😊 I understand — that makes sense. Since I’m new to contributing here, could you please suggest another issue that might be more suitable for a first contribution? I’d love to get started and learn from the process.

SahityaGupta25 avatar Nov 11 '25 02:11 SahityaGupta25

@SahityaGupta25 the best "first" issue is: https://github.com/ankidroid/Anki-Android/issues/13282

david-allison avatar Nov 12 '25 12:11 david-allison

Got it, thank you! @david-allison

SahityaGupta25 avatar Nov 13 '25 05:11 SahityaGupta25

Hi @david-allison, while trying to understand the issue I got a bit confused. From what I understand we want to have separate 2 queues, 1 for card reviews and other for updates like notes and deck addition etc. And the note queue should be the same for deckpicker and the card reviewer right?

The solution would be the state where the answer card can be undone/redone from deckpicker as well as from card review and will be queue 1. The deck creation and adding notes can only be done only from deck picker as separate undo/redo option (we can give it in card review too but I don't think it should be there).

So as of now what we have to do is to make 2 queues, add redo option in deckpicker menu (no such option exists as of now), make the addition of cards added using API uno/redoable. Right?

KT-1114 avatar Mar 11 '26 11:03 KT-1114

We should provide undo and redo throughout the app, but having two queues is not workable

The OP was not responsive. I think this can be broken into two changes:

  • [ ] Understand and explain the issue by testing our content provider, I believe Undo entries are added correctly, but the app may not be updated to pick up the change in the undo queue, so labels may be incorrect (ChangeManager)
    • [ ] If this is the case, let's split it out into a separate issue
  • [ ] We should add a "redo" action on the undo snackbar

david-allison avatar Mar 11 '26 17:03 david-allison

I believe Undo entries are added correctly, but the app may not be updated to pick up the change in the undo queue, so labels may be incorrect

The app does recognize all the changes made by the API. Till now I was thinking issue is

I definitely don't want that to be part of the same undo queue as my card reviews.

I still might be misinterpreting the issue but I think what you are trying to say is that the note is irrecoverable if we leave the reviewer without redoing. So I tested this

Tested on freshly installed version 2.24.0aplha6-debug with Sample API app.

  1. Adding note to the same deck and undo/redo from reviewer.
  • Steps: Add 3 Notes (Basic, Default deck) -> Review 1 -> Add note using api (without exiting the reviewer, default deck) -> Review 2 -> Undo till possible from reviewer -> Redo from reviewer.
  • Result: Answer Card undone, Add note undone (the note added using api) -> Answer Card undone -> 3x Add note undone (opens deckpicker screen) -> Undo delete deck (Gives 2 cards reviewed and 2 as new with last one being card added using API i.e state before we started undoing).
  • Conclusion: Instead of going back to state before undo we should get empty deck.

Note: Leaving the reviewer after undoing the Add Note and before redoing the Add Note makes it irrecoverable.

If I am still wrong than can you please explain what is the expected behavior and what is the actual behavior.

KT-1114 avatar Mar 11 '26 20:03 KT-1114