Files
forkmessager/client-mobile/core/network/ServerConfig.kt
Халимов Рустам 1fb1be47dd Приложение
2026-04-14 01:15:54 +03:00

50 lines
1.5 KiB
Kotlin

package core.network
import android.content.Context
import android.content.SharedPreferences
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: SharedPreferences = context.getSharedPreferences("server_settings", Context.MODE_PRIVATE)
companion object {
const val DEFAULT_BASE_URL = "https://your-api.com/api/"
private const val KEY_BASE_URL = "base_url"
private const val KEY_CONFIG_DATA = "config_data"
}
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) {
prefs.edit().putString(KEY_CONFIG_DATA, gson.toJson(config)).apply()
}
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)
}
}