Skip to content

Commit 512b40d

Browse files
committed
feat: disableIfAppGroupMatch/ignoreGlobalGroupMatch
1 parent 7b8c22e commit 512b40d

File tree

10 files changed

+85
-17
lines changed

10 files changed

+85
-17
lines changed

app/src/main/kotlin/li/songe/gkd/data/AppRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package li.songe.gkd.data
33
class AppRule(
44
rule: RawSubscription.RawAppRule,
55
g: ResolvedAppGroup,
6-
val appInfo: AppInfo?,
6+
appInfo: AppInfo?,
77
) : ResolvedRule(
88
rule = rule,
99
g = g,

app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class GlobalRule(
1818
rule = rule,
1919
g = g,
2020
) {
21+
val groupExcludeAppIds = g.groupExcludeAppIds
2122
val group = g.group
2223
private val matchAnyApp = rule.matchAnyApp ?: group.matchAnyApp ?: true
2324
private val matchLauncher = rule.matchLauncher ?: group.matchLauncher ?: false
@@ -53,7 +54,10 @@ class GlobalRule(
5354

5455
override val type = "global"
5556

56-
private val excludeAppIds = apps.filter { e -> !e.value.enable }.keys
57+
private val excludeAppIds = apps.filter { e ->
58+
!e.value.enable
59+
}.keys
60+
5761
private val enableApps = apps.filter { e -> e.value.enable }
5862

5963
/**
@@ -62,7 +66,7 @@ class GlobalRule(
6266
*/
6367
override fun matchActivity(appId: String, activityId: String?): Boolean {
6468
// 规则自带禁用
65-
if (excludeAppIds.contains(appId)) {
69+
if (excludeAppIds.contains(appId) || groupExcludeAppIds.contains(appId)) {
6670
return false
6771
}
6872

app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,38 @@ data class RawSubscription(
9999
appGroups.size + globalGroups.size
100100
}
101101

102+
val globalGroupAppGroupNameDisableMap by lazy {
103+
globalGroups.mapNotNull { g ->
104+
val n = g.disableIfAppGroupMatch
105+
if (n != null) {
106+
val gName = if (n.isNotEmpty()) {
107+
n
108+
} else {
109+
g.name
110+
}
111+
g.key to apps.filter { a ->
112+
a.groups.any { ag ->
113+
ag.ignoreGlobalGroupMatch != true && ag.name.startsWith(gName)
114+
}
115+
}.map { it.id }.toHashSet()
116+
} else {
117+
null
118+
}
119+
}.toMap()
120+
}
121+
122+
fun getGlobalGroupInnerDisabled(globalGroup: RawGlobalGroup, appId: String): Boolean {
123+
globalGroup.appIdEnable[appId]?.let {
124+
if (it == false) return true
125+
}
126+
globalGroupAppGroupNameDisableMap[globalGroup.key]?.let {
127+
if (it.contains(appId)) {
128+
return true
129+
}
130+
}
131+
return false
132+
}
133+
102134
val numText by lazy {
103135
val appsSize = apps.size
104136
val appGroupsSize = appGroups.size
@@ -305,10 +337,11 @@ data class RawSubscription(
305337
override val matchAnyApp: Boolean?,
306338
override val matchSystemApp: Boolean?,
307339
override val matchLauncher: Boolean?,
340+
val disableIfAppGroupMatch: String?,
308341
override val rules: List<RawGlobalRule>,
309342
override val apps: List<RawGlobalApp>?,
310343
) : RawGroupProps, RawGlobalRuleProps {
311-
val appIdEnable by lazy {
344+
val appIdEnable: Map<String, Boolean> by lazy {
312345
if (rules.all { r -> r.apps.isNullOrEmpty() }) {
313346
apps?.associate { a -> a.id to (a.enable ?: true) } ?: emptyMap()
314347
} else {
@@ -417,6 +450,7 @@ data class RawSubscription(
417450
override val excludeVersionNames: List<String>?,
418451
override val versionCodes: List<Long>?,
419452
override val excludeVersionCodes: List<Long>?,
453+
val ignoreGlobalGroupMatch: Boolean?,
420454
) : RawGroupProps, RawAppRuleProps {
421455
override val cacheMap by lazy { HashMap<String, Selector?>() }
422456
override val errorDesc by lazy { getErrorDesc() }
@@ -606,11 +640,11 @@ data class RawSubscription(
606640
if (p.isString) {
607641
p.content
608642
} else {
609-
error("Element $p is not a string")
643+
null
610644
}
611645
}
612646

613-
else -> error("Element $p is not a string")
647+
else -> null
614648
}
615649

616650
private fun getLong(jsonObject: JsonObject? = null, key: String): Long? =
@@ -620,7 +654,7 @@ data class RawSubscription(
620654
p.long
621655
}
622656

623-
else -> error("Element $p is not a long")
657+
else -> null
624658
}
625659

626660
private fun getInt(jsonObject: JsonObject? = null, key: String): Int? =
@@ -630,7 +664,7 @@ data class RawSubscription(
630664
p.int
631665
}
632666

633-
else -> error("Element $p is not a int")
667+
else -> null
634668
}
635669

636670
private fun getBoolean(jsonObject: JsonObject? = null, key: String): Boolean? =
@@ -640,7 +674,7 @@ data class RawSubscription(
640674
p.boolean
641675
}
642676

643-
else -> error("Element $p is not a boolean")
677+
else -> null
644678
}
645679

646680
private fun jsonToRuleRaw(rulesRawJson: JsonElement): RawAppRule {
@@ -728,6 +762,7 @@ data class RawSubscription(
728762
excludeVersionNames = getStringIArray(jsonObject, "excludeVersionNames"),
729763
priorityTime = getLong(jsonObject, "priorityTime"),
730764
priorityActionMaximum = getInt(jsonObject, "priorityActionMaximum"),
765+
ignoreGlobalGroupMatch = getBoolean(jsonObject, "ignoreGlobalGroupMatch"),
731766
)
732767
}
733768

@@ -839,6 +874,7 @@ data class RawSubscription(
839874
forcedTime = getLong(jsonObject, "forcedTime"),
840875
priorityTime = getLong(jsonObject, "priorityTime"),
841876
priorityActionMaximum = getInt(jsonObject, "priorityActionMaximum"),
877+
disableIfAppGroupMatch = getString(jsonObject, "disableIfAppGroupMatch"),
842878
)
843879
}
844880

app/src/main/kotlin/li/songe/gkd/data/ResolvedGroup.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ class ResolvedGlobalGroup(
3131
) : ResolvedGroup(group, subscription, subsItem, config) {
3232
override val appId: String?
3333
get() = null
34+
35+
val groupExcludeAppIds by lazy {
36+
subscription.globalGroupAppGroupNameDisableMap[group.key] ?: emptySet()
37+
}
3438
}

app/src/main/kotlin/li/songe/gkd/ui/ActionLogPage.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ private fun ActionLogDialog(
371371
val group = subs?.globalGroups?.find { g -> g.key == actionLog.groupKey }
372372
val appChecked = if (group != null) {
373373
getGlobalGroupChecked(
374+
subs,
374375
oldExclude,
375376
group,
376377
actionLog.appId,

app/src/main/kotlin/li/songe/gkd/ui/GlobalGroupExcludePage.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ fun GlobalGroupExcludePage(subsItemId: Long, groupKey: Int) {
310310
Spacer(modifier = Modifier.width(8.dp))
311311

312312
if (group != null) {
313-
val checked = getGlobalGroupChecked(excludeData, group, appInfo.id)
313+
val checked = getGlobalGroupChecked(
314+
rawSubs!!,
315+
excludeData,
316+
group,
317+
appInfo.id
318+
)
314319
if (checked != null) {
315320
key(appInfo.id) {
316321
Switch(
@@ -413,16 +418,16 @@ fun GlobalGroupExcludePage(subsItemId: Long, groupKey: Int) {
413418
// true - 启用
414419
// false - 禁用
415420
fun getGlobalGroupChecked(
421+
subscription: RawSubscription,
416422
excludeData: ExcludeData,
417423
group: RawSubscription.RawGlobalGroup,
418424
appId: String,
419425
): Boolean? {
420-
val enable = group.appIdEnable[appId]
421-
if (enable == false) {
426+
if (subscription.getGlobalGroupInnerDisabled(group, appId)) {
422427
return null
423428
}
424429
excludeData.appIds[appId]?.let { return !it }
425-
if (enable == true) return true
430+
if (group.appIdEnable[appId] == true) return true
426431
if (appId == launcherAppId) {
427432
return group.matchLauncher ?: false
428433
}

app/src/main/kotlin/li/songe/gkd/ui/component/GroupNameText.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fun GroupNameText(
3131
overflow: TextOverflow = TextOverflow.Clip,
3232
softWrap: Boolean = true,
3333
maxLines: Int = Int.MAX_VALUE,
34+
clickDisabled: Boolean = false,
3435
) {
3536
if (isGlobal) {
3637
val text = remember(preText, text) {
@@ -43,7 +44,7 @@ fun GroupNameText(
4344
}
4445
}
4546
val fontSize = style.fontSize
46-
val inlineContent = remember(fontSize) {
47+
val inlineContent = remember(fontSize, clickDisabled) {
4748
mapOf(
4849
"icon" to InlineTextContent(
4950
placeholder = Placeholder(
@@ -55,7 +56,9 @@ fun GroupNameText(
5556
Icon(
5657
imageVector = SportsBasketball,
5758
modifier = Modifier
58-
.clickable(onClick = throttle { toast("当前是全局规则组") })
59+
.runIf(!clickDisabled) {
60+
clickable(onClick = throttle { toast("当前是全局规则组") })
61+
}
5962
.fillMaxSize(),
6063
contentDescription = null,
6164
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package li.songe.gkd.ui.component
2+
3+
import androidx.compose.ui.Modifier
4+
5+
inline fun Modifier.runIf(
6+
enabled: Boolean,
7+
block: Modifier.() -> Modifier
8+
) = run {
9+
if (enabled) {
10+
block()
11+
} else {
12+
this
13+
}
14+
}

app/src/main/kotlin/li/songe/gkd/ui/component/RuleGroupCard.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fun RuleGroupCard(
101101
val excludeData = remember(subsConfig?.exclude) {
102102
ExcludeData.parse(subsConfig?.exclude)
103103
}
104-
getGlobalGroupChecked(excludeData, group, appId) to excludeData
104+
getGlobalGroupChecked(subs, excludeData, group, appId) to excludeData
105105
} else {
106106
getGroupEnable(
107107
group,
@@ -203,6 +203,7 @@ fun RuleGroupCard(
203203
maxLines = 1,
204204
softWrap = false,
205205
overflow = TextOverflow.Ellipsis,
206+
clickDisabled = isSelectedMode,
206207
)
207208
if (group.valid) {
208209
if (!group.desc.isNullOrBlank()) {

app/src/main/kotlin/li/songe/gkd/ui/component/RuleGroupState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ suspend fun batchUpdateGroupEnable(
137137
// global rule for some app
138138
targetGroup as RawSubscription.RawGlobalGroup
139139
val excludeData = ExcludeData.parse(subsConfig?.exclude)
140-
getGlobalGroupChecked(excludeData, targetGroup, g.pageAppId).let {
140+
getGlobalGroupChecked(subscription, excludeData, targetGroup, g.pageAppId).let {
141141
if (it == null) return@map null
142142
}
143143
(subsConfig ?: SubsConfig(

0 commit comments

Comments
 (0)