Skip to content

Commit e81f24a

Browse files
committed
perf: update text
1 parent c1b908b commit e81f24a

File tree

6 files changed

+42
-33
lines changed

6 files changed

+42
-33
lines changed

app/src/main/kotlin/li/songe/gkd/service/HttpService.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class HttpService : Service(), OnSimpleLife {
7979

8080
val scope = useScope()
8181

82-
private val httpServerPortFlow = storeFlow.mapState(scope) { s -> s.httpServerPort }
82+
val httpServerPortFlow = storeFlow.mapState(scope) { s -> s.httpServerPort }
8383

8484
init {
8585
useLogLifecycle()
@@ -88,7 +88,9 @@ class HttpService : Service(), OnSimpleLife {
8888
StopServiceReceiver.autoRegister()
8989
onCreated {
9090
scope.launchTry(Dispatchers.IO) {
91-
localNetworkIpsFlow.value = getIpAddressInLocalNetwork()
91+
httpServerPortFlow.collect {
92+
localNetworkIpsFlow.value = getIpAddressInLocalNetwork()
93+
}
9294
}
9395
}
9496
onDestroyed {
@@ -98,13 +100,16 @@ class HttpService : Service(), OnSimpleLife {
98100
httpServerFlow.value = null
99101
}
100102
onCreated {
101-
httpNotif.copy(text = "端口-${storeFlow.value.httpServerPort}").notifyService()
103+
httpNotif.notifyService()
102104
scope.launchTry(Dispatchers.IO) {
103105
httpServerPortFlow.collect { port ->
104-
httpServerFlow.value?.stop()
105-
httpServerFlow.value = null
106+
val isReboot = httpServerFlow.value != null
107+
httpServerFlow.apply {
108+
value?.stop()
109+
value = null
110+
}
106111
if (!isPortAvailable(port)) {
107-
toast("端口 $port 被占用, 请更换后重试")
112+
toast("端口 $port 被占用请更换后重试")
108113
stopSelf()
109114
return@collect
110115
}
@@ -117,8 +122,8 @@ class HttpService : Service(), OnSimpleLife {
117122
}
118123
if (httpServerFlow.value == null) {
119124
stopSelf()
120-
} else {
121-
httpNotif.copy(text = "端口-$port").notifyService()
125+
} else if (isReboot) {
126+
toast("HTTP服务重启成功")
122127
}
123128
}
124129
}

app/src/main/kotlin/li/songe/gkd/store/SettingsStore.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data class SettingsStore(
1818
val updateSubsInterval: Long = UpdateTimeOption.Everyday.value,
1919
val captureVolumeChange: Boolean = false,
2020
val toastWhenClick: Boolean = true,
21-
val clickToast: String = META.appName,
21+
val actionToast: String = META.appName,
2222
val autoClearMemorySubs: Boolean = true,
2323
val hideSnapshotStatusBar: Boolean = false,
2424
val enableDarkTheme: Boolean? = null,

app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ fun AdvancedPage() {
102102
mutableStateOf(false)
103103
}
104104
if (showEditPortDlg) {
105+
val portRange = remember { 1000 to 65535 }
106+
val placeholderText = remember { "请输入 ${portRange.first}-${portRange.second} 的整数" }
105107
var value by remember {
106108
mutableStateOf(store.httpServerPort.toString())
107109
}
@@ -112,7 +114,7 @@ fun AdvancedPage() {
112114
OutlinedTextField(
113115
value = value,
114116
placeholder = {
115-
Text(text = "请输入 5000-65535 的整数")
117+
Text(text = placeholderText)
116118
},
117119
onValueChange = {
118120
value = it.filter { c -> c.isDigit() }.take(5)
@@ -139,18 +141,16 @@ fun AdvancedPage() {
139141
enabled = value.isNotEmpty(),
140142
onClick = {
141143
val newPort = value.toIntOrNull()
142-
if (newPort == null || !(5000 <= newPort && newPort <= 65535)) {
143-
toast("请输入 5000-65535 的整数")
144+
if (newPort == null || !(portRange.first <= newPort && newPort <= portRange.second)) {
145+
toast(placeholderText)
144146
return@TextButton
145147
}
146-
storeFlow.value = store.copy(
147-
httpServerPort = newPort
148-
)
149148
showEditPortDlg = false
150-
if (HttpService.httpServerFlow.value != null) {
151-
toast("已更新,重启服务")
152-
} else {
153-
toast("已更新")
149+
if (newPort != store.httpServerPort) {
150+
storeFlow.value = store.copy(
151+
httpServerPort = newPort
152+
)
153+
toast("更新成功")
154154
}
155155
}
156156
) {

app/src/main/kotlin/li/songe/gkd/ui/home/SettingsPage.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fun useSettingsPage(): ScaffoldExt {
7373

7474
if (showToastInputDlg) {
7575
var value by remember {
76-
mutableStateOf(store.clickToast)
76+
mutableStateOf(store.actionToast)
7777
}
7878
val maxCharLen = 32
7979
AlertDialog(
@@ -104,7 +104,10 @@ fun useSettingsPage(): ScaffoldExt {
104104
onDismissRequest = { showToastInputDlg = false },
105105
confirmButton = {
106106
TextButton(enabled = value.isNotEmpty(), onClick = {
107-
storeFlow.update { it.copy(clickToast = value) }
107+
if (value != storeFlow.value.actionToast) {
108+
storeFlow.update { it.copy(actionToast = value) }
109+
toast("更新成功")
110+
}
108111
showToastInputDlg = false
109112
}) {
110113
Text(
@@ -208,14 +211,16 @@ fun useSettingsPage(): ScaffoldExt {
208211
confirmButton = {
209212
TextButton(onClick = {
210213
KeyboardUtils.hideSoftInput(activity)
211-
storeFlow.update {
212-
it.copy(
213-
customNotifTitle = titleValue,
214-
customNotifText = textValue
215-
)
214+
if (store.customNotifTitle != textValue || store.customNotifText != textValue) {
215+
storeFlow.update {
216+
it.copy(
217+
customNotifTitle = titleValue,
218+
customNotifText = textValue
219+
)
220+
}
221+
toast("更新成功")
216222
}
217223
showNotifTextInputDlg = false
218-
toast("更新成功")
219224
}) {
220225
Text(
221226
text = "确认",
@@ -259,7 +264,7 @@ fun useSettingsPage(): ScaffoldExt {
259264

260265
TextSwitch(
261266
title = "触发提示",
262-
subtitle = store.clickToast,
267+
subtitle = store.actionToast,
263268
checked = store.toastWhenClick,
264269
modifier = Modifier.clickable {
265270
showToastInputDlg = true

app/src/main/kotlin/li/songe/gkd/util/NetworkExt.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fun getIpAddressInLocalNetwork(): List<String> {
88
NetworkInterface.getNetworkInterfaces().asSequence()
99
} catch (e: Exception) {
1010
// android.system.ErrnoException: getifaddrs failed: EACCES (Permission denied)
11-
toast("获取host失败:" + e.message)
11+
toast("获取HOST失败:" + e.message)
1212
return emptyList()
1313
}
1414
val localAddresses = networkInterfaces.flatMap {
@@ -27,8 +27,7 @@ fun isPortAvailable(port: Int): Boolean {
2727
serverSocket = ServerSocket(port)
2828
serverSocket.reuseAddress = true
2929
true
30-
} catch (e: Exception) {
31-
e.printStackTrace()
30+
} catch (_: Exception) {
3231
false
3332
} finally {
3433
serverSocket?.close()

app/src/main/kotlin/li/songe/gkd/util/Toast.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ fun showActionToast() {
109109
if (t - triggerTime > triggerInterval + 100) { // 100ms 保证二次显示的时候上一次已经完全消失
110110
triggerTime = t
111111
if (storeFlow.value.useSystemToast) {
112-
showSystemToast(storeFlow.value.clickToast)
112+
showSystemToast(storeFlow.value.actionToast)
113113
} else {
114114
showA11yToast(
115-
storeFlow.value.clickToast
115+
storeFlow.value.actionToast
116116
)
117117
}
118118
}

0 commit comments

Comments
 (0)