perf: update config performance
Some checks failed
Build-Apk / build (push) Has been cancelled

This commit is contained in:
lisonge 2024-07-08 22:10:36 +08:00
parent 58f367b72b
commit bbd52a2f35
5 changed files with 102 additions and 10 deletions

View File

@ -77,9 +77,7 @@ android {
}
}
lint {
disable.add("ModifierFactoryUnreferencedReceiver")
}
lint {}
val currentSigning = if (project.hasProperty("GKD_STORE_FILE")) {
signingConfigs.create("release") {
@ -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"
}

View File

@ -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>>
}
}

View File

@ -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>

View File

@ -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 = {

View File

@ -7,24 +7,32 @@ 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
@ -32,8 +40,24 @@ class AppConfigVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel
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 ->
@ -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 ->
@ -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())
}