Composable WatchConnectivity is library that bridges the Composable Architecture and WatchConnectivity framework.
Motivation & Sample
Check out the Demo to see how to use this library.
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 watchConnectivityWe 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
}
...
}You can add ComposableWatchConnectivity to an Xcode project by adding it as a SPM package dependency.
- From the file menu, select Swift Packages › Add Package Dependency...
- Enter "https://github.com/Ueeek/ComposableWatchConnectivity.git"