perf: clean code
Some checks are pending
Build-Apk / build (push) Waiting to run

This commit is contained in:
lisonge 2024-10-05 23:50:42 +08:00
parent 49f535176b
commit 2af801636b
10 changed files with 39 additions and 54 deletions

View File

@ -236,7 +236,6 @@ dependencies {
implementation(libs.google.accompanist.drawablepainter)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.collections.immutable)
implementation(libs.utilcodex)
implementation(libs.activityResultLauncher)

View File

@ -1,6 +1,5 @@
package li.songe.gkd.data
import kotlinx.collections.immutable.ImmutableMap
import li.songe.gkd.service.launcherAppId
import li.songe.gkd.util.ResolvedGlobalGroup
import li.songe.gkd.util.systemAppsFlow
@ -15,7 +14,7 @@ data class GlobalApp(
class GlobalRule(
rule: RawSubscription.RawGlobalRule,
g: ResolvedGlobalGroup,
appInfoCache: ImmutableMap<String, AppInfo>,
appInfoCache: Map<String, AppInfo>,
) : ResolvedRule(
rule = rule,
g = g,

View File

@ -10,7 +10,6 @@ import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable
@ -113,6 +112,6 @@ fun deleteSubscription(vararg subsIds: Long) {
}
}
}
subsIdToRawFlow.value = newMap.toImmutableMap()
subsIdToRawFlow.value = newMap
}
}

View File

@ -56,7 +56,6 @@ import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.dylanc.activityresult.launcher.launchForResult
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
@ -354,7 +353,7 @@ fun useSubsManagePage(): ScaffoldExt {
this[index] = subsItem.copy(order = index)
}
}
}.toImmutableList()
}
draggedFlag.value = true
}
Box(

View File

@ -7,9 +7,6 @@ import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import com.blankj.utilcode.util.LogUtils
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
@ -20,11 +17,11 @@ import li.songe.gkd.appScope
import li.songe.gkd.data.AppInfo
import li.songe.gkd.data.toAppInfo
val appInfoCacheFlow = MutableStateFlow(persistentMapOf<String, AppInfo>().toImmutableMap())
val appInfoCacheFlow = MutableStateFlow(emptyMap<String, AppInfo>())
val systemAppInfoCacheFlow by lazy {
appInfoCacheFlow.map(appScope) { c ->
c.filter { a -> a.value.isSystem }.toImmutableMap()
c.filter { a -> a.value.isSystem }
}
}
@ -34,7 +31,7 @@ val orderedAppInfosFlow by lazy {
appInfoCacheFlow.map(appScope) { c ->
c.values.sortedWith { a, b ->
collator.compare(a.name, b.name)
}.toImmutableList()
}
}
}
@ -92,7 +89,7 @@ private fun updateAppInfo(appId: String) {
} else {
newMap.remove(appId)
}
appInfoCacheFlow.value = newMap.toImmutableMap()
appInfoCacheFlow.value = newMap
}
}
}
@ -116,7 +113,7 @@ suspend fun initOrResetAppInfoCache() {
}
}
}
appInfoCacheFlow.value = appMap.toImmutableMap()
appInfoCacheFlow.value = appMap
}
appRefreshingFlow.value = false
LogUtils.d("initOrResetAppInfoCache end")

View File

@ -4,12 +4,6 @@ import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.NetworkUtils
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
@ -36,8 +30,7 @@ import li.songe.json5.decodeFromJson5String
import java.net.URI
val subsItemsFlow by lazy {
DbSet.subsItemDao.query().map { s -> s.toImmutableList() }
.stateIn(appScope, SharingStarted.Eagerly, persistentListOf())
DbSet.subsItemDao.query().stateIn(appScope, SharingStarted.Eagerly, emptyList())
}
data class SubsEntry(
@ -56,9 +49,9 @@ data class SubsEntry(
}
}
val subsLoadErrorsFlow = MutableStateFlow<ImmutableMap<Long, Exception>>(persistentMapOf())
val subsRefreshErrorsFlow = MutableStateFlow<ImmutableMap<Long, Exception>>(persistentMapOf())
val subsIdToRawFlow = MutableStateFlow<ImmutableMap<Long, RawSubscription>>(persistentMapOf())
val subsLoadErrorsFlow = MutableStateFlow<Map<Long, Exception>>(emptyMap())
val subsRefreshErrorsFlow = MutableStateFlow<Map<Long, Exception>>(emptyMap())
val subsIdToRawFlow = MutableStateFlow<Map<Long, RawSubscription>>(emptyMap())
val subsEntriesFlow by lazy {
combine(
@ -70,8 +63,8 @@ val subsEntriesFlow by lazy {
subsItem = s,
subscription = subsIdToRaw[s.id],
)
}.toImmutableList()
}.stateIn(appScope, SharingStarted.Eagerly, persistentListOf())
}
}.stateIn(appScope, SharingStarted.Eagerly, emptyList())
}
@ -85,12 +78,12 @@ fun updateSubscription(subscription: RawSubscription) {
} else {
newMap[subscription.id] = subscription
}
subsIdToRawFlow.value = newMap.toImmutableMap()
subsIdToRawFlow.value = newMap
if (subsLoadErrorsFlow.value.contains(subscription.id)) {
subsLoadErrorsFlow.update {
it.toMutableMap().apply {
remove(subscription.id)
}.toImmutableMap()
}
}
}
withContext(Dispatchers.IO) {
@ -126,11 +119,11 @@ fun getGroupRawEnable(
}
data class RuleSummary(
val globalRules: ImmutableList<GlobalRule> = persistentListOf(),
val globalGroups: ImmutableList<ResolvedGlobalGroup> = persistentListOf(),
val appIdToRules: ImmutableMap<String, ImmutableList<AppRule>> = persistentMapOf(),
val appIdToGroups: ImmutableMap<String, ImmutableList<RawSubscription.RawAppGroup>> = persistentMapOf(),
val appIdToAllGroups: ImmutableMap<String, ImmutableList<ResolvedAppGroup>> = persistentMapOf(),
val globalRules: List<GlobalRule> = emptyList(),
val globalGroups: List<ResolvedGlobalGroup> = emptyList(),
val appIdToRules: Map<String, List<AppRule>> = emptyMap(),
val appIdToGroups: Map<String, List<RawSubscription.RawAppGroup>> = emptyMap(),
val appIdToAllGroups: Map<String, List<ResolvedAppGroup>> = emptyMap(),
) {
val appSize = appIdToRules.keys.size
val appGroupSize = appIdToGroups.values.sumOf { s -> s.size }
@ -276,12 +269,11 @@ val ruleSummaryFlow by lazy {
}
}
RuleSummary(
globalRules = globalRules.toImmutableList(),
globalGroups = globalGroups.toImmutableList(),
appIdToRules = appRules.mapValues { e -> e.value.toImmutableList() }.toImmutableMap(),
appIdToGroups = appGroups.mapValues { e -> e.value.toImmutableList() }.toImmutableMap(),
appIdToAllGroups = appAllGroups.mapValues { e -> e.value.toImmutableList() }
.toImmutableMap()
globalRules = globalRules,
globalGroups = globalGroups,
appIdToRules = appRules,
appIdToGroups = appGroups,
appIdToAllGroups = appAllGroups
)
}.flowOn(Dispatchers.Default).stateIn(appScope, SharingStarted.Eagerly, RuleSummary())
}
@ -321,8 +313,8 @@ private fun refreshRawSubsList(items: List<SubsItem>) {
errors[s.id] = e
}
}
subsIdToRawFlow.value = subscriptions.toImmutableMap()
subsLoadErrorsFlow.value = errors.toImmutableMap()
subsIdToRawFlow.value = subscriptions
subsLoadErrorsFlow.value = errors
}
fun initSubsState() {
@ -416,14 +408,14 @@ fun checkSubsUpdate(showToast: Boolean = false) = appScope.launchTry(Dispatchers
subsRefreshErrorsFlow.update {
it.toMutableMap().apply {
remove(subsEntry.subsItem.id)
}.toImmutableMap()
}
}
}
} catch (e: Exception) {
subsRefreshErrorsFlow.update {
it.toMutableMap().apply {
set(subsEntry.subsItem.id, e)
}.toImmutableMap()
}
}
LogUtils.d("检测更新失败", e)
}

View File

@ -17,7 +17,6 @@ kotlin_serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", v
kotlin_stdlib_common = { module = "org.jetbrains.kotlin:kotlin-stdlib-common", version.ref = "kotlin" }
kotlin_test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlinx_serialization_json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.7.3" }
kotlinx_collections_immutable = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable", version = "0.3.8" }
ktor_server_core = { module = "io.ktor:ktor-server-core", version.ref = "ktor" }
ktor_server_cio = { module = "io.ktor:ktor-server-cio", version.ref = "ktor" }
ktor_server_content_negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }

View File

@ -3,7 +3,7 @@ package li.songe.selector
import kotlin.js.JsExport
@JsExport
data class SyntaxError internal constructor(
data class SyntaxError @JsExport.Ignore constructor(
val expectedValue: String,
val position: Int,
val source: String,

View File

@ -17,7 +17,7 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
override val value: String,
) : ValueExpression(value, "var")
data class Identifier internal constructor(
data class Identifier(
override val start: Int,
val name: String,
) : Variable(name) {
@ -35,7 +35,7 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
val isNotEqual = name == "notEqual"
}
data class MemberExpression internal constructor(
data class MemberExpression(
override val start: Int,
override val end: Int,
val object0: Variable,
@ -61,7 +61,7 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
val isPropertyIfElse = property == "ifElse"
}
data class CallExpression internal constructor(
data class CallExpression(
override val start: Int,
override val end: Int,
val callee: Variable,
@ -173,26 +173,26 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
get() = emptyArray()
}
data class NullLiteral internal constructor(
data class NullLiteral(
override val start: Int,
) : LiteralExpression(null, "null") {
override val end = start + 4
}
data class BooleanLiteral internal constructor(
data class BooleanLiteral(
override val start: Int,
override val value: Boolean
) : LiteralExpression(value, "boolean") {
override val end = start + if (value) 4 else 5
}
data class IntLiteral internal constructor(
data class IntLiteral(
override val start: Int,
override val end: Int,
override val value: Int
) : LiteralExpression(value, "int")
data class StringLiteral internal constructor(
data class StringLiteral @JsExport.Ignore constructor(
override val start: Int,
override val end: Int,
override val value: String,

View File

@ -1 +1,2 @@
li.songe.gkd.*
kotlin.collections.*