mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-12-04 15:52:34 -08:00
Refactor extra field
This commit is contained in:
1 parent
c2b14d610b
commit
f7391cb4c4
8 files changed
+198
-172
No files matched your search
@@ -46,6 +46,7 @@ import com.kunzisoft.keepass.database.element.icon.IconImageStandard
|
||||
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.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
|
||||
@@ -296,9 +297,9 @@ class EntryEditActivity : LockingActivity(),
|
||||
if (expires)
|
||||
expiresDate = newEntry.expiryTime
|
||||
notes = newEntry.notes
|
||||
for (entry in newEntry.customFields.entries) {
|
||||
for ((key, value) in newEntry.customFields) {
|
||||
post {
|
||||
putCustomField(entry.key, entry.value)
|
||||
putCustomField(Field(key, value))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -360,12 +361,14 @@ class EntryEditActivity : LockingActivity(),
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewCustomFieldApproved(label: String) {
|
||||
val customFieldView = entryEditContentsView?.putCustomField(label, ProtectedString())
|
||||
override fun onNewCustomFieldApproved(label: String, protection: Boolean) {
|
||||
val customFieldView = entryEditContentsView?.putCustomField(
|
||||
Field(label, ProtectedString(protection))
|
||||
)
|
||||
scrollToView(customFieldView)
|
||||
}
|
||||
|
||||
override fun onNewCustomFieldCanceled(label: String) {}
|
||||
override fun onNewCustomFieldCanceled(label: String, protection: Boolean) {}
|
||||
|
||||
private fun setupOTP() {
|
||||
// Retrieve the current otpElement if exists
|
||||
@@ -489,7 +492,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
// Update the otp field with otpauth:// url
|
||||
val otpField = OtpEntryFields.buildOtpField(otpElement,
|
||||
mEntry?.title, mEntry?.username)
|
||||
val otpCustomView = entryEditContentsView?.putCustomField(otpField.name, otpField.protectedValue)
|
||||
val otpCustomView = entryEditContentsView?.putCustomField(otpField)
|
||||
scrollToView(otpCustomView)
|
||||
mEntry?.putExtraField(otpField.name, otpField.protectedValue)
|
||||
}
|
||||
|
||||
+18
-13
@@ -25,6 +25,7 @@ import android.os.Bundle
|
||||
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.Button
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
@@ -37,8 +38,9 @@ class EntryCustomFieldDialogFragment: DialogFragment() {
|
||||
|
||||
private var entryCustomFieldListener: EntryCustomFieldListener? = null
|
||||
|
||||
private var newFieldLabelContainer: TextInputLayout? = null
|
||||
private var newFieldLabel: TextView? = null
|
||||
private var customFieldLabelContainer: TextInputLayout? = null
|
||||
private var customFieldLabel: TextView? = null
|
||||
private var customFieldProtectionButton: CompoundButton? = null
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
@@ -54,22 +56,24 @@ class EntryCustomFieldDialogFragment: DialogFragment() {
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
activity?.let { activity ->
|
||||
val root = activity.layoutInflater.inflate(R.layout.fragment_entry_new_field, null)
|
||||
newFieldLabelContainer = root?.findViewById(R.id.new_field_label_container)
|
||||
newFieldLabel = root?.findViewById(R.id.new_field_label)
|
||||
customFieldLabelContainer = root?.findViewById(R.id.entry_custom_field_label_container)
|
||||
customFieldLabel = root?.findViewById(R.id.entry_custom_field_label)
|
||||
customFieldProtectionButton = root?.findViewById(R.id.entry_custom_field_protection)
|
||||
|
||||
val builder = AlertDialog.Builder(activity)
|
||||
builder.setView(root)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.setNegativeButton(android.R.string.cancel) { _, _ ->
|
||||
entryCustomFieldListener?.onNewCustomFieldCanceled(
|
||||
newFieldLabel?.text.toString()
|
||||
customFieldLabel?.text.toString(),
|
||||
customFieldProtectionButton?.isChecked == true
|
||||
)
|
||||
}
|
||||
val dialogCreated = builder.create()
|
||||
|
||||
newFieldLabel?.requestFocus()
|
||||
newFieldLabel?.imeOptions = EditorInfo.IME_ACTION_DONE
|
||||
newFieldLabel?.setOnEditorActionListener { _, actionId, _ ->
|
||||
customFieldLabel?.requestFocus()
|
||||
customFieldLabel?.imeOptions = EditorInfo.IME_ACTION_DONE
|
||||
customFieldLabel?.setOnEditorActionListener { _, actionId, _ ->
|
||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||
approveIfValid()
|
||||
}
|
||||
@@ -99,14 +103,15 @@ class EntryCustomFieldDialogFragment: DialogFragment() {
|
||||
private fun approveIfValid() {
|
||||
if (isValid()) {
|
||||
entryCustomFieldListener?.onNewCustomFieldApproved(
|
||||
newFieldLabel?.text.toString()
|
||||
customFieldLabel?.text.toString(),
|
||||
customFieldProtectionButton?.isChecked == true
|
||||
)
|
||||
(dialog as AlertDialog?)?.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValid(): Boolean {
|
||||
return if (newFieldLabel?.text?.toString()?.isNotEmpty() != true) {
|
||||
return if (customFieldLabel?.text?.toString()?.isNotEmpty() != true) {
|
||||
setError(R.string.error_string_key)
|
||||
false
|
||||
} else {
|
||||
@@ -116,14 +121,14 @@ class EntryCustomFieldDialogFragment: DialogFragment() {
|
||||
}
|
||||
|
||||
fun setError(@StringRes errorId: Int?) {
|
||||
newFieldLabelContainer?.error = if (errorId == null) null else {
|
||||
customFieldLabelContainer?.error = if (errorId == null) null else {
|
||||
requireContext().getString(errorId)
|
||||
}
|
||||
}
|
||||
|
||||
interface EntryCustomFieldListener {
|
||||
fun onNewCustomFieldApproved(label: String)
|
||||
fun onNewCustomFieldCanceled(label: String)
|
||||
fun onNewCustomFieldApproved(label: String, protection: Boolean)
|
||||
fun onNewCustomFieldCanceled(label: String, protection: Boolean)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -31,7 +31,6 @@ import com.google.android.material.textfield.TextInputLayout
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImage
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.icons.IconDrawableFactory
|
||||
import com.kunzisoft.keepass.icons.assignDatabaseIcon
|
||||
import com.kunzisoft.keepass.icons.assignDefaultDatabaseIcon
|
||||
@@ -202,11 +201,8 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
entryExtraFieldsContainer.let {
|
||||
try {
|
||||
for (i in 0 until it.childCount) {
|
||||
val view = it.getChildAt(i) as EntryEditCustomField
|
||||
val key = view.label
|
||||
val value = view.value
|
||||
val protect = view.isProtected
|
||||
customFieldsArray.add(Field(key, ProtectedString(protect, value)))
|
||||
val view = it.getChildAt(i) as EntryEditExtraField
|
||||
customFieldsArray.add(view.customField)
|
||||
}
|
||||
} catch (exception: Exception) {
|
||||
// Extra field container contains another type of view
|
||||
@@ -215,11 +211,11 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
return customFieldsArray
|
||||
}
|
||||
|
||||
private fun getCustomFieldByLabel(label: String): EntryEditCustomField? {
|
||||
private fun getCustomFieldByLabel(label: String): EntryEditExtraField? {
|
||||
for (i in 0..entryExtraFieldsContainer.childCount) {
|
||||
try {
|
||||
val extraFieldView = entryExtraFieldsContainer.getChildAt(i) as EntryEditCustomField?
|
||||
if (extraFieldView?.label == label) {
|
||||
val extraFieldView = entryExtraFieldsContainer.getChildAt(i) as EntryEditExtraField?
|
||||
if (extraFieldView?.customField?.name == label) {
|
||||
return extraFieldView
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
@@ -232,15 +228,13 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
/**
|
||||
* Update a custom field or create a new one if doesn't exists
|
||||
*/
|
||||
fun putCustomField(name: String,
|
||||
value: ProtectedString = ProtectedString())
|
||||
: EntryEditCustomField {
|
||||
var extraFieldView = getCustomFieldByLabel(name)
|
||||
extraFieldView?.setData(name, value, fontInVisibility)
|
||||
fun putCustomField(customField: Field)
|
||||
: EntryEditExtraField {
|
||||
var extraFieldView = getCustomFieldByLabel(customField.name)
|
||||
// Create new view if not exists
|
||||
if (extraFieldView == null) {
|
||||
extraFieldView = EntryEditCustomField(context).apply {
|
||||
setData(name, value, fontInVisibility)
|
||||
extraFieldView = EntryEditExtraField(context).apply {
|
||||
setFontVisibility(fontInVisibility)
|
||||
setDeleteButtonClickListener(OnClickListener {
|
||||
try {
|
||||
collapse(true) {
|
||||
@@ -257,6 +251,7 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
// No need animation because of scroll
|
||||
entryExtraFieldsContainer.addView(extraFieldView)
|
||||
}
|
||||
extraFieldView.customField = customField
|
||||
return extraFieldView
|
||||
}
|
||||
|
||||
@@ -271,12 +266,12 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
try {
|
||||
val customFieldLabelSet = HashSet<String>()
|
||||
for (i in 0 until it.childCount) {
|
||||
val entryEditCustomField = it.getChildAt(i) as EntryEditCustomField
|
||||
if (customFieldLabelSet.contains(entryEditCustomField.label)) {
|
||||
val entryEditCustomField = it.getChildAt(i) as EntryEditExtraField
|
||||
if (customFieldLabelSet.contains(entryEditCustomField.customField.name)) {
|
||||
entryEditCustomField.setError(R.string.error_label_exists)
|
||||
return false
|
||||
}
|
||||
customFieldLabelSet.add(entryEditCustomField.label)
|
||||
customFieldLabelSet.add(entryEditCustomField.customField.name)
|
||||
if (!entryEditCustomField.isValid()) {
|
||||
return false
|
||||
}
|
||||
|
||||
+34
-28
@@ -24,57 +24,62 @@ import android.graphics.Rect
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.CompoundButton
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.EditText
|
||||
import android.widget.RelativeLayout
|
||||
import androidx.annotation.StringRes
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
|
||||
class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyle: Int = 0)
|
||||
class EntryEditExtraField @JvmOverloads constructor(context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyle: Int = 0)
|
||||
: RelativeLayout(context, attrs, defStyle) {
|
||||
|
||||
private val valueLayoutView: TextInputLayout
|
||||
private val valueView: EditText
|
||||
private val protectionCheckView: CompoundButton
|
||||
private val editButton: View
|
||||
private val deleteButton: View
|
||||
|
||||
val label: String
|
||||
get() = valueLayoutView.hint.toString()
|
||||
private var mApplyFontVisibility = false
|
||||
private var isProtected = false
|
||||
private var mValueViewInputType: Int = 0
|
||||
|
||||
val value: String
|
||||
get() = valueView.text.toString()
|
||||
|
||||
val isProtected: Boolean
|
||||
get() = protectionCheckView.isChecked
|
||||
var customField: Field
|
||||
get() {
|
||||
return Field(valueLayoutView.hint.toString(), ProtectedString(isProtected, valueView.text.toString()))
|
||||
}
|
||||
set(value) {
|
||||
valueLayoutView.hint = value.name
|
||||
isProtected = value.protectedValue.isProtected
|
||||
if (isProtected) {
|
||||
valueLayoutView.isPasswordVisibilityToggleEnabled = true
|
||||
valueView.inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD or mValueViewInputType
|
||||
} else {
|
||||
valueLayoutView.isPasswordVisibilityToggleEnabled = false
|
||||
valueView.inputType = mValueViewInputType
|
||||
}
|
||||
valueView.setText(value.protectedValue.toString())
|
||||
}
|
||||
|
||||
init {
|
||||
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
|
||||
inflater?.inflate(R.layout.view_entry_custom_field, this)
|
||||
inflater?.inflate(R.layout.view_entry_edit_extra_field, this)
|
||||
|
||||
valueLayoutView = findViewById(R.id.entry_custom_field_value_container)
|
||||
valueView = findViewById(R.id.entry_custom_field_value)
|
||||
protectionCheckView = findViewById(R.id.entry_custom_field_protection)
|
||||
deleteButton = findViewById<View>(R.id.entry_custom_field_delete)
|
||||
valueLayoutView = findViewById(R.id.entry_extra_field_value_container)
|
||||
valueView = findViewById(R.id.entry_extra_field_value)
|
||||
editButton = findViewById(R.id.entry_extra_field_edit)
|
||||
deleteButton = findViewById(R.id.entry_extra_field_delete)
|
||||
|
||||
mValueViewInputType = valueView.inputType
|
||||
}
|
||||
|
||||
fun setDeleteButtonClickListener(listener: OnClickListener?) {
|
||||
deleteButton.setOnClickListener(listener)
|
||||
}
|
||||
|
||||
fun setData(label: String?, value: ProtectedString?, fontInVisibility: Boolean) {
|
||||
if (label != null)
|
||||
valueLayoutView.hint = label
|
||||
if (value != null) {
|
||||
valueView.setText(value.toString())
|
||||
protectionCheckView.isChecked = value.isProtected
|
||||
}
|
||||
setFontVisibility(fontInVisibility)
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate or not the entry form
|
||||
*
|
||||
@@ -82,7 +87,7 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
*/
|
||||
fun isValid(): Boolean {
|
||||
// Validate extra field
|
||||
if (label.isEmpty()) {
|
||||
if (valueLayoutView.hint.toString().isEmpty()) {
|
||||
setError(R.string.error_string_key)
|
||||
return false
|
||||
} else {
|
||||
@@ -98,6 +103,7 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
}
|
||||
|
||||
fun setFontVisibility(applyFontVisibility: Boolean) {
|
||||
mApplyFontVisibility = applyFontVisibility
|
||||
if (applyFontVisibility)
|
||||
valueView.applyFontVisibility()
|
||||
}
|
||||
Reference in new issue
Block a user