diff --git a/app/src/main/kotlin/li/songe/gkd/data/AppInfo.kt b/app/src/main/kotlin/li/songe/gkd/data/AppInfo.kt index 4d3601e..c62d546 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/AppInfo.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/AppInfo.kt @@ -21,6 +21,9 @@ data class AppInfo( val hidden: Boolean, ) +/** + * 平均单次调用时间 11ms + */ fun PackageInfo.toAppInfo(): AppInfo? { applicationInfo ?: return null return AppInfo( diff --git a/app/src/main/kotlin/li/songe/gkd/util/AppInfoState.kt b/app/src/main/kotlin/li/songe/gkd/util/AppInfoState.kt index b9b4580..f1589d4 100644 --- a/app/src/main/kotlin/li/songe/gkd/util/AppInfoState.kt +++ b/app/src/main/kotlin/li/songe/gkd/util/AppInfoState.kt @@ -6,6 +6,8 @@ import android.content.Intent import android.content.IntentFilter import android.content.pm.PackageManager import android.os.Build +import com.blankj.utilcode.util.LogUtils +import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableMap import kotlinx.coroutines.Dispatchers @@ -18,17 +20,22 @@ import li.songe.gkd.appScope import li.songe.gkd.data.AppInfo import li.songe.gkd.data.toAppInfo -val appInfoCacheFlow = MutableStateFlow(emptyMap().toImmutableMap()) +val appInfoCacheFlow = MutableStateFlow(persistentMapOf().toImmutableMap()) -val systemAppInfoCacheFlow = - appInfoCacheFlow.map(appScope) { c -> c.filter { a -> a.value.isSystem }.toImmutableMap() } +val systemAppInfoCacheFlow by lazy { + appInfoCacheFlow.map(appScope) { c -> + c.filter { a -> a.value.isSystem }.toImmutableMap() + } +} -val systemAppsFlow = systemAppInfoCacheFlow.map(appScope) { c -> c.keys } +val systemAppsFlow by lazy { systemAppInfoCacheFlow.map(appScope) { c -> c.keys } } -val orderedAppInfosFlow = appInfoCacheFlow.map(appScope) { c -> - c.values.sortedWith { a, b -> - collator.compare(a.name, b.name) - }.toImmutableList() +val orderedAppInfosFlow by lazy { + appInfoCacheFlow.map(appScope) { c -> + c.values.sortedWith { a, b -> + collator.compare(a.name, b.name) + }.toImmutableList() + } } private val packageReceiver by lazy { @@ -94,6 +101,7 @@ val appRefreshingFlow = MutableStateFlow(false) suspend fun initOrResetAppInfoCache() { if (updateAppMutex.isLocked) return + LogUtils.d("initOrResetAppInfoCache start") appRefreshingFlow.value = true updateAppMutex.withLock { val oldAppIds = appInfoCacheFlow.value.keys @@ -111,6 +119,7 @@ suspend fun initOrResetAppInfoCache() { appInfoCacheFlow.value = appMap.toImmutableMap() } appRefreshingFlow.value = false + LogUtils.d("initOrResetAppInfoCache end") } fun initAppState() {