Thanks to visit codestin.com
Credit goes to github.com

Skip to content

architecture: Reimplement & document 'schema change' code #20246

@david-allison

Description

@david-allison

/**
* [launchCatchingTask], showing a one-way sync dialog: [R.string.full_sync_confirmation]
*/
fun AnkiActivity.launchCatchingRequiringOneWaySync(block: suspend () -> Unit) =
launchCatchingTask {
try {
block()
} catch (e: ConfirmModSchemaException) {
e.log()
// .also is used to ensure the activity is used as context
val confirmModSchemaDialog =
ConfirmationDialog().also { dialog ->
dialog.setArgs(message = getString(R.string.full_sync_confirmation))
dialog.setConfirm {
launchCatchingTask {
withCol { modSchemaNoCheck() }
block()
}
}
}
showDialogFragment(confirmModSchemaDialog)
}
}

/** Mark schema modified to force a full
* sync, but with the confirmation checking function disabled This
* is equivalent to `modSchema(False)` in Anki. A distinct method
* is used so that the type does not states that an exception is
* thrown when in fact it is never thrown.
*/
@NotInPyLib
fun modSchemaNoCheck() {
db.execute(
"update col set scm=?, mod=?",
TimeManager.time.intTimeMS(),
TimeManager.time.intTimeMS(),
)
}
/** Mark schema modified to cause a one-way sync.
* ConfirmModSchemaException will be thrown if the user needs to be prompted to confirm the action.
* If the user chooses to confirm then modSchemaNoCheck should be called, after which the exception can
* be safely ignored, and the outer code called again.
*
* @throws ConfirmModSchemaException
*/
@LibAnkiAlias("mod_schema")
fun modSchema() {
if (!schemaChanged()) {
/* In Android we can't show a dialog which blocks the main UI thread
Therefore we can't wait for the user to confirm if they want to do
a one-way sync here, and we instead throw an exception asking the outer
code to handle the user's choice */
throw ConfirmModSchemaException()
}
modSchemaNoCheck()
}
/** `true` if schema changed since last sync. */
@LibAnkiAlias("schema_changed")
@RustCleanup("doesn't match upstream")
fun schemaChanged(): Boolean = scm > lastSync

class ConfirmModSchemaException : Exception() {
/**
* Add the current exception to log.
*/
fun log() {
Timber.v(this)
}
}


https://github.com/ankitects/anki/blob/6247c92dcce0204f0e666b9e9e5355d2a15649d6/pylib/anki/collection.py#L302-L314

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions