0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

load fragment after one second of text change

refresh fragment after one second of text changed. And hide preview icon if fragmented as there is no point of having this option because the fragment will auto-refresh the content.
This commit is contained in:
SanjaySargam 2024-06-10 23:54:14 +05:30 committed by David Allison
parent b1a94f5ec5
commit 35cfd4de33

View File

@ -21,6 +21,8 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.view.ActionMode
@ -65,6 +67,7 @@ import com.ichi2.anki.previewer.TemplatePreviewerArguments
import com.ichi2.anki.previewer.TemplatePreviewerFragment
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.utils.ext.isImageOcclusion
import com.ichi2.anki.utils.postDelayed
import com.ichi2.annotations.NeedsTest
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.libanki.Collection
@ -87,6 +90,7 @@ import timber.log.Timber
import java.util.regex.Pattern
import kotlin.math.max
import kotlin.math.min
import kotlin.time.Duration.Companion.seconds
/**
* Allows the user to view the template for the current note type
@ -414,6 +418,7 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
}
class CardTemplateFragment : Fragment() {
private val refreshFragmentHandler = Handler(Looper.getMainLooper())
private var currentEditorTitle: FixedTextView? = null
private lateinit var editorEditText: FixedEditText
@ -463,7 +468,14 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
// Set text change listeners
val templateEditorWatcher: TextWatcher = object : TextWatcher {
/**
* Declare a nullable variable refreshFragmentRunnable of type Runnable.
* This will hold a reference to the Runnable that refreshes the previewer fragment.
* It is used to manage delayed fragment updates and can be null if no updates in card.
*/
private var refreshFragmentRunnable: Runnable? = null
override fun afterTextChanged(arg0: Editable) {
refreshFragmentRunnable?.let { refreshFragmentHandler.removeCallbacks(it) }
templateEditor.tabToCursorPosition[cardIndex] = editorEditText.selectionStart
when (currentEditorViewId) {
R.id.styling_edit -> tempModel.updateCss(editorEditText.text.toString())
@ -471,6 +483,11 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
else -> template.put("qfmt", editorEditText.text)
}
templateEditor.tempModel!!.updateTemplate(cardIndex, template)
val updateRunnable = Runnable {
templateEditor.loadTemplatePreviewerFragmentIfFragmented()
}
refreshFragmentRunnable = updateRunnable
refreshFragmentHandler.postDelayed(updateRunnable, REFRESH_PREVIEW_DELAY)
}
override fun beforeTextChanged(arg0: CharSequence, arg1: Int, arg2: Int, arg3: Int) {
@ -755,6 +772,11 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
menu.findItem(R.id.action_delete).isVisible = false
}
// Hide preview option if the view is big enough
if (templateEditor.fragmented) {
menu.findItem(R.id.action_preview).isVisible = false
}
// marked insert field menu item invisible for style view
val isInsertFieldItemVisible = currentEditorViewId != R.id.styling_edit
menu.findItem(R.id.action_insert_field).isVisible = isInsertFieldItemVisible
@ -1198,6 +1220,9 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
private const val EDITOR_START_ORD_ID = "ordId"
private const val CARD_INDEX = "card_ord"
// Time to wait before refreshing the previewer
private val REFRESH_PREVIEW_DELAY = 1.seconds
@Suppress("unused")
private const val REQUEST_PREVIEWER = 0