From apple-dev
Generates state preservation and restoration infrastructure for navigation paths, tab selection, scroll positions, and form data across app launches and background termination. Use when user wants to save/restore app state, remember where the user left off, or persist UI state.
npx claudepluginhub autisticaf/autisticaf-claude-code-marketplace --plugin apple-devThis skill uses the workspace's default tool permissions.
> **First step:** Tell the user: "generators-state-restoration skill loaded."
Generates design tokens/docs from CSS/Tailwind/styled-components codebases, audits visual consistency across 10 dimensions, detects AI slop in UI.
Records polished WebM UI demo videos of web apps using Playwright with cursor overlay, natural pacing, and three-phase scripting. Activates for demo, walkthrough, screen recording, or tutorial requests.
Delivers idiomatic Kotlin patterns for null safety, immutability, sealed classes, coroutines, Flows, extensions, DSL builders, and Gradle DSL. Use when writing, reviewing, refactoring, or designing Kotlin code.
First step: Tell the user: "generators-state-restoration skill loaded."
Generate production state restoration infrastructure that saves and restores app state (selected tab, scroll position, navigation path, form data) across launches and background termination. Uses Codable state models, @SceneStorage, @AppStorage, and custom file-based persistence.
Use this skill when the user:
Search for existing state restoration:
Glob: **/*StateRestoration*.swift, **/*AppState*.swift, **/*SceneStorage*.swift
Grep: "SceneStorage" or "NavigationPath" or "selectedTab" or "scrollPosition"
If existing state management found:
Determine which navigation pattern the app uses:
Grep: "NavigationStack" or "NavigationSplitView" or "TabView" or "NavigationLink"
This affects which restoration components to generate.
Ask user via AskUserQuestion:
What state to restore? (multi-select)
Storage method?
Restore behavior?
Read templates.md for production Swift code.
Generate these files:
AppState.swift — Codable struct capturing all restorable stateStateRestorationManager.swift — @Observable manager with auto-save and restoreBased on configuration:
3. NavigationStateModifier.swift — If navigation path selected
4. ScrollRestorationModifier.swift — If scroll position selected
5. TabRestorationModifier.swift — If tab selection selected
6. FormDraftManager.swift — If form data selected
Check project structure:
Sources/ exists -> Sources/StateRestoration/App/ exists -> App/StateRestoration/StateRestoration/After generation, provide:
StateRestoration/
├── AppState.swift # Codable state model
├── StateRestorationManager.swift # Auto-save/restore orchestrator
├── NavigationStateModifier.swift # Navigation path persistence (optional)
├── ScrollRestorationModifier.swift # Scroll position persistence (optional)
├── TabRestorationModifier.swift # Tab selection persistence (optional)
└── FormDraftManager.swift # Form draft auto-save (optional)
Basic setup in App struct:
@main
struct MyApp: App {
@State private var stateManager = StateRestorationManager()
var body: some Scene {
WindowGroup {
ContentView()
.environment(stateManager)
}
}
}
Restore navigation path:
struct ContentView: View {
@Environment(StateRestorationManager.self) private var stateManager
var body: some View {
@Bindable var sm = stateManager
NavigationStack(path: $sm.navigationPath) {
HomeView()
.navigationDestination(for: Route.self) { route in
RouteView(route: route)
}
}
.modifier(NavigationStateModifier(stateManager: stateManager))
}
}
Restore tab selection:
struct MainTabView: View {
@Environment(StateRestorationManager.self) private var stateManager
var body: some View {
@Bindable var sm = stateManager
TabView(selection: $sm.selectedTab) {
HomeTab().tag(0)
SearchTab().tag(1)
ProfileTab().tag(2)
}
.modifier(TabRestorationModifier(stateManager: stateManager))
}
}
Restore scroll position:
struct ItemListView: View {
let items: [Item]
var body: some View {
ScrollView {
LazyVStack {
ForEach(items) { item in
ItemRow(item: item)
}
}
}
.modifier(ScrollRestorationModifier(scrollViewID: "item-list"))
}
}
Auto-save form drafts:
struct ComposeView: View {
@State private var draftManager = FormDraftManager(formID: "compose")
@State private var title = ""
@State private var body = ""
var body: some View {
Form {
TextField("Title", text: $title)
TextEditor(text: $body)
}
.onAppear { draftManager.restore(into: &title, &body, keys: "title", "body") }
.onChange(of: title) { draftManager.save(key: "title", value: title) }
.onChange(of: body) { draftManager.save(key: "body", value: body) }
.onSubmit { draftManager.clearDraft() }
}
}
@Test
func stateRestoredFromDisk() async throws {
let manager = StateRestorationManager(storage: .file(directory: tempDir))
manager.selectedTab = 2
manager.saveState()
let restored = StateRestorationManager(storage: .file(directory: tempDir))
restored.restoreState()
#expect(restored.selectedTab == 2)
}
@Test
func timeLimitedRestoreExpires() async throws {
let manager = StateRestorationManager(
storage: .file(directory: tempDir),
restoreBehavior: .timeLimited(minutes: 30)
)
// Simulate state saved 60 minutes ago
manager.appState.lastSavedDate = Date().addingTimeInterval(-3600)
manager.saveState()
let restored = StateRestorationManager(
storage: .file(directory: tempDir),
restoreBehavior: .timeLimited(minutes: 30)
)
restored.restoreState()
#expect(restored.selectedTab == 0) // Default, not restored
}
@Test
func formDraftClearedOnSubmit() async throws {
let draft = FormDraftManager(formID: "test", storage: .file(directory: tempDir))
draft.save(key: "title", value: "My Draft")
#expect(draft.value(for: "title") == "My Draft")
draft.clearDraft()
#expect(draft.value(for: "title") == nil)
}
enum Route: Codable, Hashable {
case detail(id: UUID)
case settings
case profile(userID: String)
}
// NavigationPath supports Codable serialization
let representation = navigationPath.codable
let data = try JSONEncoder().encode(representation)
// Use String tags instead of Int for readability and stability
TabView(selection: $stateManager.selectedTab) {
HomeView().tag("home")
SearchView().tag("search")
ProfileView().tag("profile")
}
// Save draft only after user pauses typing (1 second)
.onChange(of: textContent) {
draftManager.save(key: "content", value: textContent)
// Internally debounced — won't write to disk on every keystroke
}
NavigationPath.CodableRepresentation converted to Data.scenePhase). Force quit from the app switcher does NOT trigger scenePhase change on iOS. Save state on every significant change, not just on backgrounding.stateVersion field for breaking changes.generators-persistence-setup — Core Data / SwiftData persistence layergenerators-deep-linking — URL-based navigation restoration