Skip to content

Commit

Permalink
refactor: NoteEditor to use the updated MediaRegistration code
Browse files Browse the repository at this point in the history
- We get the Boolean via PASTE_IMAGES_AS_PNG from collection to see if we want to preserve the image extension or not and proceed to paste the image
  • Loading branch information
criticalAY committed Feb 8, 2025
1 parent aed6a96 commit ac56aba
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ class NoteEditor :
private var reloadRequired = false

private var fieldsLayoutContainer: LinearLayout? = null
private var mediaRegistration: MediaRegistration? = null
private var tagsDialogFactory: TagsDialogFactory? = null
private var tagsButton: AppCompatButton? = null
private var cardsButton: AppCompatButton? = null
Expand Down Expand Up @@ -395,7 +394,10 @@ class NoteEditor :

for (uri in clip.items().map { it.uri }) {
try {
onPaste(view as EditText, uri, description)
lifecycleScope.launch {
val pasteAsPng = shouldPasteAsPng()
onPaste(view as EditText, uri, description, pasteAsPng)
}
} catch (e: Exception) {
Timber.w(e)
CrashReportService.sendExceptionReport(e, "NoteEditor::onReceiveContent")
Expand Down Expand Up @@ -486,7 +488,6 @@ class NoteEditor :
// ----------------------------------------------------------------------------
override fun onCreate(savedInstanceState: Bundle?) {
tagsDialogFactory = TagsDialogFactory(this).attachToFragmentManager<TagsDialogFactory>(parentFragmentManager)
mediaRegistration = MediaRegistration(requireContext())
super.onCreate(savedInstanceState)
fieldState.setInstanceState(savedInstanceState)
val intent = requireActivity().intent
Expand Down Expand Up @@ -1685,6 +1686,9 @@ class NoteEditor :
return note
}

/** Determines whether pasted images should be handled as PNG format. **/
private suspend fun shouldPasteAsPng() = withCol { config.getBool(ConfigKey.Bool.PASTE_IMAGES_AS_PNG) }

val currentFields: Fields
get() = editorNote!!.notetype.flds

Expand Down Expand Up @@ -1731,12 +1735,16 @@ class NoteEditor :
val editLineView = editLines[i]
customViewIds.add(editLineView.id)
val newEditText = editLineView.editText
newEditText.setPasteListener { editText: EditText?, uri: Uri?, description: ClipDescription? ->
onPaste(
editText!!,
uri!!,
description!!,
)
lifecycleScope.launch {
val pasteAsPng = shouldPasteAsPng()
newEditText.setPasteListener { editText: EditText?, uri: Uri?, description: ClipDescription? ->
onPaste(
editText!!,
uri!!,
description!!,
pasteAsPng,
)
}
}
editLineView.configureView(
requireActivity(),
Expand Down Expand Up @@ -2010,8 +2018,19 @@ class NoteEditor :
editText: EditText,
uri: Uri,
description: ClipDescription,
pasteAsPng: Boolean,
): Boolean {
val mediaTag = mediaRegistration!!.onPaste(uri, description) ?: return false
val mediaTag =
MediaRegistration.onPaste(
requireContext(),
uri,
description,
pasteAsPng,
showError = { type, message ->
showSnackbar(type.toHumanReadableString(requireContext(), message ?: ""))
},
) ?: return false

insertStringInField(editText, mediaTag)
return true
}
Expand Down

0 comments on commit ac56aba

Please sign in to comment.