mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 03:52:15 +02:00
chore: extract ActionModeClass from NoteEditor
This commit is contained in:
parent
0fc602e8b0
commit
27f5f25b3d
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Ashish Yadav <mailtoashish693@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation; either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.ichi2.anki
|
||||
|
||||
import android.os.Build
|
||||
import android.view.ActionMode
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import androidx.annotation.RequiresApi
|
||||
|
||||
/**
|
||||
* Custom ActionMode.Callback implementation for adding and handling cloze deletion action
|
||||
* button in the text selection menu or long press.
|
||||
*/
|
||||
class CustomActionModeCallback(
|
||||
private val isClozeType: Boolean,
|
||||
private val clozeMenuTitle: String,
|
||||
private val clozeMenuId: Int,
|
||||
private val onActionItemSelected: (mode: ActionMode, item: MenuItem) -> Boolean
|
||||
) : ActionMode.Callback {
|
||||
@RequiresApi(Build.VERSION_CODES.N)
|
||||
private val setLanguageId = View.generateViewId()
|
||||
|
||||
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
|
||||
// Adding the cloze deletion floating context menu item, but only once.
|
||||
if (menu.findItem(clozeMenuId) != null) {
|
||||
return false
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && menu.findItem(setLanguageId) != null) {
|
||||
return false
|
||||
}
|
||||
|
||||
val item: MenuItem? = menu.findItem(android.R.id.pasteAsPlainText)
|
||||
val platformPasteMenuItem: MenuItem? = menu.findItem(android.R.id.paste)
|
||||
if (item != null && platformPasteMenuItem != null) {
|
||||
item.isVisible = false
|
||||
}
|
||||
|
||||
val initialSize = menu.size()
|
||||
if (isClozeType) {
|
||||
menu.add(
|
||||
Menu.NONE,
|
||||
clozeMenuId,
|
||||
0,
|
||||
clozeMenuTitle
|
||||
)
|
||||
}
|
||||
return initialSize != menu.size()
|
||||
}
|
||||
|
||||
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
|
||||
return onActionItemSelected(mode, item)
|
||||
}
|
||||
|
||||
override fun onDestroyActionMode(mode: ActionMode) {
|
||||
// Left empty on purpose
|
||||
}
|
||||
}
|
@ -1531,7 +1531,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
|
||||
editLineView.setEnableAnimation(animationEnabled())
|
||||
|
||||
// Use custom implementation of ActionMode.Callback customize selection and insert menus
|
||||
editLineView.setActionModeCallbacks(ActionModeCallback(newEditText))
|
||||
editLineView.setActionModeCallbacks(getActionModeCallback(newEditText, View.generateViewId()))
|
||||
editLineView.setHintLocale(getHintLocaleForField(editLineView.name))
|
||||
initFieldEditText(newEditText, i, !editModelMode)
|
||||
editFields!!.add(newEditText)
|
||||
@ -1576,6 +1576,23 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
|
||||
}
|
||||
}
|
||||
|
||||
private fun getActionModeCallback(textBox: FieldEditText, clozeMenuId: Int): ActionMode.Callback {
|
||||
return CustomActionModeCallback(
|
||||
isClozeType,
|
||||
getString(R.string.multimedia_editor_popup_cloze),
|
||||
clozeMenuId,
|
||||
onActionItemSelected = { mode, item ->
|
||||
if (item.itemId == clozeMenuId) {
|
||||
convertSelectedTextToCloze(textBox, AddClozeType.INCREMENT_NUMBER)
|
||||
mode.finish()
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun onImagePaste(editText: EditText, uri: Uri): Boolean {
|
||||
val imageTag = mediaRegistration!!.onImagePaste(uri) ?: return false
|
||||
insertStringInField(editText, imageTag)
|
||||
@ -2310,67 +2327,6 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom ActionMode.Callback implementation for adding and handling cloze deletion action
|
||||
* button in the text selection menu.
|
||||
*/
|
||||
private inner class ActionModeCallback(
|
||||
private val textBox: FieldEditText
|
||||
) : ActionMode.Callback {
|
||||
private val clozeMenuId = View.generateViewId()
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N)
|
||||
private val setLanguageId = View.generateViewId()
|
||||
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
|
||||
// Adding the cloze deletion floating context menu item, but only once.
|
||||
if (menu.findItem(clozeMenuId) != null) {
|
||||
return false
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && menu.findItem(setLanguageId) != null) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Removes paste as plain text from ContextMenu in NoteEditor
|
||||
val item: MenuItem? = menu.findItem(android.R.id.pasteAsPlainText)
|
||||
val platformPasteMenuItem: MenuItem? = menu.findItem(android.R.id.paste)
|
||||
if (item != null && platformPasteMenuItem != null) {
|
||||
item.isVisible = false
|
||||
}
|
||||
|
||||
val initialSize = menu.size()
|
||||
if (isClozeType) {
|
||||
// 10644: Do not pass in a R.string as the final parameter as MIUI on Android 12 crashes.
|
||||
menu.add(
|
||||
Menu.NONE,
|
||||
clozeMenuId,
|
||||
0,
|
||||
getString(R.string.multimedia_editor_popup_cloze)
|
||||
)
|
||||
}
|
||||
return initialSize != menu.size()
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
|
||||
val itemId = item.itemId
|
||||
return if (itemId == clozeMenuId) {
|
||||
convertSelectedTextToCloze(textBox, AddClozeType.INCREMENT_NUMBER)
|
||||
mode.finish()
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyActionMode(mode: ActionMode) {
|
||||
// Left empty on purpose
|
||||
}
|
||||
}
|
||||
|
||||
private fun convertSelectedTextToCloze(textBox: FieldEditText, addClozeType: AddClozeType) {
|
||||
var nextClozeIndex = nextClozeIndex
|
||||
if (addClozeType == AddClozeType.SAME_NUMBER) {
|
||||
|
Loading…
Reference in New Issue
Block a user