Skip to content

Commit

Permalink
feat: subsPowerWarn (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 20, 2024
1 parent 6f3e63a commit 7ed881a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 28 deletions.
6 changes: 5 additions & 1 deletion app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable
import li.songe.gkd.appScope
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.LOCAL_SUBS_IDS
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.subsFolder
import li.songe.gkd.util.subsIdToRawFlow
Expand All @@ -28,13 +29,16 @@ data class SubsItem(

@ColumnInfo(name = "ctime") val ctime: Long = System.currentTimeMillis(),
@ColumnInfo(name = "mtime") val mtime: Long = System.currentTimeMillis(),
@ColumnInfo(name = "enable") val enable: Boolean = true,
@ColumnInfo(name = "enable") val enable: Boolean = false,
@ColumnInfo(name = "enable_update") val enableUpdate: Boolean = true,
@ColumnInfo(name = "order") val order: Int,
@ColumnInfo(name = "update_url") val updateUrl: String? = null,

) {

val isLocal: Boolean
get() = LOCAL_SUBS_IDS.contains(id)

@Dao
interface SubsItemDao {
@Update
Expand Down
38 changes: 20 additions & 18 deletions app/src/main/kotlin/li/songe/gkd/ui/component/DialogOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

data class AlertDialogOptions(
val text: @Composable (() -> Unit)? = null,
val title: @Composable (() -> Unit)? = null,
val text: @Composable (() -> Unit)? = null,
val onDismissRequest: (() -> Unit)? = null,
val confirmButton: @Composable () -> Unit,
val dismissButton: @Composable (() -> Unit)? = null,
)

private fun buildDialogOptions(
title: String,
text: String,
title: @Composable (() -> Unit),
text: @Composable (() -> Unit),
confirmText: String,
confirmAction: () -> Unit,
dismissText: String? = null,
Expand All @@ -36,12 +36,8 @@ private fun buildDialogOptions(
error: Boolean = false,
): AlertDialogOptions {
return AlertDialogOptions(
text = {
Text(text = text)
},
title = {
Text(text = title)
},
title = title,
text = text,
onDismissRequest = onDismissRequest,
confirmButton = {
TextButton(
Expand Down Expand Up @@ -83,17 +79,18 @@ fun BuildDialog(stateFlow: MutableStateFlow<AlertDialogOptions?>) {

fun MutableStateFlow<AlertDialogOptions?>.updateDialogOptions(
title: String,
text: String,
confirmText: String = "我知道了",
text: String? = null,
textContent: (@Composable (() -> Unit))? = null,
confirmText: String = DEFAULT_IK_TEXT,
confirmAction: (() -> Unit)? = null,
dismissText: String? = null,
dismissAction: (() -> Unit)? = null,
onDismissRequest: (() -> Unit)? = null,
error: Boolean = false,
) {
value = buildDialogOptions(
title = title,
text = text,
title = { Text(text = title) },
text = textContent ?: { Text(text = text ?: error("miss text")) },
confirmText = confirmText,
confirmAction = confirmAction ?: { value = null },
dismissText = dismissText,
Expand All @@ -103,20 +100,23 @@ fun MutableStateFlow<AlertDialogOptions?>.updateDialogOptions(
)
}

const val DEFAULT_CONFIRM_TEXT = "确定"
const val DEFAULT_DISMISS_TEXT = "取消"
private const val DEFAULT_IK_TEXT = "我知道了"
private const val DEFAULT_CONFIRM_TEXT = "确定"
private const val DEFAULT_DISMISS_TEXT = "取消"

private suspend fun MutableStateFlow<AlertDialogOptions?>.getResult(
title: String,
text: String,
text: String? = null,
textContent: (@Composable (() -> Unit))? = null,
confirmText: String = DEFAULT_CONFIRM_TEXT,
dismissText: String = DEFAULT_DISMISS_TEXT,
error: Boolean = false,
): Boolean {
return suspendCoroutine { s ->
this.value = buildDialogOptions(
updateDialogOptions(
title = title,
text = text,
textContent = textContent,
onDismissRequest = {},
confirmText = confirmText,
confirmAction = {
Expand All @@ -135,14 +135,16 @@ private suspend fun MutableStateFlow<AlertDialogOptions?>.getResult(

suspend fun MutableStateFlow<AlertDialogOptions?>.waitResult(
title: String,
text: String,
text: String? = null,
textContent: (@Composable (() -> Unit))? = null,
confirmText: String = DEFAULT_CONFIRM_TEXT,
dismissText: String = DEFAULT_DISMISS_TEXT,
error: Boolean = false,
) {
val r = getResult(
title = title,
text = text,
textContent = textContent,
confirmText = confirmText,
dismissText = dismissText,
error = error,
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/component/SubsItemCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun SubsItemCard(
vm: HomeVm,
isSelectedMode: Boolean,
isSelected: Boolean,
onCheckedChange: ((Boolean) -> Unit)? = null,
onCheckedChange: ((Boolean) -> Unit),
onSelectedChange: (() -> Unit)? = null,
) {
val density = LocalDensity.current
Expand Down Expand Up @@ -210,7 +210,7 @@ fun SubsItemCard(
Switch(
checked = subsItem.enable,
enabled = !isSelectedMode,
onCheckedChange = if (isSelectedMode) null else onCheckedChange,
onCheckedChange = if (isSelectedMode) null else throttle(fn = onCheckedChange),
)
}
}
Expand Down
73 changes: 66 additions & 7 deletions app/src/main/kotlin/li/songe/gkd/ui/home/SubsManagePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -25,11 +27,13 @@ import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.Checkbox
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
Expand All @@ -49,6 +53,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -76,6 +81,7 @@ import li.songe.gkd.util.findOption
import li.songe.gkd.util.launchAsFn
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.map
import li.songe.gkd.util.openUri
import li.songe.gkd.util.saveFileToDownloads
import li.songe.gkd.util.shareFile
import li.songe.gkd.util.storeFlow
Expand Down Expand Up @@ -134,12 +140,44 @@ fun useSubsManagePage(): ScaffoldExt {
title = { Text("订阅设置") },
text = {
val store by storeFlow.collectAsState()
TextMenu(
modifier = Modifier.padding(0.dp, itemVerticalPadding),
title = "更新订阅",
option = UpdateTimeOption.allSubObject.findOption(store.updateSubsInterval)
) {
storeFlow.update { s -> s.copy(updateSubsInterval = it.value) }
Column {
TextMenu(
modifier = Modifier.padding(0.dp, itemVerticalPadding),
title = "更新订阅",
option = UpdateTimeOption.allSubObject.findOption(store.updateSubsInterval)
) {
storeFlow.update { s -> s.copy(updateSubsInterval = it.value) }
}

val updateValue = remember {
throttle(fn = {
storeFlow.update { it.copy(subsPowerWarn = !it.subsPowerWarn) }
})
}
Row(
modifier = Modifier
.padding(0.dp, itemVerticalPadding)
.clickable(onClick = updateValue),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Column(
modifier = Modifier.weight(1f)
) {
Text(
text = "耗电警告",
style = MaterialTheme.typography.bodyLarge,
)
Text(
text = "启用多条订阅时弹窗确认",
style = MaterialTheme.typography.bodySmall,
)
}
Checkbox(
checked = store.subsPowerWarn,
onCheckedChange = { updateValue() }
)
}
}
},
confirmButton = {
Expand Down Expand Up @@ -408,7 +446,28 @@ fun useSubsManagePage(): ScaffoldExt {
isSelectedMode = isSelectedMode,
isSelected = selectedIds.contains(subItem.id),
onCheckedChange = { checked ->
vm.viewModelScope.launch {
context.mainVm.viewModelScope.launch {
if (checked && storeFlow.value.subsPowerWarn && !subItem.isLocal && subsItemsFlow.value.count { !it.isLocal } > 1) {
context.mainVm.dialogFlow.waitResult(
title = "耗电警告",
textContent = {
Column {
Text(text = "启用多个远程订阅可能导致执行大量重复规则, 这可能造成规则执行卡顿以及多余耗电\n\n请认真考虑后再确认开启!!!\n")
Text(
text = "查看耗电说明",
modifier = Modifier.clickable(
onClick = throttle(
fn = { openUri("https://gkd.li?r=6") }
)
),
textDecoration = TextDecoration.Underline,
color = MaterialTheme.colorScheme.primary,
)
}
},
error = true
)
}
DbSet.subsItemDao.updateEnable(subItem.id, checked)
}
},
Expand Down
1 change: 1 addition & 0 deletions app/src/main/kotlin/li/songe/gkd/util/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ data class Store(
val subsExcludeSortType: Int = SortTypeOption.SortByName.value,
val subsExcludeShowSystemApp: Boolean = true,
val subsExcludeShowHiddenApp: Boolean = false,
val subsPowerWarn: Boolean = true,
)

val storeFlow by lazy {
Expand Down

0 comments on commit 7ed881a

Please sign in to comment.