mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-29 21:33:07 -08:00
Fix extra field cursor after orientation change
This commit is contained in:
1 parent
3b9a884db2
commit
6135544b72
8 files changed
+209
-32
No files matched your search
@@ -47,6 +47,7 @@ import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.education.EntryEditActivityEducation
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
import com.kunzisoft.keepass.model.FocusedEditField
|
||||
import com.kunzisoft.keepass.notifications.ClipboardEntryNotificationService
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_ENTRY_TASK
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_ENTRY_TASK
|
||||
@@ -87,6 +88,8 @@ class EntryEditActivity : LockingActivity(),
|
||||
private var validateButton: View? = null
|
||||
private var lockView: View? = null
|
||||
|
||||
private var mFocusedEditExtraField: FocusedEditField? = null
|
||||
|
||||
// Education
|
||||
private var entryEditActivityEducation: EntryEditActivityEducation? = null
|
||||
|
||||
@@ -152,8 +155,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
}
|
||||
|
||||
// Create the new entry from the current one
|
||||
if (savedInstanceState == null
|
||||
|| !savedInstanceState.containsKey(KEY_NEW_ENTRY)) {
|
||||
if (savedInstanceState?.containsKey(KEY_NEW_ENTRY) != true) {
|
||||
mEntry?.let { entry ->
|
||||
// Create a copy to modify
|
||||
mNewEntry = Entry(entry).also { newEntry ->
|
||||
@@ -168,8 +170,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
intent.getParcelableExtra<NodeId<*>>(KEY_PARENT)?.let {
|
||||
mIsNew = true
|
||||
// Create an empty new entry
|
||||
if (savedInstanceState == null
|
||||
|| !savedInstanceState.containsKey(KEY_NEW_ENTRY)) {
|
||||
if (savedInstanceState?.containsKey(KEY_NEW_ENTRY) != true) {
|
||||
mNewEntry = mDatabase?.createEntry()
|
||||
}
|
||||
mParent = mDatabase?.getGroupById(it)
|
||||
@@ -187,11 +188,14 @@ class EntryEditActivity : LockingActivity(),
|
||||
}
|
||||
|
||||
// Retrieve the new entry after an orientation change
|
||||
if (savedInstanceState != null
|
||||
&& savedInstanceState.containsKey(KEY_NEW_ENTRY)) {
|
||||
if (savedInstanceState?.containsKey(KEY_NEW_ENTRY) == true) {
|
||||
mNewEntry = savedInstanceState.getParcelable(KEY_NEW_ENTRY)
|
||||
}
|
||||
|
||||
if (savedInstanceState?.containsKey(EXTRA_FIELD_FOCUSED_ENTRY) == true) {
|
||||
mFocusedEditExtraField = savedInstanceState.getParcelable(EXTRA_FIELD_FOCUSED_ENTRY)
|
||||
}
|
||||
|
||||
// Close the activity if entry or parent can't be retrieve
|
||||
if (mNewEntry == null || mParent == null) {
|
||||
finish()
|
||||
@@ -299,7 +303,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
notes = newEntry.notes
|
||||
assignExtraFields(newEntry.customFields.mapTo(ArrayList()) {
|
||||
Field(it.key, it.value)
|
||||
})
|
||||
}, mFocusedEditExtraField)
|
||||
assignAttachments(newEntry.getAttachments()) { attachment ->
|
||||
newEntry.removeAttachment(attachment)
|
||||
}
|
||||
@@ -322,10 +326,11 @@ class EntryEditActivity : LockingActivity(),
|
||||
if (entryView.expires) {
|
||||
expiryTime = entryView.expiresDate
|
||||
}
|
||||
notes = entryView. notes
|
||||
notes = entryView.notes
|
||||
entryView.getExtraField().forEach { customField ->
|
||||
putExtraField(customField.name, customField.protectedValue)
|
||||
}
|
||||
mFocusedEditExtraField = entryView.getExtraFieldFocused()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,6 +534,10 @@ class EntryEditActivity : LockingActivity(),
|
||||
outState.putParcelable(KEY_NEW_ENTRY, it)
|
||||
}
|
||||
|
||||
mFocusedEditExtraField?.let {
|
||||
outState.putParcelable(EXTRA_FIELD_FOCUSED_ENTRY, it)
|
||||
}
|
||||
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
@@ -586,6 +595,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
|
||||
// SaveInstanceState
|
||||
const val KEY_NEW_ENTRY = "new_entry"
|
||||
const val EXTRA_FIELD_FOCUSED_ENTRY = "EXTRA_FIELD_FOCUSED_ENTRY"
|
||||
|
||||
// Keys for callback
|
||||
const val ADD_ENTRY_RESULT_CODE = 31
|
||||
|
||||
@@ -22,7 +22,7 @@ abstract class AnimatedItemsAdapter<Item, T: RecyclerView.ViewHolder>(val contex
|
||||
return itemsList.size
|
||||
}
|
||||
|
||||
fun assignItems(items: List<Item>) {
|
||||
open fun assignItems(items: List<Item>) {
|
||||
val previousSize = itemsList.size
|
||||
itemsList.apply {
|
||||
clear()
|
||||
|
||||
@@ -24,12 +24,13 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import androidx.core.widget.doOnTextChanged
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
import com.kunzisoft.keepass.model.FocusedEditField
|
||||
import com.kunzisoft.keepass.view.EditTextSelectable
|
||||
import com.kunzisoft.keepass.view.applyFontVisibility
|
||||
|
||||
class EntryExtraFieldsItemsAdapter(context: Context)
|
||||
@@ -41,7 +42,7 @@ class EntryExtraFieldsItemsAdapter(context: Context)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
private var mValueViewInputType: Int = 0
|
||||
private var mLastFocused: Field? = null
|
||||
private var mLastFocusedEditField = FocusedEditField()
|
||||
private var mLastFocusedTimestamp: Long = 0L
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntryExtraFieldViewHolder {
|
||||
@@ -69,15 +70,26 @@ class EntryExtraFieldsItemsAdapter(context: Context)
|
||||
// To Fix focus in RecyclerView
|
||||
setOnFocusChangeListener { _, hasFocus ->
|
||||
if (hasFocus) {
|
||||
focusField(extraField)
|
||||
setFocusField(extraField, selectionStart, selectionEnd)
|
||||
} else {
|
||||
requestFocusOnLastTextFocused(this, extraField)
|
||||
// request focus on last text focused
|
||||
if (focusedTimestampNotExpired()) {
|
||||
requestFocusField(this, extraField, false)
|
||||
} else {
|
||||
requestUnfocusField(extraField)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (extraField == mLastFocused) {
|
||||
post { requestFocus() }
|
||||
mLastFocused = null
|
||||
}
|
||||
addOnSelectionChangedListener(object: EditTextSelectable.OnSelectionChangedListener {
|
||||
override fun onSelectionChanged(start: Int, end: Int) {
|
||||
mLastFocusedEditField.apply {
|
||||
cursorSelectionStart = start
|
||||
cursorSelectionEnd = end
|
||||
}
|
||||
}
|
||||
})
|
||||
requestFocusField(this, extraField, true)
|
||||
setEditTextSelection(this)
|
||||
doOnTextChanged { text, _, _, _ ->
|
||||
extraField.protectedValue.stringValue = text.toString()
|
||||
}
|
||||
@@ -89,27 +101,82 @@ class EntryExtraFieldsItemsAdapter(context: Context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun focusField(field: Field, force: Boolean = false) {
|
||||
mLastFocused = field
|
||||
private fun setFocusField(field: Field,
|
||||
selectionStart: Int,
|
||||
selectionEnd: Int,
|
||||
force: Boolean = false) {
|
||||
mLastFocusedEditField.apply {
|
||||
this.field = field
|
||||
this.cursorSelectionStart = selectionStart
|
||||
this.cursorSelectionEnd = selectionEnd
|
||||
}
|
||||
setFocusField(mLastFocusedEditField, force)
|
||||
}
|
||||
|
||||
private fun setFocusField(field: FocusedEditField, force: Boolean = false) {
|
||||
mLastFocusedEditField = field
|
||||
mLastFocusedTimestamp = if (force) 0L else System.currentTimeMillis()
|
||||
}
|
||||
|
||||
fun assignItems(items: List<Field>, focusedEditField: FocusedEditField?) {
|
||||
focusedEditField?.let {
|
||||
setFocusField(it, true)
|
||||
}
|
||||
super.assignItems(items)
|
||||
}
|
||||
|
||||
override fun putItem(item: Field) {
|
||||
focusField(item, true)
|
||||
setFocusField(mLastFocusedEditField.apply {
|
||||
field = item
|
||||
cursorSelectionStart = -1
|
||||
cursorSelectionEnd = -1
|
||||
}, true)
|
||||
super.putItem(item)
|
||||
}
|
||||
|
||||
private fun requestFocusOnLastTextFocused(textView: TextView, field: Field) {
|
||||
if (field == mLastFocused) {
|
||||
if (mLastFocusedTimestamp == 0L || (mLastFocusedTimestamp + 350L) > System.currentTimeMillis())
|
||||
textView.post { textView.requestFocus() }
|
||||
mLastFocused = null
|
||||
private fun requestFocusField(editText: EditText, field: Field, setSelection: Boolean) {
|
||||
if (field == mLastFocusedEditField.field) {
|
||||
editText.apply {
|
||||
post {
|
||||
requestFocus()
|
||||
if (setSelection) {
|
||||
setEditTextSelection(editText)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestUnfocusField(field: Field) {
|
||||
if (mLastFocusedEditField.field == field) {
|
||||
mLastFocusedEditField.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setEditTextSelection(editText: EditText) {
|
||||
try {
|
||||
var newCursorPositionStart = mLastFocusedEditField.cursorSelectionStart
|
||||
var newCursorPositionEnd = mLastFocusedEditField.cursorSelectionEnd
|
||||
// Cursor at end if 0 or less
|
||||
if (newCursorPositionStart < 0 || newCursorPositionEnd < 0) {
|
||||
newCursorPositionStart = (editText.text?:"").length
|
||||
newCursorPositionEnd = newCursorPositionStart
|
||||
}
|
||||
editText.setSelection(newCursorPositionStart, newCursorPositionEnd)
|
||||
} catch (ignoredException: Exception) {}
|
||||
}
|
||||
|
||||
private fun focusedTimestampNotExpired(): Boolean {
|
||||
return mLastFocusedTimestamp == 0L || (mLastFocusedTimestamp + 350L) > System.currentTimeMillis()
|
||||
}
|
||||
|
||||
fun getFocusedField(): FocusedEditField {
|
||||
return mLastFocusedEditField
|
||||
}
|
||||
|
||||
inner class EntryExtraFieldViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
var extraFieldValueContainer: TextInputLayout = itemView.findViewById(R.id.entry_extra_field_value_container)
|
||||
var extraFieldValue: EditText = itemView.findViewById(R.id.entry_extra_field_value)
|
||||
var extraFieldValue: EditTextSelectable = itemView.findViewById(R.id.entry_extra_field_value)
|
||||
var extraFieldDeleteButton: View = itemView.findViewById(R.id.entry_extra_field_delete)
|
||||
}
|
||||
}
|
||||
@@ -49,9 +49,7 @@ class Field : Parcelable {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as Field
|
||||
if (other !is Field) return false
|
||||
|
||||
if (name != other.name) return false
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.kunzisoft.keepass.model
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
class FocusedEditField : Parcelable {
|
||||
|
||||
var field: Field? = null
|
||||
var cursorSelectionStart: Int = -1
|
||||
var cursorSelectionEnd: Int = -1
|
||||
|
||||
constructor()
|
||||
|
||||
constructor(parcel: Parcel) {
|
||||
this.field = parcel.readParcelable(Field::class.java.classLoader)
|
||||
this.cursorSelectionStart = parcel.readInt()
|
||||
this.cursorSelectionEnd = parcel.readInt()
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
this.field = null
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.writeParcelable(field, flags)
|
||||
parcel.writeInt(cursorSelectionStart)
|
||||
parcel.writeInt(cursorSelectionEnd)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is FocusedEditField) return false
|
||||
|
||||
if (field != other.field) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return field?.hashCode() ?: 0
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<FocusedEditField> {
|
||||
override fun createFromParcel(parcel: Parcel): FocusedEditField {
|
||||
return FocusedEditField(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<FocusedEditField?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.kunzisoft.keepass.view
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
|
||||
class EditTextSelectable @JvmOverloads constructor(context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyle: Int = 0)
|
||||
: TextInputEditText(context, attrs) {
|
||||
|
||||
private val mOnSelectionChangedListeners: MutableList<OnSelectionChangedListener>?
|
||||
|
||||
init {
|
||||
mOnSelectionChangedListeners = ArrayList()
|
||||
}
|
||||
|
||||
fun addOnSelectionChangedListener(onSelectionChangedListener: OnSelectionChangedListener) {
|
||||
mOnSelectionChangedListeners?.add(onSelectionChangedListener)
|
||||
}
|
||||
|
||||
fun removeOnSelectionChangedListener(onSelectionChangedListener: OnSelectionChangedListener) {
|
||||
mOnSelectionChangedListeners?.remove(onSelectionChangedListener)
|
||||
}
|
||||
|
||||
fun removeAllOnSelectionChangedListeners() {
|
||||
mOnSelectionChangedListeners?.clear()
|
||||
}
|
||||
|
||||
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
|
||||
mOnSelectionChangedListeners?.forEach {
|
||||
it.onSelectionChanged(selStart, selEnd)
|
||||
}
|
||||
}
|
||||
|
||||
interface OnSelectionChangedListener {
|
||||
fun onSelectionChanged(start: Int, end: Int)
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ import com.kunzisoft.keepass.icons.assignDatabaseIcon
|
||||
import com.kunzisoft.keepass.icons.assignDefaultDatabaseIcon
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
import com.kunzisoft.keepass.model.FocusedEditField
|
||||
import org.joda.time.Duration
|
||||
import org.joda.time.Instant
|
||||
|
||||
@@ -245,12 +246,18 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
return extraFieldsAdapter.itemsList
|
||||
}
|
||||
|
||||
fun getExtraFieldFocused(): FocusedEditField {
|
||||
// To keep focused after an orientation change
|
||||
return extraFieldsAdapter.getFocusedField()
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all children and add new views for each field
|
||||
*/
|
||||
fun assignExtraFields(fields: List<Field>) {
|
||||
fun assignExtraFields(fields: List<Field>, focusedExtraField: FocusedEditField? = null) {
|
||||
extraFieldsContainerView.visibility = if (fields.isEmpty()) View.GONE else View.VISIBLE
|
||||
extraFieldsAdapter.assignItems(fields)
|
||||
// Reinit focused field
|
||||
extraFieldsAdapter.assignItems(fields, focusedExtraField)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user