fix: error exclude data in click log

This commit is contained in:
lisonge 2024-01-25 00:36:44 +08:00
parent 3a2e5e6832
commit e8b342c2af
6 changed files with 61 additions and 54 deletions

View File

@ -164,6 +164,10 @@ data class RawSubscription(
override val rules: List<RawGlobalRule>,
) : RawGroupProps, RawGlobalRuleProps {
val appIdEnable by lazy {
(apps ?: emptyList()).associate { a -> a.id to (a.enable ?: true) }
}
val allSelectorStrings by lazy {
rules.map { r -> r.matches + (r.excludeMatches ?: emptyList()) }.flatten().distinct()
}

View File

@ -138,7 +138,7 @@ fun ExcludeData.switch(appId: String, activityId: String? = null): ExcludeData {
return if (activityId == null) {
copy(
appIds = appIds.toMutableMap().apply {
if (get(appId) == true) {
if (get(appId) != false) {
set(appId, false)
} else {
set(appId, true)

View File

@ -64,6 +64,7 @@ import li.songe.gkd.util.appInfoCacheFlow
import li.songe.gkd.util.format
import li.songe.gkd.util.launchAsFn
import li.songe.gkd.util.navigate
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.toast
@RootNavGraph
@ -77,6 +78,7 @@ fun ClickLogPage() {
val clickDataList by vm.clickDataListFlow.collectAsState()
val clickLogCount by vm.clickLogCountFlow.collectAsState()
val appInfoCache by appInfoCacheFlow.collectAsState()
val subsIdToRaw by subsIdToRawFlow.collectAsState()
var previewClickLog by remember {
mutableStateOf<ClickLog?>(null)
@ -210,6 +212,7 @@ fun ClickLogPage() {
val oldExclude = remember(key1 = previewConfig?.exclude) {
ExcludeData.parse(previewConfig?.exclude)
}
val appInfo = appInfoCache[clickLog.appId]
Column {
Text(text = "查看规则组", modifier = Modifier
@ -233,26 +236,48 @@ fun ClickLogPage() {
.fillMaxWidth()
.padding(16.dp))
if (clickLog.groupType == SubsConfig.GlobalGroupType && clickLog.appId != null) {
Text(
text = if (oldExclude.appIds.contains(clickLog.appId)) "移除在此应用的禁用" else "在此应用禁用",
modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn(Dispatchers.IO) {
val subsConfig = previewConfig ?: SubsConfig(
type = SubsConfig.GlobalGroupType,
subsItemId = clickLog.subsId,
groupKey = clickLog.groupKey,
)
val newSubsConfig = subsConfig.copy(
exclude = oldExclude
.switch(clickLog.appId)
.stringify()
)
DbSet.subsConfigDao.insert(newSubsConfig)
toast("更新禁用")
})
.fillMaxWidth()
.padding(16.dp),
)
val group =
subsIdToRaw[clickLog.subsId]?.globalGroups?.find { g -> g.key == clickLog.groupKey }
val appChecked = if (group != null) {
getChecked(
oldExclude,
group,
clickLog.appId,
appInfo
)
} else {
null
}
if (appChecked != null) {
Text(
text = if (appChecked) "在此应用禁用" else "移除在此应用的禁用",
modifier = Modifier
.clickable(
onClick = vm.viewModelScope.launchAsFn(
Dispatchers.IO
) {
val subsConfig = previewConfig ?: SubsConfig(
type = SubsConfig.GlobalGroupType,
subsItemId = clickLog.subsId,
groupKey = clickLog.groupKey,
)
val newSubsConfig = subsConfig.copy(
exclude = oldExclude
.copy(
appIds = oldExclude.appIds
.toMutableMap()
.apply {
set(clickLog.appId, appChecked)
})
.stringify()
)
DbSet.subsConfigDao.insert(newSubsConfig)
toast("更新禁用")
})
.fillMaxWidth()
.padding(16.dp),
)
}
}
if (clickLog.appId != null && clickLog.activityId != null) {
val disabled =

View File

@ -75,7 +75,6 @@ fun GlobalRuleExcludePage(subsItemId: Long, groupKey: Int) {
val rawSubs = vm.rawSubsFlow.collectAsState().value
val group = vm.groupFlow.collectAsState().value
val excludeData = vm.excludeDataFlow.collectAsState().value
val appIdEnable = vm.appIdEnableFlow.collectAsState().value
val showAppInfos = vm.showAppInfosFlow.collectAsState().value
val searchStr by vm.searchStrFlow.collectAsState()
@ -201,7 +200,7 @@ fun GlobalRuleExcludePage(subsItemId: Long, groupKey: Int) {
Spacer(modifier = Modifier.width(10.dp))
if (group != null) {
val checked = getChecked(excludeData, group, appIdEnable, appInfo)
val checked = getChecked(excludeData, group, appInfo.id, appInfo)
Switch(
checked = checked ?: false,
onCheckedChange = {
@ -298,21 +297,24 @@ fun GlobalRuleExcludePage(subsItemId: Long, groupKey: Int) {
}
private fun getChecked(
// null - 内置禁用
// true - 启用
// false - 禁用
fun getChecked(
excludeData: ExcludeData,
group: RawSubscription.RawGlobalGroup,
appIdEnable: Map<String, Boolean>,
appInfo: AppInfo
appId: String,
appInfo: AppInfo? = null
): Boolean? {
val enable = appIdEnable[appInfo.id]
val enable = group.appIdEnable[appId]
if (enable == false) {
return null
}
excludeData.appIds[appInfo.id]?.let { return !it }
if (appInfo.id == launcherAppId) {
excludeData.appIds[appId]?.let { return !it }
if (appInfo?.id == launcherAppId) {
return group.matchLauncher ?: false
}
if (appInfo.isSystem) {
if (appInfo?.isSystem == true) {
return group.matchSystemApp ?: false
}
return group.matchAnyApp ?: true

View File

@ -16,24 +16,17 @@ import li.songe.gkd.ui.destinations.GlobalRuleExcludePageDestination
import li.songe.gkd.util.map
import li.songe.gkd.util.orderedAppInfosFlow
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import javax.inject.Inject
@HiltViewModel
class GlobalRuleExcludeVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
private val args = GlobalRuleExcludePageDestination.argsFrom(stateHandle)
val subsItemFlow =
subsItemsFlow.map(viewModelScope) { it.find { s -> s.id == args.subsItemId } }
val rawSubsFlow = subsIdToRawFlow.map(viewModelScope) { it[args.subsItemId] }
val groupFlow =
rawSubsFlow.map(viewModelScope) { r -> r?.globalGroups?.find { g -> g.key == args.groupKey } }
val appIdEnableFlow = groupFlow.map(viewModelScope) { g ->
(g?.apps ?: emptyList()).associate { a -> a.id to (a.enable ?: true) }
}
val subsConfigFlow =
DbSet.subsConfigDao.queryGlobalGroupTypeConfig(args.subsItemId, args.groupKey)
.map { it.firstOrNull() }
@ -41,7 +34,6 @@ class GlobalRuleExcludeVm @Inject constructor(stateHandle: SavedStateHandle) : V
val excludeDataFlow = subsConfigFlow.map(viewModelScope) { s -> ExcludeData.parse(s?.exclude) }
val searchStrFlow = MutableStateFlow("")
private val debounceSearchStrFlow = searchStrFlow.debounce(200)
.stateIn(viewModelScope, SharingStarted.Eagerly, searchStrFlow.value)

View File

@ -1,28 +1,12 @@
package li.songe.gkd.util
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Build
import java.net.NetworkInterface
object Ext {
fun PackageManager.getApplicationInfoExt(
packageName: String,
value: Int = PackageManager.GET_META_DATA,
): ApplicationInfo {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getApplicationInfo(
packageName, PackageManager.ApplicationInfoFlags.of(value.toLong())
)
} else {
getApplicationInfo(
packageName, value
)
}
}
fun Bitmap.isEmptyBitmap(): Boolean {
val emptyBitmap = Bitmap.createBitmap(width, height, config)