Giter Site home page Giter Site logo

watchsync's Introduction

WatchSync

WatchSync provides a property wrapper to encapsulate basic sharing of a codable object between an iPhone and AppleWatch using Apple's WatchConnectivity framework.

How to use

WatchSync exposes the property wrapper @SyncedWatchState which will use Apple's WatchConnectivity framework to pass updates automatically to each device when the value is changed.

Currently, @SyncedWatchState must be used within a class that comforms to the ObservableObject protocol. Consider this as a version of Apple's @Published property wrapper that also publishes changes to the connected device.

In the example below, we have a a Counter object that contains a syncedCount parameter. This class targets both iOS and watchOS and is used by each to update the shared syncedCount variable. SwiftUI views will automatically update when this value is changed (just like @Published).

import WatchSync

class Counter: ObservableObject {
    @SyncedWatchState var syncedCount: Int = 0
    
    func increment() {
        syncedCount += 1
    }
    
    func decrement() {
        syncedCount -= 1
    }
}

The following SwiftUI View can use this object by accessing the Counter object

import SwiftUI
import WatchSync

struct ContentView: View {
    @StateObject var counter = Counter()
    
    var labelStyle: some LabelStyle {
        #if os(watchOS)
        return IconOnlyLabelStyle()
        #else
        return DefaultLabelStyle()
        #endif
    }
    
    var body: some View {
        VStack {
            Text("\(counter.syncedCount)")
                .font(.largeTitle)
            
            HStack {
                Button(action: counter.decrement) {
                    Label("Decrement", systemImage: "minus.circle")
                }
                .padding()
                
                Button(action: counter.increment) {
                    Label("Increment", systemImage: "plus.circle.fill")
                }
                .padding()
            }
            .font(.headline)
            .labelStyle(IconOnlyLabelStyle())
        }
    }
}

Limitations

Currently this has been built only with the intent to have one instance per project. It's possible creating multiple instances of the @SyncedWatchState property wrapper but this has not been tested.

Use at your own risk if you intend to use in production. This project is brand new so reliability is unclear.

watchsync's People

Contributors

cgaaf avatar

Stargazers

Darian Macharowski avatar Narcis avatar Manolis Pahlke avatar  avatar  avatar Carl-Johan Heinze avatar Sergey Minakov avatar Lorenzo Lewis avatar David Smith avatar idan boadana avatar Scott St. George avatar Andrea Sacerdoti avatar Manuel Sánchez avatar Greg Kaleka avatar Nikhil Nigade avatar Yotzin avatar Muhammad Umer avatar Nam avatar Aaron Dippner avatar Ben avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.