Updated on 2026-08-14
This commit is contained in:
parent
5747404539
commit
5987be96a7
6 changed files with 212 additions and 0 deletions
27
app/src/main/java/com/tangem/tap/domain/config/Conditions.kt
Normal file
27
app/src/main/java/com/tangem/tap/domain/config/Conditions.kt
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package com.tangem.tap.domain.config
|
||||
|
||||
import com.tangem.commands.Card
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
interface Condition {
|
||||
fun isMet(): Boolean
|
||||
}
|
||||
|
||||
class CardIsStart2CoinCondition(private val card: Card?) : Condition {
|
||||
|
||||
override fun isMet(): Boolean = card?.cardData?.issuerName?.toLowerCase(Locale.US) == "start2coin"
|
||||
}
|
||||
|
||||
class ConditionsFactory {
|
||||
companion object {
|
||||
fun create(name: String): Condition? {
|
||||
return when (name) {
|
||||
Feature.payIdIsEnabled -> CardIsStart2CoinCondition(null)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package com.tangem.tap.domain.config
|
||||
|
||||
import android.content.Context
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.tangem.wallet.BuildConfig
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
interface ConfigLoader {
|
||||
suspend fun loadConfig(onComplete: (Config) -> Unit)
|
||||
}
|
||||
|
||||
|
||||
interface NameResolver {
|
||||
fun getFeaturesName(): String
|
||||
fun getConfigValuesName(): String
|
||||
}
|
||||
|
||||
class ConfigNameResolver {
|
||||
companion object {
|
||||
fun get(): NameResolver = if (BuildConfig.DEBUG) dev() else prod()
|
||||
|
||||
private fun dev(): NameResolver {
|
||||
return object : NameResolver {
|
||||
override fun getFeaturesName(): String = "dev_features"
|
||||
override fun getConfigValuesName(): String = "dev_config_values"
|
||||
}
|
||||
}
|
||||
|
||||
private fun prod(): NameResolver {
|
||||
return object : NameResolver {
|
||||
override fun getFeaturesName(): String = "prod_features"
|
||||
override fun getConfigValuesName(): String = "prod_config_values"
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LocalLoader(
|
||||
private val context: Context,
|
||||
private val nameResolver: NameResolver,
|
||||
private val adapter: JsonAdapter<ConfigModel>
|
||||
) : ConfigLoader {
|
||||
|
||||
override suspend fun loadConfig(onComplete: (Config) -> Unit) {
|
||||
val config = try {
|
||||
val jsonFeatures = readAssetAsString(nameResolver.getFeaturesName())
|
||||
var configModel = adapter.fromJson(jsonFeatures)
|
||||
val features = configModel?.toFeatures(ConfigType.Local) ?: mutableMapOf()
|
||||
|
||||
val jsonConfigValues = readAssetAsString(nameResolver.getConfigValuesName())
|
||||
configModel = adapter.fromJson(jsonConfigValues)
|
||||
val configValues = configModel?.toConfigValues() ?: mutableMapOf()
|
||||
Config(features, configValues)
|
||||
} catch (ex: Exception) {
|
||||
Timber.e(ex)
|
||||
Config.empty()
|
||||
}
|
||||
onComplete(config)
|
||||
}
|
||||
|
||||
private fun readAssetAsString(fileName: String): String {
|
||||
return context.assets.open("$fileName.json").bufferedReader().readText()
|
||||
}
|
||||
}
|
||||
36
app/src/main/java/com/tangem/tap/domain/config/JsonModels.kt
Normal file
36
app/src/main/java/com/tangem/tap/domain/config/JsonModels.kt
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package com.tangem.tap.domain.config
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
|
||||
interface BaseConfigModel<V> {
|
||||
val name: String
|
||||
val value: V?
|
||||
}
|
||||
|
||||
class FeatureModel(
|
||||
override val name: String,
|
||||
override val value: Boolean?
|
||||
) : BaseConfigModel<Boolean>
|
||||
|
||||
class ConfigValueModel(
|
||||
override val name: String,
|
||||
override val value: String?
|
||||
) : BaseConfigModel<String>
|
||||
|
||||
class ConfigModel(val features: List<FeatureModel>?, val configValues: List<ConfigValueModel>?)
|
||||
|
||||
fun ConfigModel.toFeatures(type: ConfigType): MutableMap<String, Feature> {
|
||||
return features?.map { AppFeature(type, it, ConditionsFactory.create(it.name)) }
|
||||
?.associateBy { it.name }
|
||||
?.toMutableMap()
|
||||
?: mutableMapOf()
|
||||
}
|
||||
|
||||
fun ConfigModel.toConfigValues(): MutableMap<String, ConfigurationValue> {
|
||||
return configValues?.map { ConfigurationValue(it.name, it.value) }
|
||||
?.associateBy { it.name }
|
||||
?.toMutableMap() ?: mutableMapOf()
|
||||
}
|
||||
|
||||
45
app/src/main/java/com/tangem/tap/domain/config/Models.kt
Normal file
45
app/src/main/java/com/tangem/tap/domain/config/Models.kt
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package com.tangem.tap.domain.config
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
|
||||
interface Feature {
|
||||
val name: String
|
||||
val type: ConfigType
|
||||
fun isActive(): Boolean
|
||||
fun reset()
|
||||
fun updateCondition(condition: Condition?)
|
||||
fun hasCondition(): Boolean
|
||||
|
||||
companion object {
|
||||
val payIdIsEnabled = "payIdIsEnabled"
|
||||
}
|
||||
}
|
||||
|
||||
class AppFeature(
|
||||
override val type: ConfigType,
|
||||
private val featureModel: FeatureModel,
|
||||
private var condition: Condition?
|
||||
) : Feature {
|
||||
override val name: String = featureModel.name
|
||||
|
||||
override fun isActive(): Boolean {
|
||||
return if (condition == null) featureModel.value ?: false
|
||||
else condition!!.isMet()
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
condition = null
|
||||
}
|
||||
|
||||
override fun updateCondition(condition: Condition?) {
|
||||
if (type == ConfigType.Local) this.condition = condition
|
||||
}
|
||||
|
||||
override fun hasCondition(): Boolean {
|
||||
return condition != null
|
||||
}
|
||||
}
|
||||
|
||||
data class ConfigurationValue(val name: String, val value: String?)
|
||||
Loading…
Add table
Add a link
Reference in a new issue