| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package com.narutohuo.xindazhou.common.config
- import android.content.Context
- import android.content.SharedPreferences
- import com.narutohuo.xindazhou.core.log.ILog
- /**
- * 配置管理器
- *
- * 业务层封装,提供便捷的配置管理方式
- * 直接使用 SharedPreferences 实现配置存储
- *
- * 使用方式:
- * ```kotlin
- * // 初始化(在 Application 中)
- * ConfigManager.init(applicationContext)
- *
- * // 获取配置
- * val apiUrl = ConfigManager.getString("api_url", "https://api.example.com")
- * val isDebug = ConfigManager.getBoolean("is_debug", false)
- *
- * // 设置配置
- * ConfigManager.setString("api_url", "https://api.example.com")
- * ConfigManager.setBoolean("is_debug", true)
- *
- * // 环境切换
- * val env = ConfigManager.getEnvironment() // dev, test, prod
- * ConfigManager.setEnvironment("prod")
- *
- * // 功能开关
- * val enabled = ConfigManager.isFeatureEnabled("new_feature")
- * ConfigManager.setFeatureEnabled("new_feature", true)
- * ```
- */
- object ConfigManager {
-
- private const val TAG = "ConfigManager"
- private const val PREFS_NAME = "app_config"
- private const val KEY_ENVIRONMENT = "environment"
- private const val KEY_FEATURE_PREFIX = "feature_"
-
- private var sharedPreferences: SharedPreferences? = null
-
- /**
- * 初始化配置管理器
- * 需要在 Application 中调用
- *
- * @param context 上下文
- */
- fun init(context: Context) {
- sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
- }
-
- private fun getPrefs(): SharedPreferences {
- return sharedPreferences ?: throw IllegalStateException(
- "ConfigManager 未初始化,请在 Application 中调用 ConfigManager.init(context)"
- )
- }
-
- // ========== 基础配置操作 ==========
-
- // ========== 基础配置操作 ==========
-
- /**
- * 获取字符串配置
- *
- * @param key 配置键
- * @param defaultValue 默认值
- * @return 配置值
- */
- fun getString(key: String, defaultValue: String = ""): String {
- return getPrefs().getString(key, defaultValue) ?: defaultValue
- }
-
- /**
- * 获取整数配置
- *
- * @param key 配置键
- * @param defaultValue 默认值
- * @return 配置值
- */
- fun getInt(key: String, defaultValue: Int = 0): Int {
- return getPrefs().getInt(key, defaultValue)
- }
-
- /**
- * 获取布尔配置
- *
- * @param key 配置键
- * @param defaultValue 默认值
- * @return 配置值
- */
- fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
- return getPrefs().getBoolean(key, defaultValue)
- }
-
- /**
- * 设置字符串配置
- *
- * @param key 配置键
- * @param value 配置值
- */
- fun setString(key: String, value: String) {
- getPrefs().edit().putString(key, value).apply()
- }
-
- /**
- * 设置整数配置
- *
- * @param key 配置键
- * @param value 配置值
- */
- fun setInt(key: String, value: Int) {
- getPrefs().edit().putInt(key, value).apply()
- }
-
- /**
- * 设置布尔配置
- *
- * @param key 配置键
- * @param value 配置值
- */
- fun setBoolean(key: String, value: Boolean) {
- getPrefs().edit().putBoolean(key, value).apply()
- }
-
- // ========== 环境切换 ==========
-
- /**
- * 获取当前环境
- *
- * @return 环境名称(dev, test, prod)
- */
- fun getEnvironment(): String {
- return getString(KEY_ENVIRONMENT, "dev")
- }
-
- /**
- * 设置环境
- *
- * @param env 环境名称(dev, test, prod)
- */
- fun setEnvironment(env: String) {
- setString(KEY_ENVIRONMENT, env)
- }
-
- // ========== 功能开关 ==========
-
- /**
- * 检查功能是否启用
- *
- * @param feature 功能名称
- * @return true 表示功能已启用
- */
- fun isFeatureEnabled(feature: String): Boolean {
- return getBoolean("$KEY_FEATURE_PREFIX$feature", false)
- }
-
- /**
- * 设置功能开关
- *
- * @param feature 功能名称
- * @param enabled 是否启用
- */
- fun setFeatureEnabled(feature: String, enabled: Boolean) {
- setBoolean("$KEY_FEATURE_PREFIX$feature", enabled)
- }
-
- /**
- * 清除所有配置
- */
- fun clearAll() {
- getPrefs().edit().clear().apply()
- }
- }
|