Help on porting to CollectionOp

Hi,

I’m porting my plugin reverso importer to the lastest anki version.
While everything seems to work smoothly there’s a last issue I don’t really catch correctly and is the way the UI is to be refreshed. I understand that I should use CollectionOp, but I have not really understood how to change my code.

Current code, that adds a reverso card from a list of selected card is as follows:


def setReverseField(browser, nids):

    mw = browser.mw
    mw.checkpoint("add_reverse")
    mw.progress.start()
    browser.model.beginReset()
    cnt = 0
    optional_anki_reverse_name = _("Add Reverse")
    for nid in nids:
        note = mw.col.get_note(nid)
        if 'reverse' in note and not note['reverse']:
            note['reverse'] = "1"
            cnt += 1
            note.flush()
    if optional_anki_reverse_name in note and not note[optional_anki_reverse_name]:
        note[optional_anki_reverse_name] = "1"
        cnt += 1
        note.flush()

    browser.model.endReset()
    mw.requireReset()
    mw.progress.finish()
    mw.reset()
    if cnt:
        tooltip("<b>Added</b> {0} reversed cards.".format(cnt), parent=browser)
    else:
        msg = "No card found to reverse.\nOnly cards with field 'reverse' or '{}' are looked for"
        tooltip(msg.format(optional_anki_reverse_name), parent=browser)


def onBatchEdit(browser):
    nids = browser.selectedNotes()
    if not nids:
        tooltip("No cards selected.")
        return
    setReverseField(browser, nids)

and onBatchEdit is called by the callback of the menu entry.
I don’t mind to undo the operation, but in case it must be a single undo for the whole collection.

Any hint is appreciated.
Sandro

I sent a PR:

2 Likes

Thanks @abdo, your PR just works and I’ll accept it.

Creation of reverso card can be “undone”.

One more thing, I may be wrong but I believed CollectionOp would also handle the refresh of the UI, which I see that card list does not change on creating the reverso card. How can I force a reread of the card list?

*:slight_smile:

That’s right, the browser refreshes displayed notes automatically when you use CollectionOp. This is working for me.

If you mean “re-run the current search when changes are made”, Anki does not do that on undoable actions, as it’s potentially slow and may not be what the user wants.

| dae
January 9 |

  • | - |

If you mean “re-run the current search when changes are made”, Anki does not do that on undoable actions, as it’s potentially slow and may not be what the user wants.

thanks, it would be sufficient to just refresh the UI after the operation is terminated.
I believe (but my memory may fail here) that it used to be that way.
Is there a way to triggers the reload with same filters?

The operation works on selected notes, and Anki does refresh the editor and browser rows after CollectionOp to reflect the changes to the field contents. Isn’t that sufficient? Not sure how the old code worked exactly (only tested after making the changes)

If you wish to force the search to be repeated, you’ll need to call the code Anki does when the user hits enter in the search area.