@@ -28,7 +28,9 @@ import kotlinx.coroutines.CoroutineScope
2828import kotlinx.coroutines.Dispatchers
2929import kotlinx.coroutines.Job
3030import kotlinx.coroutines.cancel
31+ import kotlinx.coroutines.flow.combine
3132import kotlinx.coroutines.flow.first
33+ import kotlinx.coroutines.launch
3234import kotlinx.coroutines.runBlocking
3335
3436/* *
@@ -43,6 +45,7 @@ class AirSyncService : Service() {
4345 private var isScanning = false
4446
4547 private var webDavServer: WebDavServer ? = null
48+ private var webDavJob: Job ? = null
4649
4750 // Network state tracking
4851 private var networkCallback: ConnectivityManager .NetworkCallback ? = null
@@ -119,6 +122,27 @@ class AirSyncService : Service() {
119122 webDavServer = null
120123 }
121124
125+ private fun monitorWebDavRequirements () {
126+ webDavJob?.cancel()
127+ webDavJob = scope.launch {
128+ val dataStoreManager = DataStoreManager .getInstance(applicationContext)
129+ combine(
130+ dataStoreManager.isFileAccessEnabled(),
131+ dataStoreManager.getLastConnectedDevice()
132+ ) { isEnabled, device ->
133+ Log .d(TAG , " WebDAV flow evaluation: isEnabled=$isEnabled , isPlus=${device?.isPlus} " )
134+ isEnabled && device?.isPlus == true
135+ }.collect { shouldStart ->
136+ Log .d(TAG , " WebDAV requirement state updated: shouldStart = $shouldStart " )
137+ if (shouldStart) {
138+ startWebDavServer()
139+ } else {
140+ stopWebDavServer()
141+ }
142+ }
143+ }
144+ }
145+
122146 private fun handleAppForeground () {
123147 if (isScanning) {
124148 Log .d(TAG , " App in foreground, switching to ACTIVE discovery" )
@@ -156,11 +180,13 @@ class AirSyncService : Service() {
156180 UDPDiscoveryManager .setDiscoveryMode(this , DiscoveryMode .PASSIVE )
157181
158182 WakeupService .startService(this )
159- startWebDavServer ()
183+ monitorWebDavRequirements ()
160184 }
161185
162186 private fun stopSync () {
163187 Log .d(TAG , " Stopping AirSync foreground service" )
188+ webDavJob?.cancel()
189+ webDavJob = null
164190 stopWebDavServer()
165191 ShortcutUtil .refreshShortcuts(this , false )
166192 UDPDiscoveryManager .stop(this )
0 commit comments