aboutsummaryrefslogtreecommitdiff
path: root/shared/src/iosMain/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src/iosMain/kotlin')
-rw-r--r--shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt25
1 files changed, 25 insertions, 0 deletions
diff --git a/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt b/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt
new file mode 100644
index 0000000..d112c11
--- /dev/null
+++ b/shared/src/iosMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt
@@ -0,0 +1,25 @@
+package mx.trackermap.TrackerMap.controllers
+
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.asStateFlow
+import mx.trackermap.TrackerMap.Injectable
+import platform.Network.*
+import platform.darwin.*
+
+actual class NetworkController: Injectable {
+ private val monitor = nw_path_monitor_create()
+ private val queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND.toLong(), 0u)
+ private val _networkAvailable = MutableStateFlow<Boolean?>(null)
+ actual val networkAvailable = _networkAvailable.asStateFlow()
+
+ private val updateHandler: nw_path_monitor_update_handler_t = { path: nw_path_t ->
+ val status = nw_path_get_status(path)
+ _networkAvailable.value = status in arrayOf(nw_path_status_satisfied, nw_path_status_satisfiable)
+ }
+
+ init {
+ nw_path_monitor_set_update_handler(monitor, updateHandler)
+ nw_path_monitor_set_queue(monitor, queue)
+ nw_path_monitor_start(monitor)
+ }
+} \ No newline at end of file