connectivity

repository·main·Indexed 20 days ago

https://github.com/jordond/connectivity

A Kotlin Multiplatform library for monitoring network connectivity across Android, Apple devices, JVM, JS, and Wasm. It provides device-based monitoring via connectivity-device and HTTP polling via connectivity-http, including support for Compose Multiplatform through rememberConnectivityState.

Tokens
2.9K
Snippets
10
Records
13
Agent score
23%

What's inside connectivity

  1. Run or build the Connectivity Demo for Android

    main

    Running on a device or emulator

    Open the project in Android Studio and execute the imported android run configuration.

    Building an application bundle

    To build a debug APK, run the following Gradle command:

    ./gradlew :composeApp:assembleDebug

    The resulting .apk file can be found at: composeApp/build/outputs/apk/debug/composeApp-debug.apk

  2. Install Connectivity for all supported platforms

    main

    To support all platforms using a hybrid approach (Device monitoring for mobile and HTTP monitoring for others), configure your source sets to use connectivity-device for Android/Apple and connectivity-http for JVM/JS/Wasm.

    kotlin {
        sourceSets {
            commonMain.dependencies {
              implementation(libs.connectivity.core)
    
              // For compose support
              implementation(libs.connectivity.compose)
            }
    
            val deviceMain by creating {
                dependsOn(commonMain.get())
                androidMain.get().dependsOn(this)
                appleMain.get().dependsOn(this)
                dependencies {
                  implementation(libs.connectivity.device)
    
                  // For compose support
                  implementation(libs.connectivity.compose.device)
                }
            }
    
            val httpMain by creating {
                dependsOn(commonMain.get())
                jvmMain.get().dependsOn(this)
                jsMain.get().dependsOn(this)
                wasmJsMain.get().dependsOn(this)
                dependencies {
                  implementation(libs.connectivity.http)
    
                  // For compose support
                  implementation(libs.connectivity.compose.http)
                }
            }
        }
    }
  3. Install Connectivity for Multiplatform (Android and Apple)

    main

    For a Kotlin Multiplatform project targeting both Android and Apple devices, add connectivity-core and connectivity-device to your commonMain source set. For Compose support, add connectivity-compose-device.

    kotlin {
        sourceSets {
            commonMain.dependencies {
              implementation(libs.connectivity.core)
              implementation(libs.connectivity.device)
    
              // For compose support
              implementation(libs.connectivity.compose.device)
            }
        }
    }
  4. Implement multiple connectivity targets using expect/actual

    main

    If your project needs to support both Device-based monitoring (for mobile) and HTTP-based monitoring (for other platforms), use the expect/actual pattern to provide the correct Connectivity implementation for each target.

    1. Define an expect function in commonMain.
    2. Implement actual for mobile targets using connectivity-device.
    3. Implement actual for other targets using connectivity-http.
    // commonMain/Platform.kt
    expect fun createConnectivity(): Connectivity
    
    // deviceMain/Platform.device.kt
    actual fun createConnectivity(): Connectivity {
      return Connectivity {
        autoStart = true
      }
    }
    
    // httpMain/Platform.http.kt
    actual fun createConnectivity(): Connectivity {
      return Connectivity {
        autoStart = true
        urls("cloudflare.com", "my-own-domain.com")
        port = 80
        pollingIntervalMs = 10.minutes
        timeoutMs = 5.seconds
      }
    }
  5. Run the Connectivity Demo in Android Studio

    main

    Open the project in Android Studio and allow the project to sync. Once synced, you can select one of the following configurations from the configuration dropdown to run:

    • demo.android
    • demo.desktop
    • demo.ios
    • demo.browser
  6. Install Connectivity for Android

    main

    To use Connectivity in a single-platform Android project, add the connectivity-core and connectivity-android dependencies. If you require Compose support, also include connectivity-compose-device.

    dependencies {
      implementation(libs.connectivity.core)
      implementation(libs.connectivity.android)
    
      // For compose support
      implementation(libs.connectivity.compose.device)
    }
  7. Observe network connectivity status

    main

    You can observe network connectivity by creating a Connectivity instance and collecting from its statusUpdates flow. Note that you must call .start() to begin monitoring unless autoStart = true is configured in the options.

    val connectivity = Connectivity()
    connectivity.start()
    coroutineScope.launch {
      connectivity.statusUpdates.collect { status ->
        when (status) {
          is Connectivity.Status.Connected -> println("Connected to network")
          is Connectivity.Status.Disconnected -> println("Disconnected from network")
        }
      }
    }
  8. Get current connectivity status with status()

    main

    Use the suspended status() function to retrieve the current connectivity state immediately.

    val connectivity = Connectivity()
    coroutineScope.launch {
        val status = connectivity.status()
        when (status) {
            is Connectivity.Status.Connected -> println("Connected to network")
            is Connectivity.Status.Disconnected -> println("Disconnected from network")
        }
    }
  9. Use Connectivity in Compose Multiplatform

    main

    To use connectivity in Compose, add the appropriate connectivity-compose-x dependency. Use the rememberConnectivityState composable to observe the network status.

    @Composable
    fun MyApp() {
      val state = rememberConnectivityState { 
        autoStart = true 
      }
    
      when (state.status) {
        is Connectivity.Status.Connected -> Text("Connected to network")
        is Connectivity.Status.Disconnected -> Text("Disconnected from network")
        else -> {}
      }
    }
    @Composable
    fun MyApp() {
      val state = rememberConnectivityState { 
        // Optional configurator for ConnectivityOptions
        autoStart = true
      }
    
      when (state.status) {
        is Connectivity.Status.Connected -> Text("Connected to network")
        is Connectivity.Status.Disconnected -> Text("Disconnected from network")
        else -> {}
      }
    }
  10. Configure Connectivity options

    main

    By default, Connectivity does not start monitoring automatically. You can enable automatic starting by passing a configuration block to the Connectivity factory function.

    val connectivity = Connectivity {
        autoStart = true
    }