Fix back pressing re-focus Search EditText

This commit is contained in:
PhilKes 2025-04-05 12:11:24 +02:00 committed by Phil
parent 8d6b318e3b
commit ffeecdf1ca
4 changed files with 10 additions and 21 deletions

View file

@ -178,7 +178,7 @@ afterEvaluate {
}
dependencies {
val navVersion = "2.3.5"
val navVersion = "2.8.9"
val roomVersion = "2.6.1"
implementation("androidx.navigation:navigation-fragment-ktx:$navVersion")

View file

@ -31,8 +31,6 @@ import com.philkes.notallyx.R
import com.philkes.notallyx.data.NotallyDatabase
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.data.model.Folder
import com.philkes.notallyx.data.model.Type
import com.philkes.notallyx.data.model.toText
import com.philkes.notallyx.databinding.ActivityMainBinding
import com.philkes.notallyx.presentation.activity.LockedActivity
import com.philkes.notallyx.presentation.activity.main.fragment.DisplayLabelFragment.Companion.EXTRA_DISPLAYED_LABEL
@ -40,7 +38,6 @@ import com.philkes.notallyx.presentation.activity.main.fragment.NotallyFragment
import com.philkes.notallyx.presentation.activity.note.EditListActivity
import com.philkes.notallyx.presentation.activity.note.EditNoteActivity
import com.philkes.notallyx.presentation.add
import com.philkes.notallyx.presentation.applySpans
import com.philkes.notallyx.presentation.getQuantityString
import com.philkes.notallyx.presentation.movedToResId
import com.philkes.notallyx.presentation.setCancelButton
@ -78,7 +75,7 @@ class MainActivity : LockedActivity<ActivityMainBinding>() {
var getCurrentFragmentNotes: (() -> Collection<BaseNote>?)? = null
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(configuration)
return navController.navigateUp(configuration) || super.onSupportNavigateUp()
}
override fun onCreate(savedInstanceState: Bundle?) {
@ -489,7 +486,7 @@ class MainActivity : LockedActivity<ActivityMainBinding>() {
popExit = androidx.navigation.ui.R.anim.nav_default_pop_exit_anim
popEnter = androidx.navigation.ui.R.anim.nav_default_pop_enter_anim
}
popUpTo(navController.graph.startDestination) { inclusive = false }
popUpTo(navController.graph.startDestinationId) { inclusive = false }
}
navController.navigate(id, null, options)
}

View file

@ -208,16 +208,6 @@ abstract class NotallyFragment : Fragment(), ItemListener {
navController.navigate(R.id.Search, bundle)
}
}
setOnFocusChangeListener { v, hasFocus ->
if (hasFocus && navController.currentDestination?.id != R.id.Search) {
val bundle =
Bundle().apply {
putSerializable(EXTRA_INITIAL_FOLDER, model.folder.value)
putSerializable(EXTRA_INITIAL_LABEL, model.currentLabel)
}
navController.navigate(R.id.Search, bundle)
}
}
}
}

View file

@ -49,28 +49,30 @@ open class EditTextWithWatcher(context: Context, attrs: AttributeSet) :
replaceWith = ReplaceWith("changeText/applyWithoutTextWatcher/..."),
)
override fun getText(): Editable? {
return super.getText()
return getTextSafe()
}
fun getTextClone(): Editable {
return super.getText()!!.clone()
return getTextSafe().clone()
}
fun applyWithoutTextWatcher(
callback: EditTextWithWatcher.() -> Unit
): Pair<Editable, Editable> {
val textBefore = super.getText()!!.clone()
val textBefore = getTextClone()
val editTextWatcher = textWatcher
editTextWatcher?.let { removeTextChangedListener(it) }
callback()
editTextWatcher?.let { addTextChangedListener(it) }
return Pair(textBefore, super.getText()!!.clone())
return Pair(textBefore, getTextClone())
}
fun changeText(callback: (text: Editable) -> Unit): Pair<Editable, Editable> {
return applyWithoutTextWatcher { callback(super.getText()!!) }
return applyWithoutTextWatcher { callback(getTextSafe()!!) }
}
private fun getTextSafe() = super.getText() ?: Editable.Factory.getInstance().newEditable("")
fun focusAndSelect(
start: Int = selectionStart,
end: Int = selectionEnd,