Skip to content

Commit

Permalink
perf: update config performance
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 8, 2024
1 parent 58f367b commit bbd52a2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 10 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ android {
}
}

lint {
disable.add("ModifierFactoryUnreferencedReceiver")
}
lint {}

val currentSigning = if (project.hasProperty("GKD_STORE_FILE")) {
signingConfigs.create("release") {
Expand Down Expand Up @@ -131,6 +129,7 @@ android {
jvmTarget = JavaVersion.VERSION_17.majorVersion
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
freeCompilerArgs += "-opt-in=kotlinx.coroutines.FlowPreview"
freeCompilerArgs += "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
freeCompilerArgs += "-opt-in=androidx.compose.material3.ExperimentalMaterial3Api"
freeCompilerArgs += "-opt-in=androidx.compose.foundation.ExperimentalFoundationApi"
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/CategoryConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ data class CategoryConfig(
@Query("SELECT * FROM category_config WHERE subs_item_id IN (:subsItemIds)")
suspend fun querySubsItemConfig(subsItemIds: List<Long>): List<CategoryConfig>

@Query("SELECT * FROM category_config WHERE subs_item_id IN (:subsItemIds)")
fun queryBySubsIds(subsItemIds: List<Long>): Flow<List<CategoryConfig>>

}
}
6 changes: 6 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/SubsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ data class SubsConfig(
@Query("SELECT * FROM subs_config WHERE type=${GlobalGroupType} AND subs_item_id=:subsItemId AND group_key=:groupKey")
fun queryGlobalGroupTypeConfig(subsItemId: Long, groupKey: Int): Flow<List<SubsConfig>>

@Query("SELECT * FROM subs_config WHERE type=${AppGroupType} AND app_id=:appId AND subs_item_id IN (:subsItemIds)")
fun queryAppConfig(subsItemIds: List<Long>, appId: String): Flow<List<SubsConfig>>

@Query("SELECT * FROM subs_config WHERE type=${GlobalGroupType} AND subs_item_id IN (:subsItemIds)")
fun queryGlobalConfig(subsItemIds: List<Long>): Flow<List<SubsConfig>>

@Query("SELECT * FROM subs_config WHERE subs_item_id IN (:subsItemIds) ")
suspend fun querySubsItemConfig(subsItemIds: List<Long>): List<SubsConfig>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/li/songe/gkd/ui/AppConfigPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fun AppConfigPage(appId: String) {
AlertDialog(
title = { Text(text = "内置禁用") },
text = {
Text(text = "此规则组已经在其 apps 字段中配置对当前应用的禁用, 因此无法手动开启规则组\n\n提示: 这种情况一般在此全局规则无法适配/跳过适配当前应用时出现")
Text(text = "此规则组已经在其 apps 字段中配置对当前应用的禁用, 因此无法手动开启规则组\n\n提示: 这种情况一般在此全局规则无法适配/跳过适配/单独适配当前应用时出现")
},
onDismissRequest = { vm.innerDisabledDlgFlow.value = false },
confirmButton = {
Expand Down
96 changes: 90 additions & 6 deletions app/src/main/kotlin/li/songe/gkd/ui/AppConfigVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,57 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.data.SubsConfig
import li.songe.gkd.db.DbSet
import li.songe.gkd.ui.destinations.AppConfigPageDestination
import li.songe.gkd.util.ResolvedAppGroup
import li.songe.gkd.util.ResolvedGlobalGroup
import li.songe.gkd.util.RuleSortOption
import li.songe.gkd.util.collator
import li.songe.gkd.util.ruleSummaryFlow
import li.songe.gkd.util.getGroupRawEnable
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import javax.inject.Inject

@HiltViewModel
class AppConfigVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
private val args = AppConfigPageDestination.argsFrom(stateHandle)

val innerDisabledDlgFlow = MutableStateFlow(false)

private val latestGlobalLogsFlow = DbSet.clickLogDao.queryAppLatest(
args.appId,
SubsConfig.GlobalGroupType
)

private val latestAppLogsFlow = DbSet.clickLogDao.queryAppLatest(
args.appId,
SubsConfig.AppGroupType
)

val ruleSortTypeFlow = MutableStateFlow<RuleSortOption>(RuleSortOption.Default)

val globalGroupsFlow = combine(
ruleSummaryFlow.map { r -> r.globalGroups },
private val subsFlow = combine(subsIdToRawFlow, subsItemsFlow) { subsIdToRaw, subsItems ->
subsItems.mapNotNull { if (it.enable && subsIdToRaw[it.id] != null) it to subsIdToRaw[it.id]!! else null }
}
private val rawGlobalGroups = subsFlow.map {
it.map { (subsItem, subscription) ->
subscription.globalGroups.map { g ->
ResolvedGlobalGroup(
group = g,
subsItem = subsItem,
subscription = subscription,
// secondary assignment
config = null,
)
}
}.flatten()
}
private val sortedGlobalGroupsFlow = combine(
rawGlobalGroups,
ruleSortTypeFlow,
latestGlobalLogsFlow
) { list, type, logs ->
Expand All @@ -53,8 +77,39 @@ class AppConfigVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val appGroupsFlow = combine(
ruleSummaryFlow.map { r -> r.appIdToAllGroups[args.appId] ?: emptyList() },
private val globalConfigs = subsFlow.map { subs ->
DbSet.subsConfigDao.queryGlobalConfig(subs.map { it.first.id })
}.flatMapLatest { it }
val globalGroupsFlow = combine(sortedGlobalGroupsFlow, globalConfigs) { groups, configs ->
groups.map { g ->
ResolvedGlobalGroup(
group = g.group,
subsItem = g.subsItem,
subscription = g.subscription,
config = configs.find { c -> c.subsItemId == g.subsItem.id && c.groupKey == g.group.key },
)
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

private val unsortedAppGroupsFlow = subsFlow.map {
it.mapNotNull { s ->
s.second.apps.find { a -> a.id == args.appId }?.let { app ->
app.groups.map { g ->
ResolvedAppGroup(
group = g,
subsItem = s.first,
subscription = s.second,
app = app,
// secondary assignment
config = null,
enable = false
)
}
}
}.flatten()
}
private val sortedAppGroupsFlow = combine(
unsortedAppGroupsFlow,
ruleSortTypeFlow,
latestAppLogsFlow
) { list, type, logs ->
Expand All @@ -74,7 +129,36 @@ class AppConfigVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val innerDisabledDlgFlow = MutableStateFlow(false)
private val appConfigsFlow = subsFlow.map { subs ->
DbSet.subsConfigDao.queryAppConfig(subs.map { it.first.id }, args.appId)
}.flatMapLatest { it }
private val categoryConfigsFlow = subsFlow.map { subs ->
DbSet.categoryConfigDao.queryBySubsIds(subs.map { it.first.id })
}.flatMapLatest { it }
val appGroupsFlow = combine(
sortedAppGroupsFlow,
appConfigsFlow,
categoryConfigsFlow
) { groups, configs, categoryConfigs ->
groups.map { g ->
val config =
configs.find { c -> c.subsItemId == g.subsItem.id && c.groupKey == g.group.key }
val enable = g.group.valid && getGroupRawEnable(
g.group,
config,
g.subscription.groupToCategoryMap[g.group],
categoryConfigs.find { c -> c.subsItemId == g.subsItem.id && c.categoryKey == g.subscription.groupToCategoryMap[g.group]?.key }
)
ResolvedAppGroup(
group = g.group,
subsItem = g.subsItem,
subscription = g.subscription,
app = g.app,
config = config,
enable = enable
)
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

}

0 comments on commit bbd52a2

Please sign in to comment.