Skip to content

Commit

Permalink
Update README.MD
Browse files Browse the repository at this point in the history
  • Loading branch information
jordond committed Jun 10, 2024
1 parent 700e5d3 commit 0876e07
Showing 1 changed file with 113 additions and 14 deletions.
127 changes: 113 additions & 14 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ view the generated KDoc at [connectivity.jordond.dev](https://connectivity.jordo
- [Usage](#usage)
- [Options](#options)
- [HTTP monitoring](#http-monitoring)
- [Compose](#compose)
- [Multiple Targets](#multiple-targets)
- [Demo](#demo)
- [Contributing](#contributing)
- [License](#license)
Expand All @@ -39,13 +41,16 @@ Monitor network connectivity:

This library is written for Kotlin Multiplatform, and can be used on the following platforms:

| Artifact | Android | iOS | macOS | tvOS | Desktop | Browser (js/wasm) |
|------------------------|:-------:|:---:|:-----:|------|:-------:|:-----------------:|
| `connectivity-core` |||||||
| `connectivity-device` |||||||
| `connectivity-android` |||||||
| `connectivity-apple` |||||||
| `connectivity-http` |||||||
| Artifact | Android | iOS | macOS | tvOS | Desktop (JVM) | Browser (js/wasm) |
|-------------------------------|:-------:|:---:|:-----:|------|::|:-----------------:|
| `connectivity-core` |||||||
| `connectivity-device` |||||||
| `connectivity-android` |||||||
| `connectivity-apple` |||||||
| `connectivity-http` |||||||
| `connectivity-compose` |||||||
| `connectivity-compose-device` |||||||
| `connectivity-compose-http` |||||||

## Setup

Expand All @@ -61,6 +66,9 @@ connectivity-device = { module = "dev.jordond.connectivity:connectivity-device",
connectivity-android = { module = "dev.jordond.connectivity:connectivity-android", version.ref = "connectivity" }
connectivity-apple = { module = "dev.jordond.connectivity:connectivity-apple", version.ref = "connectivity" }
connectivity-http = { module = "dev.jordond.connectivity:connectivity-http", version.ref = "connectivity" }
connectivity-compose = { module = "dev.jordond.connectivity:connectivity-compose", version.ref = "connectivity" }
connectivity-compose-device = { module = "dev.jordond.connectivity:connectivity-compose-device", version.ref = "connectivity" }
connectivity-compose-http = { module = "dev.jordond.connectivity:connectivity-compose-http", version.ref = "connectivity" }
```

### Single Platform
Expand All @@ -69,8 +77,12 @@ Here is an example of how to add the dependencies to a single platform project t

```kotlin
dependencies {
implementation(libs.connectivity.core)
implementation(libs.connectivity.android)
implementation(libs.connectivity.core)
implementation(libs.connectivity.android)

// For compose support
implementation(libs.connectivity.compose.device)

}
```

Expand All @@ -83,8 +95,11 @@ Android and Apple devices:
kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.connectivity.core)
implementation(libs.connectivity.device)
implementation(libs.connectivity.core)
implementation(libs.connectivity.device)

// For compose support
implementation(libs.connectivity.compose.device)
}
}
}
Expand All @@ -100,15 +115,21 @@ It uses the `connectivity-device` for mobile targets, and `connectivity-http` fo
kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.connectivity.core)
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)
implementation(libs.connectivity.device)

// For compose support
implementation(libs.connectivity.compose.device)
}
}

Expand All @@ -118,7 +139,10 @@ kotlin {
jsMain.get().dependsOn(this)
wasmJsMain.get().dependsOn(this)
dependencies {
implementation(libs.connectivity.http)
implementation(libs.connectivity.http)

// For compose support
implementation(libs.connectivity.compose.http)
}
}
}
Expand Down Expand Up @@ -244,6 +268,81 @@ val connectivity = Connectivity {
}
```

## Compose

Connectivity also provides support for Compose Multiplatform. To use it you will have to make sure
you add the dependencies for the `connectivity-compose-x` modules.

Then you can use it like so:

**Note:** This composable is provided by either `connectivity-compose-device`
or `connectivity-compose-http` artifact.

```kotlin
@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 -> {}
}
}
```

### Multiple Targets

If you need to support both Device and HTTP monitoring in the same project, you will have to do
something similar to [this](#all-supported-platforms).

Example:

```kotlin
// commonMain/Platform.kt
expect fun createConnectivity(): Connectivity
```

Then define the `actual` functions:

```kotlin
// deviceMain/Platform.device.kt
actual fun createConnectivityState(): Connectivity {
return Connectivity {
autoStart = true
}
}

// httpMain/Platform.http.kt
actual fun createConnectivityState(): Connectivity {
return Connectivity {
autoStart = true
urls("cloudflare.com", "my-own-domain.com")
port = 80
pollingIntervalMs = 10.minutes
timeoutMs = 5.seconds
}
}
```

Then it can be used like so:

```kotlin
@Composable
fun MyApp() {
val state = createConnectivityState()

when (state.status) {
is Connectivity.Status.Connected -> Text("Connected to network")
is Connectivity.Status.Disconnected -> Text("Disconnected from network")
else -> {}
}
}
```

## Demo

A demo app is available in the `demo` directory. It is a Compose Multiplatform app that runs on
Expand Down

0 comments on commit 0876e07

Please sign in to comment.