⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ContentProviderTest : InstrumentedTest() {
col.decks.count(),
)
// Delete test note type
col.modSchemaNoCheck()
col.modSchema(check = false)
removeAllNoteTypesByName(col, BASIC_NOTE_TYPE_NAME)
removeAllNoteTypesByName(col, TEST_NOTE_TYPE_NAME)
}
Expand Down Expand Up @@ -642,7 +642,7 @@ class ContentProviderTest : InstrumentedTest() {
}
} finally {
// Delete the note type (this will force a full-sync)
col.modSchemaNoCheck()
col.modSchema(check = false)
try {
val noteType = col.notetypes.get(noteTypeId)
assertNotNull("Check note type", noteType)
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ open class CardTemplateEditor :
*/
private fun executeWithSyncCheck(schemaChangingAction: Runnable) {
try {
templateEditor.getColUnsafe.modSchema()
templateEditor.getColUnsafe.modSchema(check = true)
schemaChangingAction.run()
templateEditor.loadTemplatePreviewerFragmentIfFragmented()
} catch (e: ConfirmModSchemaException) {
Expand All @@ -1366,7 +1366,7 @@ open class CardTemplateEditor :
d.setArgs(resources.getString(R.string.full_sync_confirmation))
val confirm =
Runnable {
templateEditor.getColUnsafe.modSchemaNoCheck()
templateEditor.getColUnsafe.modSchema(check = false)
schemaChangingAction.run()
templateEditor.dismissAllDialogFragments()
}
Expand Down
53 changes: 0 additions & 53 deletions AnkiDroid/src/main/java/com/ichi2/anki/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,21 @@ import androidx.lifecycle.coroutineScope
import androidx.lifecycle.viewModelScope
import anki.collection.Progress
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.CrashReportData.Companion.throwIfDialogUnusable
import com.ichi2.anki.CrashReportData.Companion.toCrashReportData
import com.ichi2.anki.CrashReportData.HelpAction
import com.ichi2.anki.CrashReportData.HelpAction.AnkiBackendLink
import com.ichi2.anki.CrashReportData.HelpAction.OpenDeckOptions
import com.ichi2.anki.common.annotations.UseContextParameter
import com.ichi2.anki.exception.StorageAccessException
import com.ichi2.anki.libanki.Collection
import com.ichi2.anki.pages.DeckOptionsDestination
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.utils.openUrl
import com.ichi2.utils.create
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
import com.ichi2.utils.neutralButton
import com.ichi2.utils.positiveButton
import com.ichi2.utils.setupEnterKeyHandler
import com.ichi2.utils.show
import com.ichi2.utils.title
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -79,8 +75,6 @@ import org.jetbrains.annotations.VisibleForTesting
import timber.log.Timber
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlin.time.Duration

/** Overridable reference to [Dispatchers.IO]. Useful if tests can't use it */
Expand Down Expand Up @@ -549,53 +543,6 @@ private fun ProgressContext.updateDialog(dialog: android.app.ProgressDialog) {
dialog.setMessage(text + progressText)
}

/**
* If a one-way sync is not already required, confirm the user wishes to proceed.
* If the user agrees, the schema is bumped and the routine will return true.
* On false, calling routine should abort.
*/
suspend fun AnkiActivity.userAcceptsSchemaChange(col: Collection): Boolean {
if (col.schemaChanged()) {
return true
}
return suspendCoroutine { coroutine ->
AlertDialog.Builder(this).show {
message(text = col.tr.deckConfigWillRequireFullSync()) // generic message
positiveButton(R.string.dialog_ok) {
col.modSchemaNoCheck()
coroutine.resume(true)
}
negativeButton(R.string.dialog_cancel) { coroutine.resume(false) }
setOnCancelListener { coroutine.resume(false) }
}
}
}

/**
* Returns whether we are allowed to change the schema.
*
* If changing the schema would require the next sync to be a full sync, and it's not already required, ask
* the user whether or not they still allow the schema change.
*/
suspend fun AnkiActivity.userAcceptsSchemaChange(): Boolean {
if (withCol { schemaChanged() }) {
return true
}
val hasAcceptedSchemaChange =
suspendCoroutine { coroutine ->
AlertDialog.Builder(this).show {
message(text = TR.deckConfigWillRequireFullSync().replace("\\s+".toRegex(), " "))
positiveButton(R.string.dialog_ok) { coroutine.resume(true) }
negativeButton(R.string.dialog_cancel) { coroutine.resume(false) }
setOnCancelListener { coroutine.resume(false) }
}
}
if (hasAcceptedSchemaChange) {
withCol { modSchemaNoCheck() }
}
return hasAcceptedSchemaChange
}

/**
* Ensures that current continuation is not [cancelled][CancellableContinuation.isCancelled].
*
Expand Down
32 changes: 4 additions & 28 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ import com.ichi2.anki.settings.Prefs
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.sync.launchCatchingRequiringOneWaySyncDiscardUndo
import com.ichi2.anki.ui.ResizablePaneManager
import com.ichi2.anki.ui.animations.fadeIn
import com.ichi2.anki.ui.animations.fadeOut
Expand Down Expand Up @@ -686,7 +687,7 @@ open class DeckPicker :
SchedulerUpgradeDialog(
activity = this,
onUpgrade = {
launchCatchingRequiringOneWaySync {
launchCatchingRequiringOneWaySyncDiscardUndo {
this@DeckPicker.withProgress { withCol { sched.upgradeToV2() } }
showThemedToast(this@DeckPicker, TR.schedulingUpdateDone(), false)
}
Expand Down Expand Up @@ -1727,7 +1728,7 @@ open class DeckPicker :
if (recommendOneWaySync) {
recommendOneWaySync = false
try {
getColUnsafe.modSchema()
getColUnsafe.modSchema(check = true)
} catch (e: ConfirmModSchemaException) {
Timber.w("Forcing one-way sync")
e.log()
Expand Down Expand Up @@ -2474,7 +2475,7 @@ class OneWaySyncDialog(
val confirm =
Runnable {
// Bypass the check once the user confirms
CollectionManager.getColUnsafe().modSchemaNoCheck()
CollectionManager.getColUnsafe().modSchema(check = false)
}
dialog.setConfirm(confirm)
dialog.setArgs(message)
Expand All @@ -2492,30 +2493,5 @@ class OneWaySyncDialog(
}
}

/**
* [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)
}
}

val ActivityHomescreenBinding.studyoptionsFrame: FragmentContainerView?
get() = studyoptionsFragment
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ import com.ichi2.anki.servicelayer.NoteService
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.sync.userAcceptsSchemaChange
import com.ichi2.anki.ui.setupNoteTypeSpinner
import com.ichi2.anki.utils.RunOnlyOnce
import com.ichi2.anki.utils.ext.sharedPrefs
Expand Down
23 changes: 13 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteTypeFieldEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {
fieldName ?: return
// Name is valid, now field is added
if (modSchemaCheck) {
getColUnsafe.modSchema()
getColUnsafe.modSchema(check = true)
} else {
getColUnsafe.modSchemaNoCheck()
getColUnsafe.modSchema(check = false)
}
launchCatchingTask {
Timber.d("doInBackgroundAddField")
Expand All @@ -259,18 +259,21 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {
private fun deleteFieldDialog() {
val confirm =
Runnable {
getColUnsafe.modSchemaNoCheck()
getColUnsafe.modSchema(check = false)
deleteField()

// This ensures that the context menu closes after the field has been deleted
supportFragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
supportFragmentManager.popBackStackImmediate(
null,
FragmentManager.POP_BACK_STACK_INCLUSIVE,
)
}

if (fieldsLabels.size < 2) {
showThemedToast(this, resources.getString(R.string.toast_last_field), true)
} else {
try {
getColUnsafe.modSchema()
getColUnsafe.modSchema(check = true)
val fieldName = noteFields[currentPos].name
ConfirmationDialog().let {
it.setArgs(
Expand Down Expand Up @@ -342,7 +345,7 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {
c.setArgs(resources.getString(R.string.full_sync_confirmation))
val confirm =
Runnable {
getColUnsafe.modSchemaNoCheck()
getColUnsafe.modSchema(check = false)
try {
renameField()
} catch (e1: ConfirmModSchemaException) {
Expand Down Expand Up @@ -400,7 +403,7 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {

Timber.i("Repositioning field from %d to %d", currentPos, newPosition)
try {
getColUnsafe.modSchema()
getColUnsafe.modSchema(check = true)
repositionField(newPosition - 1)
} catch (e: ConfirmModSchemaException) {
e.log()
Expand All @@ -411,7 +414,7 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {
val confirm =
Runnable {
try {
getColUnsafe.modSchemaNoCheck()
getColUnsafe.modSchema(check = false)
repositionField(newPosition - 1)
} catch (e1: JSONException) {
throw RuntimeException(e1)
Expand Down Expand Up @@ -466,7 +469,7 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {
*/
private fun sortByField() {
try {
getColUnsafe.modSchema()
getColUnsafe.modSchema(check = true)
launchCatchingTask { changeSortField(notetype, currentPos) }
} catch (e: ConfirmModSchemaException) {
e.log()
Expand All @@ -475,7 +478,7 @@ class NoteTypeFieldEditor : AnkiActivity(R.layout.note_type_field_editor) {
c.setArgs(resources.getString(R.string.full_sync_confirmation))
val confirm =
Runnable {
getColUnsafe.modSchemaNoCheck()
getColUnsafe.modSchema(check = false)
launchCatchingTask { changeSortField(notetype, currentPos) }
}
c.setConfirm(confirm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ import com.ichi2.anki.dialogs.ConversionType.CLOZE_TO_CLOZE
import com.ichi2.anki.dialogs.ConversionType.CLOZE_TO_REGULAR
import com.ichi2.anki.dialogs.ConversionType.REGULAR_TO_CLOZE
import com.ichi2.anki.dialogs.ConversionType.REGULAR_TO_REGULAR
import com.ichi2.anki.launchCatchingRequiringOneWaySync
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.libanki.NoteId
import com.ichi2.anki.libanki.NoteTypeId
import com.ichi2.anki.requireAnkiActivity
import com.ichi2.anki.showError
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.sync.launchCatchingRequiringOneWaySync
import com.ichi2.anki.ui.BasicItemSelectedListener
import com.ichi2.anki.ui.internationalization.toSentenceCase
import com.ichi2.anki.utils.InitStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,6 @@ class ChangeNoteTypeViewModel(
Timber.d("Field map: %s", fieldChangeMap)
Timber.d("Card map: %s", templateChangeMap)

withCol { modSchema() }

val changes =
changeNoteTypeOfNotes(
noteIds = noteIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import com.ichi2.anki.dialogs.showLoadingDialog
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.notetype.ManageNoteTypesState.UserMessage
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.userAcceptsSchemaChange
import com.ichi2.anki.sync.userAcceptsSchemaChange
import com.ichi2.anki.utils.Destination
import com.ichi2.ui.AccessibleSearchView
import com.ichi2.utils.getInputField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SyncSettingsFragment : SettingsFragment() {
setMessage(TR.preferencesOnNextSyncForceChangesIn())
setPositiveButton(R.string.dialog_ok) { _, _ ->
launchCatchingTask {
withCol { modSchemaNoCheck() }
withCol { modSchema(check = false) }
showSnackbar(R.string.one_way_sync_confirmation, Snackbar.LENGTH_SHORT)
}
}
Expand Down
Loading
Loading