Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from gkd-kit:main #1

Merged
merged 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 显示不启用规则组数量
  • Loading branch information
lisonge committed Sep 12, 2023
commit 9a7639c27cf238099a05ac838ba363a58b5a89b9
3 changes: 3 additions & 0 deletions app/src/main/java/li/songe/gkd/data/SubsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ data class SubsConfig(
@Query("SELECT * FROM subs_config WHERE type=${AppType} and subs_item_id=:subsItemId")
fun queryAppTypeConfig(subsItemId: Long): Flow<List<SubsConfig>>

@Query("SELECT * FROM subs_config WHERE type=${GroupType} and subs_item_id=:subsItemId")
fun querySubsGroupTypeConfig(subsItemId: Long,): Flow<List<SubsConfig>>

@Query("SELECT * FROM subs_config WHERE type=${GroupType} and subs_item_id=:subsItemId and app_id=:appId")
fun queryGroupTypeConfig(subsItemId: Long, appId: String): Flow<List<SubsConfig>>
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/li/songe/gkd/data/Tuple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package li.songe.gkd.data

data class Tuple3<T0, T1, T2>(
val t0: T0,
val t1: T1,
val t2: T2,
) {
override fun toString() = "($t0, $t1, $t2)"
}

data class Tuple4<T0, T1, T2, T3>(
val t0: T0,
val t1: T1,
val t2: T2,
val t3: T3,
) {
override fun toString() = "($t0, $t1, $t2, $t3)"
}

5 changes: 3 additions & 2 deletions app/src/main/java/li/songe/gkd/ui/SubsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ fun SubsPage(
LazyColumn(
verticalArrangement = Arrangement.spacedBy(0.dp), modifier = Modifier.padding(padding)
) {
itemsIndexed(appAndConfigs, { i, a -> i.toString() + a.first.id }) { _, a ->
val (appRaw, subsConfig) = a
itemsIndexed(appAndConfigs, { i, a -> i.toString() + a.t0.id }) { _, a ->
val (appRaw, subsConfig, enableSize) = a
SubsAppCard(appRaw = appRaw,
appInfo = appInfoCache[appRaw.id],
subsConfig = subsConfig,
enableSize=enableSize,
onClick = {
navController.navigate(AppItemPageDestination(subsItemId, appRaw.id))
},
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/java/li/songe/gkd/ui/SubsVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.data.Tuple3
import li.songe.gkd.db.DbSet
import li.songe.gkd.ui.destinations.SubsPageDestination
import li.songe.gkd.util.appInfoCacheFlow
Expand All @@ -22,22 +23,30 @@ class SubsVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
val subsItemFlow = DbSet.subsItemDao.queryById(args.subsItemId)
.stateIn(viewModelScope, SharingStarted.Eagerly, null)

private val subsConfigsFlow = DbSet.subsConfigDao.queryAppTypeConfig(args.subsItemId)
private val appSubsConfigsFlow = DbSet.subsConfigDao.queryAppTypeConfig(args.subsItemId)
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

private val groupSubsConfigsFlow = DbSet.subsConfigDao.querySubsGroupTypeConfig(args.subsItemId)
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val appAndConfigsFlow = combine(
subsItemFlow,
subsConfigsFlow,
appInfoCacheFlow
) { subsItem, subsConfigs, appInfoCache ->
subsItemFlow, appSubsConfigsFlow, appInfoCacheFlow, groupSubsConfigsFlow
) { subsItem, appSubsConfigs, appInfoCache, groupSubsConfigs ->
if (subsItem == null) return@combine emptyList()
val apps = (subsIdToRawFlow.value[subsItem.id]?.apps ?: emptyList()).sortedWith { a, b ->
Collator.getInstance(Locale.CHINESE)
.compare(appInfoCache[a.id]?.name ?: a.id, appInfoCache[b.id]?.name ?: b.id)
// 使用 \\uFFFF 确保 id 排在名字后面
Collator.getInstance(Locale.CHINESE).compare(
appInfoCache[a.id]?.name ?: a.name ?: ("\uFFFF" + a.id),
appInfoCache[b.id]?.name ?: b.name ?: ("\uFFFF" + b.id)
)
}
apps.map { app ->
val subsConfig = subsConfigs.find { s -> s.appId == app.id }
app to subsConfig
val subsConfig = appSubsConfigs.find { s -> s.appId == app.id }
val appGroupSubsConfigs = groupSubsConfigs.filter { s -> s.appId == app.id }
val enableSize = app.groups.count { g ->
appGroupSubsConfigs.find { s -> s.groupKey == g.key }?.enable ?: g.enable ?: true
}
Tuple3(app, subsConfig, enableSize)
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/li/songe/gkd/ui/component/SubsAppCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fun SubsAppCard(
appRaw: SubscriptionRaw.AppRaw,
appInfo: AppInfo? = null,
subsConfig: SubsConfig? = null,
enableSize: Int = appRaw.groups.count { g -> g.enable ?: true },
onClick: (() -> Unit)? = null,
onValueChange: ((Boolean) -> Unit)? = null,
) {
Expand Down Expand Up @@ -71,8 +72,14 @@ fun SubsAppCard(
overflow = TextOverflow.Ellipsis,
modifier = Modifier.fillMaxWidth()
)

val enableDesc = when (enableSize) {
0 -> "${appRaw.groups.size}组规则/${appRaw.groups.size}关闭"
appRaw.groups.size -> "${appRaw.groups.size}组规则"
else -> "${appRaw.groups.size}组规则/${enableSize}启用/${appRaw.groups.size - enableSize}关闭"
}
Text(
text = appRaw.groups.size.toString() + "组规则",
text = enableDesc,
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Ellipsis,
Expand Down