From 471859e4484e6288f79046a5216ea52ffdcd3784 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 13 Jan 2022 18:08:20 +0100 Subject: [PATCH] Add color in entry edit --- .../keepass/view/TemplateAbstractView.kt | 4 ++ .../keepass/view/TemplateEditView.kt | 46 ++++++++++++------- app/src/main/res/layout/view_template.xml | 39 +++++++++++----- 3 files changed, 62 insertions(+), 27 deletions(-) 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 c8271d7d3..f5c4305f7 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt @@ -46,6 +46,8 @@ abstract class TemplateAbstractView< protected var headerContainerView: ViewGroup protected var entryIconView: ImageView + protected var backgroundColorView: View + protected var foregroundColorView: View protected var backgroundColorButton: ImageView protected var foregroundColorButton: ImageView private var titleContainerView: ViewGroup @@ -59,6 +61,8 @@ abstract class TemplateAbstractView< headerContainerView = findViewById(R.id.template_header_container) entryIconView = findViewById(R.id.template_icon_button) + backgroundColorView = findViewById(R.id.template_background_color) + foregroundColorView = findViewById(R.id.template_foreground_color) backgroundColorButton = findViewById(R.id.template_background_color_button) foregroundColorButton = findViewById(R.id.template_foreground_color_button) titleContainerView = findViewById(R.id.template_title_container) 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 7d5c76a06..bc42ae0e3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt @@ -1,19 +1,21 @@ package com.kunzisoft.keepass.view import android.content.Context -import android.graphics.PorterDuff -import android.graphics.PorterDuffColorFilter import android.os.Build import android.util.AttributeSet import android.view.View import androidx.annotation.IdRes +import androidx.core.graphics.BlendModeColorFilterCompat +import androidx.core.graphics.BlendModeCompat import androidx.core.view.isVisible import com.kunzisoft.keepass.R import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.Field import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.database.element.security.ProtectedString -import com.kunzisoft.keepass.database.element.template.* +import com.kunzisoft.keepass.database.element.template.TemplateAttribute +import com.kunzisoft.keepass.database.element.template.TemplateAttributeAction +import com.kunzisoft.keepass.database.element.template.TemplateField import com.kunzisoft.keepass.otp.OtpEntryFields import org.joda.time.DateTime @@ -53,7 +55,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context, fun setIcon(iconImage: IconImage) { mEntryInfo?.icon = iconImage - populateIconMethod?.invoke(entryIconView, iconImage) + refreshIcon() } fun setOnBackgroundColorClickListener(onClickListener: OnClickListener) { @@ -65,12 +67,20 @@ class TemplateEditView @JvmOverloads constructor(context: Context, } fun setBackgroundColor(color: Int?) { - color?.let { - backgroundColorButton.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP) - } + applyBackgroundColor(color) mEntryInfo?.backgroundColor = color } + private fun applyBackgroundColor(color: Int?) { + if (color != null) { + backgroundColorView.background.colorFilter = BlendModeColorFilterCompat + .createBlendModeColorFilterCompat(color, BlendModeCompat.SRC_ATOP) + backgroundColorView.visibility = View.VISIBLE + } else { + backgroundColorView.visibility = View.GONE + } + } + fun setOnForegroundColorClickListener(onClickListener: OnClickListener) { foregroundColorButton.setOnClickListener(onClickListener) } @@ -80,12 +90,20 @@ class TemplateEditView @JvmOverloads constructor(context: Context, } fun setForegroundColor(color: Int?) { - color?.let { - foregroundColorButton.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP) - } + applyForegroundColor(color) mEntryInfo?.foregroundColor = color } + private fun applyForegroundColor(color: Int?) { + if (color != null) { + foregroundColorView.background.colorFilter = BlendModeColorFilterCompat + .createBlendModeColorFilterCompat(color, BlendModeCompat.SRC_ATOP) + foregroundColorView.visibility = View.VISIBLE + } else { + foregroundColorView.visibility = View.GONE + } + } + override fun preProcessTemplate() { headerContainerView.isVisible = true } @@ -228,12 +246,8 @@ class TemplateEditView @JvmOverloads constructor(context: Context, override fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List { refreshIcon() - mEntryInfo?.backgroundColor?.let { - backgroundColorButton.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP) - } - mEntryInfo?.foregroundColor?.let { - foregroundColorButton.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP) - } + applyBackgroundColor(mEntryInfo?.backgroundColor) + applyForegroundColor(mEntryInfo?.foregroundColor) return super.populateViewsWithEntryInfo(showEmptyFields) } diff --git a/app/src/main/res/layout/view_template.xml b/app/src/main/res/layout/view_template.xml index d07d12258..61d9c01db 100644 --- a/app/src/main/res/layout/view_template.xml +++ b/app/src/main/res/layout/view_template.xml @@ -30,28 +30,44 @@ android:layout_width="match_parent" android:layout_height="wrap_content" style="?attr/cardViewStyle"> + - + + + +