Code cleanup

This commit is contained in:
Benoit Marty 2021-12-31 14:45:35 +01:00
parent 364457d102
commit 07d2a15cf8
2 changed files with 26 additions and 17 deletions

View File

@ -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<String, String>>() }
.map { it.content.toModel<Map<String, String>>() }
.onEach { userColorAccountDataContent ->
if (userColorAccountDataContent == null) {
Timber.w("Invalid account data im.vector.setting.override_colors")
}
matrixItemColorProvider.setOverrideColors(userColorAccountDataContent)
}
.launchIn(viewModelScope)

View File

@ -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<String, String>?) {
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
}
}