0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 12:02:16 +02:00

Made Focusable DeckOptions Preferences Auto-Focus (#11087)

* Made Focusable DeckOptions Preferences Auto-Focus
* Made Default AutoFocus behavior "Cursor at the end"
This commit is contained in:
dorrin-sot 2022-04-26 19:11:08 +04:30 committed by GitHub
parent 611844a8d7
commit 4939304351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 6 deletions

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2022 Dorrin Sotoudeh <dorrinsotoudeh123@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.preferences
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.EditText
import com.ichi2.utils.AndroidUiUtils.setFocusAndOpenKeyboard
interface AutoFocusable {
fun autoFocusAndMoveCursorToEnd(editText: EditText) {
// focus EditText and place cursor at the end of text
setFocusAndOpenKeyboard(editText) { editText.setSelection(editText.text.length) }
}
}
@Suppress("deprecation")
open class AutoFocusEditTextPreference(context: Context?, attrs: AttributeSet?) : android.preference.EditTextPreference(context, attrs), AutoFocusable {
override fun onBindView(view: View?) {
super.onBindView(view)
autoFocusAndMoveCursorToEnd(editText)
}
}

View File

@ -21,11 +21,12 @@ import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import com.ichi2.anki.AnkiDroidApp
import timber.log.Timber
@Suppress("deprecation") // TODO Tracked in https://github.com/ankidroid/Anki-Android/issues/5019 : use NumberRangePreferenceCompat
open class NumberRangePreference : android.preference.EditTextPreference {
open class NumberRangePreference : android.preference.EditTextPreference, AutoFocusable {
protected val mMin: Int
private val mMax: Int
@ -47,6 +48,11 @@ open class NumberRangePreference : android.preference.EditTextPreference {
updateSettings()
}
override fun onBindView(view: View?) {
super.onBindView(view)
autoFocusAndMoveCursorToEnd(editText)
}
override fun onDialogClosed(positiveResult: Boolean) {
if (positiveResult) {
val validated = getValidatedRangeFromString(editText.text.toString())

View File

@ -20,6 +20,7 @@ import android.content.Context
import android.text.InputType
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.R
import com.ichi2.anki.UIUtils.showThemedToast
@ -28,7 +29,7 @@ import com.ichi2.utils.JSONException
import timber.log.Timber
@Suppress("deprecation") // TODO Tracked in https://github.com/ankidroid/Anki-Android/issues/5019
class StepsPreference : android.preference.EditTextPreference {
class StepsPreference : android.preference.EditTextPreference, AutoFocusable {
private val mAllowEmpty: Boolean
@Suppress("unused")
@ -49,6 +50,11 @@ class StepsPreference : android.preference.EditTextPreference {
updateSettings()
}
override fun onBindView(view: View?) {
super.onBindView(view)
autoFocusAndMoveCursorToEnd(editText)
}
/**
* Update settings to show a numeric keyboard instead of the default keyboard.
* <p>

View File

@ -47,14 +47,16 @@ object AndroidUiUtils {
/**
* Focuses on View and opens the soft keyboard.
* @param view The View which requires the focus to be set (typically an EditText).
* @param runnable The Optional Runnable that will be executed at the end.
*/
@JvmStatic
fun setFocusAndOpenKeyboard(view: View) {
fun setFocusAndOpenKeyboard(view: View, runnable: Runnable? = null) {
// Required on some Android 9, 10 devices to show keyboard: https://stackoverflow.com/a/7784904
view.postDelayed({
view.requestFocus()
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
runnable?.run()
}, 200)
}
}

View File

@ -29,7 +29,7 @@
android:key="currentConf"
android:title="@string/deck_conf_current_group" />
<EditTextPreference
<com.ichi2.preferences.AutoFocusEditTextPreference
android:key="confAdd"
android:summary="@string/deck_conf_add_summ"
android:title="@string/deck_conf_add" />
@ -43,7 +43,7 @@
android:positiveButtonText="@string/dialog_positive_remove"
android:title="@string/dialog_positive_remove" />
<EditTextPreference
<com.ichi2.preferences.AutoFocusEditTextPreference
android:key="confRename"
android:title="@string/deck_conf_rename" />
@ -229,7 +229,7 @@
android:title="@string/deck_conf_reminders_time"
android:dependency="reminderEnabled" />
</PreferenceScreen>
<EditTextPreference
<com.ichi2.preferences.AutoFocusEditTextPreference
android:key="desc"
android:title="@string/deck_conf_deck_description" />
</PreferenceScreen>