@@ -37,6 +37,7 @@ import androidx.compose.runtime.Composable
3737import androidx.compose.runtime.getValue
3838import androidx.compose.runtime.mutableStateOf
3939import androidx.compose.runtime.remember
40+ import androidx.compose.runtime.rememberCoroutineScope
4041import androidx.compose.runtime.setValue
4142import androidx.compose.runtime.snapshots.SnapshotStateList
4243import androidx.compose.ui.Alignment
@@ -72,6 +73,8 @@ import compose.icons.tablericons.InfoCircle
7273import compose.icons.tablericons.Qrcode
7374import compose.icons.tablericons.Server
7475import compose.icons.tablericons.Wifi
76+ import kotlinx.coroutines.delay
77+ import kotlinx.coroutines.launch
7578import org.koin.androidx.compose.koinViewModel
7679
7780data 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
428432private 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)
0 commit comments