Skip to content

Commit

Permalink
Fix iOS device implementation (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordond authored Jun 25, 2024
1 parent 3ac07f0 commit aac71b3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import platform.Network.nw_interface_type_wifi
import platform.Network.nw_path_get_status
import platform.Network.nw_path_is_constrained
import platform.Network.nw_path_is_expensive
import platform.Network.nw_path_monitor_cancel
import platform.Network.nw_path_monitor_create
import platform.Network.nw_path_monitor_set_queue
import platform.Network.nw_path_monitor_set_update_handler
import platform.Network.nw_path_monitor_start
import platform.Network.nw_path_status_satisfied
import platform.Network.nw_path_uses_interface_type
import platform.NetworkExtension.NWPath
import platform.NetworkExtension.NWPathStatus
import platform.darwin.DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL
import platform.darwin.dispatch_queue_create

Expand All @@ -29,16 +30,17 @@ internal object AppleConnectivityProvider : ConnectivityProvider {

return callbackFlow {
nw_path_monitor_set_update_handler(monitor) { path ->
val nwPath: NWPath? = path as? NWPath
val status: NWPathStatus? = nwPath?.status()
val status = nw_path_get_status(path)
when {
status != null && status == nw_path_status_satisfied.toLong() -> {
status == nw_path_status_satisfied -> {
val isWifi = nw_path_uses_interface_type(path, nw_interface_type_wifi)
val isMetered = !isWifi && (path.isExpensive() || path.isConstrained())
val isExpensive = nw_path_is_expensive(path)
val isConstrained = nw_path_is_constrained(path)
val isMetered = !isWifi && (isExpensive || isConstrained)

trySend(Connectivity.Status.Connected(isMetered))
}
else -> Connectivity.Status.Disconnected
else -> trySend(Connectivity.Status.Disconnected)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class DefaultConnectivity(
Update(isMonitoring, status)
}.stateIn(
scope = scope,
started = SharingStarted.WhileSubscribed(),
started = SharingStarted.WhileSubscribed(5000),
initialValue = Update(isMonitoring = false, Connectivity.Status.Disconnected)
)

Expand Down
4 changes: 2 additions & 2 deletions demo/composeApp/src/commonMain/kotlin/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HomeScreen : Screen {
)

Card {
val connectivityState = createConnectivityState()
val connectivityState = createConnectivityState(false)
ConnectivityContent(
title = "Compose Connectivity",
status = connectivityState.status,
Expand All @@ -62,7 +62,7 @@ class HomeScreen : Screen {
}

@Composable
internal expect fun createConnectivityState(): ConnectivityState
internal expect fun createConnectivityState(autoStart: Boolean = true): ConnectivityState

@Composable
private fun ConnectivityContent(
Expand Down
4 changes: 2 additions & 2 deletions demo/composeApp/src/deviceMain/kotlin/HomeScreen.device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import dev.jordond.connectivity.compose.ConnectivityState
import dev.jordond.connectivity.compose.rememberConnectivityState

@Composable
internal actual fun createConnectivityState(): ConnectivityState {
internal actual fun createConnectivityState(autoStart: Boolean): ConnectivityState {
return rememberConnectivityState {
autoStart = true
this.autoStart = autoStart
}
}
4 changes: 2 additions & 2 deletions demo/composeApp/src/httpMain/kotlin/HomeScreen.http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import dev.jordond.connectivity.compose.ConnectivityState
import dev.jordond.connectivity.compose.rememberConnectivityState

@Composable
internal actual fun createConnectivityState(): ConnectivityState {
internal actual fun createConnectivityState(autoStart: Boolean): ConnectivityState {
return rememberConnectivityState {
autoStart = true
this.autoStart = autoStart
url("www.google.com")
pollingIntervalMs = 5.seconds
}
Expand Down
6 changes: 3 additions & 3 deletions demo/iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
058557BA273AAA24004C7B11 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
2152FB032600AC8F00CF470E /* iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSApp.swift; sourceTree = "<group>"; };
7555FF7B242A565900829871 /* iosApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; name = iosApp.app; path = demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
7555FF7B242A565900829871 /* demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AB3632DC29227652001CCB65 /* Config.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -62,7 +62,7 @@
7555FF7C242A565900829871 /* Products */ = {
isa = PBXGroup;
children = (
7555FF7B242A565900829871 /* iosApp.app */,
7555FF7B242A565900829871 /* demo.app */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -107,7 +107,7 @@
packageProductDependencies = (
);
productName = iosApp;
productReference = 7555FF7B242A565900829871 /* iosApp.app */;
productReference = 7555FF7B242A565900829871 /* demo.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
Expand Down

0 comments on commit aac71b3

Please sign in to comment.