From 7e4c20ca0585c11289853fa11f5de66eb64f3f1a Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 4 Feb 2023 02:42:45 +0800 Subject: [PATCH] Modify merge seek bar changed event to SeekBarFactory --- .../com/fankes/miui/notify/data/ConfigData.kt | 48 ++++++++-------- .../notify/data/factory/SeekBarFactory.kt | 57 +++++++++++++++++++ .../miui/notify/ui/activity/MainActivity.kt | 18 +----- 3 files changed, 84 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/com/fankes/miui/notify/data/factory/SeekBarFactory.kt diff --git a/app/src/main/java/com/fankes/miui/notify/data/ConfigData.kt b/app/src/main/java/com/fankes/miui/notify/data/ConfigData.kt index 772b3bb..cdf1a3b 100644 --- a/app/src/main/java/com/fankes/miui/notify/data/ConfigData.kt +++ b/app/src/main/java/com/fankes/miui/notify/data/ConfigData.kt @@ -96,30 +96,6 @@ object ConfigData { } } - /** - * 读取 [Int] 数据 - * @param data 键值数据模板 - * @return [Int] - */ - private fun getInt(data: PrefsData) = when (instance) { - is Context -> (instance as Context).modulePrefs.get(data) - is PackageParam -> (instance as PackageParam).prefs.get(data) - else -> error("Unknown type for get prefs data") - } - - /** - * 存入 [Int] 数据 - * @param data 键值数据模板 - * @param value 键值内容 - */ - private fun putInt(data: PrefsData, value: Int) { - when (instance) { - is Context -> (instance as Context).modulePrefs.put(data, value) - is PackageParam -> loggerW(msg = "Not support for this method") - else -> error("Unknown type for put prefs data") - } - } - /** * 读取 [String] 数据 * @param data 键值数据模板 @@ -144,6 +120,30 @@ object ConfigData { } } + /** + * 读取 [Int] 数据 + * @param data 键值数据模板 + * @return [Int] + */ + internal fun getInt(data: PrefsData) = when (instance) { + is Context -> (instance as Context).modulePrefs.get(data) + is PackageParam -> (instance as PackageParam).prefs.get(data) + else -> error("Unknown type for get prefs data") + } + + /** + * 存入 [Int] 数据 + * @param data 键值数据模板 + * @param value 键值内容 + */ + internal fun putInt(data: PrefsData, value: Int) { + when (instance) { + is Context -> (instance as Context).modulePrefs.put(data, value) + is PackageParam -> loggerW(msg = "Not support for this method") + else -> error("Unknown type for put prefs data") + } + } + /** * 读取 [Boolean] 数据 * @param data 键值数据模板 diff --git a/app/src/main/java/com/fankes/miui/notify/data/factory/SeekBarFactory.kt b/app/src/main/java/com/fankes/miui/notify/data/factory/SeekBarFactory.kt new file mode 100644 index 0000000..5e61d9a --- /dev/null +++ b/app/src/main/java/com/fankes/miui/notify/data/factory/SeekBarFactory.kt @@ -0,0 +1,57 @@ +/* + * MIUINativeNotifyIcon - Fix the native notification bar icon function abandoned by the MIUI development team. + * Copyright (C) 2019-2022 Fankes Studio(qzmmcn@163.com) + * https://github.com/fankes/MIUINativeNotifyIcon + * + * This software is non-free but opensource software: you can redistribute it + * and/or modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + *

+ * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * and eula along with this software. If not, see + * + * + * This file is Created by fankes on 2023/2/4. + */ +@file:Suppress("SetTextI18n") + +package com.fankes.miui.notify.data.factory + +import android.widget.SeekBar +import android.widget.TextView +import com.fankes.miui.notify.data.ConfigData +import com.highcapable.yukihookapi.hook.xposed.prefs.data.PrefsData + +/** + * 绑定到 [SeekBar] 自动设置进度与 [TextView] + * @param data 键值数据模板 + * @param textView 文本框 + * @param suffix 文本显示的后缀 - 默认空 + * @param onChange 当改变停止时回调 + */ +fun SeekBar.bind(data: PrefsData, textView: TextView, suffix: String = "", onChange: (Int) -> Unit = {}) { + ConfigData.getInt(data).also { value -> + textView.text = "$value$suffix" + progress = value + } + setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { + + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + textView.text = "$progress$suffix" + } + + override fun onStopTrackingTouch(seekBar: SeekBar) { + ConfigData.putInt(data, seekBar.progress) + onChange(seekBar.progress) + } + + override fun onStartTrackingTouch(seekBar: SeekBar?) {} + }) +} \ No newline at end of file diff --git a/app/src/main/java/com/fankes/miui/notify/ui/activity/MainActivity.kt b/app/src/main/java/com/fankes/miui/notify/ui/activity/MainActivity.kt index 6254dd3..781726a 100644 --- a/app/src/main/java/com/fankes/miui/notify/ui/activity/MainActivity.kt +++ b/app/src/main/java/com/fankes/miui/notify/ui/activity/MainActivity.kt @@ -24,7 +24,6 @@ package com.fankes.miui.notify.ui.activity -import android.widget.SeekBar import androidx.core.view.isGone import androidx.core.view.isVisible import com.fankes.miui.notify.BuildConfig @@ -160,8 +159,6 @@ class MainActivity : BaseActivity() { binding.mainTextMiuiVersion.text = "系统版本:[$androidVersionCodeName] $miuiFullVersion" binding.warnSCountDisTip.isGone = miuiVersionCode > 12.5 binding.warnMiuiNotifyStyleTip.isGone = miuiVersionCode > 12 - binding.notifyIconCustomCornerSeekbar.progress = ConfigData.notifyIconCornerSize - binding.notifyIconCustomCornerText.text = "${ConfigData.notifyIconCornerSize} dp" binding.statusIconCountText.text = ConfigData.liftedStatusIconCount.toString() binding.notifyIconAutoSyncText.text = ConfigData.notifyIconFixAutoTime binding.moduleEnableSwitch.bind(ConfigData.ENABLE_MODULE) { @@ -248,18 +245,9 @@ class MainActivity : BaseActivity() { SystemUITool.refreshSystemUI(context = this@MainActivity, isRefreshCacheOnly = true) } } - binding.notifyIconCustomCornerSeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { - override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - binding.notifyIconCustomCornerText.text = "$progress dp" - } - - override fun onStopTrackingTouch(seekBar: SeekBar) { - ConfigData.notifyIconCornerSize = seekBar.progress - SystemUITool.refreshSystemUI(context = this@MainActivity) - } - - override fun onStartTrackingTouch(seekBar: SeekBar?) {} - }) + binding.notifyIconCustomCornerSeekbar.bind(ConfigData.NOTIFY_ICON_CORNER_SIZE, binding.notifyIconCustomCornerText, suffix = " dp") { + SystemUITool.refreshSystemUI(context = this) + } /** MIUI 通知显示设置按钮点击事件 */ binding.miuiNotifyStyleButton.setOnClickListener { SystemUITool.openMiuiNotificationDisplaySettings(context = this) } /** 通知图标优化名单按钮点击事件 */