Coder Social home page Coder Social logo

lagrange1813 / observationbp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from onevcat/observationbp

0.0 0.0 0.0 52 KB

Proof of concept for back-porting Observation framework to earlier iOS versions

C++ 19.13% Objective-C 0.64% Swift 80.23%

observationbp's Introduction

Observation Back-Porting

Proof of Concept for back-porting Observation framework to earlier iOS versions.

The same APIs as the Observation framework are provided but without the limitation of iOS 17.

Verified on Xcode 15.0 beta 5 (15A5209g). May break with a future version of Xcode.

Just a toy and only for PoC. Do not use it in your production code (or at your own risk)!

Usage

Macro

Just as the official Observation framework, but importing ObservationBP:

import ObservationBP

@Observable fileprivate class Person {
  init(name: String, age: Int) {
    self.name = name
    self.age = age
  }

  var name: String = ""
  var age: Int = 0
}

Results in:

Observation Tracking

let p = Person(name: "Tom", age: 12)
withObservationTracking {
  _ = p.name
} onChange: {
  print("Changed!")
}

p.age = 20
print("No log")

p.name = "John"
print("'Changed' is printed")

// No log
// Changed!
// 'Changed' is printed

SwiftUI

There is no way to make it as transparent as SwiftUI 5.0 does. But you can use it with ObservationView:

struct ContentView: View {

  private var person = Person(name: "Tom", age: 12)

  var body: some View {
    ObservationView {
      VStack {
        Text(person.name)
        Text("\(person.age)")
        HStack {
          Button("+") { person.age += 1 }
          Button("-") { person.age -= 1 }
        }
      }
      .padding()
    }
  }
}

Switching to Official Observation Framework

When you can set iOS 17 or later as your deploy target, you can switch back to the official framework.

  1. Instead of importing ObservationBP, change to import Observation.
  2. Add typealias ObservationView = Group to allow the project building. Then remove all ObservationView eventually.

observationbp's People

Contributors

onevcat 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.