From 1087dcd714164e47e18ace1b136ef0331c065c4d Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Mon, 3 Aug 2020 18:45:50 +0200 Subject: [PATCH] New UI for custom field --- .../keepass/activities/EntryEditActivity.kt | 14 +- .../dialogs/EntryCustomFieldDialogFragment.kt | 126 ++++++++++++++++++ .../keepass/view/EntryEditContentsView.kt | 19 +-- .../keepass/view/EntryEditCustomField.kt | 17 +-- .../main/res/layout/activity_entry_edit.xml | 46 +++---- .../res/layout/fragment_entry_new_field.xml | 80 +++++++++++ ..._field.xml => view_entry_custom_field.xml} | 68 ++++------ app/src/main/res/menu/entry_edit.xml | 5 + 8 files changed, 281 insertions(+), 94 deletions(-) create mode 100644 app/src/main/java/com/kunzisoft/keepass/activities/dialogs/EntryCustomFieldDialogFragment.kt create mode 100644 app/src/main/res/layout/fragment_entry_new_field.xml rename app/src/main/res/layout/{view_entry_new_field.xml => view_entry_custom_field.xml} (65%) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index 06157706c..73d68c8b1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -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 diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/EntryCustomFieldDialogFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/EntryCustomFieldDialogFragment.kt new file mode 100644 index 000000000..3bd12a55d --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/EntryCustomFieldDialogFragment.kt @@ -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 . + * + */ +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() + } + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt index 16d57411f..65a6186fa 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt @@ -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() } } diff --git a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditCustomField.kt b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditCustomField.kt index b9e9a1bc6..63a4648b8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditCustomField.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditCustomField.kt @@ -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(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) } } diff --git a/app/src/main/res/layout/activity_entry_edit.xml b/app/src/main/res/layout/activity_entry_edit.xml index ae6d82ea6..25e1df0c4 100644 --- a/app/src/main/res/layout/activity_entry_edit.xml +++ b/app/src/main/res/layout/activity_entry_edit.xml @@ -43,28 +43,6 @@ app:toolbarId="@+id/toolbar" app:layout_scrollFlags="enterAlways|enterAlwaysCollapsed|scroll|exitUntilCollapsed|snap"> - - - - - - - + android:layout_height="wrap_content" + android:paddingBottom="@dimen/default_margin"> + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/view_entry_new_field.xml b/app/src/main/res/layout/view_entry_custom_field.xml similarity index 65% rename from app/src/main/res/layout/view_entry_new_field.xml rename to app/src/main/res/layout/view_entry_custom_field.xml index 8079417cd..99b1c7a2b 100644 --- a/app/src/main/res/layout/view_entry_new_field.xml +++ b/app/src/main/res/layout/view_entry_custom_field.xml @@ -18,11 +18,11 @@ along with KeePassDX. If not, see . --> - - - - - - - - - + android:paddingEnd="@dimen/default_margin"> - + + + \ No newline at end of file diff --git a/app/src/main/res/menu/entry_edit.xml b/app/src/main/res/menu/entry_edit.xml index 4935468c9..84fd3c5f9 100644 --- a/app/src/main/res/menu/entry_edit.xml +++ b/app/src/main/res/menu/entry_edit.xml @@ -21,6 +21,11 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" tools:ignore="AlwaysShowAction"> +