forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can be tested by importing an apkg from the deck list, then choosing the undo option. - If a backend undo entry is present, it takes priority over the old undo system to ensure data integrity. AD's reviewer saves config vars and decks as part of the review process, and it was triggering undo entries for those changes, which prevent the v2 scheduler review from being undone. The calls have been updated to clear the backend undo, so that the review takes precedence. The desktop code has the same problem, and it will be avoidable once updating to the v3 scheduler. - Introduce a helper to await a job in unit tests, to avoid race conditions
- Loading branch information
Showing
15 changed files
with
291 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2022 Ankitects Pty Ltd <https://apps.ankiweb.net> * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify it under * | ||
* the terms of the GNU General Public License as published by the Free Software * | ||
* Foundation; either version 3 of the License, or (at your option) any later * | ||
* version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License along with * | ||
* this program. If not, see <http://www.gnu.org/licenses/>. * | ||
****************************************************************************************/ | ||
|
||
package com.ichi2.anki | ||
|
||
import com.ichi2.anki.UIUtils.showSimpleSnackbar | ||
import com.ichi2.libanki.CollectionV16 | ||
import com.ichi2.libanki.undoNew | ||
import com.ichi2.libanki.undoableOp | ||
import net.ankiweb.rsdroid.BackendException | ||
|
||
suspend fun AnkiActivity.backendUndoAndShowPopup(col: CollectionV16): Boolean { | ||
return try { | ||
val changes = runInBackgroundWithProgress() { | ||
undoableOp { | ||
col.undoNew() | ||
} | ||
} | ||
showSimpleSnackbar( | ||
this, | ||
col.tr.undoActionUndone(changes.operation), | ||
false | ||
) | ||
true | ||
} catch (exc: BackendException) { | ||
// FIXME: -Backend module should export this as a separate Exception | ||
if (exc.localizedMessage == "UndoEmpty") { | ||
// backend undo queue empty | ||
false | ||
} else { | ||
throw exc | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2022 Ankitects Pty Ltd <https://apps.ankiweb.net> * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify it under * | ||
* the terms of the GNU General Public License as published by the Free Software * | ||
* Foundation; either version 3 of the License, or (at your option) any later * | ||
* version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License along with * | ||
* this program. If not, see <http://www.gnu.org/licenses/>. * | ||
****************************************************************************************/ | ||
|
||
package com.ichi2.libanki | ||
|
||
import anki.collection.OpChangesAfterUndo | ||
import net.ankiweb.rsdroid.RustCleanup | ||
import anki.collection.UndoStatus as UndoStatusProto | ||
|
||
/** | ||
* If undo/redo available, a localized string describing the action will be set. | ||
*/ | ||
data class UndoStatus( | ||
val undo: String?, | ||
val redo: String?, | ||
// not currently used | ||
val lastStep: Int | ||
) { | ||
companion object { | ||
fun from(proto: UndoStatusProto): UndoStatus { | ||
return UndoStatus( | ||
undo = proto.undo.ifEmpty { null }, | ||
redo = proto.redo.ifEmpty { null }, | ||
lastStep = proto.lastStep | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Undo the last backend operation. | ||
* | ||
* Should be called via collection.op(), which will notify | ||
* [ChangeManager.Subscriber] of the changes. | ||
* | ||
* Will throw if no undo operation is possible (due to legacy code | ||
* directly mutating the database). | ||
*/ | ||
@RustCleanup("Once fully migrated, and v2 scheduler dropped, rename to undo()") | ||
fun CollectionV16.undoNew(): OpChangesAfterUndo { | ||
val changes = backend.undo() | ||
// clear legacy undo log | ||
clearUndo() | ||
return changes | ||
} | ||
|
||
/** Redoes the previously-undone operation. See the docs for | ||
[CollectionV16.undoOperation] | ||
*/ | ||
fun CollectionV16.redo(): OpChangesAfterUndo { | ||
val changes = backend.redo() | ||
// clear legacy undo log | ||
clearUndo() | ||
return changes | ||
} | ||
|
||
/** See [UndoStatus] */ | ||
fun CollectionV16.undoStatus(): UndoStatus { | ||
return UndoStatus.from(backend.getUndoStatus()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.