From dd206f968cb245cfffca1443cfb6f6080f8dfd86 Mon Sep 17 00:00:00 2001 From: lisonge Date: Fri, 27 Oct 2023 11:46:46 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=BF=AB=E7=85=A7=E6=B7=BB=E5=8A=A0=20?= =?UTF-8?q?githubAssetId=20=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/li/songe/gkd/data/BaseSnapshot.kt | 1 + .../java/li/songe/gkd/data/ComplexSnapshot.kt | 3 ++ .../main/java/li/songe/gkd/data/Snapshot.kt | 2 ++ app/src/main/java/li/songe/gkd/db/AppDb.kt | 4 ++- .../java/li/songe/gkd/service/GkdAbService.kt | 2 +- .../main/java/li/songe/gkd/ui/SnapshotPage.kt | 34 +++---------------- .../main/java/li/songe/gkd/ui/SnapshotVm.kt | 10 +----- app/src/main/java/li/songe/gkd/util/Store.kt | 1 - .../main/java/li/songe/gkd/util/SubsState.kt | 4 +-- 9 files changed, 18 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/li/songe/gkd/data/BaseSnapshot.kt b/app/src/main/java/li/songe/gkd/data/BaseSnapshot.kt index 5f0afe4..df1cf94 100644 --- a/app/src/main/java/li/songe/gkd/data/BaseSnapshot.kt +++ b/app/src/main/java/li/songe/gkd/data/BaseSnapshot.kt @@ -12,4 +12,5 @@ interface BaseSnapshot { val screenHeight: Int val screenWidth: Int val isLandscape: Boolean + } \ No newline at end of file diff --git a/app/src/main/java/li/songe/gkd/data/ComplexSnapshot.kt b/app/src/main/java/li/songe/gkd/data/ComplexSnapshot.kt index 58723fd..2efb283 100644 --- a/app/src/main/java/li/songe/gkd/data/ComplexSnapshot.kt +++ b/app/src/main/java/li/songe/gkd/data/ComplexSnapshot.kt @@ -3,6 +3,7 @@ package li.songe.gkd.data import com.blankj.utilcode.util.AppUtils import com.blankj.utilcode.util.ScreenUtils import kotlinx.serialization.Serializable +import li.songe.gkd.BuildConfig import li.songe.gkd.service.GkdAbService import li.songe.gkd.service.safeActiveWindow import li.songe.gkd.service.topActivityFlow @@ -21,6 +22,8 @@ data class ComplexSnapshot( override val screenWidth: Int, override val isLandscape: Boolean, + val gkdVersionCode: Int = BuildConfig.VERSION_CODE, + val device: DeviceInfo, val nodes: List, ) : BaseSnapshot diff --git a/app/src/main/java/li/songe/gkd/data/Snapshot.kt b/app/src/main/java/li/songe/gkd/data/Snapshot.kt index 3db905d..ba4262f 100644 --- a/app/src/main/java/li/songe/gkd/data/Snapshot.kt +++ b/app/src/main/java/li/songe/gkd/data/Snapshot.kt @@ -31,6 +31,8 @@ data class Snapshot( @ColumnInfo(name = "screen_width") override val screenWidth: Int, @ColumnInfo(name = "is_landscape") override val isLandscape: Boolean, + @ColumnInfo(name = "github_asset_id") val githubAssetId: Int? = null, + ) : BaseSnapshot { val screenshotFile by lazy { diff --git a/app/src/main/java/li/songe/gkd/db/AppDb.kt b/app/src/main/java/li/songe/gkd/db/AppDb.kt index 3ef1889..d17be29 100644 --- a/app/src/main/java/li/songe/gkd/db/AppDb.kt +++ b/app/src/main/java/li/songe/gkd/db/AppDb.kt @@ -1,5 +1,6 @@ package li.songe.gkd.db +import androidx.room.AutoMigration import androidx.room.Database import androidx.room.RoomDatabase import li.songe.gkd.data.ClickLog @@ -8,8 +9,9 @@ import li.songe.gkd.data.SubsConfig import li.songe.gkd.data.SubsItem @Database( - version = 1, + version = 2, entities = [SubsItem::class, Snapshot::class, SubsConfig::class, ClickLog::class], + autoMigrations = [AutoMigration(from = 1, to = 2)] ) abstract class AppDb : RoomDatabase() { abstract fun subsItemDao(): SubsItem.SubsItemDao diff --git a/app/src/main/java/li/songe/gkd/service/GkdAbService.kt b/app/src/main/java/li/songe/gkd/service/GkdAbService.kt index 096a2a8..7ae0fb3 100644 --- a/app/src/main/java/li/songe/gkd/service/GkdAbService.kt +++ b/app/src/main/java/li/songe/gkd/service/GkdAbService.kt @@ -116,7 +116,7 @@ class GkdAbService : CompositionAbService({ lastTriggerShizukuTime = if (newActivityId.startsWith("android.view.") || newActivityId.startsWith("android.widget.")) { val t = System.currentTimeMillis() - if (t - lastTriggerShizukuTime < 100) { + if (t - lastTriggerShizukuTime < if (currentRulesFlow.value.isNotEmpty()) 100 else 200) { return@onAccessibilityEvent } t diff --git a/app/src/main/java/li/songe/gkd/ui/SnapshotPage.kt b/app/src/main/java/li/songe/gkd/ui/SnapshotPage.kt index 6ae943c..9ea4db2 100644 --- a/app/src/main/java/li/songe/gkd/ui/SnapshotPage.kt +++ b/app/src/main/java/li/songe/gkd/ui/SnapshotPage.kt @@ -71,9 +71,7 @@ import li.songe.gkd.util.ProfileTransitions import li.songe.gkd.util.format import li.songe.gkd.util.launchAsFn import li.songe.gkd.util.navigate -import li.songe.gkd.util.recordStoreFlow import li.songe.gkd.util.snapshotZipDir -import li.songe.gkd.util.updateStorage import java.io.File @RootNavGraph @@ -91,7 +89,6 @@ fun SnapshotPage() { val vm = hiltViewModel() val snapshots by vm.snapshotsState.collectAsState() val uploadStatus by vm.uploadStatusFlow.collectAsState() - val recordStore by recordStoreFlow.collectAsState() var selectedSnapshot by remember { mutableStateOf(null) @@ -217,14 +214,12 @@ fun SnapshotPage() { .then(modifier) ) Divider() - if (recordStore.snapshotIdMap.containsKey(snapshotVal.id)) { - val url = - "https://i.gkd.li/import/" + recordStore.snapshotIdMap[snapshotVal.id] + if (snapshotVal.githubAssetId != null) { Text( text = "复制链接", modifier = Modifier .clickable(onClick = { selectedSnapshot = null - ClipboardUtils.copyText(url) + ClipboardUtils.copyText(IMPORT_BASE_URL + snapshotVal.githubAssetId) ToastUtils.showShort("复制成功") }) .then(modifier) @@ -278,15 +273,9 @@ fun SnapshotPage() { File(snapshotZipDir, "${snapshotVal.id}.zip").apply { if (exists()) delete() } - if (recordStore.snapshotIdMap.containsKey(snapshotVal.id)) { - updateStorage( - recordStoreFlow, - recordStore.copy(snapshotIdMap = recordStore.snapshotIdMap - .toMutableMap() - .apply { - remove(snapshotVal.id) - }) - ) + if (snapshotVal.githubAssetId != null) { + // 当本地快照变更时, 移除快照链接 + DbSet.snapshotDao.update(snapshotVal.copy(githubAssetId = null)) } } else { ToastUtils.showShort("截图尺寸不一致,无法替换") @@ -305,16 +294,6 @@ fun SnapshotPage() { DbSet.snapshotDao.delete(snapshotVal) withContext(Dispatchers.IO) { SnapshotExt.removeAssets(snapshotVal.id) - if (recordStore.snapshotIdMap.containsKey(snapshotVal.id)) { - updateStorage( - recordStoreFlow, - recordStore.copy(snapshotIdMap = recordStore.snapshotIdMap - .toMutableMap() - .apply { - remove(snapshotVal.id) - }) - ) - } } selectedSnapshot = null }) @@ -397,9 +376,6 @@ fun SnapshotPage() { SnapshotExt.removeAssets(s.id) } DbSet.snapshotDao.deleteAll() - updateStorage( - recordStoreFlow, recordStoreFlow.value.copy(snapshotIdMap = emptyMap()) - ) }, ) { Text(text = "是", color = MaterialTheme.colorScheme.error) diff --git a/app/src/main/java/li/songe/gkd/ui/SnapshotVm.kt b/app/src/main/java/li/songe/gkd/ui/SnapshotVm.kt index b0b59ea..d281af4 100644 --- a/app/src/main/java/li/songe/gkd/ui/SnapshotVm.kt +++ b/app/src/main/java/li/songe/gkd/ui/SnapshotVm.kt @@ -25,8 +25,6 @@ import li.songe.gkd.util.FILE_UPLOAD_URL import li.songe.gkd.util.LoadStatus import li.songe.gkd.util.Singleton import li.songe.gkd.util.launchTry -import li.songe.gkd.util.recordStoreFlow -import li.songe.gkd.util.updateStorage import javax.inject.Inject @@ -60,13 +58,7 @@ class SnapshotVm @Inject constructor() : ViewModel() { if (response.headers["X_RPC_OK"] == "true") { val policiesAsset = response.body() uploadStatusFlow.value = LoadStatus.Success(policiesAsset) - updateStorage( - recordStoreFlow, - recordStoreFlow.value.copy(snapshotIdMap = recordStoreFlow.value.snapshotIdMap.toMutableMap() - .apply { - set(snapshot.id, policiesAsset.id) - }) - ) + DbSet.snapshotDao.update(snapshot.copy(githubAssetId = policiesAsset.id)) } else if (response.headers["X_RPC_OK"] == "false") { uploadStatusFlow.value = LoadStatus.Failure(response.body()) } else { diff --git a/app/src/main/java/li/songe/gkd/util/Store.kt b/app/src/main/java/li/songe/gkd/util/Store.kt index 84481b1..fe8069c 100644 --- a/app/src/main/java/li/songe/gkd/util/Store.kt +++ b/app/src/main/java/li/songe/gkd/util/Store.kt @@ -103,7 +103,6 @@ val storeFlow by lazy { @Serializable data class RecordStore( val clickCount: Int = 0, - val snapshotIdMap: Map = emptyMap(), ) val recordStoreFlow by lazy { diff --git a/app/src/main/java/li/songe/gkd/util/SubsState.kt b/app/src/main/java/li/songe/gkd/util/SubsState.kt index 59747a6..6733d56 100644 --- a/app/src/main/java/li/songe/gkd/util/SubsState.kt +++ b/app/src/main/java/li/songe/gkd/util/SubsState.kt @@ -114,7 +114,7 @@ val appIdToRulesFlow by lazy { actionDelay = actionDelay, index = ruleIndex, matches = ruleRaw.matches.map { Selector.parse(it) }, - excludeMatches = ruleRaw.excludeMatches.map { + excludeMatches = (ruleRaw.excludeMatches ?: emptyList()).map { Selector.parse( it ) @@ -126,7 +126,7 @@ val appIdToRulesFlow by lazy { activityIds = activityIds, excludeActivityIds = excludeActivityIds, key = ruleRaw.key, - preKeys = ruleRaw.preKeys.toSet(), + preKeys = (ruleRaw.preKeys ?: emptyList()).toSet(), rule = ruleRaw, group = groupRaw, app = appRaw,