Skip to content

Commit

Permalink
feat: ruleGroupState
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jan 21, 2025
1 parent a3fc1d3 commit 4496b13
Show file tree
Hide file tree
Showing 39 changed files with 1,866 additions and 2,002 deletions.
1 change: 1 addition & 0 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class MainActivity : ComponentActivity() {
SubsSheet(mainVm, mainVm.sheetSubsIdFlow)
ShareDataDialog(mainVm, mainVm.showShareDataIdsFlow)
mainVm.inputSubsLinkOption.ContentDialog()
mainVm.ruleGroupState.Render()
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import li.songe.gkd.db.DbSet
import li.songe.gkd.permission.AuthReason
import li.songe.gkd.ui.component.AlertDialogOptions
import li.songe.gkd.ui.component.InputSubsLinkOption
import li.songe.gkd.ui.component.RuleGroupState
import li.songe.gkd.ui.component.UploadOptions
import li.songe.gkd.util.LOCAL_SUBS_ID
import li.songe.gkd.util.UpdateStatus
Expand Down Expand Up @@ -115,6 +116,8 @@ class MainViewModel : ViewModel() {
}
}

val ruleGroupState = RuleGroupState(this)

init {
viewModelScope.launchTry(Dispatchers.IO) {
val subsItems = DbSet.subsItemDao.queryAll()
Expand Down
28 changes: 25 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/data/AppInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ data class AppInfo(
val isSystem: Boolean,
val mtime: Long,
val hidden: Boolean,
// null=0
val userId: Int? = null,
)
val userId: Int? = null, // null=0
// val activities: List<String> = emptyList(),
) {
// 重写 equals 和 hashCode 便于 compose 重组比较
override fun equals(other: Any?): Boolean {
if (other === this) return true
return (other is AppInfo && id == other.id && mtime == other.mtime)
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + id.hashCode()
result = 31 * result + mtime.hashCode()
return result
}
}

val selfAppInfo by lazy {
app.packageManager.getPackageInfo(app.packageName, 0).toAppInfo()
Expand Down Expand Up @@ -49,5 +62,14 @@ fun PackageInfo.toAppInfo(
icon = applicationInfo?.loadIcon(app.packageManager),
userId = userId,
hidden = hidden ?: (app.packageManager.getLaunchIntentForPackage(packageName) == null),
// activities = (activities ?: emptyArray()).map {
// if (
// it.name.startsWith(packageName) && it.name.getOrNull(packageName.length) == '.'
// ) {
// it.name.substring(packageName.length)
// } else {
// it.name
// }
// },
)
}
45 changes: 27 additions & 18 deletions app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.long
import li.songe.gkd.service.checkSelector
import li.songe.gkd.util.LOCAL_SUBS_IDS
import li.songe.gkd.util.json
import li.songe.gkd.util.toast
import li.songe.json5.Json5
import li.songe.json5.encodeToJson5String
import li.songe.selector.Selector
import net.objecthunter.exp4j.Expression
import net.objecthunter.exp4j.ExpressionBuilder
Expand Down Expand Up @@ -49,6 +51,9 @@ data class RawSubscription(
return result
}

val isLocal: Boolean
get() = LOCAL_SUBS_IDS.contains(id)

val categoryToGroupsMap by lazy {
val allAppGroups = apps.flatMap { a -> a.groups.map { g -> g to a } }
allAppGroups.groupBy { g ->
Expand Down Expand Up @@ -116,7 +121,6 @@ data class RawSubscription(
}
}


@Serializable
data class RawApp(
val id: String,
Expand Down Expand Up @@ -217,7 +221,6 @@ data class RawSubscription(
val anyMatches: List<String>?
}


sealed interface RawGroupProps : RawCommonProps {
val name: String
val key: Int
Expand All @@ -230,6 +233,7 @@ data class RawSubscription(
val errorDesc: String?
val allExampleUrls: List<String>
val cacheMap: MutableMap<String, Selector?>
val cacheStr: String
}

sealed interface RawAppRuleProps {
Expand Down Expand Up @@ -265,8 +269,8 @@ data class RawSubscription(

@Serializable
data class RawGlobalGroup(
override val name: String,
override val key: Int,
override val name: String,
override val desc: String?,
override val enable: Boolean?,
override val scopeKeys: List<Int>?,
Expand All @@ -291,8 +295,8 @@ data class RawSubscription(
override val matchAnyApp: Boolean?,
override val matchSystemApp: Boolean?,
override val matchLauncher: Boolean?,
override val apps: List<RawGlobalApp>?,
override val rules: List<RawGlobalRule>,
override val apps: List<RawGlobalApp>?,
) : RawGroupProps, RawGlobalRuleProps {
val appIdEnable by lazy {
(apps ?: emptyList()).associate { a -> a.id to (a.enable ?: true) }
Expand All @@ -306,11 +310,14 @@ data class RawSubscription(
r.exampleUrls ?: emptyList()
}).distinct()
}
override val cacheStr by lazy { json.encodeToJson5String(this) }
}


@Serializable
data class RawGlobalRule(
override val key: Int?,
override val name: String?,
override val actionCd: Long?,
override val actionDelay: Long?,
override val quickFind: Boolean?,
Expand All @@ -329,8 +336,6 @@ data class RawSubscription(
override val snapshotUrls: List<String>?,
override val excludeSnapshotUrls: List<String>?,
override val exampleUrls: List<String>?,
override val name: String?,
override val key: Int?,
override val preKeys: List<Int>?,
override val action: String?,
override val position: Position?,
Expand All @@ -345,8 +350,8 @@ data class RawSubscription(

@Serializable
data class RawAppGroup(
override val name: String,
override val key: Int,
override val name: String,
override val desc: String?,
override val enable: Boolean?,
override val scopeKeys: List<Int>?,
Expand Down Expand Up @@ -384,12 +389,13 @@ data class RawSubscription(
r.exampleUrls ?: emptyList()
}).distinct()
}
override val cacheStr by lazy { json.encodeToJson5String(this) }
}

@Serializable
data class RawAppRule(
override val name: String?,
override val key: Int?,
override val name: String?,
override val preKeys: List<Int>?,
override val action: String?,
override val position: Position?,
Expand Down Expand Up @@ -794,7 +800,7 @@ data class RawSubscription(
jsonToGlobalApp(
jsonElement.jsonObject, index
)
} ?: emptyList(),
},
rules = jsonObject["rules"]?.jsonArray?.map { jsonElement ->
jsonToGlobalRule(jsonElement.jsonObject)
} ?: emptyList(),
Expand All @@ -807,7 +813,8 @@ data class RawSubscription(
}

private fun jsonToSubscriptionRaw(rootJson: JsonObject): RawSubscription {
return RawSubscription(id = getLong(rootJson, "id") ?: error("miss subscription.id"),
return RawSubscription(
id = getLong(rootJson, "id") ?: error("miss subscription.id"),
name = getString(rootJson, "name") ?: error("miss subscription.name"),
version = getInt(rootJson, "version") ?: error("miss subscription.version"),
author = getString(rootJson, "author"),
Expand All @@ -816,7 +823,8 @@ data class RawSubscription(
checkUpdateUrl = getString(rootJson, "checkUpdateUrl"),
apps = (rootJson["apps"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToAppRaw(
jsonElement.jsonObject, index
jsonElement.jsonObject,
index
)
} ?: emptyList()),
categories = (rootJson["categories"]?.jsonArray?.mapIndexed { index, jsonElement ->
Expand All @@ -830,7 +838,8 @@ data class RawSubscription(
} ?: emptyList()),
globalGroups = (rootJson["globalGroups"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToGlobalGroups(jsonElement.jsonObject, index)
} ?: emptyList()))
} ?: emptyList())
)
}

private fun <T> List<T>.findDuplicatedItem(predicate: (T) -> Any?): T? {
Expand Down Expand Up @@ -887,9 +896,9 @@ data class RawSubscription(
return a
}

fun parseRawApp(source: String): RawApp {
return parseApp(Json5.parseToJson5Element(source).jsonObject)
}
// fun parseRawApp(source: String): RawApp {
// return parseApp(Json5.parseToJson5Element(source).jsonObject)
// }

fun parseGroup(jsonObject: JsonObject): RawAppGroup {
val g = jsonToGroupRaw(jsonObject, 0)
Expand All @@ -899,9 +908,9 @@ data class RawSubscription(
return g
}

fun parseRawGroup(source: String): RawAppGroup {
return parseGroup(Json5.parseToJson5Element(source).jsonObject)
}
// fun parseRawGroup(source: String): RawAppGroup {
// return parseGroup(Json5.parseToJson5Element(source).jsonObject)
// }

fun parseRawGlobalGroup(source: String): RawGlobalGroup {
val g = jsonToGlobalGroups(Json5.parseToJson5Element(source).jsonObject, 0)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/ResolvedGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ResolvedAppGroup(
config: SubsConfig?,
val app: RawSubscription.RawApp,
val enable: Boolean,
val category: RawSubscription.RawCategory?,
val categoryConfig: CategoryConfig?,
) : ResolvedGroup(group, subscription, subsItem, config)

class ResolvedGlobalGroup(
Expand Down
Loading

0 comments on commit 4496b13

Please sign in to comment.