mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 12:02:16 +02:00
[GSoC] Simplify add note task (#12070)
* Removed unnecessary callbacks When adding a single note, it is not required to show continuous progress since it a short task and user can be notified after the task has been finished. So instead of using onProgressUpdate callback, it is good to use onPostExecute for UI updates while adding notes. * Code simplification in NoteEditor.SaveNoteHandler Both the conditions perform same task, sequential execution of both eventually nullifies the effect of !closeEditorAfterSave, so can be removed.
This commit is contained in:
parent
ae5e648bdd
commit
b13b2a0480
@ -187,59 +187,52 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
|
||||
}
|
||||
|
||||
private class SaveNoteHandler(noteEditor: NoteEditor) :
|
||||
TaskListenerWithContext<NoteEditor, Int, Boolean?>(noteEditor) {
|
||||
private var closeEditorAfterSave = false
|
||||
private var closeIntent: Intent? = null
|
||||
TaskListenerWithContext<NoteEditor, Void, Int?>(noteEditor) {
|
||||
override fun actualOnPreExecute(context: NoteEditor) {
|
||||
val res = context.resources
|
||||
context.progressDialog =
|
||||
StyledProgressDialog.show(context, null, res.getString(R.string.saving_facts), false)
|
||||
}
|
||||
|
||||
override fun actualOnProgressUpdate(context: NoteEditor, value: Int) {
|
||||
if (value > 0) {
|
||||
context.changed = true
|
||||
context.sourceText = null
|
||||
context.refreshNoteData(FieldChangeType.refreshWithStickyFields(shouldReplaceNewlines()))
|
||||
UIUtils.showThemedToast(
|
||||
context,
|
||||
context.resources.getQuantityString(
|
||||
R.plurals.factadder_cards_added,
|
||||
value,
|
||||
value
|
||||
),
|
||||
true
|
||||
)
|
||||
} else {
|
||||
context.displayErrorSavingNote()
|
||||
}
|
||||
if (!context.addNote || context.caller == CALLER_NOTEEDITOR || context.aedictIntent) {
|
||||
context.changed = true
|
||||
closeEditorAfterSave = true
|
||||
} else if (context.caller == CALLER_NOTEEDITOR_INTENT_ADD) {
|
||||
if (value > 0) {
|
||||
context.changed = true
|
||||
}
|
||||
closeEditorAfterSave = true
|
||||
closeIntent = Intent().apply { putExtra(EXTRA_ID, context.intent.getStringExtra(EXTRA_ID)) }
|
||||
} else if (!context.mEditFields!!.isEmpty()) {
|
||||
context.mEditFields!!.first!!.focusWithKeyboard()
|
||||
}
|
||||
if (!closeEditorAfterSave && context.progressDialog != null && context.progressDialog!!.isShowing) {
|
||||
try {
|
||||
context.progressDialog!!.dismiss()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.e(e, "Note Editor: Error on dismissing progress dialog")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param result noException
|
||||
* @param result noOfSavedCards, null if any exception occurred internally
|
||||
*/
|
||||
@KotlinCleanup("invert if")
|
||||
override fun actualOnPostExecute(context: NoteEditor, result: Boolean?) {
|
||||
if (result!!) {
|
||||
@KotlinCleanup("return early and simplify if possible")
|
||||
override fun actualOnPostExecute(context: NoteEditor, result: Int?) {
|
||||
var closeEditorAfterSave = false
|
||||
var closeIntent: Intent? = null
|
||||
if (result != null) {
|
||||
// if task executed without any exception
|
||||
if (result > 0) {
|
||||
context.changed = true
|
||||
context.sourceText = null
|
||||
context.refreshNoteData(FieldChangeType.refreshWithStickyFields(shouldReplaceNewlines()))
|
||||
UIUtils.showThemedToast(
|
||||
context,
|
||||
context.resources.getQuantityString(
|
||||
R.plurals.factadder_cards_added,
|
||||
result,
|
||||
result
|
||||
),
|
||||
true
|
||||
)
|
||||
} else {
|
||||
context.displayErrorSavingNote()
|
||||
}
|
||||
|
||||
if (!context.addNote || context.caller == CALLER_NOTEEDITOR || context.aedictIntent) {
|
||||
context.changed = true
|
||||
closeEditorAfterSave = true
|
||||
} else if (context.caller == CALLER_NOTEEDITOR_INTENT_ADD) {
|
||||
if (result > 0) {
|
||||
context.changed = true
|
||||
}
|
||||
closeEditorAfterSave = true
|
||||
closeIntent = Intent().apply { putExtra(EXTRA_ID, context.intent.getStringExtra(EXTRA_ID)) }
|
||||
} else if (!context.mEditFields!!.isEmpty()) {
|
||||
context.mEditFields!!.first!!.focusWithKeyboard()
|
||||
}
|
||||
|
||||
if (context.progressDialog != null && context.progressDialog!!.isShowing) {
|
||||
try {
|
||||
context.progressDialog!!.dismiss()
|
||||
@ -247,6 +240,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
|
||||
Timber.e(e, "Note Editor: Error on dismissing progress dialog")
|
||||
}
|
||||
}
|
||||
|
||||
if (closeEditorAfterSave) {
|
||||
context.closeNoteEditor(closeIntent ?: Intent())
|
||||
} else {
|
||||
|
@ -162,21 +162,16 @@ open class CollectionTask<Progress, Result>(val task: TaskDelegateBase<Progress,
|
||||
}
|
||||
|
||||
@KotlinCleanup("non-null return")
|
||||
class AddNote(private val note: Note) : TaskDelegate<Int, Boolean?>() {
|
||||
override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener<Int>): Boolean {
|
||||
class AddNote(private val note: Note) : TaskDelegate<Void, Int?>() {
|
||||
override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener<Void>) = try {
|
||||
Timber.d("doInBackgroundAddNote")
|
||||
try {
|
||||
val db = col.db
|
||||
db.executeInTransaction {
|
||||
val value = col.addNote(note, Models.AllowEmpty.ONLY_CLOZE)
|
||||
collectionTask.doProgress(value)
|
||||
}
|
||||
} catch (e: RuntimeException) {
|
||||
Timber.e(e, "doInBackgroundAddNote - RuntimeException on adding note")
|
||||
CrashReportService.sendExceptionReport(e, "doInBackgroundAddNote")
|
||||
return false
|
||||
col.db.executeInTransaction {
|
||||
col.addNote(note, Models.AllowEmpty.ONLY_CLOZE)
|
||||
}
|
||||
return true
|
||||
} catch (e: RuntimeException) {
|
||||
Timber.e(e, "doInBackgroundAddNote - RuntimeException on adding note")
|
||||
CrashReportService.sendExceptionReport(e, "doInBackgroundAddNote")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user