perf: update store
Some checks failed
Build-Apk / build (push) Has been cancelled

This commit is contained in:
lisonge 2024-09-20 23:55:12 +08:00
parent 90af46cf15
commit 97857f505b
7 changed files with 62 additions and 45 deletions

View File

@ -21,6 +21,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import li.songe.gkd.data.selfAppInfo import li.songe.gkd.data.selfAppInfo
import li.songe.gkd.debug.clearHttpSubs import li.songe.gkd.debug.clearHttpSubs
import li.songe.gkd.notif.initChannel import li.songe.gkd.notif.initChannel
import li.songe.gkd.permission.updatePermissionState
import li.songe.gkd.service.GkdAbService import li.songe.gkd.service.GkdAbService
import li.songe.gkd.util.SafeR import li.songe.gkd.util.SafeR
import li.songe.gkd.util.initAppState import li.songe.gkd.util.initAppState
@ -135,6 +136,7 @@ class App : Application() {
initSubsState() initSubsState()
initChannel() initChannel()
clearHttpSubs() clearHttpSubs()
updatePermissionState()
} }
} }
} }

View File

@ -17,10 +17,9 @@ import li.songe.gkd.data.SubsConfig
import li.songe.gkd.db.DbSet import li.songe.gkd.db.DbSet
import li.songe.gkd.isActivityVisible import li.songe.gkd.isActivityVisible
import li.songe.gkd.util.RuleSummary import li.songe.gkd.util.RuleSummary
import li.songe.gkd.util.actionCountFlow
import li.songe.gkd.util.getDefaultLauncherAppId import li.songe.gkd.util.getDefaultLauncherAppId
import li.songe.gkd.util.increaseClickCount
import li.songe.gkd.util.launchTry import li.songe.gkd.util.launchTry
import li.songe.gkd.util.recordStoreFlow
import li.songe.gkd.util.ruleSummaryFlow import li.songe.gkd.util.ruleSummaryFlow
import li.songe.gkd.util.storeFlow import li.songe.gkd.util.storeFlow
@ -173,7 +172,7 @@ fun updateLauncherAppId() {
val clickLogMutex by lazy { Mutex() } val clickLogMutex by lazy { Mutex() }
suspend fun insertClickLog(rule: ResolvedRule) { suspend fun insertClickLog(rule: ResolvedRule) {
clickLogMutex.withLock { clickLogMutex.withLock {
increaseClickCount() actionCountFlow.value++
val clickLog = ClickLog( val clickLog = ClickLog(
appId = topActivityFlow.value.appId, appId = topActivityFlow.value.appId,
activityId = topActivityFlow.value.activityId, activityId = topActivityFlow.value.activityId,
@ -188,7 +187,7 @@ suspend fun insertClickLog(rule: ResolvedRule) {
ruleKey = rule.key, ruleKey = rule.key,
) )
DbSet.clickLogDao.insert(clickLog) DbSet.clickLogDao.insert(clickLog)
if (recordStoreFlow.value.clickCount % 100 == 0) { if (actionCountFlow.value % 100 == 0L) {
DbSet.clickLogDao.deleteKeepLatest() DbSet.clickLogDao.deleteKeepLatest()
} }
} }

View File

@ -16,7 +16,7 @@ import li.songe.gkd.composition.CompositionService
import li.songe.gkd.notif.abNotif import li.songe.gkd.notif.abNotif
import li.songe.gkd.notif.createNotif import li.songe.gkd.notif.createNotif
import li.songe.gkd.notif.defaultChannel 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.getSubsStatus
import li.songe.gkd.util.ruleSummaryFlow import li.songe.gkd.util.ruleSummaryFlow
import li.songe.gkd.util.storeFlow import li.songe.gkd.util.storeFlow
@ -31,7 +31,7 @@ class ManageService : CompositionService({
GkdAbService.isRunning, GkdAbService.isRunning,
storeFlow, storeFlow,
ruleSummaryFlow, ruleSummaryFlow,
clickCountFlow, actionCountFlow,
) { abRunning, store, ruleSummary, count -> ) { abRunning, store, ruleSummary, count ->
if (!abRunning) return@combine "无障碍未授权" if (!abRunning) return@combine "无障碍未授权"
if (!store.enableMatch) return@combine "暂停规则匹配" if (!store.enableMatch) return@combine "暂停规则匹配"

View File

@ -144,7 +144,7 @@ fun useControlPage(): ScaffoldExt {
SettingItem( SettingItem(
title = "触发记录", title = "触发记录",
subtitle = "如误触可在此快速定位关闭规则", subtitle = "如误触可定位关闭规则",
onClick = { onClick = {
navController.toDestinationsNavigator().navigate(ClickLogPageDestination) navController.toDestinationsNavigator().navigate(ClickLogPageDestination)
} }

View File

@ -18,8 +18,8 @@ import li.songe.gkd.data.SubsItem
import li.songe.gkd.db.DbSet import li.songe.gkd.db.DbSet
import li.songe.gkd.ui.component.InputSubsLinkOption import li.songe.gkd.ui.component.InputSubsLinkOption
import li.songe.gkd.util.SortTypeOption import li.songe.gkd.util.SortTypeOption
import li.songe.gkd.util.actionCountFlow
import li.songe.gkd.util.appInfoCacheFlow import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.clickCountFlow
import li.songe.gkd.util.client import li.songe.gkd.util.client
import li.songe.gkd.util.getSubsStatus import li.songe.gkd.util.getSubsStatus
import li.songe.gkd.util.launchTry import li.songe.gkd.util.launchTry
@ -59,7 +59,7 @@ class HomeVm : ViewModel() {
}.stateIn(viewModelScope, SharingStarted.Eagerly, null) }.stateIn(viewModelScope, SharingStarted.Eagerly, null)
val subsStatusFlow by lazy { val subsStatusFlow by lazy {
combine(ruleSummaryFlow, clickCountFlow) { ruleSummary, count -> combine(ruleSummaryFlow, actionCountFlow) { ruleSummary, count ->
getSubsStatus(ruleSummary, count) getSubsStatus(ruleSummary, count)
}.stateIn(appScope, SharingStarted.Eagerly, "") }.stateIn(appScope, SharingStarted.Eagerly, "")
} }

View File

@ -4,7 +4,6 @@ import com.blankj.utilcode.util.LogUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -12,9 +11,10 @@ import kotlinx.serialization.encodeToString
import li.songe.gkd.META import li.songe.gkd.META
import li.songe.gkd.appScope import li.songe.gkd.appScope
private inline fun <reified T> createStorageFlow( private inline fun <reified T> createJsonFlow(
key: String, key: String,
crossinline init: () -> T, crossinline default: () -> T,
crossinline transform: (T) -> T = { it }
): MutableStateFlow<T> { ): MutableStateFlow<T> {
val str = kv.getString(key, null) val str = kv.getString(key, null)
val initValue = if (str != null) { val initValue = if (str != null) {
@ -28,7 +28,7 @@ private inline fun <reified T> createStorageFlow(
} else { } else {
null null
} }
val stateFlow = MutableStateFlow(initValue ?: init()) val stateFlow = MutableStateFlow(transform(initValue ?: default()))
appScope.launch { appScope.launch {
stateFlow.drop(1).collect { stateFlow.drop(1).collect {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
@ -39,6 +39,20 @@ private inline fun <reified T> createStorageFlow(
return stateFlow return stateFlow
} }
private fun createLongFlow(
key: String,
default: Long = 0,
transform: (Long) -> Long = { it }
): MutableStateFlow<Long> {
val stateFlow = MutableStateFlow(transform(kv.getLong(key, default)))
appScope.launch {
stateFlow.drop(1).collect {
withContext(Dispatchers.IO) { kv.encode(key, it) }
}
}
return stateFlow
}
@Serializable @Serializable
data class Store( data class Store(
val enableService: Boolean = true, val enableService: Boolean = true,
@ -72,36 +86,46 @@ data class Store(
) )
val storeFlow by lazy { val storeFlow by lazy {
createStorageFlow("store-v2") { Store() }.apply { createJsonFlow(
if (UpdateTimeOption.allSubObject.all { it.value != value.updateSubsInterval }) { key = "store-v2",
update { default = { Store() },
transform = {
if (UpdateTimeOption.allSubObject.all { e -> e.value != it.updateSubsInterval }) {
it.copy( it.copy(
updateSubsInterval = UpdateTimeOption.Everyday.value updateSubsInterval = UpdateTimeOption.Everyday.value
) )
} else {
it
} }
} }
} )
} }
//@Deprecated("use actionCountFlow instead")
@Serializable @Serializable
data class RecordStore( private data class RecordStore(
val clickCount: Int = 0, val clickCount: Int = 0,
) )
val recordStoreFlow by lazy { //@Deprecated("use actionCountFlow instead")
createStorageFlow("record_store-v2") { RecordStore() } private val recordStoreFlow by lazy {
createJsonFlow(
key = "record_store-v2",
default = { RecordStore() }
)
} }
val clickCountFlow by lazy { val actionCountFlow by lazy {
recordStoreFlow.map(appScope) { r -> r.clickCount } createLongFlow(
} key = "action_count",
transform = {
fun increaseClickCount(n: Int = 1) { if (it == 0L) {
recordStoreFlow.update { recordStoreFlow.value.clickCount.toLong()
it.copy( } else {
clickCount = it.clickCount + n it
) }
} }
)
} }
@Serializable @Serializable
@ -110,25 +134,17 @@ data class PrivacyStore(
) )
val privacyStoreFlow by lazy { val privacyStoreFlow by lazy {
createStorageFlow("privacy_store") { PrivacyStore() } createJsonFlow(
} key = "privacy_store",
default = { PrivacyStore() }
private fun createLongFlow(key: String, defaultValue: Long): MutableStateFlow<Long> { )
val stateFlow = MutableStateFlow(kv.getLong(key, defaultValue))
appScope.launch {
stateFlow.drop(1).collect {
withContext(Dispatchers.IO) { kv.encode(key, it) }
}
}
return stateFlow
} }
val lastRestartA11yServiceTimeFlow by lazy { val lastRestartA11yServiceTimeFlow by lazy {
createLongFlow("last_restart_a11y_service_time", 0) createLongFlow("last_restart_a11y_service_time")
} }
fun initStore() { fun initStore() {
storeFlow.value storeFlow.value
recordStoreFlow.value actionCountFlow.value
} }

View File

@ -286,7 +286,7 @@ val ruleSummaryFlow by lazy {
}.flowOn(Dispatchers.Default).stateIn(appScope, SharingStarted.Eagerly, RuleSummary()) }.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) { return if (count > 0) {
"${ruleSummary.numText}/${count}触发" "${ruleSummary.numText}/${count}触发"
} else { } else {