Skip to content

Composable WatchConnectivity is library that bridges the Composable Architecture and WatchConnectivity framework.

License

Notifications You must be signed in to change notification settings

Ueeek/ComposableWatchConnectivity

Repository files navigation

ComposableWatchConnectivity

Composable WatchConnectivity is library that bridges the Composable Architecture and WatchConnectivity framework.

Japanese Guide

Motivation & Sample

Example

Check out the Demo to see how to use this library.

Basic Usage

To use ComposableWatchConnectivity in your application, you can add an action to your domain that represents all of the actions the WacthConnectivityClient can emit via the WCSessionDelegate methods:

import ComposableWatchConnectivity

enum Action {
    case watchConnectivity(WatchConnectivityClient.Action)
    // Other actions:
    ...
}

The WatchConnectivityClient.Action enum holds a case for each delegate method of WCSessionDelegate, such as activationDidCompleteWith(:), didReceiveMessage(:), didReceiveUserInfo(:) and so on.

WatchConnectivityClient is declared as DependencyClient, so you can access it easily

    @DependencyClient(\.watchConnectivity) var watchConnectivity

We need to activate the Client and subscribe the action they emit.

    await watchConnectivity.activate()
    await withTaskGroup(of: Void.self) { group in
        await withTaskCancellation(id: CancelID.watchConnectivity, cancelInFlight: true) {
            for await action in await watchClient.delegate() {
                await send(.watchConnectivity(action), animation: .default)
            }
        }
    }

For Sender, we can call watchClient.sendData

Reducer { state, action in
    switch action {
        case .sendCurrentDate:
            return .run { _ in
                if let data = try? JSONEncoder().encode(Date.now) {
                    await watchClient.sendMessage(("date", data))
                }
            }
    }
    ...
}

For Receiver, we can receive message via .watchConnectivity(.didReceiveMessage)

Reducer { state, action in
    switch action {
        case .watchConnectivity(.didReceiveMessage(let message)):
        if let data = message?["date"] as? Data,
               let receivedDate = try? JSONDecoder().decode(Date.self, from: data) {
                   // Use receivedDate
               } else {
                   // Cannot parse the data
               }
            return .none
        }
    ...
}

Installation

You can add ComposableWatchConnectivity to an Xcode project by adding it as a SPM package dependency.

  1. From the file menu, select Swift Packages › Add Package Dependency...
  2. Enter "https://github.com/Ueeek/ComposableWatchConnectivity.git"

About

Composable WatchConnectivity is library that bridges the Composable Architecture and WatchConnectivity framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages