Skip to content

Commit

Permalink
feat: reset app group enable
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Aug 5, 2024
1 parent 2381bd6 commit 05faa34
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
14 changes: 14 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 @@ -9,6 +9,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -94,6 +95,19 @@ data class SubsConfig(
@Query("SELECT * FROM subs_config WHERE subs_item_id IN (:subsItemIds) ")
suspend fun querySubsItemConfig(subsItemIds: List<Long>): List<SubsConfig>

@Query("UPDATE subs_config SET enable = null WHERE type=${AppGroupType} AND subs_item_id=:subsItemId AND app_id=:appId AND group_key=:groupKey AND enable IS NOT NULL")
suspend fun resetAppGroupTypeEnable(subsItemId: Long, appId: String, groupKey: Int): Int

@Transaction
suspend fun batchResetAppGroupEnable(
subsItemId: Long,
list: List<Pair<RawSubscription.RawAppGroup, RawSubscription.RawApp>>
): List<Pair<RawSubscription.RawAppGroup, RawSubscription.RawApp>> {
return list.filter { (g, a) ->
resetAppGroupTypeEnable(subsItemId, a.id, g.key) > 0
}
}

}

}
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/kotlin/li/songe/gkd/ui/AppItemPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ fun AppItemPage(
Spacer(modifier = Modifier.height(10.dp))
}
itemsIndexed(appRaw.groups, { i, g -> i.toString() + g.key }) { _, group ->
val subsConfig = subsConfigs.find { it.groupKey == group.key }
val groupEnable = getGroupRawEnable(
group,
subsConfig,
groupToCategoryMap[group],
categoryConfigs.find { c -> c.categoryKey == groupToCategoryMap[group]?.key }
)

Row(
modifier = Modifier
.background(
Expand Down Expand Up @@ -264,6 +272,19 @@ fun AppItemPage(
expanded = false
},
)
if (subsConfig?.enable != null) {
DropdownMenuItem(
text = {
Text(text = "重置开关")
},
onClick = {
expanded = false
vm.viewModelScope.launchTry(Dispatchers.IO) {
DbSet.subsConfigDao.insert(subsConfig.copy(enable = null))
}
},
)
}
if (editable && subsItem != null && subsRaw != null) {
DropdownMenuItem(
text = {
Expand Down Expand Up @@ -303,14 +324,6 @@ fun AppItemPage(
}

Spacer(modifier = Modifier.width(10.dp))

val groupEnable = getGroupRawEnable(
group,
subsConfigs.find { c -> c.groupKey == group.key },
groupToCategoryMap[group],
categoryConfigs.find { c -> c.categoryKey == groupToCategoryMap[group]?.key }
)
val subsConfig = subsConfigs.find { it.groupKey == group.key }
Switch(
checked = groupEnable, modifier = Modifier,
onCheckedChange = vm.viewModelScope.launchAsFn { enable ->
Expand Down
53 changes: 37 additions & 16 deletions app/src/main/kotlin/li/songe/gkd/ui/CategoryPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ fun CategoryPage(subsItemId: Long) {
Row(modifier = Modifier
.clickable { selectedExpanded = true }
.itemPadding(),
verticalAlignment = Alignment.CenterVertically) {
val size = categoriesGroups[category]?.size ?: 0
verticalAlignment = Alignment.CenterVertically
) {

val groups = categoriesGroups[category] ?: emptyList()
val size = groups.size
Column(modifier = Modifier.weight(1f)) {
Text(
text = category.name,
Expand All @@ -143,21 +146,39 @@ fun CategoryPage(subsItemId: Long) {
)
}
}
if (editable) {
var expanded by remember { mutableStateOf(false) }
Box(
modifier = Modifier.wrapContentSize(Alignment.TopStart)
var expanded by remember { mutableStateOf(false) }
Box(
modifier = Modifier.wrapContentSize(Alignment.TopStart)
) {
IconButton(onClick = {
expanded = true
}) {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = null,
)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
IconButton(onClick = {
expanded = true
}) {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = null,
)
}
DropdownMenu(expanded = expanded,
onDismissRequest = { expanded = false }) {
DropdownMenuItem(text = {
Text(text = "重置开关")
}, onClick = {
expanded = false
vm.viewModelScope.launchTry(Dispatchers.IO) {
val updatedList = DbSet.subsConfigDao.batchResetAppGroupEnable(
subsItemId,
groups
)
if (updatedList.isNotEmpty()) {
toast("成功重置 ${updatedList.size} 规则组开关")
} else {
toast("无可重置规则组")
}
}
})
if (editable) {
DropdownMenuItem(text = {
Text(text = "编辑")
}, onClick = {
Expand Down

0 comments on commit 05faa34

Please sign in to comment.