fix: reset appInfoCache (#543)

This commit is contained in:
lisonge 2024-03-09 02:06:06 +08:00
parent fbf32d7859
commit 951e3971c9
2 changed files with 24 additions and 10 deletions

View File

@ -9,9 +9,11 @@ import li.songe.gkd.data.RawSubscription
import li.songe.gkd.data.SubsItem import li.songe.gkd.data.SubsItem
import li.songe.gkd.db.DbSet import li.songe.gkd.db.DbSet
import li.songe.gkd.service.updateLauncherAppId import li.songe.gkd.service.updateLauncherAppId
import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.authActionFlow import li.songe.gkd.util.authActionFlow
import li.songe.gkd.util.checkUpdate import li.songe.gkd.util.checkUpdate
import li.songe.gkd.util.initFolder import li.songe.gkd.util.initFolder
import li.songe.gkd.util.initOrResetAppInfoCache
import li.songe.gkd.util.launchTry import li.songe.gkd.util.launchTry
import li.songe.gkd.util.logZipDir import li.songe.gkd.util.logZipDir
import li.songe.gkd.util.newVersionApkDir import li.songe.gkd.util.newVersionApkDir
@ -28,6 +30,13 @@ class MainViewModel : ViewModel() {
// 每次打开页面更新记录桌面 appId // 每次打开页面更新记录桌面 appId
updateLauncherAppId() updateLauncherAppId()
// https://github.com/gkd-kit/gkd/issues/543
viewModelScope.launchTry(Dispatchers.IO) {
if (appInfoCacheFlow.value.size < 16) {
initOrResetAppInfoCache()
}
}
val localSubsItem = SubsItem( val localSubsItem = SubsItem(
id = -2, order = -2, mtime = System.currentTimeMillis() id = -2, order = -2, mtime = System.currentTimeMillis()
) )

View File

@ -92,18 +92,23 @@ fun updateAppInfo(appIds: List<String>) {
} }
} }
suspend fun initOrResetAppInfoCache() {
if (updateAppMutex.isLocked) return
updateAppMutex.withLock {
val appMap = mutableMapOf<String, AppInfo>()
app.packageManager.getInstalledPackages(0).forEach { packageInfo ->
val info = packageInfo.toAppInfo()
if (info != null) {
appMap[packageInfo.packageName] = info
}
}
appInfoCacheFlow.value = appMap.toImmutableMap()
}
}
fun initAppState() { fun initAppState() {
packageReceiver packageReceiver
appScope.launchTry(Dispatchers.IO) { appScope.launchTry(Dispatchers.IO) {
updateAppMutex.withLock { initOrResetAppInfoCache()
val appMap = mutableMapOf<String, AppInfo>()
app.packageManager.getInstalledPackages(0).forEach { packageInfo ->
val info = packageInfo.toAppInfo()
if (info != null) {
appMap[packageInfo.packageName] = info
}
}
appInfoCacheFlow.value = appMap.toImmutableMap()
}
} }
} }