Skip to content

Commit

Permalink
feat: add note edit button the new previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 12, 2023
1 parent f27a479 commit ee66393
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
22 changes: 21 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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"
Expand All @@ -2161,6 +2180,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
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

// preferences keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.previewer.PreviewerViewModel.Companion.stdHtml
import com.ichi2.themes.Themes
Expand Down Expand Up @@ -191,14 +193,30 @@ 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()
}
}
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit ee66393

Please sign in to comment.