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.google.accompanist.drawablepainter)
implementation(libs.kotlinx.serialization.json) implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.collections.immutable)
implementation(libs.utilcodex) implementation(libs.utilcodex)
implementation(libs.activityResultLauncher) implementation(libs.activityResultLauncher)

View File

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

View File

@ -10,7 +10,6 @@ import androidx.room.PrimaryKey
import androidx.room.Query import androidx.room.Query
import androidx.room.Transaction import androidx.room.Transaction
import androidx.room.Update import androidx.room.Update
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable 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.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.dylanc.activityresult.launcher.launchForResult import com.dylanc.activityresult.launcher.launchForResult
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -354,7 +353,7 @@ fun useSubsManagePage(): ScaffoldExt {
this[index] = subsItem.copy(order = index) this[index] = subsItem.copy(order = index)
} }
} }
}.toImmutableList() }
draggedFlag.value = true draggedFlag.value = true
} }
Box( Box(

View File

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

View File

@ -4,12 +4,6 @@ import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.NetworkUtils import com.blankj.utilcode.util.NetworkUtils
import io.ktor.client.request.get import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText 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.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -36,8 +30,7 @@ import li.songe.json5.decodeFromJson5String
import java.net.URI import java.net.URI
val subsItemsFlow by lazy { val subsItemsFlow by lazy {
DbSet.subsItemDao.query().map { s -> s.toImmutableList() } DbSet.subsItemDao.query().stateIn(appScope, SharingStarted.Eagerly, emptyList())
.stateIn(appScope, SharingStarted.Eagerly, persistentListOf())
} }
data class SubsEntry( data class SubsEntry(
@ -56,9 +49,9 @@ data class SubsEntry(
} }
} }
val subsLoadErrorsFlow = MutableStateFlow<ImmutableMap<Long, Exception>>(persistentMapOf()) val subsLoadErrorsFlow = MutableStateFlow<Map<Long, Exception>>(emptyMap())
val subsRefreshErrorsFlow = MutableStateFlow<ImmutableMap<Long, Exception>>(persistentMapOf()) val subsRefreshErrorsFlow = MutableStateFlow<Map<Long, Exception>>(emptyMap())
val subsIdToRawFlow = MutableStateFlow<ImmutableMap<Long, RawSubscription>>(persistentMapOf()) val subsIdToRawFlow = MutableStateFlow<Map<Long, RawSubscription>>(emptyMap())
val subsEntriesFlow by lazy { val subsEntriesFlow by lazy {
combine( combine(
@ -70,8 +63,8 @@ val subsEntriesFlow by lazy {
subsItem = s, subsItem = s,
subscription = subsIdToRaw[s.id], subscription = subsIdToRaw[s.id],
) )
}.toImmutableList() }
}.stateIn(appScope, SharingStarted.Eagerly, persistentListOf()) }.stateIn(appScope, SharingStarted.Eagerly, emptyList())
} }
@ -85,12 +78,12 @@ fun updateSubscription(subscription: RawSubscription) {
} else { } else {
newMap[subscription.id] = subscription newMap[subscription.id] = subscription
} }
subsIdToRawFlow.value = newMap.toImmutableMap() subsIdToRawFlow.value = newMap
if (subsLoadErrorsFlow.value.contains(subscription.id)) { if (subsLoadErrorsFlow.value.contains(subscription.id)) {
subsLoadErrorsFlow.update { subsLoadErrorsFlow.update {
it.toMutableMap().apply { it.toMutableMap().apply {
remove(subscription.id) remove(subscription.id)
}.toImmutableMap() }
} }
} }
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
@ -126,11 +119,11 @@ fun getGroupRawEnable(
} }
data class RuleSummary( data class RuleSummary(
val globalRules: ImmutableList<GlobalRule> = persistentListOf(), val globalRules: List<GlobalRule> = emptyList(),
val globalGroups: ImmutableList<ResolvedGlobalGroup> = persistentListOf(), val globalGroups: List<ResolvedGlobalGroup> = emptyList(),
val appIdToRules: ImmutableMap<String, ImmutableList<AppRule>> = persistentMapOf(), val appIdToRules: Map<String, List<AppRule>> = emptyMap(),
val appIdToGroups: ImmutableMap<String, ImmutableList<RawSubscription.RawAppGroup>> = persistentMapOf(), val appIdToGroups: Map<String, List<RawSubscription.RawAppGroup>> = emptyMap(),
val appIdToAllGroups: ImmutableMap<String, ImmutableList<ResolvedAppGroup>> = persistentMapOf(), val appIdToAllGroups: Map<String, List<ResolvedAppGroup>> = emptyMap(),
) { ) {
val appSize = appIdToRules.keys.size val appSize = appIdToRules.keys.size
val appGroupSize = appIdToGroups.values.sumOf { s -> s.size } val appGroupSize = appIdToGroups.values.sumOf { s -> s.size }
@ -276,12 +269,11 @@ val ruleSummaryFlow by lazy {
} }
} }
RuleSummary( RuleSummary(
globalRules = globalRules.toImmutableList(), globalRules = globalRules,
globalGroups = globalGroups.toImmutableList(), globalGroups = globalGroups,
appIdToRules = appRules.mapValues { e -> e.value.toImmutableList() }.toImmutableMap(), appIdToRules = appRules,
appIdToGroups = appGroups.mapValues { e -> e.value.toImmutableList() }.toImmutableMap(), appIdToGroups = appGroups,
appIdToAllGroups = appAllGroups.mapValues { e -> e.value.toImmutableList() } appIdToAllGroups = appAllGroups
.toImmutableMap()
) )
}.flowOn(Dispatchers.Default).stateIn(appScope, SharingStarted.Eagerly, RuleSummary()) }.flowOn(Dispatchers.Default).stateIn(appScope, SharingStarted.Eagerly, RuleSummary())
} }
@ -321,8 +313,8 @@ private fun refreshRawSubsList(items: List<SubsItem>) {
errors[s.id] = e errors[s.id] = e
} }
} }
subsIdToRawFlow.value = subscriptions.toImmutableMap() subsIdToRawFlow.value = subscriptions
subsLoadErrorsFlow.value = errors.toImmutableMap() subsLoadErrorsFlow.value = errors
} }
fun initSubsState() { fun initSubsState() {
@ -416,14 +408,14 @@ fun checkSubsUpdate(showToast: Boolean = false) = appScope.launchTry(Dispatchers
subsRefreshErrorsFlow.update { subsRefreshErrorsFlow.update {
it.toMutableMap().apply { it.toMutableMap().apply {
remove(subsEntry.subsItem.id) remove(subsEntry.subsItem.id)
}.toImmutableMap() }
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
subsRefreshErrorsFlow.update { subsRefreshErrorsFlow.update {
it.toMutableMap().apply { it.toMutableMap().apply {
set(subsEntry.subsItem.id, e) set(subsEntry.subsItem.id, e)
}.toImmutableMap() }
} }
LogUtils.d("检测更新失败", e) 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_stdlib_common = { module = "org.jetbrains.kotlin:kotlin-stdlib-common", version.ref = "kotlin" }
kotlin_test = { module = "org.jetbrains.kotlin:kotlin-test", 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_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_core = { module = "io.ktor:ktor-server-core", version.ref = "ktor" }
ktor_server_cio = { module = "io.ktor:ktor-server-cio", 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" } 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 import kotlin.js.JsExport
@JsExport @JsExport
data class SyntaxError internal constructor( data class SyntaxError @JsExport.Ignore constructor(
val expectedValue: String, val expectedValue: String,
val position: Int, val position: Int,
val source: String, 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, override val value: String,
) : ValueExpression(value, "var") ) : ValueExpression(value, "var")
data class Identifier internal constructor( data class Identifier(
override val start: Int, override val start: Int,
val name: String, val name: String,
) : Variable(name) { ) : Variable(name) {
@ -35,7 +35,7 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
val isNotEqual = name == "notEqual" val isNotEqual = name == "notEqual"
} }
data class MemberExpression internal constructor( data class MemberExpression(
override val start: Int, override val start: Int,
override val end: Int, override val end: Int,
val object0: Variable, val object0: Variable,
@ -61,7 +61,7 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
val isPropertyIfElse = property == "ifElse" val isPropertyIfElse = property == "ifElse"
} }
data class CallExpression internal constructor( data class CallExpression(
override val start: Int, override val start: Int,
override val end: Int, override val end: Int,
val callee: Variable, val callee: Variable,
@ -173,26 +173,26 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Posi
get() = emptyArray() get() = emptyArray()
} }
data class NullLiteral internal constructor( data class NullLiteral(
override val start: Int, override val start: Int,
) : LiteralExpression(null, "null") { ) : LiteralExpression(null, "null") {
override val end = start + 4 override val end = start + 4
} }
data class BooleanLiteral internal constructor( data class BooleanLiteral(
override val start: Int, override val start: Int,
override val value: Boolean override val value: Boolean
) : LiteralExpression(value, "boolean") { ) : LiteralExpression(value, "boolean") {
override val end = start + if (value) 4 else 5 override val end = start + if (value) 4 else 5
} }
data class IntLiteral internal constructor( data class IntLiteral(
override val start: Int, override val start: Int,
override val end: Int, override val end: Int,
override val value: Int override val value: Int
) : LiteralExpression(value, "int") ) : LiteralExpression(value, "int")
data class StringLiteral internal constructor( data class StringLiteral @JsExport.Ignore constructor(
override val start: Int, override val start: Int,
override val end: Int, override val end: Int,
override val value: String, override val value: String,

View File

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