Add default option

This commit is contained in:
J-Jamet committed 2021-07-07 19:26:32 +02:00
1 parent 6eb80eea2f
commit a99ca00bb3
6 files changed
+85 -31

No files matched your search

@@ -28,16 +28,14 @@ data class TemplateAttribute(var label: String,
var type: TemplateAttributeType,
var protected: Boolean = false,
var options: MutableMap<String, String> = 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>() ?: TemplateAttributeType.TEXT,
parcel.readByte() != 0.toByte(),
ParcelableUtil.readStringParcelableMap(parcel),
parcel.readEnum<TemplateAttributeAction>() ?: TemplateAttributeAction.NONE,
parcel.readString() ?: "")
parcel.readEnum<TemplateAttributeAction>() ?: 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<TemplateAttribute> {
override fun createFromParcel(parcel: Parcel): TemplateAttribute {
return TemplateAttribute(parcel)
@@ -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<String, String>) {
if (value == null)
options.remove(ALIAS_ATTR)
else
options[ALIAS_ATTR] = value
}
fun getDefault(options: Map<String, String>): String? {
return options[DEFAULT_ATTR]
}
fun setDefault(value: String?, options: MutableMap<String, String>) {
if (value == null)
options.remove(DEFAULT_ATTR)
else
options[DEFAULT_ATTR] = value
}
fun getNumberLines(options: Map<String, String>): Int {
return try {
val value = options[TEXT_NUMBER_LINES_ATTR]
@@ -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"
@@ -102,8 +102,9 @@ abstract class TemplateAbstractView<TEntryFieldView: GenericEntryFieldView, TDat
TemplateAttributeType.TEXT,
false,
LinkedHashMap(),
TemplateAttributeAction.NONE,
template.title)
TemplateAttributeAction.NONE).apply {
default = template.title
}
val titleView = buildViewForTemplateField(
titleAttribute,
Field(
@@ -189,8 +190,9 @@ abstract class TemplateAbstractView<TEntryFieldView: GenericEntryFieldView, TDat
LinkedHashMap<String, String>().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)
}
@@ -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)
@@ -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