From f862ecb44538fa4cb862ae80cea7ed3dda200b1c Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 24 May 2026 12:55:06 +0200 Subject: [PATCH] fix: Loader state and color customization #2541 --- .../keepass/activities/EntryActivity.kt | 106 ++++++------------ .../keepass/viewmodels/EntryViewModel.kt | 66 ++++++++++- 2 files changed, 97 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt index 2ed378480..4608e171d 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -20,7 +20,6 @@ package com.kunzisoft.keepass.activities import android.app.Activity import android.content.Intent -import android.graphics.Color import android.net.Uri import android.os.Bundle import android.os.Handler @@ -37,10 +36,8 @@ import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.graphics.BlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat -import androidx.core.graphics.ColorUtils import androidx.core.graphics.drawable.toDrawable import androidx.core.view.ViewCompat -import androidx.core.view.isVisible import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle @@ -62,11 +59,10 @@ import com.kunzisoft.keepass.credentialprovider.UserVerificationData import com.kunzisoft.keepass.credentialprovider.UserVerificationHelper.Companion.checkUserVerification import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.element.Attachment -import com.kunzisoft.keepass.database.element.icon.IconImage +import com.kunzisoft.keepass.database.element.Tags import com.kunzisoft.keepass.database.element.node.NodeId import com.kunzisoft.keepass.education.EntryActivityEducation import com.kunzisoft.keepass.model.EntryAttachmentState -import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.otp.OtpType import com.kunzisoft.keepass.services.AttachmentFileNotificationService import com.kunzisoft.keepass.services.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_DELETE_ENTRY_HISTORY @@ -83,6 +79,7 @@ import com.kunzisoft.keepass.view.changeTitleColor import com.kunzisoft.keepass.view.hideByFading import com.kunzisoft.keepass.view.setTransparentNavigationBar import com.kunzisoft.keepass.view.showActionErrorIfNeeded +import com.kunzisoft.keepass.view.showByFading import com.kunzisoft.keepass.view.showError import com.kunzisoft.keepass.viewmodels.EntryViewModel import com.kunzisoft.keepass.viewmodels.UserVerificationViewModel @@ -121,14 +118,6 @@ class EntryActivity : DatabaseLockActivity() { mEntryViewModel.loadDatabase(mDatabase) } - private var mIcon: IconImage? = null - private var mColorSecondary: Int = 0 - private var mColorSurface: Int = 0 - private var mColorOnSurface: Int = 0 - private var mColorBackground: Int = 0 - private var mBackgroundColor: Int? = null - private var mForegroundColor: Int? = null - override fun manageDatabaseInfo(): Boolean = true override fun onCreate(savedInstanceState: Bundle?) { @@ -171,19 +160,8 @@ class EntryActivity : DatabaseLockActivity() { collapsingToolbarLayout?.title = " " toolbar?.title = " " - // Retrieve the textColor to tint the toolbar - val taColorSecondary = theme.obtainStyledAttributes(intArrayOf(R.attr.colorSecondary)) - val taColorSurface = theme.obtainStyledAttributes(intArrayOf(R.attr.colorSurface)) - val taColorOnSurface = theme.obtainStyledAttributes(intArrayOf(R.attr.colorOnSurface)) - val taColorBackground = theme.obtainStyledAttributes(intArrayOf(android.R.attr.windowBackground)) - mColorSecondary = taColorSecondary.getColor(0, Color.BLACK) - mColorSurface = taColorSurface.getColor(0, Color.BLACK) - mColorOnSurface = taColorOnSurface.getColor(0, Color.BLACK) - mColorBackground = taColorBackground.getColor(0, Color.BLACK) - taColorSecondary.recycle() - taColorSurface.recycle() - taColorOnSurface.recycle() - taColorBackground.recycle() + // Set the theme to retrieve the toolbar color + mEntryViewModel.setTheme(theme) // Init Tags adapter tagsAdapter = TagsAdapter(this) @@ -241,36 +219,43 @@ class EntryActivity : DatabaseLockActivity() { requestEdition() } - fun setEntryInfoViews(entryInfo: EntryInfo) { - // Assign title icon - mIcon = entryInfo.icon - // Assign title text - val entryTitle = entryInfo.title.ifEmpty { entryInfo.nodeId.toString() } - collapsingToolbarLayout?.title = entryTitle - toolbar?.title = entryTitle - // Assign tags - val tags = entryInfo.tags - tagsListView?.visibility = if (tags.isEmpty()) View.GONE else View.VISIBLE - tagsAdapter?.setTags(tags) - // Assign colors - val showEntryColors = PreferencesUtil.showEntryColors(this) - mBackgroundColor = if (showEntryColors) entryInfo.backgroundColor else null - mForegroundColor = if (showEntryColors) entryInfo.foregroundColor else null - } - lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { launch { mEntryViewModel.entryUIState.collect { entryState -> // Define Loading if (entryState.loaded) - loadingView?.isVisible = true - else loadingView?.hideByFading() - // Assign Entry views - entryState.entryInfo?.let { entryInfo -> - setEntryInfoViews(entryInfo) + else + loadingView?.showByFading() + val entryInfo = entryState.entryInfo + // Assign title text + val entryTitle = entryInfo?.title?.ifEmpty { entryInfo.nodeId.toString() } ?: "" + collapsingToolbarLayout?.title = entryTitle + toolbar?.title = entryTitle + // Assign tags + val tags = entryInfo?.tags ?: Tags() + tagsListView?.visibility = if (tags.isEmpty()) View.GONE else View.VISIBLE + tagsAdapter?.setTags(tags) + // Icon + titleIconView?.background?.colorFilter = BlendModeColorFilterCompat + .createBlendModeColorFilterCompat(entryState.iconBackgroundColor, BlendModeCompat.SRC_IN) + entryInfo?.icon?.let { icon -> + titleIconView?.let { iconView -> + mDatabase?.iconDrawableFactory?.assignDatabaseIcon( + imageView = iconView, + icon = icon, + tintColor = entryState.iconColor + ) + } } + // Toolbar customization + val toolbarColor = entryState.toolbarColor + val onToolbarColor = entryState.onToolbarColor + collapsingToolbarLayout?.setBackgroundColor(toolbarColor) + collapsingToolbarLayout?.contentScrim = (toolbarColor).toDrawable() + toolbar?.changeControlColor(onToolbarColor) + collapsingToolbarLayout?.changeTitleColor(onToolbarColor) // Manage FAB visibility editFab?.visibility = if (!mDatabaseReadOnly && entryState.showFloatingActionButton) View.VISIBLE else View.GONE @@ -479,29 +464,6 @@ class EntryActivity : DatabaseLockActivity() { super.onPause() } - private fun applyToolbarColors() { - collapsingToolbarLayout?.setBackgroundColor(mBackgroundColor ?: mColorSurface) - collapsingToolbarLayout?.contentScrim = (mBackgroundColor ?: mColorSurface).toDrawable() - val backgroundDarker = if (mBackgroundColor != null) { - ColorUtils.blendARGB(mBackgroundColor!!, Color.WHITE, 0.1f) - } else { - mColorBackground - } - titleIconView?.background?.colorFilter = BlendModeColorFilterCompat - .createBlendModeColorFilterCompat(backgroundDarker, BlendModeCompat.SRC_IN) - mIcon?.let { icon -> - titleIconView?.let { iconView -> - mDatabase?.iconDrawableFactory?.assignDatabaseIcon( - iconView, - icon, - mForegroundColor ?: mColorSecondary - ) - } - } - toolbar?.changeControlColor(mForegroundColor ?: mColorOnSurface) - collapsingToolbarLayout?.changeTitleColor(mForegroundColor ?: mColorOnSurface) - } - override fun onCreateOptionsMenu(menu: Menu): Boolean { super.onCreateOptionsMenu(menu) if (mEntryViewModel.entryLoaded) { @@ -532,7 +494,7 @@ class EntryActivity : DatabaseLockActivity() { menu?.findItem(R.id.menu_merge_database)?.isVisible = false menu?.findItem(R.id.menu_reload_database)?.isVisible = false } - applyToolbarColors() + mEntryViewModel.applyToolbarColors() return super.onPrepareOptionsMenu(menu) } diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt index 473ecefbf..b9fef11f9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt +++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt @@ -20,8 +20,13 @@ package com.kunzisoft.keepass.viewmodels import android.app.Application +import android.content.res.Resources +import android.graphics.Color +import androidx.annotation.ColorInt +import androidx.core.graphics.ColorUtils import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope +import com.kunzisoft.keepass.R import com.kunzisoft.keepass.credentialprovider.magikeyboard.MagikeyboardService import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.element.Attachment @@ -62,8 +67,22 @@ class EntryViewModel(application: Application): AndroidViewModel(application) { val entryLoaded: Boolean get() = entryUIState.value.loaded + @ColorInt + private var colorSecondary: Int = 0 + @ColorInt + private var colorSurface: Int = 0 + @ColorInt + private var colorOnSurface: Int = 0 + @ColorInt + private var colorBackground: Int = 0 + @ColorInt + private var backgroundColor: Int? = null + @ColorInt + private var foregroundColor: Int? = null + private var autoSwitchToMagikeyboard: Boolean = false private var keyboardEntrySelectionEnabled: Boolean = false + private var showEntryColors: Boolean = false private val _clipboardHelper: ClipboardHelper = ClipboardHelper(application) @@ -106,6 +125,23 @@ class EntryViewModel(application: Application): AndroidViewModel(application) { // Init preferences autoSwitchToMagikeyboard = PreferencesUtil.isAutoSwitchToMagikeyboardEnable(application) keyboardEntrySelectionEnabled = PreferencesUtil.isKeyboardEntrySelectionEnable(application) + showEntryColors = PreferencesUtil.showEntryColors(application) + } + + fun setTheme(theme: Resources.Theme) { + // Retrieve the textColor to tint the toolbar + theme.obtainStyledAttributes(intArrayOf(R.attr.colorSecondary)).also { taColorSecondary -> + colorSecondary = taColorSecondary.getColor(0, Color.BLACK) + }.recycle() + theme.obtainStyledAttributes(intArrayOf(R.attr.colorSurface)).also { taColorSurface -> + colorSurface = taColorSurface.getColor(0, Color.BLACK) + }.recycle() + theme.obtainStyledAttributes(intArrayOf(R.attr.colorOnSurface)).also { taColorOnSurface -> + colorOnSurface = taColorOnSurface.getColor(0, Color.BLACK) + }.recycle() + theme.obtainStyledAttributes(intArrayOf(android.R.attr.windowBackground)).also { taColorBackground -> + colorBackground = taColorBackground.getColor(0, Color.BLACK) + }.recycle() } fun loadDatabase(database: ContextualDatabase?) { @@ -136,10 +172,15 @@ class EntryViewModel(application: Application): AndroidViewModel(application) { _onEntryLoaded.emit(OnEntryLoaded( entryInfo = entryInfo )) + + // Assign colors + backgroundColor = if (showEntryColors) entryInfo.backgroundColor else null + foregroundColor = if (showEntryColors) entryInfo.foregroundColor else null + // To show Entry UI _entryUIState.update { it.copy( - loaded = false, + loaded = true, entryInfo = entryInfo, showFloatingActionButton = !isHistory, showHistoryView = isHistory @@ -237,6 +278,21 @@ class EntryViewModel(application: Application): AndroidViewModel(application) { _clipboardHelper.timeoutCopyToClipboard(text, text) } + fun applyToolbarColors() { + viewModelScope.launch { + _entryUIState.update { + it.copy( + toolbarColor = backgroundColor ?: colorSurface, + onToolbarColor = foregroundColor ?: colorOnSurface, + iconColor = foregroundColor ?: colorSecondary, + iconBackgroundColor = backgroundColor?.let { background -> + ColorUtils.blendARGB(background, Color.WHITE, 0.1f) + } ?: colorBackground + ) + } + } + } + /** * Custom data class to manage entry history. */ @@ -264,10 +320,14 @@ class EntryViewModel(application: Application): AndroidViewModel(application) { ) data class EntryState( - val loaded: Boolean = true, + val loaded: Boolean = false, val entryInfo: EntryInfo? = null, val showFloatingActionButton: Boolean = false, - val showHistoryView: Boolean = false + val showHistoryView: Boolean = false, + val toolbarColor: Int = 0, + val onToolbarColor: Int = 0, + val iconColor: Int = 0, + val iconBackgroundColor: Int = 0 ) data class EntryHistoryState(