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.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()
}
}
}

View File

@ -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()
}
}

View File

@ -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 "暂停规则匹配"

View File

@ -144,7 +144,7 @@ fun useControlPage(): ScaffoldExt {
SettingItem(
title = "触发记录",
subtitle = "如误触可在此快速定位关闭规则",
subtitle = "如误触可定位关闭规则",
onClick = {
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.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, "")
}

View File

@ -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 <reified T> createStorageFlow(
private inline fun <reified T> createJsonFlow(
key: String,
crossinline init: () -> T,
crossinline default: () -> T,
crossinline transform: (T) -> T = { it }
): MutableStateFlow<T> {
val str = kv.getString(key, null)
val initValue = if (str != null) {
@ -28,7 +28,7 @@ private inline fun <reified T> 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 <reified T> createStorageFlow(
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
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<Long> {
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
}

View File

@ -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 {