Model object observation for Skip apps.
See what API is included here.
SkipModel vends the skip.model
Kotlin package. This package contains Observable
and ObservableObject
interfaces, representing the two core protocols that SwiftUI uses to observe changes to model objects. It also includes limited Publisher
support.
SkipLib depends on the skip transpiler plugin and the SkipFoundation package.
SkipModel is part of the core SkipStack and is not intended to be imported directly. The transpiler includes import skip.model.*
in generated Kotlin for any Swift source that imports the Combine
, Observation
, or SwiftUI
frameworks.
From the Observation
package, SkipModel supports the @Observable
and @ObservationIgnored
macros.
From Combine
, SkipModel supports the ObservableObject
protocol, the @Published
property wrapper, and limited Publisher
functionality. See API support below.
Much of Skip's model support is implemented directly in the Skip transpiler. The Observable
and ObservableObject
marker protocols are are sufficient for the Skip transpiler to recognize your observable types. When generating their corresponding Kotlin classes, the transpiler then adds the necessary code so that their state can be tracked by the Compose runtime.
We welcome contributions to SkipModel. The Skip product documentation includes helpful instructions and tips on local Skip library development. When submitting code, please include unit tests in your PR.
Like Skip itself, SkipModel objects are dual-platform! Not only do your @Observable
and ObservableObject
properties participate in SwiftUI state tracking, but they are tracked by Compose as well. The Skip transpiler backs your observable properties with MutableState
values in Kotlin, so Compose automatically tracks reads and writes and performs recomposition as needed.
This means that you can write shared model-layer Swift code using observable objects, and use it to power both SwiftUI (whether iOS-only or dual-platform with Skip) as well as pure Android Compose UI code. For example, the following model class:
@Observable class TapCounter {
var tapCount = 0
}
could power a Compose UI:
val tapCounter = TapCounter()
...
TapIt(counter = tapCounter)
...
@Composable fun TapIt(counter: TapCounter) {
Button(onClick = { counter.tapCount += 1 }) {
Text("Tap Count: ${counter.tapCount}")
}
}
The following table summarizes SkipModel's API support on Android. Anything not listed here is likely not supported. Note that in your iOS-only code - i.e. code within #if !SKIP
blocks - you can use any Swift API you want. Additionally:
- In all Combine publishes and related API, the
Failure
type must beNever
: throwing errors in Combine chains is not supported. - In Skip, Combine is not automatically imported when you
import Foundation
. Make sure toimport Combine
orimport SwiftUI
explicitly.
Support levels:
- ✅ – Full
- 🟢 – High
- 🟡 – Medium
- 🟠 – Low
Support | API |
---|---|
🟢 |
|
🟠 |
|
🟢 |
|
🟠 |
|
✅ | func NotificationCenter.publisher(for: Notification.Name, object: Any? = nil): Publisher<Notification, Never> |
🟢 |
|
🟢 |
|
🟠 |
|
✅ | @ObservationIgnored |
🟠 |
|
✅ | @Published |
🟠 |
|
✅ | func Timer.publish(every: TimeInterval, tolerance: TimeInterval? = nil, on runLoop: RunLoop, in mode: RunLoop.Mode, options: RunLoop.SchedulerOptions? = nil) -> ConnectablePublisher<Date, Never> |