Skip to content

Commit

Permalink
perf: dialog onDismissRequest auto close
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Aug 5, 2024
1 parent 3025a96 commit 602879d
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 64 deletions.
97 changes: 49 additions & 48 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -365,57 +364,59 @@ fun AdvancedPage() {
}

if (showPortDlg) {
Dialog(onDismissRequest = { showPortDlg = false }) {
var value by remember {
mutableStateOf(store.httpServerPort.toString())
}
AlertDialog(title = { Text(text = "服务端口") }, text = {
OutlinedTextField(
value = value,
placeholder = {
Text(text = "请输入 5000-65535 的整数")
},
onValueChange = {
value = it.filter { c -> c.isDigit() }.take(5)
},
singleLine = true,
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
supportingText = {
Text(
text = "${value.length} / 5",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.End,
)
},
)
}, onDismissRequest = { showPortDlg = false }, confirmButton = {
TextButton(
enabled = value.isNotEmpty(),
onClick = {
val newPort = value.toIntOrNull()
if (newPort == null || !(5000 <= newPort && newPort <= 65535)) {
toast("请输入 5000-65535 的整数")
return@TextButton
}
storeFlow.value = store.copy(
httpServerPort = newPort
)
showPortDlg = false
}
) {
var value by remember {
mutableStateOf(store.httpServerPort.toString())
}
AlertDialog(title = { Text(text = "服务端口") }, text = {
OutlinedTextField(
value = value,
placeholder = {
Text(text = "请输入 5000-65535 的整数")
},
onValueChange = {
value = it.filter { c -> c.isDigit() }.take(5)
},
singleLine = true,
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
supportingText = {
Text(
text = "确认", modifier = Modifier
text = "${value.length} / 5",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.End,
)
}
}, dismissButton = {
TextButton(onClick = { showPortDlg = false }) {
Text(
text = "取消"
},
)
}, onDismissRequest = {
if (value.isEmpty()) {
showPortDlg = false
}
}, confirmButton = {
TextButton(
enabled = value.isNotEmpty(),
onClick = {
val newPort = value.toIntOrNull()
if (newPort == null || !(5000 <= newPort && newPort <= 65535)) {
toast("请输入 5000-65535 的整数")
return@TextButton
}
storeFlow.value = store.copy(
httpServerPort = newPort
)
showPortDlg = false
}
})
}
) {
Text(
text = "确认", modifier = Modifier
)
}
}, dismissButton = {
TextButton(onClick = { showPortDlg = false }) {
Text(
text = "取消"
)
}
})
}
}

Expand Down
18 changes: 15 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/AppItemPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,11 @@ fun AppItemPage(
focusRequester.requestFocus()
}
},
onDismissRequest = { setEditGroupRaw(null) },
onDismissRequest = {
if (source.isEmpty()) {
setEditGroupRaw(null)
}
},
dismissButton = {
TextButton(onClick = { setEditGroupRaw(null) }) {
Text(text = "取消")
Expand Down Expand Up @@ -488,7 +492,11 @@ fun AppItemPage(
focusRequester.requestFocus()
}
},
onDismissRequest = { setExcludeGroupRaw(null) },
onDismissRequest = {
if (source.isEmpty()) {
setExcludeGroupRaw(null)
}
},
dismissButton = {
TextButton(onClick = { setExcludeGroupRaw(null) }) {
Text(text = "取消")
Expand Down Expand Up @@ -532,7 +540,11 @@ fun AppItemPage(
placeholder = { Text(text = "请输入规则组\n可以是APP规则\n也可以是单个规则组") },
maxLines = 10,
)
}, onDismissRequest = { showAddDlg = false }, confirmButton = {
}, onDismissRequest = {
if (source.isEmpty()) {
showAddDlg = false
}
}, confirmButton = {
TextButton(onClick = {
val newAppRaw = try {
RawSubscription.parseRawApp(source)
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/CategoryPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ fun CategoryPage(subsItemId: Long) {
placeholder = { Text(text = "请输入类别名称") },
singleLine = true
)
}, onDismissRequest = { setEditNameCategory(null) }, dismissButton = {
}, onDismissRequest = {
if (source.isEmpty()) {
setEditNameCategory(null)
}
}, dismissButton = {
TextButton(onClick = { setEditNameCategory(null) }) {
Text(text = "取消")
}
Expand Down Expand Up @@ -300,7 +304,11 @@ fun CategoryPage(subsItemId: Long) {
placeholder = { Text(text = "请输入类别名称") },
singleLine = true
)
}, onDismissRequest = { showAddDlg = false }, dismissButton = {
}, onDismissRequest = {
if (source.isEmpty()) {
showAddDlg = false
}
}, dismissButton = {
TextButton(onClick = { showAddDlg = false }) {
Text(text = "取消")
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/kotlin/li/songe/gkd/ui/GlobalRuleExcludePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ fun GlobalRuleExcludePage(subsItemId: Long, groupKey: Int) {
focusRequester.requestFocus()
}
},
onDismissRequest = { showEditDlg = false },
onDismissRequest = {
if (source.isEmpty()) {
showEditDlg = false
}
},
dismissButton = {
TextButton(onClick = { showEditDlg = false }) {
Text(text = "取消")
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/GlobalRulePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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.defaultMinSize
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand Down Expand Up @@ -322,7 +321,11 @@ fun GlobalRulePage(subsItemId: Long, focusGroupKey: Int? = null) {
placeholder = { Text(text = "请输入规则组") },
maxLines = 10,
)
}, onDismissRequest = { showAddDlg = false }, confirmButton = {
}, onDismissRequest = {
if (source.isEmpty()) {
showAddDlg = false
}
}, confirmButton = {
TextButton(onClick = {
val newGroup = try {
RawSubscription.parseRawGlobalGroup(source)
Expand Down Expand Up @@ -403,7 +406,11 @@ fun GlobalRulePage(subsItemId: Long, focusGroupKey: Int? = null) {
focusRequester.requestFocus()
}
},
onDismissRequest = { setEditGroupRaw(null) },
onDismissRequest = {
if (source.isEmpty()) {
setEditGroupRaw(null)
}
},
dismissButton = {
TextButton(onClick = { setEditGroupRaw(null) }) {
Text(text = "取消")
Expand Down Expand Up @@ -452,7 +459,6 @@ fun GlobalRulePage(subsItemId: Long, focusGroupKey: Int? = null) {

if (showGroupItem != null) {
AlertDialog(
modifier = Modifier.defaultMinSize(300.dp),
onDismissRequest = { setShowGroupItem(null) },
title = {
Text(text = "规则组详情")
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/SubsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ fun SubsPage(
modifier = Modifier.fillMaxWidth(),
placeholder = { Text(text = "请输入规则\n若应用规则已经存在则追加") },
)
}, onDismissRequest = { showAddDlg = false }, confirmButton = {
}, onDismissRequest = {
if (source.isEmpty()) {
showAddDlg = false
}
}, confirmButton = {
TextButton(onClick = {
val newAppRaw = try {
RawSubscription.parseRawApp(source)
Expand Down Expand Up @@ -401,7 +405,11 @@ fun SubsPage(
focusRequester.requestFocus()
}
},
onDismissRequest = { editRawApp = null }, confirmButton = {
onDismissRequest = {
if (source.isEmpty()) {
editRawApp = null
}
}, confirmButton = {
TextButton(onClick = {
try {
val newAppRaw = RawSubscription.parseRawApp(source)
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/home/SettingsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ fun useSettingsPage(): ScaffoldExt {
)
},
)
}, onDismissRequest = { showToastInputDlg = false }, confirmButton = {
}, onDismissRequest = {
if (value.isEmpty()) {
showToastInputDlg = false
}
}, confirmButton = {
TextButton(enabled = value.isNotEmpty(), onClick = {
storeFlow.update { it.copy(clickToast = value) }
showToastInputDlg = false
Expand Down Expand Up @@ -186,7 +190,11 @@ fun useSettingsPage(): ScaffoldExt {
)
},
)
}, onDismissRequest = { showNotifTextInputDlg = false }, confirmButton = {
}, onDismissRequest = {
if (value.isEmpty()) {
showNotifTextInputDlg = false
}
}, confirmButton = {
TextButton(enabled = value.isNotEmpty(), onClick = {
storeFlow.update { it.copy(customNotifText = value) }
showNotifTextInputDlg = false
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/home/SubsManagePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,17 @@ fun useSubsManagePage(): ScaffoldExt {
},
isError = link.isNotEmpty() && !URLUtil.isNetworkUrl(link),
)
}, onDismissRequest = { showAddLinkDialog = false }, confirmButton = {
}, onDismissRequest = {
if (link.isEmpty()) {
showAddLinkDialog = false
}
}, dismissButton = {
TextButton(onClick = {
showAddLinkDialog = false
}) {
Text(text = "取消")
}
}, confirmButton = {
TextButton(enabled = link.isNotBlank(), onClick = {
if (!URLUtil.isNetworkUrl(link)) {
toast("非法链接")
Expand All @@ -166,7 +176,7 @@ fun useSubsManagePage(): ScaffoldExt {
vm.addSubsFromUrl(url = link)
}
}) {
Text(text = "添加")
Text(text = "确认")
}
})
}
Expand Down

0 comments on commit 602879d

Please sign in to comment.