diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/noteeditor/FieldState.kt b/AnkiDroid/src/main/java/com/ichi2/anki/noteeditor/FieldState.kt index fb62eeec1c23..554913150e2a 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/noteeditor/FieldState.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/noteeditor/FieldState.kt @@ -19,7 +19,6 @@ package com.ichi2.anki.noteeditor import android.content.Context import android.os.Bundle import android.view.View -import androidx.core.os.BundleCompat import com.ichi2.anki.FieldEditLine import com.ichi2.anki.NoteEditor import com.ichi2.anki.R @@ -37,19 +36,20 @@ import kotlin.math.min class FieldState private constructor( private val editor: NoteEditor, ) { - private var savedFieldData: List? = null + private var customViewIds: List? = null fun loadFieldEditLines(type: FieldChangeType): List { - val fieldEditLines: List - if (type.type == Type.INIT && savedFieldData != null) { - fieldEditLines = recreateFieldsFromState() - savedFieldData = null - } else { - fieldEditLines = createFields(type) - } - for (l in fieldEditLines) { - l.id = View.generateViewId() - } + val fieldEditLines: List = + if (type.type == Type.INIT && customViewIds != null) { + recreateFields(customViewIds!!) + } else { + createFields(type).also { fields -> + for (field in fields) { + field.id = View.generateViewId() + } + } + } + if (type.type == Type.CLEAR_KEEP_STICKY) { // we use the UI values here as the model will post-processing steps (newline -> br). val currentFieldStrings = editor.currentFieldStrings @@ -69,18 +69,12 @@ class FieldState private constructor( return fieldEditLines } - private fun recreateFieldsFromState(): List { - val editLines: MutableList = ArrayList(savedFieldData!!.size) - for (state in savedFieldData!!) { - val editLineView = FieldEditLine(editor.requireContext()) - if (editLineView.id == 0) { - editLineView.id = View.generateViewId() - } - editLineView.loadState(state) - editLines.add(editLineView) - } - return editLines - } + /** + * Given a list of [viewIds]: create the fields, assign the IDs and let Android + * restore the state via [FieldEditLine.onRestoreInstanceState] + */ + private fun recreateFields(viewIds: List): List = + viewIds.map { id -> FieldEditLine(editor.requireContext()).also { field -> field.id = id } } private fun createFields(type: FieldChangeType): List { val fields = getFields(type) @@ -105,28 +99,7 @@ class FieldState private constructor( } fun setInstanceState(savedInstanceState: Bundle?) { - if (savedInstanceState == null) { - return - } - if (!savedInstanceState.containsKey("customViewIds") || !savedInstanceState.containsKey("android:viewHierarchyState")) { - return - } - val customViewIds = savedInstanceState.getIntegerArrayList("customViewIds") - val viewHierarchyState = savedInstanceState.getBundle("android:viewHierarchyState") - if (customViewIds == null || viewHierarchyState == null) { - return - } - val views = - BundleCompat.getSparseParcelableArray( - viewHierarchyState, - "android:views", - View.BaseSavedState::class.java, - ) ?: return - val important: MutableList = ArrayList(customViewIds.size) - for (i in customViewIds) { - important.add(views[i!!] as View.BaseSavedState) - } - savedFieldData = important + customViewIds = savedInstanceState?.getIntegerArrayList("customViewIds") } /** How fields should be changed when the UI is rebuilt */