david-swift ed46533740
Some checks are pending
Deploy Docs / publish (push) Waiting to run
SwiftLint / SwiftLint (push) Waiting to run
App updates automatically after constructing UI
Before, a backend had to implement the update right after the
construction of the UI manually
2026-02-02 22:07:57 +01:00

93 lines
2.6 KiB
Swift

//
// App.swift
// Meta
//
// Created by david-swift on 01.07.24.
//
/// A structure conforming to `App` is the entry point of your app.
///
/// ```swift
/// @main
/// struct Test: App {
///
/// let app = BackendApp()
///
/// var scene: Scene {
/// WindowScene()
/// }
///
/// }
/// ```
///
public protocol App {
/// The app storage type.
associatedtype Storage: AppStorage
/// The app's scene.
@SceneBuilder var scene: Scene { get }
/// The app storage.
var app: Storage { get }
/// An app has to have an `init()` initializer.
init()
}
/// Extend the app.
extension App {
/// The application's entry point.
public static func main() {
let app = setupApp()
app.app.run {
for element in app.scene where element is Storage.SceneElementType {
element.setupInitialContainers(app: app.app)
}
StateManager.updateViews(force: true)
}
}
/// Initialize and get the app with the app storage.
/// - Returns: The app instance.
///
/// To run the app, call the ``AppStorage/run(setup:)`` function.
public static func setupApp() -> Self {
let appInstance = self.init()
appInstance.app.storage.app = { appInstance }
StateManager.addUpdateHandler { force in
let updateProperties = force || appInstance.getState().contains { $0.value.content.update }
var removeIndices: [Int] = []
for (index, element) in appInstance.app.storage.sceneStorage.enumerated() {
if element.destroy {
removeIndices.insert(index, at: 0)
} else if let scene = appInstance.scene.first(
where: { $0.id == element.id }
) as? Storage.SceneElementType as? SceneElement {
scene.update(element, app: appInstance.app, updateProperties: updateProperties)
}
}
for index in removeIndices {
appInstance.app.storage.sceneStorage.remove(at: index)
}
}
let state = appInstance.getState()
appInstance.app.storage.stateStorage = state
return appInstance
}
/// Get the state from the properties.
/// - Returns: The state.
func getState() -> [String: StateProtocol] {
var state: [String: StateProtocol] = [:]
for property in Mirror(reflecting: self).children {
if let label = property.label, let value = property.value as? StateProtocol {
state[label] = value
}
}
return state
}
}