From a99ca00bb3b3d1ccc26d0c71a69c3d155c002bab Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 7 Jul 2021 19:26:32 +0200 Subject: [PATCH] Add default option --- .../element/template/TemplateAttribute.kt | 27 ++++++++++++--- .../template/TemplateAttributeOption.kt | 34 ++++++++++++++----- .../template/TemplateEngineCompatible.kt | 28 ++++++++++++--- .../keepass/view/TemplateAbstractView.kt | 10 +++--- .../keepass/view/TemplateEditView.kt | 10 +++--- .../kunzisoft/keepass/view/TemplateView.kt | 7 ++-- 6 files changed, 85 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt index c5ea6de22..b7f0f9dc6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttribute.kt @@ -28,16 +28,14 @@ data class TemplateAttribute(var label: String, var type: TemplateAttributeType, var protected: Boolean = false, var options: MutableMap = mutableMapOf(), - var action: TemplateAttributeAction = TemplateAttributeAction.NONE, - var defaultValue: String = ""): Parcelable { + var action: TemplateAttributeAction = TemplateAttributeAction.NONE): Parcelable { constructor(parcel: Parcel) : this( parcel.readString() ?: "", parcel.readEnum() ?: TemplateAttributeType.TEXT, parcel.readByte() != 0.toByte(), ParcelableUtil.readStringParcelableMap(parcel), - parcel.readEnum() ?: TemplateAttributeAction.NONE, - parcel.readString() ?: "") + parcel.readEnum() ?: TemplateAttributeAction.NONE) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(label) @@ -45,13 +43,32 @@ data class TemplateAttribute(var label: String, parcel.writeByte(if (protected) 1 else 0) ParcelableUtil.writeStringParcelableMap(parcel, LinkedHashMap(options)) parcel.writeEnum(action) - parcel.writeString(defaultValue) } override fun describeContents(): Int { return 0 } + var alias: String? + get() { + return TemplateAttributeOption.getAlias(this.options) + } + set(value) { + TemplateAttributeOption.setAlias(value, this.options) + } + + var default: String + get() { + return TemplateAttributeOption.getDefault(this.options) ?: "" + } + set(value) { + TemplateAttributeOption.setDefault(value, this.options) + } + + fun getNumberLines(): Int { + return TemplateAttributeOption.getNumberLines(this.options) + } + companion object CREATOR : Parcelable.Creator { override fun createFromParcel(parcel: Parcel): TemplateAttribute { return TemplateAttribute(parcel) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt index 95dcc6869..4277b7240 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt @@ -16,6 +16,14 @@ class TemplateAttributeOption { const val ALIAS_ATTR = "alias" const val ALIAS_VALUE_DEFAULT = "" + /** + * Applicable to each type + * Define a default string element representation + * For a type LIST, represents a single string element representation + */ + const val DEFAULT_ATTR = "default" + const val DEFAULT_VALUE = "" + /** * Applicable to type TEXT * Integer, can be "-1" or "many" to infinite value @@ -47,14 +55,6 @@ class TemplateAttributeOption { */ const val LIST_ITEMS = "items" - /** - * Applicable to type LIST - * Default string element representation - * 1st element if not defined - */ - const val LIST_DEFAULT_ITEM_ATTR = "default" - const val LIST_DEFAULT_ITEM_VALUE_DEFAULT = "1" - /** * Applicable to type DATETIME * String ("date" or "time" or "datetime" or based on https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) @@ -69,6 +69,24 @@ class TemplateAttributeOption { return options[ALIAS_ATTR] } + fun setAlias(value: String?, options: MutableMap) { + if (value == null) + options.remove(ALIAS_ATTR) + else + options[ALIAS_ATTR] = value + } + + fun getDefault(options: Map): String? { + return options[DEFAULT_ATTR] + } + + fun setDefault(value: String?, options: MutableMap) { + if (value == null) + options.remove(DEFAULT_ATTR) + else + options[DEFAULT_ATTR] = value + } + fun getNumberLines(options: Map): Int { return try { val value = options[TEXT_NUMBER_LINES_ATTR] diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt index 47e625803..fdfc3609f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt @@ -163,13 +163,24 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) val attributeName = label.substring(TEMPLATE_ATTRIBUTE_OPTIONS_PREFIX.length) val attribute = getOrRetrieveAttributeFromName(attributes, attributeName) if (value.isNotEmpty()) { - attribute.attribute.options[TEMPLATE_ATTRIBUTE_OPTIONS_DEFAULT] = value + attribute.attribute.options[TEMPLATE_ATTRIBUTE_OPTIONS_TEMP] = value } entryCopy.removeField(field.name) } catch (e: Exception) { Log.e(TAG, "Unable to retrieve template options", e) } } + else -> { + // To retrieve default values + if (attributes.containsKey(label)) { + if (value.isNotEmpty()) { + val attribute = attributes[label]!! + attribute.attribute.options[TemplateAttributeOption.DEFAULT_ATTR] = + value + } + } + entryCopy.removeField(label) + } } } @@ -178,8 +189,8 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) val attribute = it.attribute - // Recognize each default option - attribute.options[TEMPLATE_ATTRIBUTE_OPTIONS_DEFAULT]?.let { defaultOption -> + // Recognize each temp option + attribute.options[TEMPLATE_ATTRIBUTE_OPTIONS_TEMP]?.let { defaultOption -> when (attribute.type) { TemplateAttributeType.TEXT -> { try { @@ -232,7 +243,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) // No option here } } - attribute.options.remove(TEMPLATE_ATTRIBUTE_OPTIONS_DEFAULT) + attribute.options.remove(TEMPLATE_ATTRIBUTE_OPTIONS_TEMP) } // Add position for each attribute @@ -329,6 +340,13 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) TEMPLATE_ATTRIBUTE_OPTIONS_PREFIX + label, ProtectedString(false, defaultOption) ) + // Add default elements + options[TemplateAttributeOption.DEFAULT_ATTR]?.let { defaultValue -> + entryCopy.putField( + label, + ProtectedString(value.isProtected, defaultValue) + ) + } index++ } } @@ -357,7 +375,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) private const val TEMPLATE_ATTRIBUTE_TITLE_PREFIX = "_etm_title_" private const val TEMPLATE_ATTRIBUTE_TYPE_PREFIX = "_etm_type_" private const val TEMPLATE_ATTRIBUTE_OPTIONS_PREFIX = "_etm_options_" - private const val TEMPLATE_ATTRIBUTE_OPTIONS_DEFAULT = "default_option" + private const val TEMPLATE_ATTRIBUTE_OPTIONS_TEMP = "temp_option" private const val TEMPLATE_ATTRIBUTE_TYPE_PROTECTED = "Protected" private const val TEMPLATE_ATTRIBUTE_TYPE_INLINE = "Inline" private const val TEMPLATE_ATTRIBUTE_TYPE_INLINE_URL = "Inline URL" 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 34680da53..27250438d 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt @@ -102,8 +102,9 @@ abstract class TemplateAbstractView().apply { put(TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR, "3") }, - TemplateAttributeAction.CUSTOM_EDITION, - field.protectedValue.stringValue) + TemplateAttributeAction.CUSTOM_EDITION).apply { + default = field.protectedValue.stringValue + } return buildViewForTemplateField(customFieldTemplateAttribute, field, FIELD_CUSTOM_TAG) } 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 f6510bc81..c2540dd0c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt @@ -63,13 +63,13 @@ class TemplateEditView @JvmOverloads constructor(context: Context, EntryEditFieldView(it).apply { applyFontVisibility(mFontInVisibility) setProtection(field.protectedValue.isProtected, mHideProtectedValue) - val alias = TemplateAttributeOption.getAlias(templateAttribute.options) - label = alias ?: TemplateField.getLocalizedName(context, field.name) + label = templateAttribute.alias + ?: TemplateField.getLocalizedName(context, field.name) // TODO Max chars - val maxLinesOption = TemplateAttributeOption.getNumberLines(templateAttribute.options) - setMaxLines(maxLinesOption) + setMaxLines(templateAttribute.getNumberLines()) // TODO List items - value = field.protectedValue.stringValue + val fieldValue = field.protectedValue.stringValue + value = if (fieldValue.isEmpty()) templateAttribute.default else fieldValue when (templateAttribute.action) { TemplateAttributeAction.NONE -> { setOnActionClickListener(null) 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 a0b51f5f9..f016e56a6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt @@ -50,10 +50,9 @@ class TemplateView @JvmOverloads constructor(context: Context, EntryFieldView(it).apply { applyFontVisibility(mFontInVisibility) setProtection(field.protectedValue.isProtected, mHideProtectedValue) - val alias = TemplateAttributeOption.getAlias(templateAttribute.options) - label = alias ?: TemplateField.getLocalizedName(context, field.name) - val maxLines = TemplateAttributeOption.getNumberLines(templateAttribute.options) - setMaxLines(maxLines) + label = templateAttribute.alias + ?: TemplateField.getLocalizedName(context, field.name) + setMaxLines(templateAttribute.getNumberLines()) // TODO Linkify value = field.protectedValue.stringValue