diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt index bf5937047..1bcef3cd7 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt @@ -39,7 +39,7 @@ abstract class TemplateAbstractView< private var mTemplate: Template? = null protected var mEntryInfo: EntryInfo? = null - protected var mCustomFieldIds = mutableListOf() + private var mViewFields = mutableListOf() protected var mFontInVisibility: Boolean = PreferencesUtil.fieldFontIsInVisibility(context) protected var mHideProtectedValue: Boolean = PreferencesUtil.hideProtectedValue(context) @@ -104,7 +104,7 @@ abstract class TemplateAbstractView< templateContainerView.removeAllViews() customFieldsContainerView.removeAllViews() notReferencedFieldsContainerView.removeAllViews() - mCustomFieldIds.clear() + mViewFields.clear() mTemplate?.let { template -> @@ -231,13 +231,13 @@ abstract class TemplateAbstractView< // Add new custom view id to the custom field list if (fieldTag == FIELD_CUSTOM_TAG) { - val indexOldItem = indexCustomFieldIdByName(field.name) + val indexOldItem = getIndexViewFieldByName(field.name) if (indexOldItem >= 0) - mCustomFieldIds.removeAt(indexOldItem) + mViewFields.removeAt(indexOldItem) if (itemView?.id != null) { - mCustomFieldIds.add( - FieldId( - itemView.id, + mViewFields.add( + ViewField( + itemView, field ) ) @@ -320,7 +320,7 @@ abstract class TemplateAbstractView< /** * Return empty custom fields */ - protected open fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { + protected open fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { mEntryInfo?.let { entryInfo -> populateEntryFieldView(FIELD_TITLE_TAG, @@ -350,15 +350,14 @@ abstract class TemplateAbstractView< showEmptyFields) customFieldsContainerView.removeAllViews() - val emptyCustomFields = mutableListOf().also { it.addAll(mCustomFieldIds) } + val emptyCustomFields = mutableListOf().also { it.addAll(mViewFields) } entryInfo.customFields.forEach { customField -> - val indexFieldViewId = indexCustomFieldIdByName(customField.name) + val indexFieldViewId = getIndexViewFieldByName(customField.name) if (indexFieldViewId >= 0) { // Template contains the custom view - val customFieldId = mCustomFieldIds[indexFieldViewId] - emptyCustomFields.remove(customFieldId) - templateContainerView.findViewById(customFieldId.viewId) - ?.let { customView -> + val viewField = mViewFields[indexFieldViewId] + emptyCustomFields.remove(viewField) + viewField.view.let { customView -> if (customView is GenericTextFieldView) { customView.value = customField.protectedValue.stringValue customView.applyFontVisibility(mFontInVisibility) @@ -459,22 +458,22 @@ abstract class TemplateAbstractView< * ------------- */ - protected data class FieldId(var viewId: Int, var field: Field) + protected data class ViewField(var view: View, var field: Field) private fun isStandardFieldName(name: String): Boolean { return TemplateField.isStandardFieldName(name) } - private fun customFieldIdByName(name: String): FieldId? { - return mCustomFieldIds.find { it.field.name.equals(name, true) } + protected fun getViewFieldByName(name: String): ViewField? { + return mViewFields.find { it.field.name.equals(name, true) } } - protected fun indexCustomFieldIdByName(name: String): Int { - return mCustomFieldIds.indexOfFirst { it.field.name.equals(name, true) } + private fun getIndexViewFieldByName(name: String): Int { + return mViewFields.indexOfFirst { it.field.name.equals(name, true) } } private fun retrieveCustomFieldsFromView(templateFieldNotEmpty: Boolean = false) { - mEntryInfo?.customFields = mCustomFieldIds.mapNotNull { + mEntryInfo?.customFields = mViewFields.mapNotNull { getCustomField(it.field.name, templateFieldNotEmpty) }.toMutableList() } @@ -485,9 +484,8 @@ abstract class TemplateAbstractView< } private fun getCustomField(fieldName: String, templateFieldNotEmpty: Boolean): Field? { - customFieldIdByName(fieldName)?.let { fieldId -> - val editView: View? = templateContainerView.findViewById(fieldId.viewId) - ?: customFieldsContainerView.findViewById(fieldId.viewId) + getViewFieldByName(fieldName)?.let { fieldId -> + val editView: View? = fieldId.view if (editView is GenericFieldView) { // Do not return field with a default value val defaultViewValue = if (editView.value == editView.default) "" else editView.value @@ -514,7 +512,7 @@ abstract class TemplateAbstractView< return if (!isStandardFieldName(customField.name)) { customFieldsContainerView.visibility = View.VISIBLE - if (indexCustomFieldIdByName(customField.name) >= 0) { + if (getIndexViewFieldByName(customField.name) >= 0) { // Update a custom field with a new value, // new field name must be the same as old field name replaceCustomField(customField, customField, false, focus) @@ -522,14 +520,14 @@ abstract class TemplateAbstractView< val newCustomView = buildViewForCustomField(customField) newCustomView?.let { customFieldsContainerView.addView(newCustomView) - val fieldId = FieldId( - newCustomView.id, + val fieldId = ViewField( + newCustomView, customField ) - val indexOldItem = indexCustomFieldIdByName(fieldId.field.name) + val indexOldItem = getIndexViewFieldByName(fieldId.field.name) if (indexOldItem >= 0) - mCustomFieldIds.removeAt(indexOldItem) - mCustomFieldIds.add(indexOldItem, fieldId) + mViewFields.removeAt(indexOldItem) + mViewFields.add(indexOldItem, fieldId) if (focus) newCustomView.requestFocus() } @@ -551,39 +549,37 @@ abstract class TemplateAbstractView< keepOldValue: Boolean, focus: Boolean): Boolean { if (!isStandardFieldName(newField.name)) { - customFieldIdByName(oldField.name)?.viewId?.let { viewId -> - customFieldsContainerView.findViewById(viewId)?.let { viewToReplace -> - val oldValue = getCustomField(oldField.name).protectedValue.toString() + getViewFieldByName(oldField.name)?.view?.let { viewToReplace -> + val oldValue = getCustomField(oldField.name).protectedValue.toString() - val parentGroup = viewToReplace.parent as ViewGroup - val indexInParent = parentGroup.indexOfChild(viewToReplace) - parentGroup.removeView(viewToReplace) + val parentGroup = viewToReplace.parent as ViewGroup + val indexInParent = parentGroup.indexOfChild(viewToReplace) + parentGroup.removeView(viewToReplace) - val newCustomFieldWithValue = if (keepOldValue) - Field(newField.name, - ProtectedString(newField.protectedValue.isProtected, oldValue) + val newCustomFieldWithValue = if (keepOldValue) + Field(newField.name, + ProtectedString(newField.protectedValue.isProtected, oldValue) + ) + else + newField + val oldPosition = getIndexViewFieldByName(oldField.name) + if (oldPosition >= 0) + mViewFields.removeAt(oldPosition) + + val newCustomView = buildViewForCustomField(newCustomFieldWithValue) + newCustomView?.let { + parentGroup.addView(newCustomView, indexInParent) + mViewFields.add( + oldPosition, + ViewField( + newCustomView, + newCustomFieldWithValue ) - else - newField - val oldPosition = indexCustomFieldIdByName(oldField.name) - if (oldPosition >= 0) - mCustomFieldIds.removeAt(oldPosition) - - val newCustomView = buildViewForCustomField(newCustomFieldWithValue) - newCustomView?.let { - parentGroup.addView(newCustomView, indexInParent) - mCustomFieldIds.add( - oldPosition, - FieldId( - newCustomView.id, - newCustomFieldWithValue - ) - ) - if (focus) - newCustomView.requestFocus() - } - return true + ) + if (focus) + newCustomView.requestFocus() } + return true } } return false @@ -599,12 +595,12 @@ abstract class TemplateAbstractView< } fun removeCustomField(oldCustomField: Field) { - val indexOldField = indexCustomFieldIdByName(oldCustomField.name) + val indexOldField = getIndexViewFieldByName(oldCustomField.name) if (indexOldField >= 0) { - mCustomFieldIds[indexOldField].viewId.let { viewId -> - customFieldsContainerView.removeViewById(viewId) + mViewFields[indexOldField].let { fieldView -> + customFieldsContainerView.removeViewById(fieldView.view.id) } - mCustomFieldIds.removeAt(indexOldField) + mViewFields.removeAt(indexOldField) } retrieveCustomFieldsFromView() } diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt index 4e20c8138..f1e967227 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt @@ -192,7 +192,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context, } } - override fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { + override fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { refreshIcon() return super.populateViewsWithEntryInfo(showEmptyFields) } diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt index c452f7485..51925f679 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt @@ -117,22 +117,22 @@ class TemplateView @JvmOverloads constructor(context: Context, return findViewWithTag(FIELD_PASSWORD_TAG)?.getCopyButtonView() } - override fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { + override fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { val emptyCustomFields = super.populateViewsWithEntryInfo(false) // Hide empty custom fields emptyCustomFields.forEach { customFieldId -> - templateContainerView.findViewById(customFieldId.viewId) - ?.isVisible = false + customFieldId.view.isVisible = false } + removeOtpRunnable() mEntryInfo?.let { entryInfo -> // Assign specific OTP dynamic view - removeOtpRunnable() entryInfo.otpModel?.let { assignOtp(it) } } + return emptyCustomFields } @@ -149,11 +149,10 @@ class TemplateView @JvmOverloads constructor(context: Context, private var mOnOtpElementUpdated: ((OtpElement?) -> Unit)? = null private fun getOtpTokenView(): TextFieldView? { - val indexFieldViewId = indexCustomFieldIdByName(OTP_TOKEN_FIELD) - if (indexFieldViewId >= 0) { - // Template contains the custom view - val customFieldId = mCustomFieldIds[indexFieldViewId] - return findViewById(customFieldId.viewId) + getViewFieldByName(OTP_TOKEN_FIELD)?.let { viewField -> + val view = viewField.view + if (view is TextFieldView) + return view } return null }