From f8a2748edea70aaa9018a189241a812b66c18968 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 7 Jul 2021 20:59:33 +0200 Subject: [PATCH] Refactor options --- .../database/element/template/Template.kt | 17 +- .../element/template/TemplateAttribute.kt | 18 +- .../template/TemplateAttributeOption.kt | 250 ++++++++++++------ .../element/template/TemplateBuilder.kt | 13 +- .../template/TemplateEngineCompatible.kt | 88 +++--- .../keepass/view/TemplateAbstractView.kt | 12 +- .../keepass/view/TemplateEditView.kt | 4 +- .../kunzisoft/keepass/view/TemplateView.kt | 6 +- 8 files changed, 240 insertions(+), 168 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt index 1bfa9d224..13194cf63 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt @@ -119,8 +119,8 @@ class Template : Parcelable { TemplateField.LABEL_PASSWORD, TemplateAttributeType.TEXT, true, - LinkedHashMap().apply { - put(TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR, "3") + TemplateAttributeOption().apply { + setNumberLines(3) }, TemplateAttributeAction.PASSWORD_GENERATION ) @@ -128,22 +128,19 @@ class Template : Parcelable { TemplateField.LABEL_URL, TemplateAttributeType.TEXT, false, - LinkedHashMap().apply { - put(TemplateAttributeOption.TEXT_LINK_ATTR, "true") + TemplateAttributeOption().apply { + setLink(true) }) val EXPIRATION_ATTRIBUTE = TemplateAttribute( TemplateField.LABEL_EXPIRATION, TemplateAttributeType.DATETIME, - false, - LinkedHashMap().apply { - put(TemplateAttributeOption.DATETIME_FORMAT_ATTR, "datetime") - }) + false) val NOTES_ATTRIBUTE = TemplateAttribute( TemplateField.LABEL_NOTES, TemplateAttributeType.TEXT, false, - LinkedHashMap().apply { - put(TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR, "-1") + TemplateAttributeOption().apply { + setNumberLinesToMany() }) val STANDARD: Template 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 b7f0f9dc6..734413be5 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 @@ -27,21 +27,21 @@ import com.kunzisoft.keepass.utils.writeEnum data class TemplateAttribute(var label: String, var type: TemplateAttributeType, var protected: Boolean = false, - var options: MutableMap = mutableMapOf(), + var options: TemplateAttributeOption = TemplateAttributeOption(), var action: TemplateAttributeAction = TemplateAttributeAction.NONE): Parcelable { constructor(parcel: Parcel) : this( parcel.readString() ?: "", parcel.readEnum() ?: TemplateAttributeType.TEXT, parcel.readByte() != 0.toByte(), - ParcelableUtil.readStringParcelableMap(parcel), + parcel.readParcelable(TemplateAttributeOption::class.java.classLoader) ?: TemplateAttributeOption(), parcel.readEnum() ?: TemplateAttributeAction.NONE) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(label) parcel.writeEnum(type) parcel.writeByte(if (protected) 1 else 0) - ParcelableUtil.writeStringParcelableMap(parcel, LinkedHashMap(options)) + parcel.writeParcelable(options, flags) parcel.writeEnum(action) } @@ -51,24 +51,20 @@ data class TemplateAttribute(var label: String, var alias: String? get() { - return TemplateAttributeOption.getAlias(this.options) + return options.alias } set(value) { - TemplateAttributeOption.setAlias(value, this.options) + options.alias = value } var default: String get() { - return TemplateAttributeOption.getDefault(this.options) ?: "" + return this.options.default } set(value) { - TemplateAttributeOption.setDefault(value, this.options) + this.options.default = value } - 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 4277b7240..77873f310 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 @@ -1,130 +1,209 @@ package com.kunzisoft.keepass.database.element.template +import android.os.Parcel +import android.os.Parcelable import com.kunzisoft.keepass.database.element.DateInstant +import com.kunzisoft.keepass.utils.ParcelableUtil import java.net.URLDecoder import java.net.URLEncoder -class TemplateAttributeOption { +class TemplateAttributeOption() : Parcelable { - companion object { + private val mOptions: MutableMap = mutableMapOf() + + constructor(parcel: Parcel) : this() { + ParcelableUtil.readStringParcelableMap(parcel) + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + ParcelableUtil.writeStringParcelableMap(parcel, LinkedHashMap(mOptions)) + } + + override fun describeContents(): Int { + return 0 + } + + var alias: String? + get() { + val tempAlias = mOptions[ALIAS_ATTR] + if (tempAlias.isNullOrEmpty()) + return null + return tempAlias + } + set(value) { + if (value == null) + mOptions.remove(ALIAS_ATTR) + else + mOptions[ALIAS_ATTR] = value + } + + var default: String + get() { + return mOptions[DEFAULT_ATTR] ?: DEFAULT_VALUE + } + set(value) { + mOptions[DEFAULT_ATTR] = value + } + + fun getNumberChars(): Int { + return try { + if (mOptions[TEXT_NUMBER_CHARS_ATTR].equals(TEXT_NUMBER_CHARS_VALUE_MANY_STRING, true)) + TEXT_NUMBER_CHARS_VALUE_MANY + else + mOptions[TEXT_NUMBER_CHARS_ATTR]?.toInt() ?: TEXT_NUMBER_CHARS_VALUE_DEFAULT + } catch (e: Exception) { + TEXT_NUMBER_CHARS_VALUE_DEFAULT + } + } + + fun setNumberChars(numberChars: Int) { + mOptions[TEXT_NUMBER_CHARS_ATTR] = numberChars.toString() + } + + fun setNumberCharsToMany() { + mOptions[TEXT_NUMBER_CHARS_ATTR] = TEXT_NUMBER_CHARS_VALUE_MANY_STRING + } + + fun getNumberLines(): Int { + return try { + if (mOptions[TEXT_NUMBER_LINES_ATTR].equals(TEXT_NUMBER_LINES_VALUE_MANY_STRING, true)) + TEXT_NUMBER_LINES_VALUE_MANY + else + mOptions[TEXT_NUMBER_LINES_ATTR]?.toInt() ?: TEXT_NUMBER_LINES_VALUE_DEFAULT + } catch (e: Exception) { + TEXT_NUMBER_LINES_VALUE_DEFAULT + } + } + + fun setNumberLines(numberLines: Int) { + val lines = if (numberLines == 0) 1 else numberLines + mOptions[TEXT_NUMBER_LINES_ATTR] = lines.toString() + } + + fun setNumberLinesToMany() { + mOptions[TEXT_NUMBER_LINES_ATTR] = TEXT_NUMBER_LINES_VALUE_MANY_STRING + } + + fun isLink(): Boolean { + return try { + mOptions[TEXT_LINK_ATTR]?.toBoolean() ?: TEXT_LINK_VALUE_DEFAULT + } catch (e: Exception) { + TEXT_LINK_VALUE_DEFAULT + } + } + + fun setLink(isLink: Boolean) { + mOptions[TEXT_LINK_ATTR] = isLink.toString() + } + + fun getListItems(): List { + return mOptions[LIST_ITEMS]?.split("|") ?: listOf() + } + + fun setListItems(items: List) { + mOptions[LIST_ITEMS] = items.joinToString("|") + } + + fun getDateFormat(): DateInstant.Type { + return when (mOptions[DATETIME_FORMAT_ATTR]) { + DATETIME_FORMAT_VALUE_DATE -> DateInstant.Type.DATE + DATETIME_FORMAT_VALUE_TIME -> DateInstant.Type.TIME + else -> DateInstant.Type.DATE_TIME + } + } + + fun setDateFormatToDate() { + mOptions[DATETIME_FORMAT_ATTR] = DATETIME_FORMAT_VALUE_DATE + } + + fun setDateFormatToTime() { + mOptions[DATETIME_FORMAT_ATTR] = DATETIME_FORMAT_VALUE_TIME + } + + fun get(label: String): String? { + return mOptions[label] + } + + fun add(label: String, value: String) { + mOptions[label] = value + } + + fun remove(label: String) { + mOptions.remove(label) + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): TemplateAttributeOption { + return TemplateAttributeOption(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } /** * Applicable to each type * Define a text replacement for a label, * Useful to keep compatibility with old keepass apps by replacing standard field label */ - const val ALIAS_ATTR = "alias" - const val ALIAS_VALUE_DEFAULT = "" + private const val ALIAS_ATTR = "alias" /** * 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 = "" + private const val DEFAULT_ATTR = "default" + private const val DEFAULT_VALUE = "" /** * Applicable to type TEXT - * Integer, can be "-1" or "many" to infinite value + * Integer, can be "many" or "-1" to infinite value * "1" if not defined */ - const val TEXT_NUMBER_CHARS_ATTR = "chars" - const val TEXT_NUMBER_CHARS_VALUE_DEFAULT = "many" + private const val TEXT_NUMBER_CHARS_ATTR = "chars" + private const val TEXT_NUMBER_CHARS_VALUE_MANY = -1 + private const val TEXT_NUMBER_CHARS_VALUE_MANY_STRING = "many" + private const val TEXT_NUMBER_CHARS_VALUE_DEFAULT = -1 /** * Applicable to type TEXT - * Integer, can be "many" to infinite value + * Integer, can be "-1" to infinite value * "1" if not defined */ - const val TEXT_NUMBER_LINES_ATTR = "lines" - const val TEXT_NUMBER_LINES_VALUE_MANY = "many" - const val TEXT_NUMBER_LINES_VALUE_DEFAULT = "1" + private const val TEXT_NUMBER_LINES_ATTR = "lines" + private const val TEXT_NUMBER_LINES_VALUE_MANY = -1 + private const val TEXT_NUMBER_LINES_VALUE_MANY_STRING = "many" + private const val TEXT_NUMBER_LINES_VALUE_DEFAULT = 1 /** * Applicable to type TEXT * Boolean ("true" or "false") * "true" if not defined - */ - const val TEXT_LINK_ATTR = "link" - const val TEXT_LINK_VALUE_DEFAULT = "false" + */ + private const val TEXT_LINK_ATTR = "link" + private const val TEXT_LINK_VALUE_DEFAULT = false /** * Applicable to type LIST * List of items, separator is '|' */ - const val LIST_ITEMS = "items" + private const val LIST_ITEMS = "items" /** * 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) * "datetime" if not defined */ - const val DATETIME_FORMAT_ATTR = "format" - const val DATETIME_FORMAT_VALUE_DATE = "date" - const val DATETIME_FORMAT_VALUE_TIME = "time" - const val DATETIME_FORMAT_VALUE_DEFAULT = "datetime" + private const val DATETIME_FORMAT_ATTR = "format" + private const val DATETIME_FORMAT_VALUE_DATE = "date" + private const val DATETIME_FORMAT_VALUE_TIME = "time" + private const val DATETIME_FORMAT_VALUE_DEFAULT = "datetime" - fun getAlias(options: Map): String? { - 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] - if (value == TEXT_NUMBER_LINES_VALUE_MANY) - -1 - else - options[TEXT_NUMBER_LINES_ATTR]?.toInt() ?: 1 - } catch (e: Exception) { - 1 - } - } - - fun isLinkify(options: Map): Boolean { - return try { - options[TEXT_LINK_ATTR]?.toBoolean() ?: true - } catch (e: Exception) { - true - } - } - - fun listItemsFromString(itemsString: String): List { - return itemsString.split("|") - } - - fun stringFromListItems(items: List): String { - return items.joinToString("|") - } - - fun getDateFormat(options: MutableMap): DateInstant.Type { - return when (options[DATETIME_FORMAT_ATTR]) { - DATETIME_FORMAT_VALUE_DATE -> DateInstant.Type.DATE - DATETIME_FORMAT_VALUE_TIME -> DateInstant.Type.TIME - else -> DateInstant.Type.DATE_TIME - } - } - - fun getOptionsFromString(label: String): MutableMap { - return if (label.contains("{") || label.contains("}")) { + fun getOptionsFromString(label: String): TemplateAttributeOption { + val options = TemplateAttributeOption() + val optionsMap = if (label.contains("{") || label.contains("}")) { try { label.trim().substringAfter("{").substringBefore("}") .split(",").associate { @@ -137,14 +216,19 @@ class TemplateAttributeOption { } else { mutableMapOf() } + options.mOptions.apply { + clear() + putAll(optionsMap) + } + return options } - fun getStringFromOptions(options: Map): String { + fun getStringFromOptions(options: TemplateAttributeOption): String { var optionsString = "" - if (options.isNotEmpty()) { + if (options.mOptions.isNotEmpty()) { optionsString += " {" var first = true - for ((key, value) in options) { + for ((key, value) in options.mOptions) { if (!first) optionsString += "," first = false diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt index a12aef8e7..7f1a0116b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateBuilder.kt @@ -13,8 +13,8 @@ class TemplateBuilder { TemplateField.LABEL_NOTES, TemplateAttributeType.TEXT, false, - LinkedHashMap().apply { - put(TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR, TemplateAttributeOption.TEXT_NUMBER_LINES_VALUE_MANY) + TemplateAttributeOption().apply { + setNumberLinesToMany() }) private val holderAttribute = TemplateAttribute(TemplateField.LABEL_HOLDER, TemplateAttributeType.TEXT) private val numberAttribute = TemplateAttribute(TemplateField.LABEL_NUMBER, TemplateAttributeType.TEXT) @@ -25,13 +25,16 @@ class TemplateBuilder { private val dateOfIssueAttribute = TemplateAttribute( TemplateField.LABEL_DATE_OF_ISSUE, TemplateAttributeType.DATETIME, - false) + false, + TemplateAttributeOption().apply { + setDateFormatToDate() + }) private val expirationDateAttribute = TemplateAttribute( TemplateField.LABEL_EXPIRATION, TemplateAttributeType.DATETIME, false, - LinkedHashMap().apply { - put(TemplateAttributeOption.DATETIME_FORMAT_ATTR, TemplateAttributeOption.DATETIME_FORMAT_VALUE_DATE) + TemplateAttributeOption().apply { + setDateFormatToDate() }) private val emailAddressAttribute = TemplateAttribute(TemplateField.LABEL_EMAIL_ADDRESS, TemplateAttributeType.TEXT) private val passwordAttribute = TemplateAttribute(TemplateField.LABEL_PASSWORD, TemplateAttributeType.TEXT, true) 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 fdfc3609f..6dccd86a5 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 @@ -1,6 +1,7 @@ package com.kunzisoft.keepass.database.element.template import android.util.Log +import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.Field import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.entry.EntryKDBX @@ -97,7 +98,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) val attribute = getOrRetrieveAttributeFromName(attributes, attributeName) // Here title is an alias if different (often the same) if (attributeName != value) { - attribute.attribute.options[TemplateAttributeOption.ALIAS_ATTR] = value + attribute.attribute.alias = value } entryCopy.removeField(field.name) } catch (e: Exception) { @@ -114,8 +115,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) when { value.contains(TEMPLATE_ATTRIBUTE_TYPE_INLINE_URL, true) -> { attribute.attribute.type = TemplateAttributeType.TEXT - attribute.attribute.options[TemplateAttributeOption.TEXT_LINK_ATTR] = - true.toString() + attribute.attribute.options.setLink(true) } value.contains(TEMPLATE_ATTRIBUTE_TYPE_INLINE, true) || value.contains(TEMPLATE_ATTRIBUTE_TYPE_POPOUT, true) -> { @@ -123,15 +123,12 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) } value.contains(TEMPLATE_ATTRIBUTE_TYPE_MULTILINE, true) -> { attribute.attribute.type = TemplateAttributeType.TEXT - attribute.attribute.options[TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR] = - TemplateAttributeOption.TEXT_NUMBER_LINES_VALUE_MANY + attribute.attribute.options.setNumberLinesToMany() } value.contains(TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX, true) -> { attribute.attribute.type = TemplateAttributeType.TEXT - attribute.attribute.options[TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR] = - TemplateAttributeOption.TEXT_NUMBER_LINES_VALUE_MANY - attribute.attribute.options[TemplateAttributeOption.TEXT_LINK_ATTR] = - true.toString() + attribute.attribute.options.setNumberLinesToMany() + attribute.attribute.options.setLink(true) } value.contains(TEMPLATE_ATTRIBUTE_TYPE_LISTBOX, true) -> { attribute.attribute.type = TemplateAttributeType.LIST @@ -141,13 +138,11 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) } value.contains(TEMPLATE_ATTRIBUTE_TYPE_DATE, true) -> { attribute.attribute.type = TemplateAttributeType.DATETIME - attribute.attribute.options[TemplateAttributeOption.DATETIME_FORMAT_ATTR] = - TemplateAttributeOption.DATETIME_FORMAT_VALUE_DATE + attribute.attribute.options.setDateFormatToDate() } value.contains(TEMPLATE_ATTRIBUTE_TYPE_TIME, true) -> { attribute.attribute.type = TemplateAttributeType.DATETIME - attribute.attribute.options[TemplateAttributeOption.DATETIME_FORMAT_ATTR] = - TemplateAttributeOption.DATETIME_FORMAT_VALUE_TIME + attribute.attribute.options.setDateFormatToTime() } value.contains(TEMPLATE_ATTRIBUTE_TYPE_DIVIDER, true) -> { attribute.attribute.type = TemplateAttributeType.DIVIDER @@ -163,7 +158,7 @@ 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_TEMP] = value + attribute.attribute.options.add(TEMPLATE_ATTRIBUTE_OPTIONS_TEMP, value) } entryCopy.removeField(field.name) } catch (e: Exception) { @@ -175,8 +170,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) if (attributes.containsKey(label)) { if (value.isNotEmpty()) { val attribute = attributes[label]!! - attribute.attribute.options[TemplateAttributeOption.DEFAULT_ATTR] = - value + attribute.attribute.default = value } } entryCopy.removeField(label) @@ -190,19 +184,19 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) val attribute = it.attribute // Recognize each temp option - attribute.options[TEMPLATE_ATTRIBUTE_OPTIONS_TEMP]?.let { defaultOption -> + attribute.options.get(TEMPLATE_ATTRIBUTE_OPTIONS_TEMP)?.let { defaultOption -> when (attribute.type) { TemplateAttributeType.TEXT -> { try { - val linesString = attribute.options[TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR] - if (linesString == null || linesString == "1") { - // If one line, default attribute option is number of chars - attribute.options[TemplateAttributeOption.TEXT_NUMBER_CHARS_ATTR] = - defaultOption - } else { - // else it's number of lines - attribute.options[TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR] = - defaultOption + when (attribute.options.getNumberLines()) { + 1 -> { + // If one line, default attribute option is number of chars + attribute.options.setNumberChars(defaultOption.toInt()) + } + else -> { + // else it's number of lines + attribute.options.setNumberLines(defaultOption.toInt()) + } } } catch (e: Exception) { Log.e(TAG, "Unable to transform default text option", e) @@ -212,8 +206,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) try { // Default attribute option is items of the list val items = defaultOption.split(",") - attribute.options[TemplateAttributeOption.LIST_ITEMS] = - TemplateAttributeOption.stringFromListItems(items) + attribute.options.setListItems(items) // TODO Add default item } catch (e: Exception) { Log.e(TAG, "Unable to transform default list option", e) @@ -227,12 +220,10 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) // Do not add option if it's datetime } defaultOption.equals(TEMPLATE_ATTRIBUTE_TYPE_DATE, true) -> { - attribute.options[TemplateAttributeOption.DATETIME_FORMAT_ATTR] = - TemplateAttributeOption.DATETIME_FORMAT_VALUE_DATE + attribute.options.setDateFormatToDate() } defaultOption.equals(TEMPLATE_ATTRIBUTE_TYPE_TIME, true) -> { - attribute.options[TemplateAttributeOption.DATETIME_FORMAT_ATTR] = - TemplateAttributeOption.DATETIME_FORMAT_VALUE_TIME + attribute.options.setDateFormatToTime() } } } catch (e: Exception) { @@ -288,7 +279,7 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) ProtectedString(false, index.toString()) ) // Add Title attribute (or alias if defined) - val title = options[TemplateAttributeOption.ALIAS_ATTR] ?: label + val title = options.alias ?: label entryCopy.putField( TEMPLATE_ATTRIBUTE_TITLE_PREFIX + label, ProtectedString(false, title) @@ -296,18 +287,18 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) // Add Type attribute var typeString: String = when { value.stringValue.contains(TemplateAttributeType.TEXT.typeString, true) -> { - when (options[TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR]) { - TemplateAttributeOption.TEXT_NUMBER_LINES_VALUE_MANY -> TEMPLATE_ATTRIBUTE_TYPE_MULTILINE - else -> TEMPLATE_ATTRIBUTE_TYPE_INLINE + when (options.getNumberLines()) { + 1 -> TEMPLATE_ATTRIBUTE_TYPE_INLINE + else -> TEMPLATE_ATTRIBUTE_TYPE_MULTILINE } } value.stringValue.contains(TemplateAttributeType.LIST.typeString, true) -> { TEMPLATE_ATTRIBUTE_TYPE_LISTBOX } value.stringValue.contains(TemplateAttributeType.DATETIME.typeString, true) -> { - when (options[TemplateAttributeOption.DATETIME_FORMAT_ATTR]) { - TemplateAttributeOption.DATETIME_FORMAT_VALUE_DATE -> TEMPLATE_ATTRIBUTE_TYPE_DATE - TemplateAttributeOption.DATETIME_FORMAT_VALUE_TIME -> TEMPLATE_ATTRIBUTE_TYPE_TIME + when (options.getDateFormat()) { + DateInstant.Type.DATE -> TEMPLATE_ATTRIBUTE_TYPE_DATE + DateInstant.Type.TIME -> TEMPLATE_ATTRIBUTE_TYPE_TIME else -> TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME } } @@ -327,24 +318,27 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) // Add Options attribute (here only number of chars and lines are supported) var defaultOption = "" try { - options[TemplateAttributeOption.TEXT_NUMBER_CHARS_ATTR]?.let { numberChars -> - defaultOption = if (numberChars.toInt() > 1) numberChars else defaultOption - } - options[TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR]?.let { numberLines -> - defaultOption = if (numberLines.toInt() > 1) numberLines else defaultOption + val numberChars = options.getNumberChars() + if (numberChars > 1) { + defaultOption = numberChars.toString() + } else { + val numberLines = options.getNumberLines() + if (numberLines > 1) { + defaultOption = numberLines.toString() + } } } catch (e: Exception) { - // Ignore, can be "many" + // Ignore, default option is defined } entryCopy.putField( TEMPLATE_ATTRIBUTE_OPTIONS_PREFIX + label, ProtectedString(false, defaultOption) ) // Add default elements - options[TemplateAttributeOption.DEFAULT_ATTR]?.let { defaultValue -> + if (options.default.isNotEmpty()) { entryCopy.putField( label, - ProtectedString(value.isProtected, defaultValue) + ProtectedString(value.isProtected, options.default) ) } index++ 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 27250438d..088fd77f8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt @@ -99,10 +99,7 @@ abstract class TemplateAbstractView().apply { - put(TemplateAttributeOption.TEXT_NUMBER_LINES_ATTR, "3") + TemplateAttributeOption().apply { + setNumberLines(3) }, - TemplateAttributeAction.CUSTOM_EDITION).apply { + 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 c2540dd0c..e34d43043 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt @@ -66,7 +66,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context, label = templateAttribute.alias ?: TemplateField.getLocalizedName(context, field.name) // TODO Max chars - setMaxLines(templateAttribute.getNumberLines()) + setMaxLines(templateAttribute.options.getNumberLines()) // TODO List items val fieldValue = field.protectedValue.stringValue value = if (fieldValue.isEmpty()) templateAttribute.default else fieldValue @@ -94,7 +94,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context, return context?.let { DateTimeEditView(it).apply { label = TemplateField.getLocalizedName(context, field.name) - val dateInstantType = TemplateAttributeOption.getDateFormat(templateAttribute.options) + val dateInstantType = templateAttribute.options.getDateFormat() try { val value = field.protectedValue.toString().trim() type = dateInstantType 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 f016e56a6..6a0c6c38b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateView.kt @@ -51,8 +51,8 @@ class TemplateView @JvmOverloads constructor(context: Context, applyFontVisibility(mFontInVisibility) setProtection(field.protectedValue.isProtected, mHideProtectedValue) label = templateAttribute.alias - ?: TemplateField.getLocalizedName(context, field.name) - setMaxLines(templateAttribute.getNumberLines()) + ?: TemplateField.getLocalizedName(context, field.name) + setMaxLines(templateAttribute.options.getNumberLines()) // TODO Linkify value = field.protectedValue.stringValue @@ -88,7 +88,7 @@ class TemplateView @JvmOverloads constructor(context: Context, return context?.let { DateTimeView(it).apply { label = TemplateField.getLocalizedName(context, field.name) - val dateInstantType = TemplateAttributeOption.getDateFormat(templateAttribute.options) + val dateInstantType = templateAttribute.options.getDateFormat() try { val value = field.protectedValue.toString().trim() type = dateInstantType