Skip to content

Commit 2a2cffd

Browse files
committed
Close IO editor after adding/updating note
1 parent d10c1e3 commit 2a2cffd

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ abstract class AbstractFlashcardViewer :
733733
} else if (resultCode == RESULT_CANCELED && !reloadRequired) {
734734
// nothing was changed by the note editor so just redraw the card
735735
redrawCard()
736+
} else if (resultCode == NoteEditor.RESULT_IO_EDITOR_UPDATED_NOTE) {
737+
displayCardQuestion(true)
736738
}
737739
}
738740
}

AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
255255
}
256256
)
257257

258+
private val requestIOEditorCloser = registerForActivityResult(
259+
ActivityResultContracts.StartActivityForResult(),
260+
NoteEditorActivityResultCallback { result ->
261+
if (result.resultCode != RESULT_CANCELED) {
262+
changed = true
263+
if (!addNote) {
264+
mReloadRequired = true
265+
closeNoteEditor(RESULT_UPDATED_IO_NOTE, null)
266+
}
267+
}
268+
}
269+
)
270+
258271
private inner class NoteEditorActivityResultCallback(private val callback: (result: ActivityResult) -> Unit) : ActivityResultCallback<ActivityResult> {
259272
override fun onActivityResult(result: ActivityResult) {
260273
Timber.d("onActivityResult() with result: %s", result.resultCode)
@@ -1999,7 +2012,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
19992012
id = mEditorNote?.id!!
20002013
}
20012014
val intent = ImageOcclusion.getIntent(this@NoteEditor, kind, id, imagePath)
2002-
startActivity(intent)
2015+
launchActivityForResultWithAnimation(intent, requestIOEditorCloser, START)
20032016
}
20042017

20052018
// ----------------------------------------------------------------------------
@@ -2268,6 +2281,8 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
22682281
const val CALLER_NOTEEDITOR = 8
22692282
const val CALLER_NOTEEDITOR_INTENT_ADD = 10
22702283

2284+
const val RESULT_UPDATED_IO_NOTE = 11
2285+
22712286
// preferences keys
22722287
const val PREF_NOTE_EDITOR_SCROLL_TOOLBAR = "noteEditorScrollToolbar"
22732288
private const val PREF_NOTE_EDITOR_SHOW_TOOLBAR = "noteEditorShowToolbar"

AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiServer.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package com.ichi2.anki.pages
1919

20+
import android.app.Activity
2021
import androidx.fragment.app.FragmentActivity
2122
import com.ichi2.anki.CollectionManager
2223
import com.ichi2.anki.CollectionManager.withCol
24+
import com.ichi2.anki.NoteEditor
2325
import com.ichi2.anki.importCsvRaw
2426
import com.ichi2.anki.importJsonFileRaw
27+
import com.ichi2.anki.launchCatchingTask
2528
import com.ichi2.anki.searchInBrowser
2629
import com.ichi2.libanki.*
2730
import com.ichi2.libanki.sched.computeFsrsWeightsRaw
@@ -30,6 +33,7 @@ import com.ichi2.libanki.sched.evaluateWeightsRaw
3033
import com.ichi2.libanki.stats.*
3134
import fi.iki.elonen.NanoHTTPD
3235
import kotlinx.coroutines.CoroutineScope
36+
import kotlinx.coroutines.delay
3337
import kotlinx.coroutines.runBlocking
3438
import timber.log.Timber
3539
import java.io.ByteArrayInputStream
@@ -96,8 +100,22 @@ open class AnkiServer(
96100
"getImageForOcclusion" -> withCol { getImageForOcclusionRaw(bytes) }
97101
"getImageOcclusionNote" -> withCol { getImageOcclusionNoteRaw(bytes) }
98102
"getImageForOcclusionFields" -> withCol { getImageOcclusionFieldsRaw(bytes) }
99-
"addImageOcclusionNote" -> withCol { addImageOcclusionNoteRaw(bytes) }
100-
"updateImageOcclusionNote" -> withCol { updateImageOcclusionNoteRaw(bytes) }
103+
"addImageOcclusionNote" -> withCol { addImageOcclusionNoteRaw(bytes) }.also {
104+
activity.launchCatchingTask {
105+
// Allow time for toast message to appear before closing editor
106+
delay(1000)
107+
activity.setResult(Activity.RESULT_OK)
108+
activity.finish()
109+
}
110+
}
111+
"updateImageOcclusionNote" -> withCol { updateImageOcclusionNoteRaw(bytes) }.also {
112+
activity.launchCatchingTask {
113+
// Allow time for toast message to appear before closing editor
114+
delay(1000)
115+
activity.setResult(NoteEditor.RESULT_UPDATED_IO_NOTE)
116+
activity.finish()
117+
}
118+
}
101119
else -> { throw Exception("unhandled request: $methodName") }
102120
}
103121
}

0 commit comments

Comments
 (0)