Skip to content

📡 A lightweight Kotlin Multiplatform library to monitor network state changes

License

Notifications You must be signed in to change notification settings

mirego/konnectivity


A lightweight Kotlin Multiplatform library to monitor network state changes

Installation

Gradle

Add the dependency to your common source-set dependencies:

sourceSets {
    commonMain {
        dependencies {
             api("com.mirego:konnectivity:0.3.0")
        }
    }
}

Make sure you have Mirego's public Maven in your list of repositories:

repositories {
    maven("https://s3.amazonaws.com/mirego-maven/public")
}

Using the Kotlin CocoaPods Plugin, add the Reachability pod dependency:

kotlin {
    cocoapods {
        pod("Reachability", "~> 3.2")
    }
} 

Usage

import com.mirego.konnectivity.Konnectivity
import com.mirego.konnectivity.NetworkState
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

val konnectivity = Konnectivity()

konnectivity.networkState
    .onEach { networkState ->
        when (networkState) {
            is NetworkState.Reachable -> when {
                networkState.metered -> println("You're online, but your connection is metered.")
                else -> println("You're online!")
            }
            NetworkState.Unreachable -> println("You're offline.")
        }
    }
    .launchIn(MainScope())

License

Konnectivity is © 2023 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.