-
-
Notifications
You must be signed in to change notification settings - Fork 3
Home
AppState is a Swift Package designed to simplify application state management in a thread-safe, type-safe, and SwiftUI-friendly way. It provides developers with a centralized class, Application
, that houses all application-wide data. This singleton instance is easily accessible and observable for reactive changes. It also incorporates a cache-based system to persistently store and retrieve any application-wide data at any given time.
AppState provides a centralized Application
class which houses all application-wide data. This singleton instance is easily accessible and observable for reactive changes.
AppState includes dedicated struct types for managing state (State
), stored state (StoredState
), sync state (SyncState
), and dependencies (Dependency
). These allow encapsulation and broadcasting of value changes within the application's scope.
AppState uses property wrappers (AppState
, StoredState
, SyncState
, AppDependency
) to elegantly bridge Application
state with SwiftUI for seamless integration.
The load
function in AppState ensures dependencies are initialized. If a dependency isn't loaded, it will be initialized when next used.
The dependency
function retrieves a state from the Application
instance using the provided keypath.
AppState is particularly useful when managing global or shared state like cached images or a list of items fetched from a database or network. AppState can also be used for Dependency Injection to provide global access to dependencies.
AppState requires Swift 5.7 or later, iOS 16.0 or later, watchOS 9.0 or later, macOS 13.0 or later, and tvOS 16.0 or later.
Here's an example of extending the Application
singleton provided by AppState to include custom dependencies:
extension Application {
public var network: Dependency<Network> {
dependency(Network())
}
}
This repository is open to suggestions and contributions. Feel free to submit issues and pull requests to help improve AppState.
🚧 Under Construction 🚧