-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
4 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
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
81 changes: 81 additions & 0 deletions
81
AnkiDroid/src/main/java/com/ichi2/libanki/ChangeManager.kt
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,81 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2022 Ankitects Pty Ltd <http://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/>. * | ||
****************************************************************************************/ | ||
|
||
/** | ||
* With the Rust backend, operations that modify the collection return a description of changes (OpChanges). | ||
* The UI can subscribe to these changes, so it can update itself when actions have been performed | ||
* (eg, the deck list can check if studyQueues has been updated, and if so, it will redraw the list). | ||
* | ||
* The optional handler argument can be used so that the initiator of an action can tell when a | ||
* OpChanges action was caused by itself. This can be useful when the default change behaviour | ||
* should be ignored, in favour of specific handling (eg the UI wishes to directly update the | ||
* displayed flag, without redrawing the entire review screen). | ||
*/ | ||
|
||
// BackendFactory.defaultLegacySchema must be false to use this code. | ||
|
||
package com.ichi2.libanki | ||
|
||
import anki.collection.OpChanges | ||
import anki.collection.OpChangesAfterUndo | ||
import anki.collection.OpChangesWithCount | ||
import anki.collection.OpChangesWithId | ||
import anki.import_export.ImportAnkiPackageResponse | ||
import java.lang.ref.WeakReference | ||
|
||
object ChangeManager { | ||
interface ChangeSubscriber { | ||
/** | ||
* Called after a backend method invoked via col.op() or col.opWithProgress() | ||
* has modified the collection. Subscriber should inspect the changes, and update | ||
* the UI if necessary. | ||
*/ | ||
fun opExecuted(changes: OpChanges, handler: Any?) | ||
} | ||
|
||
private val subscribers = mutableListOf<WeakReference<ChangeSubscriber>>() | ||
|
||
fun subscribe(subscriber: ChangeSubscriber) { | ||
subscribers.add(WeakReference(subscriber)) | ||
} | ||
|
||
private fun notifySubscribers(changes: OpChanges, handler: Any?) { | ||
val expired = mutableListOf<WeakReference<ChangeSubscriber>>() | ||
for (subscriber in subscribers) { | ||
val ref = subscriber.get() | ||
if (ref == null) { | ||
expired.add(subscriber) | ||
} else { | ||
ref.opExecuted(changes, handler) | ||
} | ||
} | ||
for (item in expired) { | ||
subscribers.remove(item) | ||
} | ||
} | ||
|
||
fun<T> notifySubscribers(changes: T, initiator: Any?) { | ||
val opChanges = when (changes) { | ||
is OpChanges -> changes | ||
is OpChangesWithCount -> changes.changes | ||
is OpChangesWithId -> changes.changes | ||
is OpChangesAfterUndo -> changes.changes | ||
is ImportAnkiPackageResponse -> changes.changes | ||
else -> TODO("unhandled change type") | ||
} | ||
notifySubscribers(opChanges, initiator) | ||
} | ||
} |