Files
forkmessager/client-mobile/core/network/ServerConfig.kt
2026-04-17 16:23:52 +03:00

51 lines
1.6 KiB
Kotlin

package core.network
import android.content.Context
import com.google.gson.Gson
import core.domain.model.FeaturesConfig
import core.domain.model.ServerConfigModel
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ServerConfig @Inject constructor(
private val context: Context,
private val gson: Gson
) {
private val prefs = context.getSharedPreferences("server_config_prefs", Context.MODE_PRIVATE)
companion object {
private const val KEY_BASE_URL = "base_url"
private const val KEY_CONFIG_DATA = "server_config_data"
private const val DEFAULT_BASE_URL = "" // Пустой по умолчанию
}
fun getBaseUrl(): String {
return prefs.getString(KEY_BASE_URL, DEFAULT_BASE_URL) ?: DEFAULT_BASE_URL
}
fun setBaseUrl(url: String) {
val formattedUrl = if (url.endsWith("/")) url else "$url/"
prefs.edit().putString(KEY_BASE_URL, formattedUrl).apply()
}
fun saveServerConfig(config: ServerConfigModel) {
android.util.Log.d("ServerConfig", "Saving new config: $config")
prefs.edit().putString(KEY_CONFIG_DATA, gson.toJson(config)).commit()
}
fun getServerConfig(): ServerConfigModel {
val json = prefs.getString(KEY_CONFIG_DATA, null) ?: return ServerConfigModel()
return try {
gson.fromJson(json, ServerConfigModel::class.java)
} catch (e: Exception) {
ServerConfigModel()
}
}
fun isFeatureEnabled(feature: (FeaturesConfig) -> Boolean): Boolean {
return feature(getServerConfig().features)
}
}