From 07d2a15cf847222329fdf2074f08cdc429a0eb36 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 31 Dec 2021 14:45:35 +0100 Subject: [PATCH] Code cleanup --- .../home/UserColorAccountDataViewModel.kt | 7 +++- .../helper/MatrixItemColorProvider.kt | 36 ++++++++++--------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/UserColorAccountDataViewModel.kt b/vector/src/main/java/im/vector/app/features/home/UserColorAccountDataViewModel.kt index 9e13fe56d0..77f6eade4a 100644 --- a/vector/src/main/java/im/vector/app/features/home/UserColorAccountDataViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/UserColorAccountDataViewModel.kt @@ -28,12 +28,14 @@ import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.onEach import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.flow.flow +import timber.log.Timber data class DummyState( val dummy: Boolean = false @@ -60,8 +62,11 @@ class UserColorAccountDataViewModel @AssistedInject constructor( session.flow() .liveUserAccountData(setOf(UserAccountDataTypes.TYPE_OVERRIDE_COLORS)) .mapNotNull { it.firstOrNull() } - .mapNotNull { it.content.toModel>() } + .map { it.content.toModel>() } .onEach { userColorAccountDataContent -> + if (userColorAccountDataContent == null) { + Timber.w("Invalid account data im.vector.setting.override_colors") + } matrixItemColorProvider.setOverrideColors(userColorAccountDataContent) } .launchIn(viewModelScope) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MatrixItemColorProvider.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MatrixItemColorProvider.kt index 97028c47f2..4c99894bf3 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MatrixItemColorProvider.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MatrixItemColorProvider.kt @@ -22,6 +22,7 @@ import androidx.annotation.VisibleForTesting import im.vector.app.R import im.vector.app.core.resources.ColorProvider import org.matrix.android.sdk.api.util.MatrixItem +import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton import kotlin.math.abs @@ -45,35 +46,38 @@ class MatrixItemColorProvider @Inject constructor( } fun setOverrideColors(overrideColors: Map?) { - overrideColors?.forEach() { + cache.clear() + overrideColors?.forEach { setOverrideColor(it.key, it.value) } } - fun setOverrideColor(id: String, colorSpec: String?) : Boolean { + fun setOverrideColor(id: String, colorSpec: String?): Boolean { val color = parseUserColorSpec(colorSpec) - if (color == null) { + return if (color == null) { cache.remove(id) - return false + false } else { - cache.put(id, color) - return true + cache[id] = color + true } } @ColorInt private fun parseUserColorSpec(colorText: String?): Int? { - if (colorText.isNullOrBlank()) { - return null - } - try { - if (colorText.first() == '#') { - return (colorText.substring(1).toLong(radix = 16) or 0xff000000L).toInt() - } else { - return colorProvider.getColor(getUserColorByIndex(colorText.toInt())) + return if (colorText.isNullOrBlank()) { + null + } else { + try { + if (colorText.first() == '#') { + (colorText.substring(1).toLong(radix = 16) or 0xff000000L).toInt() + } else { + colorProvider.getColor(getUserColorByIndex(colorText.toInt())) + } + } catch (e: Throwable) { + Timber.e(e, "Unable to parse color $colorText") + null } - } catch (e: Throwable) { - return null } }