Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/camji55/observation-uikit

Apple Observation framework integration with UIKit
https://github.com/camji55/observation-uikit

Last synced: 14 days ago
JSON representation

Apple Observation framework integration with UIKit

Awesome Lists containing this project

README

        

# Observation-UIKit
Apple Observation framework integration with UIKit

## How it Works

### Declare your object as `@Observable`
```swift
@Observable
final class Counter {

var count = 0

func increment() {
count += 1
}

func decrement() {
count -= 1
}
}
```

### Observe and Render the Changes
```swift
class ViewController: UIViewController {

@IBOutlet weak var countLabel: UILabel!

let counter = Counter()

override func viewDidLoad() {
super.viewDidLoad()

render()
}

private func render() {
withObservationTracking {
// Any value that's read from the `Observed` class will be automatically tracked for changes.
countLabel.text = "\(counter.count)"
} onChange: { [weak self] in
Task { @MainActor [weak self] in
// Recursivly call this same function any time an `Observed` propery changes to re-render the view.
self?.render()
}
}
}

@IBAction func incrementButtonTapped(_ sender: Any) {
counter.increment()
}

@IBAction func decrementButtonTapped(_ sender: Any) {
counter.decrement()
}
}
```