Skip to content

Commit

Permalink
Fix rmonitoring status (#19)
Browse files Browse the repository at this point in the history
closes #18
  • Loading branch information
jordond authored Jun 18, 2024
1 parent 4a2ab3f commit bb338db
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 37 deletions.
45 changes: 22 additions & 23 deletions .run/demo.desktop.run.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration name="demo.desktop" default="false" factoryName="Gradle"
type="GradleRunConfiguration">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="demo:composeApp:run" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
<configuration default="false" name="demo.desktop" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value=":demo:composeApp:run" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import android.net.NetworkCapabilities
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
import android.net.NetworkCapabilities.TRANSPORT_WIFI
import android.net.NetworkRequest
import android.os.Build
import androidx.core.content.getSystemService
import dev.jordond.connectivity.Connectivity
import dev.jordond.connectivity.ConnectivityProvider
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOf
Expand Down Expand Up @@ -49,9 +50,31 @@ internal class AndroidConnectivityProvider(
}
}

manager.registerNetworkCallback(networkRequest, networkCallback)
try {
manager.registerNetworkCallback(networkRequest, networkCallback)

awaitClose { manager.unregisterNetworkCallback(networkCallback) }
val initialStatus = manager.initialStatus()
trySend(initialStatus)

awaitCancellation()
} finally {
manager.unregisterNetworkCallback(networkCallback)
}
}
}

private fun ConnectivityManager.initialStatus(): Connectivity.Status {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activeNetwork?.let { network ->
getNetworkCapabilities(network)?.let { capabilities ->
status(capabilities)
}
} ?: Connectivity.Status.Disconnected
} else {
@Suppress("DEPRECATION")
val isConnected = activeNetworkInfo?.isConnected == true
if (isConnected) Connectivity.Status.Connected(isActiveNetworkMetered)
else Connectivity.Status.Disconnected
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlinx.coroutines.CoroutineScope
*/
@Composable
public fun rememberConnectivityState(
options: ConnectivityOptions = ConnectivityOptions(),
options: ConnectivityOptions = remember { ConnectivityOptions() },
scope: CoroutineScope = rememberCoroutineScope(),
): ConnectivityState {
val connectivity = remember(options, scope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import kotlinx.coroutines.CoroutineScope
*/
@Composable
public fun rememberConnectivityState(
options: HttpConnectivityOptions = HttpConnectivityOptions(),
options: HttpConnectivityOptions = remember { HttpConnectivityOptions() },
scope: CoroutineScope = rememberCoroutineScope(),
httpClient: HttpClient = HttpClient()
httpClient: HttpClient = remember { HttpClient() },
): ConnectivityState {
val connectivity = remember(options, scope, httpClient) {
val connectivity = remember(options, scope) {
Connectivity(options, scope, httpClient)
}

return remember(connectivity, scope) {
return remember(connectivity) {
ConnectivityState(connectivity, scope)
}
}
Expand Down
5 changes: 4 additions & 1 deletion connectivity-compose/api/android/connectivity-compose.api
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
public class dev/jordond/connectivity/compose/ConnectivityState {
public final class dev/jordond/connectivity/compose/ConnectivityState {
public static final field $stable I
public fun <init> (Ldev/jordond/connectivity/Connectivity;Lkotlinx/coroutines/CoroutineScope;)V
public fun equals (Ljava/lang/Object;)Z
public final fun forceCheck ()V
public final fun getStatus ()Ldev/jordond/connectivity/Connectivity$Status;
public fun hashCode ()I
public final fun isConnected ()Z
public final fun isDisconnected ()Z
public final fun isMetered ()Z
public final fun isMonitoring ()Z
public final fun startMonitoring ()V
public final fun stopMonitoring ()V
public fun toString ()Ljava/lang/String;
}

public final class dev/jordond/connectivity/compose/ConnectivityStateKt {
Expand Down
5 changes: 4 additions & 1 deletion connectivity-compose/api/jvm/connectivity-compose.api
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
public class dev/jordond/connectivity/compose/ConnectivityState {
public final class dev/jordond/connectivity/compose/ConnectivityState {
public static final field $stable I
public fun <init> (Ldev/jordond/connectivity/Connectivity;Lkotlinx/coroutines/CoroutineScope;)V
public fun equals (Ljava/lang/Object;)Z
public final fun forceCheck ()V
public final fun getStatus ()Ldev/jordond/connectivity/Connectivity$Status;
public fun hashCode ()I
public final fun isConnected ()Z
public final fun isDisconnected ()Z
public final fun isMetered ()Z
public final fun isMonitoring ()Z
public final fun startMonitoring ()V
public final fun stopMonitoring ()V
public fun toString ()Ljava/lang/String;
}

public final class dev/jordond/connectivity/compose/ConnectivityStateKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import dev.drewhamilton.poko.Poko
import dev.jordond.connectivity.Connectivity
import dev.jordond.connectivity.Connectivity.Status.Connected
import dev.jordond.connectivity.Connectivity.Status.Disconnected
Expand Down Expand Up @@ -40,8 +41,9 @@ public fun rememberConnectivityState(
* @property connectivity The [Connectivity] instance to use for monitoring the network status.
* @property scope The [CoroutineScope] in which to launch the network status monitoring coroutine.
*/
@Poko
@Stable
public open class ConnectivityState(
public class ConnectivityState(
private val connectivity: Connectivity,
private val scope: CoroutineScope,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.jordond.connectivity.internal

import dev.drewhamilton.poko.Poko
import dev.jordond.connectivity.Connectivity
import dev.jordond.connectivity.Connectivity.Update
import dev.jordond.connectivity.ConnectivityOptions
Expand All @@ -20,6 +21,7 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

@Poko
internal class DefaultConnectivity(
scope: CoroutineScope,
private val provider: ConnectivityProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.jordond.connectivity.internal

import dev.drewhamilton.poko.Poko
import dev.jordond.connectivity.Connectivity
import dev.jordond.connectivity.Connectivity.Update
import dev.jordond.connectivity.HttpConnectivityOptions
Expand Down Expand Up @@ -27,6 +28,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

@Poko
internal class HttpConnectivity(
scope: CoroutineScope,
private val httpOptions: HttpConnectivityOptions,
Expand Down
2 changes: 2 additions & 0 deletions demo/composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
2 changes: 1 addition & 1 deletion demo/composeApp/src/httpMain/kotlin/HomeScreen.http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ internal actual fun createConnectivityState(): ConnectivityState {
return rememberConnectivityState {
autoStart = true
url("www.google.com")
pollingIntervalMs = 1.minutes
pollingIntervalMs = 5.seconds
}
}
4 changes: 2 additions & 2 deletions demo/composeApp/src/httpMain/kotlin/Platform.http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import dev.jordond.connectivity.Connectivity

actual fun platformConnectivity(): Connectivity {
return Connectivity {
pollingIntervalMs = 1000
pollingIntervalMs = 5000
onPollResult { response ->
Logger.d { "Poll Result: ${response}" }
Logger.d { "Poll Result: $response" }
}
}
}

0 comments on commit bb338db

Please sign in to comment.