Skip to content

Commit

Permalink
feat: 删除插件时也删除插件的Prefs数据
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Oct 25, 2024
1 parent 1c59a2f commit 848057b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.pm.PackageManager
import android.os.Environment
import androidx.compose.runtime.Immutable
import androidx.datastore.preferences.core.edit
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.muedsa.tvbox.plugin.LoadedPlugins
Expand All @@ -12,6 +13,7 @@ import com.muedsa.tvbox.plugin.PluginManager
import com.muedsa.tvbox.room.dao.EpisodeProgressDao
import com.muedsa.tvbox.room.dao.FavoriteMediaDao
import com.muedsa.tvbox.store.DataStoreRepo
import com.muedsa.tvbox.store.PluginKeyCache
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -121,6 +123,14 @@ class PluginManageScreenViewModel @Inject constructor(
if (PluginManager.uninstallPlugin(context, pluginInfo)) {
favoriteMediaDao.deleteByPluginPackage(pluginPackage = pluginInfo.packageName)
episodeProgressDao.deleteByPluginPackage(pluginPackage = pluginInfo.packageName)
dateStoreRepo.pluginDataStore.edit { prefs ->
val keys = prefs.asMap().keys
keys.forEach { key ->
if (key.name.startsWith(PluginKeyCache.getGlobalKeyPrefix(pluginPackage = pluginInfo.packageName))) {
prefs.remove(key)
}
}
}
withContext(Dispatchers.Main) {
onSuccess()
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/muedsa/tvbox/store/PluginKeyCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ object PluginKeyCache {

private const val GLOBAL_KEY_NAME_SEPARATOR = ":"

fun getGlobalKeyPrefix(pluginPackage: String): String = "$pluginPackage$GLOBAL_KEY_NAME_SEPARATOR"

@Synchronized
@Suppress("UNCHECKED_CAST")
fun <T> getGlobalKey(pluginPackage: String, key: PluginPerfKey<T>): Preferences.Key<T> {
val name = "$pluginPackage$GLOBAL_KEY_NAME_SEPARATOR${key.name}"
val name = "${getGlobalKeyPrefix(pluginPackage)}${key.name}"
return cache.computeIfAbsent(name) {
key.getAndroidKey(name)
} as Preferences.Key<T>
Expand Down

0 comments on commit 848057b

Please sign in to comment.