ConfigManager.kt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.narutohuo.xindazhou.common.config
  2. import android.content.Context
  3. import android.content.SharedPreferences
  4. import com.narutohuo.xindazhou.core.log.ILog
  5. /**
  6. * 配置管理器
  7. *
  8. * 业务层封装,提供便捷的配置管理方式
  9. * 直接使用 SharedPreferences 实现配置存储
  10. *
  11. * 使用方式:
  12. * ```kotlin
  13. * // 初始化(在 Application 中)
  14. * ConfigManager.init(applicationContext)
  15. *
  16. * // 获取配置
  17. * val apiUrl = ConfigManager.getString("api_url", "https://api.example.com")
  18. * val isDebug = ConfigManager.getBoolean("is_debug", false)
  19. *
  20. * // 设置配置
  21. * ConfigManager.setString("api_url", "https://api.example.com")
  22. * ConfigManager.setBoolean("is_debug", true)
  23. *
  24. * // 环境切换
  25. * val env = ConfigManager.getEnvironment() // dev, test, prod
  26. * ConfigManager.setEnvironment("prod")
  27. *
  28. * // 功能开关
  29. * val enabled = ConfigManager.isFeatureEnabled("new_feature")
  30. * ConfigManager.setFeatureEnabled("new_feature", true)
  31. * ```
  32. */
  33. object ConfigManager {
  34. private const val TAG = "ConfigManager"
  35. private const val PREFS_NAME = "app_config"
  36. private const val KEY_ENVIRONMENT = "environment"
  37. private const val KEY_FEATURE_PREFIX = "feature_"
  38. private var sharedPreferences: SharedPreferences? = null
  39. /**
  40. * 初始化配置管理器
  41. * 需要在 Application 中调用
  42. *
  43. * @param context 上下文
  44. */
  45. fun init(context: Context) {
  46. sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
  47. }
  48. private fun getPrefs(): SharedPreferences {
  49. return sharedPreferences ?: throw IllegalStateException(
  50. "ConfigManager 未初始化,请在 Application 中调用 ConfigManager.init(context)"
  51. )
  52. }
  53. // ========== 基础配置操作 ==========
  54. // ========== 基础配置操作 ==========
  55. /**
  56. * 获取字符串配置
  57. *
  58. * @param key 配置键
  59. * @param defaultValue 默认值
  60. * @return 配置值
  61. */
  62. fun getString(key: String, defaultValue: String = ""): String {
  63. return getPrefs().getString(key, defaultValue) ?: defaultValue
  64. }
  65. /**
  66. * 获取整数配置
  67. *
  68. * @param key 配置键
  69. * @param defaultValue 默认值
  70. * @return 配置值
  71. */
  72. fun getInt(key: String, defaultValue: Int = 0): Int {
  73. return getPrefs().getInt(key, defaultValue)
  74. }
  75. /**
  76. * 获取布尔配置
  77. *
  78. * @param key 配置键
  79. * @param defaultValue 默认值
  80. * @return 配置值
  81. */
  82. fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
  83. return getPrefs().getBoolean(key, defaultValue)
  84. }
  85. /**
  86. * 设置字符串配置
  87. *
  88. * @param key 配置键
  89. * @param value 配置值
  90. */
  91. fun setString(key: String, value: String) {
  92. getPrefs().edit().putString(key, value).apply()
  93. }
  94. /**
  95. * 设置整数配置
  96. *
  97. * @param key 配置键
  98. * @param value 配置值
  99. */
  100. fun setInt(key: String, value: Int) {
  101. getPrefs().edit().putInt(key, value).apply()
  102. }
  103. /**
  104. * 设置布尔配置
  105. *
  106. * @param key 配置键
  107. * @param value 配置值
  108. */
  109. fun setBoolean(key: String, value: Boolean) {
  110. getPrefs().edit().putBoolean(key, value).apply()
  111. }
  112. // ========== 环境切换 ==========
  113. /**
  114. * 获取当前环境
  115. *
  116. * @return 环境名称(dev, test, prod)
  117. */
  118. fun getEnvironment(): String {
  119. return getString(KEY_ENVIRONMENT, "dev")
  120. }
  121. /**
  122. * 设置环境
  123. *
  124. * @param env 环境名称(dev, test, prod)
  125. */
  126. fun setEnvironment(env: String) {
  127. setString(KEY_ENVIRONMENT, env)
  128. }
  129. // ========== 功能开关 ==========
  130. /**
  131. * 检查功能是否启用
  132. *
  133. * @param feature 功能名称
  134. * @return true 表示功能已启用
  135. */
  136. fun isFeatureEnabled(feature: String): Boolean {
  137. return getBoolean("$KEY_FEATURE_PREFIX$feature", false)
  138. }
  139. /**
  140. * 设置功能开关
  141. *
  142. * @param feature 功能名称
  143. * @param enabled 是否启用
  144. */
  145. fun setFeatureEnabled(feature: String, enabled: Boolean) {
  146. setBoolean("$KEY_FEATURE_PREFIX$feature", enabled)
  147. }
  148. /**
  149. * 清除所有配置
  150. */
  151. fun clearAll() {
  152. getPrefs().edit().clear().apply()
  153. }
  154. }