From 97857f505bc4098554835da5d632f9fecde7056a Mon Sep 17 00:00:00 2001 From: lisonge Date: Fri, 20 Sep 2024 23:55:12 +0800 Subject: [PATCH] perf: update store --- app/src/main/kotlin/li/songe/gkd/App.kt | 2 + .../kotlin/li/songe/gkd/service/AbState.kt | 7 +- .../li/songe/gkd/service/ManageService.kt | 4 +- .../li/songe/gkd/ui/home/ControlPage.kt | 2 +- .../kotlin/li/songe/gkd/ui/home/HomeVm.kt | 4 +- .../main/kotlin/li/songe/gkd/util/Store.kt | 86 +++++++++++-------- .../kotlin/li/songe/gkd/util/SubsState.kt | 2 +- 7 files changed, 62 insertions(+), 45 deletions(-) diff --git a/app/src/main/kotlin/li/songe/gkd/App.kt b/app/src/main/kotlin/li/songe/gkd/App.kt index eb8519c..352d7a5 100644 --- a/app/src/main/kotlin/li/songe/gkd/App.kt +++ b/app/src/main/kotlin/li/songe/gkd/App.kt @@ -21,6 +21,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import li.songe.gkd.data.selfAppInfo import li.songe.gkd.debug.clearHttpSubs import li.songe.gkd.notif.initChannel +import li.songe.gkd.permission.updatePermissionState import li.songe.gkd.service.GkdAbService import li.songe.gkd.util.SafeR import li.songe.gkd.util.initAppState @@ -135,6 +136,7 @@ class App : Application() { initSubsState() initChannel() clearHttpSubs() + updatePermissionState() } } } diff --git a/app/src/main/kotlin/li/songe/gkd/service/AbState.kt b/app/src/main/kotlin/li/songe/gkd/service/AbState.kt index 0889c1b..5d156a0 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/AbState.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/AbState.kt @@ -17,10 +17,9 @@ import li.songe.gkd.data.SubsConfig import li.songe.gkd.db.DbSet import li.songe.gkd.isActivityVisible import li.songe.gkd.util.RuleSummary +import li.songe.gkd.util.actionCountFlow import li.songe.gkd.util.getDefaultLauncherAppId -import li.songe.gkd.util.increaseClickCount import li.songe.gkd.util.launchTry -import li.songe.gkd.util.recordStoreFlow import li.songe.gkd.util.ruleSummaryFlow import li.songe.gkd.util.storeFlow @@ -173,7 +172,7 @@ fun updateLauncherAppId() { val clickLogMutex by lazy { Mutex() } suspend fun insertClickLog(rule: ResolvedRule) { clickLogMutex.withLock { - increaseClickCount() + actionCountFlow.value++ val clickLog = ClickLog( appId = topActivityFlow.value.appId, activityId = topActivityFlow.value.activityId, @@ -188,7 +187,7 @@ suspend fun insertClickLog(rule: ResolvedRule) { ruleKey = rule.key, ) DbSet.clickLogDao.insert(clickLog) - if (recordStoreFlow.value.clickCount % 100 == 0) { + if (actionCountFlow.value % 100 == 0L) { DbSet.clickLogDao.deleteKeepLatest() } } diff --git a/app/src/main/kotlin/li/songe/gkd/service/ManageService.kt b/app/src/main/kotlin/li/songe/gkd/service/ManageService.kt index 5390b57..e574e84 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/ManageService.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/ManageService.kt @@ -16,7 +16,7 @@ import li.songe.gkd.composition.CompositionService import li.songe.gkd.notif.abNotif import li.songe.gkd.notif.createNotif import li.songe.gkd.notif.defaultChannel -import li.songe.gkd.util.clickCountFlow +import li.songe.gkd.util.actionCountFlow import li.songe.gkd.util.getSubsStatus import li.songe.gkd.util.ruleSummaryFlow import li.songe.gkd.util.storeFlow @@ -31,7 +31,7 @@ class ManageService : CompositionService({ GkdAbService.isRunning, storeFlow, ruleSummaryFlow, - clickCountFlow, + actionCountFlow, ) { abRunning, store, ruleSummary, count -> if (!abRunning) return@combine "无障碍未授权" if (!store.enableMatch) return@combine "暂停规则匹配" diff --git a/app/src/main/kotlin/li/songe/gkd/ui/home/ControlPage.kt b/app/src/main/kotlin/li/songe/gkd/ui/home/ControlPage.kt index f0b3792..710baaf 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/home/ControlPage.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/home/ControlPage.kt @@ -144,7 +144,7 @@ fun useControlPage(): ScaffoldExt { SettingItem( title = "触发记录", - subtitle = "如误触可在此快速定位关闭规则", + subtitle = "如误触可定位关闭规则", onClick = { navController.toDestinationsNavigator().navigate(ClickLogPageDestination) } diff --git a/app/src/main/kotlin/li/songe/gkd/ui/home/HomeVm.kt b/app/src/main/kotlin/li/songe/gkd/ui/home/HomeVm.kt index b62e6d8..dbfed3c 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/home/HomeVm.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/home/HomeVm.kt @@ -18,8 +18,8 @@ import li.songe.gkd.data.SubsItem import li.songe.gkd.db.DbSet import li.songe.gkd.ui.component.InputSubsLinkOption import li.songe.gkd.util.SortTypeOption +import li.songe.gkd.util.actionCountFlow import li.songe.gkd.util.appInfoCacheFlow -import li.songe.gkd.util.clickCountFlow import li.songe.gkd.util.client import li.songe.gkd.util.getSubsStatus import li.songe.gkd.util.launchTry @@ -59,7 +59,7 @@ class HomeVm : ViewModel() { }.stateIn(viewModelScope, SharingStarted.Eagerly, null) val subsStatusFlow by lazy { - combine(ruleSummaryFlow, clickCountFlow) { ruleSummary, count -> + combine(ruleSummaryFlow, actionCountFlow) { ruleSummary, count -> getSubsStatus(ruleSummary, count) }.stateIn(appScope, SharingStarted.Eagerly, "") } diff --git a/app/src/main/kotlin/li/songe/gkd/util/Store.kt b/app/src/main/kotlin/li/songe/gkd/util/Store.kt index 9f4fcf0..b37bc0f 100644 --- a/app/src/main/kotlin/li/songe/gkd/util/Store.kt +++ b/app/src/main/kotlin/li/songe/gkd/util/Store.kt @@ -4,7 +4,6 @@ import com.blankj.utilcode.util.LogUtils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.drop -import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlinx.serialization.Serializable @@ -12,9 +11,10 @@ import kotlinx.serialization.encodeToString import li.songe.gkd.META import li.songe.gkd.appScope -private inline fun createStorageFlow( +private inline fun createJsonFlow( key: String, - crossinline init: () -> T, + crossinline default: () -> T, + crossinline transform: (T) -> T = { it } ): MutableStateFlow { val str = kv.getString(key, null) val initValue = if (str != null) { @@ -28,7 +28,7 @@ private inline fun createStorageFlow( } else { null } - val stateFlow = MutableStateFlow(initValue ?: init()) + val stateFlow = MutableStateFlow(transform(initValue ?: default())) appScope.launch { stateFlow.drop(1).collect { withContext(Dispatchers.IO) { @@ -39,6 +39,20 @@ private inline fun createStorageFlow( return stateFlow } +private fun createLongFlow( + key: String, + default: Long = 0, + transform: (Long) -> Long = { it } +): MutableStateFlow { + val stateFlow = MutableStateFlow(transform(kv.getLong(key, default))) + appScope.launch { + stateFlow.drop(1).collect { + withContext(Dispatchers.IO) { kv.encode(key, it) } + } + } + return stateFlow +} + @Serializable data class Store( val enableService: Boolean = true, @@ -72,36 +86,46 @@ data class Store( ) val storeFlow by lazy { - createStorageFlow("store-v2") { Store() }.apply { - if (UpdateTimeOption.allSubObject.all { it.value != value.updateSubsInterval }) { - update { + createJsonFlow( + key = "store-v2", + default = { Store() }, + transform = { + if (UpdateTimeOption.allSubObject.all { e -> e.value != it.updateSubsInterval }) { it.copy( updateSubsInterval = UpdateTimeOption.Everyday.value ) + } else { + it } } - } + ) } +//@Deprecated("use actionCountFlow instead") @Serializable -data class RecordStore( +private data class RecordStore( val clickCount: Int = 0, ) -val recordStoreFlow by lazy { - createStorageFlow("record_store-v2") { RecordStore() } +//@Deprecated("use actionCountFlow instead") +private val recordStoreFlow by lazy { + createJsonFlow( + key = "record_store-v2", + default = { RecordStore() } + ) } -val clickCountFlow by lazy { - recordStoreFlow.map(appScope) { r -> r.clickCount } -} - -fun increaseClickCount(n: Int = 1) { - recordStoreFlow.update { - it.copy( - clickCount = it.clickCount + n - ) - } +val actionCountFlow by lazy { + createLongFlow( + key = "action_count", + transform = { + if (it == 0L) { + recordStoreFlow.value.clickCount.toLong() + } else { + it + } + } + ) } @Serializable @@ -110,25 +134,17 @@ data class PrivacyStore( ) val privacyStoreFlow by lazy { - createStorageFlow("privacy_store") { PrivacyStore() } -} - -private fun createLongFlow(key: String, defaultValue: Long): MutableStateFlow { - val stateFlow = MutableStateFlow(kv.getLong(key, defaultValue)) - appScope.launch { - stateFlow.drop(1).collect { - withContext(Dispatchers.IO) { kv.encode(key, it) } - } - } - return stateFlow + createJsonFlow( + key = "privacy_store", + default = { PrivacyStore() } + ) } val lastRestartA11yServiceTimeFlow by lazy { - createLongFlow("last_restart_a11y_service_time", 0) + createLongFlow("last_restart_a11y_service_time") } fun initStore() { storeFlow.value - recordStoreFlow.value + actionCountFlow.value } - diff --git a/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt b/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt index efdc4cf..c710e21 100644 --- a/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt +++ b/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt @@ -286,7 +286,7 @@ val ruleSummaryFlow by lazy { }.flowOn(Dispatchers.Default).stateIn(appScope, SharingStarted.Eagerly, RuleSummary()) } -fun getSubsStatus(ruleSummary: RuleSummary, count: Int): String { +fun getSubsStatus(ruleSummary: RuleSummary, count: Long): String { return if (count > 0) { "${ruleSummary.numText}/${count}触发" } else {