Updated on 2026-08-14
This commit is contained in:
parent
ca7088e2f5
commit
2ffe05e93a
7 changed files with 39 additions and 55 deletions
|
|
@ -3,13 +3,14 @@ package com.tangem.datasource.api.common.config
|
|||
/**
|
||||
* Api config
|
||||
*
|
||||
* @property currentEnvironment current api environment
|
||||
*
|
||||
* @see <a href="https://www.notion.so/tangem/API-eacb264e7daf420a88b419a8a26f5b26?pvs=4">API configuration</a>
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
sealed class ApiConfig(open val currentEnvironment: ApiEnvironment) {
|
||||
sealed class ApiConfig {
|
||||
|
||||
/** Default environment */
|
||||
abstract val defaultEnvironment: ApiEnvironment
|
||||
|
||||
/** Available environments with base url */
|
||||
abstract val environments: Map<ApiEnvironment, String>
|
||||
|
|
@ -22,14 +23,6 @@ sealed class ApiConfig(open val currentEnvironment: ApiEnvironment) {
|
|||
TangemTech,
|
||||
}
|
||||
|
||||
/** Copy method for sealed class [ApiConfig] */
|
||||
fun copySealed(currentEnvironment: ApiEnvironment): ApiConfig {
|
||||
return when (this) {
|
||||
is Express -> copy(currentEnvironment = currentEnvironment)
|
||||
is TangemTech -> copy(currentEnvironment = currentEnvironment)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initializeId(): ID {
|
||||
return when (this) {
|
||||
is Express -> ID.Express
|
||||
|
|
|
|||
|
|
@ -2,14 +2,10 @@ package com.tangem.datasource.api.common.config
|
|||
|
||||
import com.tangem.datasource.BuildConfig
|
||||
|
||||
/**
|
||||
* Express [ApiConfig]
|
||||
*
|
||||
* @property currentEnvironment current api environment
|
||||
*/
|
||||
internal data class Express(
|
||||
override val currentEnvironment: ApiEnvironment = initializeCurrentEnvironment(),
|
||||
) : ApiConfig(currentEnvironment) {
|
||||
/** Express [ApiConfig] */
|
||||
internal class Express : ApiConfig() {
|
||||
|
||||
override val defaultEnvironment: ApiEnvironment = getInitialEnvironment()
|
||||
|
||||
override val environments: Map<ApiEnvironment, String> = mapOf(
|
||||
ApiEnvironment.DEV to "[REDACTED_ENV_URL]",
|
||||
|
|
@ -19,7 +15,7 @@ internal data class Express(
|
|||
|
||||
private companion object {
|
||||
|
||||
fun initializeCurrentEnvironment(): ApiEnvironment {
|
||||
fun getInitialEnvironment(): ApiEnvironment {
|
||||
return when (BuildConfig.BUILD_TYPE) {
|
||||
DEBUG_BUILD_TYPE -> ApiEnvironment.DEV
|
||||
INTERNAL_BUILD_TYPE,
|
||||
|
|
|
|||
|
|
@ -1,13 +1,9 @@
|
|||
package com.tangem.datasource.api.common.config
|
||||
|
||||
/**
|
||||
* TangemTech [ApiConfig]
|
||||
*
|
||||
* @property currentEnvironment current api environment
|
||||
*/
|
||||
internal data class TangemTech(
|
||||
override val currentEnvironment: ApiEnvironment = ApiEnvironment.PROD,
|
||||
) : ApiConfig(currentEnvironment) {
|
||||
/** TangemTech [ApiConfig] */
|
||||
internal class TangemTech : ApiConfig() {
|
||||
|
||||
override val defaultEnvironment: ApiEnvironment = ApiEnvironment.PROD
|
||||
|
||||
override val environments: Map<ApiEnvironment, String> = mapOf(
|
||||
ApiEnvironment.DEV to "https://devapi.tangem-tech.com/v1/",
|
||||
|
|
|
|||
|
|
@ -7,10 +7,7 @@ import com.tangem.datasource.local.preferences.PreferencesKeys
|
|||
import com.tangem.datasource.local.preferences.utils.getObjectMap
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* Implementation of [ApiConfigsManager] in DEV environment
|
||||
|
|
@ -23,9 +20,9 @@ internal class DevApiConfigsManager(
|
|||
private val dispatchers: CoroutineDispatcherProvider,
|
||||
) : MutableApiConfigsManager {
|
||||
|
||||
override val configs: Flow<List<ApiConfig>> get() = _apiConfigs
|
||||
override val configs: Flow<Map<ApiConfig, ApiEnvironment>> get() = _apiConfigs
|
||||
|
||||
private val _apiConfigs = MutableStateFlow(value = ApiConfig.values())
|
||||
private val _apiConfigs = MutableStateFlow(value = ApiConfig.values().associateWith { it.defaultEnvironment })
|
||||
|
||||
override suspend fun initialize() {
|
||||
// We can't use appPreferencesStore.getObjectMap as base flow,
|
||||
|
|
@ -33,13 +30,11 @@ internal class DevApiConfigsManager(
|
|||
// See [getBaseUrl]
|
||||
appPreferencesStore.getObjectMap<ApiEnvironment>(PreferencesKeys.apiConfigsEnvironmentKey)
|
||||
.onEach { savedEnvironments ->
|
||||
_apiConfigs.value = _apiConfigs.value.map { config ->
|
||||
val savedEnvironment = savedEnvironments[config.id.name]
|
||||
_apiConfigs.update { apiConfigs ->
|
||||
apiConfigs.mapValues {
|
||||
val (config, currentEnvironment) = it
|
||||
|
||||
if (savedEnvironment != null) {
|
||||
config.copySealed(currentEnvironment = savedEnvironment)
|
||||
} else {
|
||||
config
|
||||
savedEnvironments[config.id.name] ?: currentEnvironment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -47,14 +42,16 @@ internal class DevApiConfigsManager(
|
|||
}
|
||||
|
||||
override fun getBaseUrl(id: ApiConfig.ID): String {
|
||||
val config = _apiConfigs.value.firstOrNull { it.id == id }
|
||||
?: error("Api config with id [$id] not found. Check ApiConfig implementations")
|
||||
val apiConfigs = _apiConfigs.value
|
||||
|
||||
return config.environments[config.currentEnvironment]
|
||||
?: error(
|
||||
"Api config with id [$id] doesn't contain environment [${config.currentEnvironment}]. " +
|
||||
"Check ApiConfig implementations",
|
||||
)
|
||||
val config = apiConfigs.map { it }.firstOrNull { it.key.id == id }?.key
|
||||
?: error("Api config with id [$id] not found")
|
||||
|
||||
val currentEnvironment = apiConfigs[config]
|
||||
?: error("Current environment of api config with id [$id] not found")
|
||||
|
||||
return config.environments[currentEnvironment]
|
||||
?: error("Api config with id [$id] doesn't contain environment [$currentEnvironment]")
|
||||
}
|
||||
|
||||
override suspend fun changeEnvironment(id: String, environment: ApiEnvironment) {
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ import kotlinx.coroutines.flow.Flow
|
|||
*/
|
||||
interface MutableApiConfigsManager : ApiConfigsManager {
|
||||
|
||||
/** Configs */
|
||||
val configs: Flow<List<ApiConfig>>
|
||||
/** Api configs with current [ApiEnvironment] */
|
||||
val configs: Flow<Map<ApiConfig, ApiEnvironment>>
|
||||
|
||||
/** Change api environment [environment] by [id] */
|
||||
suspend fun changeEnvironment(id: String, environment: ApiEnvironment)
|
||||
|
|
|
|||
|
|
@ -9,10 +9,10 @@ internal class ProdApiConfigsManager : ApiConfigsManager {
|
|||
val config = ApiConfig.values().firstOrNull { it.id == id }
|
||||
?: error("Api config with id [$id] not found. Check ApiConfig implementations")
|
||||
|
||||
return config.environments[config.currentEnvironment]
|
||||
return config.environments[config.defaultEnvironment]
|
||||
?: error(
|
||||
"Api config with id [$id] doesn't contain " +
|
||||
"environment [${config.currentEnvironment}]. Check ApiConfig implementations",
|
||||
"environment [${config.defaultEnvironment}]. Check ApiConfig implementations",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -62,12 +62,14 @@ internal class EnvironmentsTogglesViewModel @Inject constructor(
|
|||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
private fun List<ApiConfig>.toUiModel(): ImmutableSet<EnvironmentTogglesScreenUM.ApiInfoUM> {
|
||||
return map { config ->
|
||||
private fun Map<ApiConfig, ApiEnvironment>.toUiModel(): ImmutableSet<EnvironmentTogglesScreenUM.ApiInfoUM> {
|
||||
return map {
|
||||
val (config, currentEnvironment) = it
|
||||
|
||||
EnvironmentTogglesScreenUM.ApiInfoUM(
|
||||
name = config.id.name,
|
||||
select = config.currentEnvironment.name,
|
||||
url = config.environments[config.currentEnvironment]
|
||||
select = currentEnvironment.name,
|
||||
url = config.environments[currentEnvironment]
|
||||
?: error("Current environment's url isn't found"),
|
||||
environments = config.environments
|
||||
.map { environment -> environment.key.name }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue