NetworkMonitor

class NetworkMonitor(context: Context)

Observes device connectivity via ConnectivityManager. The Kotlin counterpart of SwiftyNetwork's NetworkMonitor (which wraps NWPathMonitor).

status is NetworkReachability.UNKNOWN until start is called. updates is a hot StateFlow that replays the current value to new collectors and then emits changes — the Kotlin analog of SwiftyNetwork's AsyncStream.

Constructors

Link copied to clipboard
constructor(context: Context)

Properties

Link copied to clipboard

Whether the network is currently reachable.

Link copied to clipboard

The current reachability.

Link copied to clipboard

A hot stream of reachability changes (replays the current value on subscribe).

Functions

Link copied to clipboard
suspend fun awaitReachable()

Suspends until status becomes NetworkReachability.REACHABLE, returning immediately if it already is. Requires start to have been called; otherwise the status stays NetworkReachability.UNKNOWN and this never resumes — bound it with withTimeout for standalone use (the retry path already does).

Link copied to clipboard
fun start()

Begins monitoring. Seeds status from the current network, then listens for changes.

Link copied to clipboard
fun stop()

Stops monitoring.