Skip to content

Commit 243a6eb

Browse files
Fix server port change handling and update restart message (#259)
1 parent 920be28 commit 243a6eb

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/ui/screens/LocalNetworkServer.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import androidx.compose.runtime.Composable
3737
import androidx.compose.runtime.getValue
3838
import androidx.compose.runtime.mutableStateOf
3939
import androidx.compose.runtime.remember
40+
import androidx.compose.runtime.rememberCoroutineScope
4041
import androidx.compose.runtime.setValue
4142
import androidx.compose.runtime.snapshots.SnapshotStateList
4243
import androidx.compose.ui.Alignment
@@ -72,6 +73,8 @@ import compose.icons.tablericons.InfoCircle
7273
import compose.icons.tablericons.Qrcode
7374
import compose.icons.tablericons.Server
7475
import compose.icons.tablericons.Wifi
76+
import kotlinx.coroutines.delay
77+
import kotlinx.coroutines.launch
7578
import org.koin.androidx.compose.koinViewModel
7679

7780
data object LocalNetworkServer
@@ -89,6 +92,7 @@ fun LocalNetworkServerScreen(
8992
val serverUrl by viewModel.serverUrl.collectAsStateWithLifecycle()
9093
val serverPort by viewModel.serverPort.collectAsStateWithLifecycle()
9194
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
95+
val coroutine = rememberCoroutineScope()
9296

9397
// Track if user wants to start the server (used for permission flow)
9498
var pendingStart by remember { mutableStateOf(false) }
@@ -104,7 +108,7 @@ fun LocalNetworkServerScreen(
104108
rememberPermissionState(Manifest.permission.POST_NOTIFICATIONS) {
105109
if (pendingStart) {
106110
pendingStart = false
107-
LocalServerService.startService(context = context, port = 8080)
111+
LocalServerService.startService(context = context, port = serverPort)
108112
}
109113
}
110114
} else {
@@ -148,7 +152,7 @@ fun LocalNetworkServerScreen(
148152
verticalArrangement = Arrangement.spacedBy(12.dp),
149153
) {
150154
// Server Status Card
151-
ServerSwitch(isRunning, { pendingStart = it }, notificationPermissionState)
155+
ServerSwitch(isRunning, serverPort, { pendingStart = it }, notificationPermissionState)
152156

153157
// Port Configuration Card
154158
PortConfigurationCard(
@@ -378,13 +382,6 @@ fun LocalNetworkServerScreen(
378382
singleLine = true,
379383
modifier = Modifier.fillMaxWidth(),
380384
)
381-
if (isRunning) {
382-
Text(
383-
text = stringResource(R.string.port_changed_restart),
384-
style = MaterialTheme.typography.bodySmall,
385-
color = MaterialTheme.colorScheme.error,
386-
)
387-
}
388385
}
389386
},
390387
confirmButton = {
@@ -394,6 +391,13 @@ fun LocalNetworkServerScreen(
394391
if (port != null && port in 1024..65535) {
395392
viewModel.setServerPort(port)
396393
showPortDialog = false
394+
if (isRunning) {
395+
coroutine.launch {
396+
LocalServerService.stopService(context)
397+
delay(200)
398+
LocalServerService.startService(context = context, port = port)
399+
}
400+
}
397401
Toast
398402
.makeText(
399403
context,
@@ -427,6 +431,7 @@ fun LocalNetworkServerScreen(
427431
@Composable
428432
private fun ServerSwitch(
429433
isRunning: Boolean = false,
434+
currentPort: Int = 8080,
430435
setPendingStart: (Boolean) -> Unit = {},
431436
notificationPermissionState: PermissionState? = null,
432437
) {
@@ -513,7 +518,7 @@ private fun ServerSwitch(
513518
setPendingStart(true)
514519
notificationPermissionState.launchPermissionRequest()
515520
} else {
516-
LocalServerService.startService(context = context, port = 8080)
521+
LocalServerService.startService(context = context, port = currentPort)
517522
}
518523
} else {
519524
LocalServerService.stopService(context)

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
<string name="default_port">Default: 8080</string>
222222
<string name="port_range_hint">Enter port (1024–65535)</string>
223223
<string name="invalid_port">Invalid port number. Must be between 1024 and 65535.</string>
224-
<string name="port_changed_restart">Port changed. Please restart the server to apply changes.</string>
224+
<string name="port_changed_restart">Port changed. Server restarting...</string>
225225
<string name="change_port">Change Port</string>
226226

227227
<!-- Transfer Link Server -->

0 commit comments

Comments
 (0)