Skip to content

Commit

Permalink
Bug fix: sometimes not sync settings with cfg file.
Browse files Browse the repository at this point in the history
  • Loading branch information
KyuubiRan committed Apr 26, 2021
1 parent 72c8103 commit daa6879
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DO_NOT_DISTURB_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_POWER_MODE_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE
import me.kyuubiran.qqcleaner.utils.ConfigManager.checkCfg
import me.kyuubiran.qqcleaner.utils.ConfigManager.getBool
import me.kyuubiran.qqcleaner.utils.ConfigManager.getInt
import me.kyuubiran.qqcleaner.utils.ConfigManager.getJsonArray
import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong
import me.kyuubiran.qqcleaner.utils.ConfigManager.setArray
import me.kyuubiran.qqcleaner.utils.ConfigManager.getString
import me.kyuubiran.qqcleaner.utils.ConfigManager.setConfig
import me.kyuubiran.qqcleaner.utils.ConfigManager.setJsonArray
import me.kyuubiran.qqcleaner.utils.ConfigManager.toHashSet
import me.kyuubiran.qqcleaner.utils.HostApp
import me.kyuubiran.qqcleaner.utils.formatSize
import me.kyuubiran.qqcleaner.utils.isInNightMode
Expand Down Expand Up @@ -112,12 +116,12 @@ class SettingsActivity : Activity() {

//初始化函数
private fun init() {
initSummary()
setCustomerCleanList()
refreshCleanedSize()
initItemStatus()
toggleSwitchItemCtrl()
setClickable()
setVersionName()
setCustomerCleanList()
setArray(CFG_CUSTOMER_CLEAN_LIST, customerCleanList.values as HashSet<String>)
setSummary()
}

private fun setCustomerCleanList() {
Expand All @@ -137,6 +141,15 @@ class SettingsActivity : Activity() {
}
}

private fun initItemStatus() {
autoClean.isChecked = getBool(CFG_AUTO_CLEAN_ENABLED)
powerMode.isChecked = getBool(CFG_POWER_MODE_ENABLED)
enableDateLimit.isChecked = getBool(CFG_DATE_LIMIT_ENABLED)
doNotDisturb.isChecked = getBool(CFG_DO_NOT_DISTURB_ENABLED)
autoCleanMode.value = getString(CFG_AUTO_CLEAN_MODE, HALF_MODE)
customerCleanList.values = getJsonArray(CFG_CUSTOMER_CLEAN_LIST)?.toHashSet<String>()
}

//设置Item点击事件
private fun setClickable() {
halfClean.setOnPreferenceClickListener {
Expand All @@ -150,7 +163,7 @@ class SettingsActivity : Activity() {
customerCleanList.setOnPreferenceChangeListener { _, newValue ->
try {
@Suppress("UNCHECKED_CAST")
setArray(CFG_CUSTOMER_CLEAN_LIST, newValue as HashSet<String>)
setJsonArray(CFG_CUSTOMER_CLEAN_LIST, newValue as HashSet<String>)
appContext.show("好耶 保存自定义瘦身列表成功了!")
} catch (e: Exception) {
Log.e(e)
Expand Down Expand Up @@ -201,7 +214,7 @@ class SettingsActivity : Activity() {
}
cleanedHistory.setOnPreferenceClickListener {
appContext.show("已刷新统计信息")
initSummary()
refreshCleanedSize()
true
}
cleanDelay.setOnPreferenceClickListener {
Expand Down Expand Up @@ -286,22 +299,22 @@ class SettingsActivity : Activity() {
}
}

private fun initSummary() {
private fun refreshCleanedSize() {
//腾出空间
if (getInt(CFG_TOTAL_CLEANED_SIZE) != 0) {
cleanedHistory.summary =
"总共为您腾出:${formatSize(getLong(CFG_TOTAL_CLEANED_SIZE))}空间"
} else {
cleanedHistory.setSummary(R.string.no_cleaned_his_hint)
}
}

private fun setSummary() {
//自动瘦身
autoClean.summary =
if (autoClean.isEnabled) "当前清理的间隔为${getInt(CFG_CLEAN_DELAY, 24)}小时" else "未开启"
//设置清理超过日期
setDateLimit.summary = "当前会清理存在超过${getInt(CFG_DATE_LIMIT, 3)}天的文件"
}

private fun setVersionName() {
moduleInfo.summary = "${BuildConfig.VERSION_NAME}(${BuildConfig.VERSION_CODE})"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DATE_LIMIT_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DO_NOT_DISTURB_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_POWER_MODE_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE
import me.kyuubiran.qqcleaner.utils.ConfigManager.getArray
import me.kyuubiran.qqcleaner.utils.ConfigManager.getBool
import me.kyuubiran.qqcleaner.utils.ConfigManager.getInt
import me.kyuubiran.qqcleaner.utils.ConfigManager.getJsonArray
import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong
import me.kyuubiran.qqcleaner.utils.ConfigManager.getString
import me.kyuubiran.qqcleaner.utils.clean.CleanQQ
Expand Down Expand Up @@ -44,7 +44,7 @@ object CleanManager {
* @return 获取用户自定义的瘦身列表
*/
private fun getCustomerList(): ArrayList<File> {
val customerList = getArray(ConfigManager.CFG_CUSTOMER_CLEAN_LIST)
val customerList = getJsonArray(ConfigManager.CFG_CUSTOMER_CLEAN_LIST)
val arr = ArrayList<File>()
customerList?.forEach<String> { s ->
when (hostApp) {
Expand Down
36 changes: 31 additions & 5 deletions app/src/main/java/me/kyuubiran/qqcleaner/utils/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ object ConfigManager {
}

private fun <T> checkArrayHasValue(key: String, defValue: ArrayList<T>) {
setArray(key, defValue)
if (getJsonArray(key).isNullOrEmpty()) {
setJsonArray(key, defValue)
}
}

private fun getConfig(): JSONObject? {
Expand Down Expand Up @@ -129,34 +131,58 @@ object ConfigManager {
}
}

fun <T> setArray(key: String, arr: ArrayList<T>) {
fun <T> setJsonArray(key: String, arr: ArrayList<T>) {
val jsonArray = JSONArray()
for (v in arr) {
jsonArray.put(v)
}
setConfig(key, jsonArray)
}

fun <T> setArray(key: String, hs: HashSet<T>) {
fun <T> setJsonArray(key: String, hs: HashSet<T>) {
val jsonArray = JSONArray()
for (v in hs) {
jsonArray.put(v)
}
setConfig(key, jsonArray)
}

fun <T> setArray(key: String, arr: Array<T>) {
fun <T> setJsonArray(key: String, arr: Array<T>) {
val jsonArray = JSONArray()
for (v in arr) {
jsonArray.put(v)
}
setConfig(key, jsonArray)
}

fun getArray(key: String): JSONArray? {
fun getJsonArray(key: String): JSONArray? {
return getConfig()?.getJSONArray(key)
}

fun <T> JSONArray.toArrayList(): ArrayList<T> {
val arr = ArrayList<T>()
this.forEach<T> { arr.add(it) }
return arr
}

inline fun <reified T> JSONArray.toArray(): Array<T> {
return this.toArrayList<T>().toTypedArray()
}

fun <E> JSONArray.toHashSet(): HashSet<E> {
val hs = HashSet<E>()
this.forEach<E> { hs.add(it) }
return hs
}

fun JSONArray?.isNotNullOrEmpty(): Boolean {
return this != null || this?.length() != 0
}

fun JSONArray?.isNullOrEmpty(): Boolean {
return !this.isNotNullOrEmpty()
}

private fun save(jsonObject: JSONObject) {
save(jsonObject.toString())
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
<string name="update_info">更新日志</string>
<string name="update_info_content">-1.7.0(34)\n
1.极限压缩APK体积\n
2.修复强力模式开关无效的问题(如果你已经开启了强力模式 则需要再手动开关一次)\n
3.[QQ/TIM]修改部分瘦身内容 新增一些路径\n
4.新增静默模式(不弹出清理Toast)\n
5.修改Toast样式
2.修复强力模式开关无效的问题\n
3.现在瘦身设置完全由配置文件控制 避免一些设置与配置文件不同步的BUG\n
4.[QQ/TIM]修改部分瘦身内容 新增一些路径\n
5.新增静默模式(不弹出清理Toast)\n
6.修改Toast样式
</string>
<string name="join_qq_group">点击加入QQ群</string>
<string name="join_qq_group_hint">有问题可以来群里反馈哦~</string>
Expand Down

0 comments on commit daa6879

Please sign in to comment.