diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt index 3b8e47678576..4be6be4be36c 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt @@ -419,6 +419,12 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags mEditorNote = mCurrentEditedCard!!.note() addNote = false } + CALLER_PREVIEWER_EDIT -> { + val id = intent.extras?.getLong(EXTRA_EDIT_FROM_CARD_ID) + ?: throw IllegalArgumentException("null EXTRA_EDIT_FROM_CARD_ID") + mCurrentEditedCard = col.getCard(id) + mEditorNote = mCurrentEditedCard!!.note() + } CALLER_STUDYOPTIONS, CALLER_DECKPICKER, CALLER_REVIEWER_ADD, CALLER_CARDBROWSER_ADD, CALLER_NOTEEDITOR -> addNote = true @@ -858,8 +864,20 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags if (modified) { mEditorNote!!.setTagsFromStr(tagsAsString(mSelectedTags!!)) changed = true + if (caller == CALLER_PREVIEWER_EDIT) { + // only close the activity when the note is actually updated + withProgress { + undoableOp { + updateNote(mCurrentEditedCard!!.note()) + } + closeNoteEditor() + } + } else { + closeNoteEditor() + } + } else { + closeNoteEditor() } - closeNoteEditor() } } @@ -2147,6 +2165,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags const val EXTRA_ID = "ID" const val EXTRA_DID = "DECK_ID" const val EXTRA_TEXT_FROM_SEARCH_VIEW = "SEARCH" + const val EXTRA_EDIT_FROM_CARD_ID = "editCid" private const val ACTION_CREATE_FLASHCARD = "org.openintents.action.CREATE_FLASHCARD" private const val ACTION_CREATE_FLASHCARD_SEND = "android.intent.action.SEND" const val NOTE_CHANGED_EXTRA_KEY = "noteChanged" @@ -2157,11 +2176,15 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags const val CALLER_REVIEWER_EDIT = 1 const val CALLER_STUDYOPTIONS = 2 const val CALLER_DECKPICKER = 3 - const val CALLER_REVIEWER_ADD = 11 const val CALLER_CARDBROWSER_EDIT = 6 const val CALLER_CARDBROWSER_ADD = 7 const val CALLER_NOTEEDITOR = 8 + const val CALLER_PREVIEWER_EDIT = 9 const val CALLER_NOTEEDITOR_INTENT_ADD = 10 + const val CALLER_REVIEWER_ADD = 11 + const val REQUEST_ADD = 0 + const val REQUEST_MULTIMEDIA_EDIT = 2 + const val REQUEST_TEMPLATE_EDIT = 3 // preferences keys const val PREF_NOTE_EDITOR_SCROLL_TOOLBAR = "noteEditorScrollToolbar" diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt index d891d0404d9e..7403e337ec48 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerActivity.kt @@ -24,6 +24,7 @@ import android.view.View import android.webkit.CookieManager import android.webkit.WebView import android.webkit.WebViewClient +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.flowWithLifecycle @@ -33,6 +34,7 @@ import com.google.android.material.slider.Slider import com.google.android.material.textview.MaterialTextView import com.ichi2.anki.AnkiActivity import com.ichi2.anki.CollectionHelper +import com.ichi2.anki.NoteEditor import com.ichi2.anki.R import com.ichi2.anki.preferences.sharedPrefs import com.ichi2.anki.previewer.PreviewerViewModel.Companion.stdHtml @@ -199,7 +201,7 @@ class PreviewerActivity : AnkiActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.action_edit -> TODO() // in the next commit :) + R.id.action_edit -> editCard() R.id.action_back_side_only -> { viewModel.toggleBacksideOnly() } @@ -207,6 +209,22 @@ class PreviewerActivity : AnkiActivity() { return super.onOptionsItemSelected(item) } + private val editCardLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.data?.getBooleanExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, false) == true || + result.data?.getBooleanExtra(NoteEditor.NOTE_CHANGED_EXTRA_KEY, false) == true + ) { + viewModel.loadCurrentCard(reload = true) + } + } + + private fun editCard() { + val intent = Intent(this, NoteEditor::class.java).apply { + putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_PREVIEWER_EDIT) + putExtra(NoteEditor.EXTRA_EDIT_FROM_CARD_ID, viewModel.cardId()) + } + editCardLauncher.launch(intent) + } + companion object { private const val CURRENT_INDEX_EXTRA = "currentIndex" private const val CARD_IDS_EXTRA = "cardIds" diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt index 2952ce974698..c9651afb589e 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerViewModel.kt @@ -64,6 +64,8 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra fun serverBaseUrl() = server.baseUrl() + fun cardId() = currentCard.id + /** * MUST be called once before accessing [currentCard] for the first time *