mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-30 13:52:58 -08:00
New UI for custom field
This commit is contained in:
1 parent
9679d24414
commit
1087dcd714
8 files changed
+281
-94
No files matched your search
@@ -45,6 +45,7 @@ import com.kunzisoft.keepass.database.element.Group
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImage
|
||||
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.notifications.ClipboardEntryNotificationService
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_ENTRY_TASK
|
||||
@@ -62,6 +63,7 @@ import java.util.*
|
||||
|
||||
class EntryEditActivity : LockingActivity(),
|
||||
IconPickerDialogFragment.IconPickerListener,
|
||||
EntryCustomFieldDialogFragment.EntryCustomFieldListener,
|
||||
GeneratePasswordDialogFragment.GeneratePasswordListener,
|
||||
SetOTPDialogFragment.CreateOtpListener,
|
||||
DatePickerDialog.OnDateSetListener,
|
||||
@@ -201,7 +203,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
entryEditContentsView?.setOnIconViewClickListener { IconPickerDialogFragment.launch(this@EntryEditActivity) }
|
||||
|
||||
// Bottom Bar
|
||||
entryEditAddToolBar = findViewById(R.id.entry_edit_bottom_bar)
|
||||
entryEditAddToolBar = findViewById(R.id.entry_edit_bottom_menu_bar)
|
||||
entryEditAddToolBar?.apply {
|
||||
menuInflater.inflate(R.menu.entry_edit, menu)
|
||||
|
||||
@@ -335,12 +337,18 @@ class EntryEditActivity : LockingActivity(),
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new customized field view and scroll to bottom
|
||||
* Add a new customized field
|
||||
*/
|
||||
private fun addNewCustomField() {
|
||||
entryEditContentsView?.addEmptyCustomField()
|
||||
EntryCustomFieldDialogFragment.getInstance().show(supportFragmentManager, "customFieldDialog")
|
||||
}
|
||||
|
||||
override fun onNewCustomFieldApproved(label: String, name: ProtectedString) {
|
||||
entryEditContentsView?.putCustomField(label, name, true)
|
||||
}
|
||||
|
||||
override fun onNewCustomFieldCanceled(label: String, name: ProtectedString) {}
|
||||
|
||||
private fun setupOTP() {
|
||||
// Retrieve the current otpElement if exists
|
||||
// and open the dialog to set up the OTP
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.activities.dialogs
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
|
||||
class EntryCustomFieldDialogFragment: DialogFragment() {
|
||||
|
||||
private var entryCustomFieldListener: EntryCustomFieldListener? = null
|
||||
|
||||
private var newFieldLabel: TextView? = null
|
||||
private var newFieldValue: TextView? = null
|
||||
private var newFieldProtection: CompoundButton? = null
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
try {
|
||||
entryCustomFieldListener = context as EntryCustomFieldListener
|
||||
} catch (e: ClassCastException) {
|
||||
// The activity doesn't implement the interface, throw exception
|
||||
throw ClassCastException(context.toString()
|
||||
+ " must implement " + EntryCustomFieldListener::class.java.name)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
activity?.let { activity ->
|
||||
val root = activity.layoutInflater.inflate(R.layout.fragment_entry_new_field, null)
|
||||
newFieldLabel = root?.findViewById(R.id.entry_new_field_label)
|
||||
newFieldValue = root?.findViewById(R.id.entry_new_field_value)
|
||||
newFieldProtection = root?.findViewById(R.id.entry_new_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(),
|
||||
ProtectedString(
|
||||
newFieldProtection?.isChecked == true,
|
||||
newFieldValue?.text.toString()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return builder.create()
|
||||
}
|
||||
return super.onCreateDialog(savedInstanceState)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
// To prevent auto dismiss
|
||||
val d = dialog as AlertDialog?
|
||||
if (d != null) {
|
||||
val positiveButton = d.getButton(Dialog.BUTTON_POSITIVE) as Button
|
||||
positiveButton.setOnClickListener {
|
||||
if (isValid()) {
|
||||
entryCustomFieldListener?.onNewCustomFieldApproved(
|
||||
newFieldLabel?.text.toString(),
|
||||
ProtectedString(
|
||||
newFieldProtection?.isChecked == true,
|
||||
newFieldValue?.text.toString()
|
||||
)
|
||||
)
|
||||
d.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValid(): Boolean {
|
||||
return if (newFieldLabel?.text?.toString()?.isNotEmpty() != true) {
|
||||
setError(R.string.error_string_key)
|
||||
false
|
||||
} else {
|
||||
setError(null)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fun setError(@StringRes errorId: Int?) {
|
||||
newFieldLabel?.error = if (errorId == null) null else {
|
||||
requireContext().getString(errorId)
|
||||
}
|
||||
}
|
||||
|
||||
interface EntryCustomFieldListener {
|
||||
fun onNewCustomFieldApproved(label: String, name: ProtectedString)
|
||||
fun onNewCustomFieldCanceled(label: String, name: ProtectedString)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(): EntryCustomFieldDialogFragment {
|
||||
return EntryCustomFieldDialogFragment()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -215,25 +215,12 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
return customFieldsArray
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new view to fill in the information of the customized field and focus it
|
||||
*/
|
||||
fun addEmptyCustomField() {
|
||||
// Fix current custom field before adding a new one
|
||||
if (isValid()) {
|
||||
val entryEditCustomField = EntryEditCustomField(context).apply {
|
||||
setFontVisibility(fontInVisibility)
|
||||
requestFocus()
|
||||
}
|
||||
entryExtraFieldsContainer.addView(entryEditCustomField)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a custom field or create a new one if doesn't exists
|
||||
*/
|
||||
fun putCustomField(name: String,
|
||||
value: ProtectedString = ProtectedString()) {
|
||||
value: ProtectedString = ProtectedString(),
|
||||
requestFocus: Boolean = false) {
|
||||
var updateField = false
|
||||
for (i in 0..entryExtraFieldsContainer.childCount) {
|
||||
try {
|
||||
@@ -252,6 +239,8 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
setData(name, value, fontInVisibility)
|
||||
}
|
||||
entryExtraFieldsContainer.addView(entryEditCustomField)
|
||||
if (requestFocus)
|
||||
entryEditCustomField.requestFocus()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
package com.kunzisoft.keepass.view
|
||||
|
||||
import android.content.Context
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import android.util.AttributeSet
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
@@ -29,8 +28,8 @@ import android.view.ViewGroup
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.EditText
|
||||
import android.widget.RelativeLayout
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.StringRes
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
|
||||
@@ -39,13 +38,12 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
defStyle: Int = 0)
|
||||
: RelativeLayout(context, attrs, defStyle) {
|
||||
|
||||
private val labelLayoutView: TextInputLayout
|
||||
private val labelView: TextView
|
||||
private val valueLayoutView: TextInputLayout
|
||||
private val valueView: EditText
|
||||
private val protectionCheckView: CompoundButton
|
||||
|
||||
val label: String
|
||||
get() = labelView.text.toString()
|
||||
get() = valueLayoutView.hint.toString()
|
||||
|
||||
val value: String
|
||||
get() = valueView.text.toString()
|
||||
@@ -56,20 +54,19 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
init {
|
||||
|
||||
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
|
||||
inflater?.inflate(R.layout.view_entry_new_field, this)
|
||||
inflater?.inflate(R.layout.view_entry_custom_field, this)
|
||||
|
||||
val deleteView = findViewById<View>(R.id.entry_new_field_delete)
|
||||
deleteView.setOnClickListener { deleteViewFromParent() }
|
||||
|
||||
labelLayoutView = findViewById(R.id.title_container)
|
||||
labelView = findViewById(R.id.entry_new_field_label)
|
||||
valueLayoutView = findViewById(R.id.value_container)
|
||||
valueView = findViewById(R.id.entry_new_field_value)
|
||||
protectionCheckView = findViewById(R.id.protection)
|
||||
}
|
||||
|
||||
fun setData(label: String?, value: ProtectedString?, fontInVisibility: Boolean) {
|
||||
if (label != null)
|
||||
labelView.text = label
|
||||
valueLayoutView.hint = label
|
||||
if (value != null) {
|
||||
valueView.setText(value.toString())
|
||||
protectionCheckView.isChecked = value.isProtected
|
||||
@@ -94,7 +91,7 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
|
||||
}
|
||||
|
||||
fun setError(@StringRes errorId: Int?) {
|
||||
labelLayoutView.error = if (errorId == null) null else {
|
||||
valueLayoutView.error = if (errorId == null) null else {
|
||||
context.getString(errorId)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user