Skip to content

Commit

Permalink
load fragment after one second of text change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SanjaySargam committed Jul 29, 2024
1 parent 62c4f4b commit 0f77591
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -463,14 +468,26 @@ 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())
R.id.back_edit -> template.put("afmt", editorEditText.text)
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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0f77591

Please sign in to comment.