Skip to content

Commit

Permalink
Chore: Remove deprecated Connectivity code (#92)
Browse files Browse the repository at this point in the history
**BREAKING CHANGES**

- Removed `Connectivity.Update` class, `Connectivity.updates` flow and `Connectivity.activeUpdates`.
    - This class was removed so you need to use `Connectivity.status`
    - To get the monitoring status use `Connectivity.monitoring` or `Connectivity.isMonitoring`.
- `Connectivity.isMonitoring: StateFlow<Boolean>` renamed to `Connectivity.monitoring`
- Added `Connectivity.isMonitoring: Boolean` to replace the `isMonitoring` from `Connectivity.Update`
  • Loading branch information
jordond authored Jan 3, 2025
1 parent 4082c86 commit 331d24f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ConnectivityState(
private val scope: CoroutineScope,
) {

public var isMonitoring: Boolean by mutableStateOf(connectivity.isMonitoring.value)
public var isMonitoring: Boolean by mutableStateOf(connectivity.monitoring.value)
private set

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ public class ConnectivityState(

init {
scope.launch {
connectivity.isMonitoring.collect { isMonitoring = it }
connectivity.monitoring.collect { isMonitoring = it }
}

scope.launch {
Expand Down
20 changes: 3 additions & 17 deletions connectivity-core/api/android/connectivity-core.api
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
public abstract interface class dev/jordond/connectivity/Connectivity {
public abstract fun getActiveUpdates ()Lkotlinx/coroutines/flow/Flow;
public abstract fun getMonitoring ()Lkotlinx/coroutines/flow/StateFlow;
public abstract fun getStatusUpdates ()Lkotlinx/coroutines/flow/SharedFlow;
public abstract fun getUpdates ()Lkotlinx/coroutines/flow/StateFlow;
public abstract fun isMonitoring ()Lkotlinx/coroutines/flow/StateFlow;
public abstract fun isMonitoring ()Z
public abstract fun start ()V
public abstract fun status (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun stop ()V
}

public final class dev/jordond/connectivity/Connectivity$DefaultImpls {
public static fun getActiveUpdates (Ldev/jordond/connectivity/Connectivity;)Lkotlinx/coroutines/flow/Flow;
public static fun isMonitoring (Ldev/jordond/connectivity/Connectivity;)Z
}

public abstract interface class dev/jordond/connectivity/Connectivity$Status {
Expand Down Expand Up @@ -48,19 +47,6 @@ public final class dev/jordond/connectivity/Connectivity$Status$Disconnected : d
public fun toString ()Ljava/lang/String;
}

public final class dev/jordond/connectivity/Connectivity$Update {
public fun <init> (ZLdev/jordond/connectivity/Connectivity$Status;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getStatus ()Ldev/jordond/connectivity/Connectivity$Status;
public fun hashCode ()I
public final fun isActive ()Z
public final fun isConnected ()Z
public final fun isDisconnected ()Z
public final fun isMetered ()Z
public final fun isMonitoring ()Z
public fun toString ()Ljava/lang/String;
}

public final class dev/jordond/connectivity/ConnectivityKt {
public static final fun Connectivity (Ldev/jordond/connectivity/ConnectivityProvider;Ldev/jordond/connectivity/ConnectivityOptions;Lkotlinx/coroutines/CoroutineScope;)Ldev/jordond/connectivity/Connectivity;
public static final fun Connectivity (Ldev/jordond/connectivity/ConnectivityProvider;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function1;)Ldev/jordond/connectivity/Connectivity;
Expand Down
20 changes: 3 additions & 17 deletions connectivity-core/api/jvm/connectivity-core.api
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
public abstract interface class dev/jordond/connectivity/Connectivity {
public abstract fun getActiveUpdates ()Lkotlinx/coroutines/flow/Flow;
public abstract fun getMonitoring ()Lkotlinx/coroutines/flow/StateFlow;
public abstract fun getStatusUpdates ()Lkotlinx/coroutines/flow/SharedFlow;
public abstract fun getUpdates ()Lkotlinx/coroutines/flow/StateFlow;
public abstract fun isMonitoring ()Lkotlinx/coroutines/flow/StateFlow;
public abstract fun isMonitoring ()Z
public abstract fun start ()V
public abstract fun status (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun stop ()V
}

public final class dev/jordond/connectivity/Connectivity$DefaultImpls {
public static fun getActiveUpdates (Ldev/jordond/connectivity/Connectivity;)Lkotlinx/coroutines/flow/Flow;
public static fun isMonitoring (Ldev/jordond/connectivity/Connectivity;)Z
}

public abstract interface class dev/jordond/connectivity/Connectivity$Status {
Expand Down Expand Up @@ -48,19 +47,6 @@ public final class dev/jordond/connectivity/Connectivity$Status$Disconnected : d
public fun toString ()Ljava/lang/String;
}

public final class dev/jordond/connectivity/Connectivity$Update {
public fun <init> (ZLdev/jordond/connectivity/Connectivity$Status;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getStatus ()Ldev/jordond/connectivity/Connectivity$Status;
public fun hashCode ()I
public final fun isActive ()Z
public final fun isConnected ()Z
public final fun isDisconnected ()Z
public final fun isMetered ()Z
public final fun isMonitoring ()Z
public fun toString ()Ljava/lang/String;
}

public final class dev/jordond/connectivity/ConnectivityKt {
public static final fun Connectivity (Ldev/jordond/connectivity/ConnectivityProvider;Ldev/jordond/connectivity/ConnectivityOptions;Lkotlinx/coroutines/CoroutineScope;)Ldev/jordond/connectivity/Connectivity;
public static final fun Connectivity (Ldev/jordond/connectivity/ConnectivityProvider;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function1;)Ldev/jordond/connectivity/Connectivity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
package dev.jordond.connectivity

import dev.drewhamilton.poko.Poko
import dev.jordond.connectivity.Connectivity.Status.Connected
import dev.jordond.connectivity.Connectivity.Status.Disconnected
import dev.jordond.connectivity.internal.DefaultConnectivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow

/**
* The Connectivity interface provides a way to monitor the network connectivity status.
*
* @property statusUpdates A [SharedFlow] representing the current connectivity status.
* @property isMonitoring A [StateFlow] representing whether the connectivity monitoring is active.
* @property monitoring A [StateFlow] representing whether the connectivity monitoring is active.
* @property updates A [StateFlow] representing the current connectivity status and whether
* the monitoring is active.
*/
public interface Connectivity {

public val statusUpdates: SharedFlow<Status>

public val isMonitoring: StateFlow<Boolean>
public val monitoring: StateFlow<Boolean>

@Deprecated(
message = "Use statusUpdates instead. Will be removed in a future release.",
replaceWith = ReplaceWith("statusUpdates"),
level = DeprecationLevel.WARNING,
)
public val updates: StateFlow<Update>

/**
* A [Flow] representing status updates when the connectivity monitoring is active.
*/
@Deprecated(
message = "Use statusUpdates instead. Will be removed in a future release.",
replaceWith = ReplaceWith("statusUpdates"),
level = DeprecationLevel.WARNING,
)
public val activeUpdates: Flow<Status>
get() = statusUpdates
public val isMonitoring: Boolean
get() = monitoring.value

/**
* Gets the current connectivity status.
Expand All @@ -59,59 +40,6 @@ public interface Connectivity {
*/
public fun stop()

/**
* Represents an update to the connectivity status.
*
* @property isMonitoring A Boolean indicating whether the connectivity monitoring is active.
* @property status The [Status] of the connectivity.
* @constructor Creates an update to the connectivity status.
*/
@Deprecated(
message = "This current usage of this class does not provide any additional value. Will be removed in a future release.",
level = DeprecationLevel.WARNING,
)
@Poko
public class Update(
public val isMonitoring: Boolean,
public val status: Status,
) {

/**
* A Boolean indicating whether the connectivity monitoring is active.
*/
@Deprecated(
message = "Use isMonitoring instead. Will be removed in a future release.",
replaceWith = ReplaceWith("isMonitoring"),
level = DeprecationLevel.WARNING,
)
public val isActive: Boolean = isMonitoring

/**
* A Boolean indicating whether the device is connected to the network.
*/
public val isConnected: Boolean
get() = status is Connected

/**
* A Boolean indicating whether the device is connected to a metered network.
*/
public val isMetered: Boolean
get() = status is Connected && status.metered

/**
* A Boolean indicating whether the device is disconnected from the network.
*/
public val isDisconnected: Boolean
get() = status is Disconnected

@InternalConnectivityApi
public companion object {

@InternalConnectivityApi
public val default: Update = Update(isMonitoring = false, Disconnected)
}
}

/**
* Represents the connectivity status.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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
import dev.jordond.connectivity.ConnectivityProvider
import kotlinx.coroutines.CoroutineScope
Expand All @@ -11,13 +10,10 @@ import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

Expand All @@ -36,18 +32,8 @@ internal class DefaultConnectivity(
)
override val statusUpdates: SharedFlow<Connectivity.Status> = _statusUpdates.asSharedFlow()

private val _isMonitoring = MutableStateFlow(false)
override val isMonitoring: StateFlow<Boolean> = _isMonitoring.asStateFlow()

@Deprecated("Use statusUpdates instead", ReplaceWith("statusUpdates"))
override val updates: StateFlow<Update> =
combine(statusUpdates, isMonitoring) { status, isMonitoring ->
Update(isMonitoring, status)
}.stateIn(
scope = scope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = Update(isMonitoring = false, Connectivity.Status.Disconnected)
)
private val _monitoring = MutableStateFlow(false)
override val monitoring: StateFlow<Boolean> = _monitoring.asStateFlow()

init {
if (options.autoStart) {
Expand All @@ -64,7 +50,7 @@ internal class DefaultConnectivity(
override fun start() {
job?.cancel()
job = launch {
_isMonitoring.update { true }
_monitoring.update { true }
provider.monitor().collect { status ->
_statusUpdates.emit(status)
}
Expand All @@ -74,6 +60,6 @@ internal class DefaultConnectivity(
override fun stop() {
job?.cancel()
job = null
_isMonitoring.update { false }
_monitoring.update { false }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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
import dev.jordond.connectivity.PollResult
import io.ktor.client.HttpClient
Expand All @@ -18,12 +17,9 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
Expand All @@ -43,22 +39,8 @@ internal class HttpConnectivity(
)
override val statusUpdates: SharedFlow<Connectivity.Status> = _statusUpdates.asSharedFlow()

private val _isMonitoring = MutableStateFlow(value = false)
override val isMonitoring: StateFlow<Boolean> = _isMonitoring.asStateFlow()

@Deprecated(
message = "Use statusUpdates instead. Will be removed in a future release.",
replaceWith = ReplaceWith("statusUpdates"),
level = DeprecationLevel.WARNING,
)
override val updates: StateFlow<Update> =
combine(statusUpdates, isMonitoring) { status, isMonitoring ->
Update(isMonitoring, status)
}.stateIn(
scope = scope,
started = SharingStarted.WhileSubscribed(),
initialValue = Update(isMonitoring = false, Connectivity.Status.Disconnected)
)
private val _monitoring = MutableStateFlow(value = false)
override val monitoring: StateFlow<Boolean> = _monitoring.asStateFlow()

init {
if (httpOptions.options.autoStart) {
Expand All @@ -75,13 +57,13 @@ internal class HttpConnectivity(
override fun start() {
if (job != null) return
poll()
_isMonitoring.update { true }
_monitoring.update { true }
}

override fun stop() {
job?.cancel()
job = null
_isMonitoring.update { false }
_monitoring.update { false }
}

internal fun forcePoll() {
Expand Down
16 changes: 11 additions & 5 deletions demo/composeApp/src/commonMain/kotlin/HomeModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ class HomeModel : StateScreenModel<HomeModel.State>(State()) {

init {
screenModelScope.launch(Dispatchers.Default) {
connectivity.updates.collect { update ->
connectivity.monitoring.collect { isMonitoring ->
Logger.i { "Connectivity isMonitoring: $isMonitoring" }
updateState { state ->
state.copy(monitoring = isMonitoring)
}
}
}

screenModelScope.launch(Dispatchers.Default) {
connectivity.statusUpdates.collect { update ->
Logger.i { "Connectivity update: $update" }
updateState { state ->
state.copy(
monitoring = update.isActive,
status = if (update.isActive) update.status else state.status,
)
state.copy(status = update)
}
}
}
Expand Down

0 comments on commit 331d24f

Please sign in to comment.