Skip to content
Zach edited this page Nov 30, 2023 · 4 revisions

AppState Wiki

Introduction

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.

Key Features

Application Singleton

AppState provides a centralized Application class which houses all application-wide data. This singleton instance is easily accessible and observable for reactive changes.

State Structs

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.

Property Wrappers

AppState uses property wrappers (AppState, StoredState, SyncState, AppDependency) to elegantly bridge Application state with SwiftUI for seamless integration.

Load Function

The load function in AppState ensures dependencies are initialized. If a dependency isn't loaded, it will be initialized when next used.

Dependency Function

The dependency function retrieves a state from the Application instance using the provided keypath.

Usage

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.

Requirements

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.

Example

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.