Приложение
This commit is contained in:
49
client-mobile/core/network/ServerConfig.kt
Normal file
49
client-mobile/core/network/ServerConfig.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user