@@ -17,100 +17,82 @@ package net.ankio.auto.ui.fragment.components
1717
1818import androidx.annotation.ColorInt
1919import androidx.annotation.DrawableRes
20- import net.ankio.auto.BuildConfig
20+ import androidx.lifecycle.findViewTreeLifecycleOwner
2121import net.ankio.auto.R
22- import net.ankio.auto.constant.WorkMode
2322import net.ankio.auto.databinding.CardStatusBinding
24- import net.ankio.auto.service.CoreService
25- import net.ankio.auto.service.OcrService
2623import net.ankio.auto.ui.api.BaseComponent
2724import net.ankio.auto.ui.theme.DynamicColors
28- import net.ankio.auto.update.AppUpdateHelper
29- import net.ankio.auto.update.RuleUpdateHelper
3025import net.ankio.auto.ui.utils.toDrawable
26+ import net.ankio.auto.ui.vm.HomeActivityVm
27+ import net.ankio.auto.ui.vm.StatusData
3128import net.ankio.auto.utils.PrefManager
32- import net.ankio.auto.xposed.XposedModule
3329import java.util.Locale
3430
31+ /* *
32+ * 状态卡组件,展示工作状态、规则/应用版本,点击检查更新。
33+ * 需通过 [setVm] 注入 [HomeActivityVm],UI 数据与点击逻辑均走 VM。
34+ */
3535class StatusCardComponent (binding : CardStatusBinding ) :
3636 BaseComponent <CardStatusBinding >(binding) {
3737
38+ private var vm: HomeActivityVm ? = null
39+
40+ /* * 注入 ViewModel,必须在 [onComponentCreate] 之后由宿主调用 */
41+ fun setVm (vm : HomeActivityVm ) {
42+ this .vm = vm
43+ setupVm()
44+ }
45+
3846 override fun onComponentCreate () {
3947 super .onComponentCreate()
48+ binding.cardContentRule.setBackgroundColor(DynamicColors .SurfaceColor3 )
49+ binding.cardContentApp.setBackgroundColor(DynamicColors .SurfaceColor3 )
4050
41- // 整卡点击:检查规则更新
51+ // 点击:检查更新(走 VM)
4252 binding.cardContentRule.setOnClickListener {
43- launch {
44- RuleUpdateHelper .checkAndShow(context, true ) { updateRuleTexts() }
45- }
53+ vm?.checkRuleUpdate(fromUser = true )
4654 }
47-
4855 binding.cardContentApp.setOnClickListener {
49- launch {
50- AppUpdateHelper .checkAndShow(context, true )
51- }
56+ vm?.checkAppUpdate(fromUser = true )
5257 }
5358
54- // 长按:强制规则更新
59+ // 长按:强制规则更新后检查
5560 binding.cardContentRule.setOnLongClickListener {
5661 PrefManager .ruleVersion = " "
57- launch {
58- RuleUpdateHelper .checkAndShow(context, true ) { updateRuleTexts() }
59- }
62+ vm?.checkRuleUpdate(fromUser = true )
6063 true
6164 }
6265 binding.cardContentApp.setOnLongClickListener {
63- launch {
64- AppUpdateHelper .checkAndShow(context, true )
65- }
66+ vm?.checkAppUpdate(fromUser = true )
6667 true
6768 }
68-
69- binding.cardContentRule.setBackgroundColor(DynamicColors .SurfaceColor3 )
70- binding.cardContentApp.setBackgroundColor(DynamicColors .SurfaceColor3 )
71- // 初始化状态显示
72- updateActiveStatus()
73- updateRuleTexts()
7469 }
7570
7671 override fun onComponentResume () {
7772 super .onComponentResume()
78- updateActiveStatus()
79- updateRuleTexts()
73+ vm?.refreshStatus(context)
8074 }
8175
82- /* *
83- * 当前模式是否处于"工作中"
84- */
85- private fun isCurrentModeActive (): Boolean {
86- return when (PrefManager .workMode) {
87- WorkMode .Ocr -> OcrService .serverStarted
88- WorkMode .LSPatch -> CoreService .isRunning(context)
89- WorkMode .Xposed -> XposedModule .active()
76+ /* * 绑定 VM 后调用:注册观察、首次刷新 */
77+ private fun setupVm () {
78+ val v = vm ? : return
79+ val owner = binding.root.findViewTreeLifecycleOwner() ? : return
80+ v.refreshStatus(context)
81+ v.statusData.observe(owner) { data ->
82+ applyStatusData(data)
9083 }
9184 }
9285
93- /* *
94- * 统一更新激活状态显示(第一行:工作状态 + 模式标签)
95- */
96- private fun updateActiveStatus () {
97- val isActive = isCurrentModeActive()
98- val versionName = BuildConfig .VERSION_NAME
99-
100- // 工作状态文本
101- val statusText = if (isActive) {
102- context.getString(R .string.status_working)
103- } else {
104- context.getString(R .string.status_inactive)
105- }
106- binding.titleText.text = statusText
107-
108- binding.modeText.text = PrefManager .workMode.name.uppercase()
109- // 调试模式时显示小标签,与 mode 标签风格统一
86+ /* * 根据 [StatusData] 更新 UI */
87+ private fun applyStatusData (data : StatusData ) {
88+ binding.titleText.text =
89+ if (data.isActive) context.getString(R .string.status_working)
90+ else context.getString(R .string.status_inactive)
91+ binding.modeText.text = data.workMode.name.uppercase(Locale .getDefault())
11092 binding.debugTag.visibility =
111- if (PrefManager .debugMode) android.view.View .VISIBLE else android.view.View .GONE
93+ if (data .debugMode) android.view.View .VISIBLE else android.view.View .GONE
11294
113- if (isActive) {
95+ if (data. isActive) {
11496 setActive(
11597 backgroundColor = DynamicColors .SecondaryContainer ,
11698 textColor = DynamicColors .OnPrimaryContainer ,
@@ -123,21 +105,15 @@ class StatusCardComponent(binding: CardStatusBinding) :
123105 drawable = R .drawable.home_active_error
124106 )
125107 }
126- binding.subtitleText.text = " v$versionName "
127- val channelValue = PrefManager .appChannel.lowercase(Locale .getDefault())
108+ binding.subtitleText.text = " v${data.versionName} "
109+ binding.ruleVersionText.text = data.ruleVersion
110+ binding.ruleUpdateText.text = data.ruleUpdate
111+
128112 val values = context.resources.getStringArray(R .array.update_channel_values)
129113 val texts = context.resources.getStringArray(R .array.update_channel_texts)
130- val channelIndex = values.indexOf(channelValue).coerceAtLeast(0 )
114+ val channelIndex = values.indexOf(data. channelValue).coerceAtLeast(0 )
131115 binding.channelText.text =
132- texts.getOrElse(channelIndex) { texts.firstOrNull() ? : channelValue }
133- }
134-
135- /* *
136- * 更新第三、四行:规则版本与规则更新时间
137- */
138- private fun updateRuleTexts () {
139- binding.ruleVersionText.text = PrefManager .ruleVersion
140- binding.ruleUpdateText.text = PrefManager .ruleUpdate
116+ texts.getOrElse(channelIndex) { texts.firstOrNull() ? : data.channelValue }
141117 }
142118
143119 private fun setActive (
0 commit comments