Skip to content

Commit

Permalink
perf: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 5, 2024
1 parent 49f5351 commit 2af8016
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 54 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +112,6 @@ fun deleteSubscription(vararg subsIds: Long) {
}
}
}
subsIdToRawFlow.value = newMap.toImmutableMap()
subsIdToRawFlow.value = newMap
}
}
3 changes: 1 addition & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/home/SubsManagePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -354,7 +353,7 @@ fun useSubsManagePage(): ScaffoldExt {
this[index] = subsItem.copy(order = index)
}
}
}.toImmutableList()
}
draggedFlag.value = true
}
Box(
Expand Down
13 changes: 5 additions & 8 deletions app/src/main/kotlin/li/songe/gkd/util/AppInfoState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
}
}

Expand All @@ -34,7 +31,7 @@ val orderedAppInfosFlow by lazy {
appInfoCacheFlow.map(appScope) { c ->
c.values.sortedWith { a, b ->
collator.compare(a.name, b.name)
}.toImmutableList()
}
}
}

Expand Down Expand Up @@ -92,7 +89,7 @@ private fun updateAppInfo(appId: String) {
} else {
newMap.remove(appId)
}
appInfoCacheFlow.value = newMap.toImmutableMap()
appInfoCacheFlow.value = newMap
}
}
}
Expand All @@ -116,7 +113,7 @@ suspend fun initOrResetAppInfoCache() {
}
}
}
appInfoCacheFlow.value = appMap.toImmutableMap()
appInfoCacheFlow.value = appMap
}
appRefreshingFlow.value = false
LogUtils.d("initOrResetAppInfoCache end")
Expand Down
52 changes: 22 additions & 30 deletions app/src/main/kotlin/li/songe/gkd/util/SubsState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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())
}


Expand All @@ -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) {
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions stability_config.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
li.songe.gkd.*
kotlin.collections.*

0 comments on commit 2af8016

Please sign in to comment.