Index of every page on this site: llms.txt
All skills
kimeur avatar

/duo-continuity

@cffd95d
by Gazouzi Marouanekimeur/duology46 stars
2
Run it yourself

Keeps app state alive when iPhone Duo, Apple's foldable, folds, unfolds, rotates or resizes between the outer display and the inner display. Use when scroll position, video or audio playback, typed text, keyboard focus, selection, form input, the selected item, the navigation stack, an upload or sign-in resets or reloads on fold or unfold, and when writing or reviewing @Observable models, @SceneStorage, NavigationPath, NavigationSplitView, ScrollPosition, AnyLayout, .id, onAppear, .task, registerForTraitChanges or viewWillTransition(to:with:).

Use this Skill: https://skilld.dev/gh/kimeur/duology/duo-continuity

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesswiftui.md

≈4.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

SwiftUI continuity patterns

Full code for each row of the "what must survive" table in SKILL.md. Every block typechecks against the iOS 27.1 simulator SDK, together with the shared types below, in Swift 5 mode, Swift 6 mode, and Swift 6 with MainActor as the default isolation.

Measured results come from the probes in evidence.md. They flip the size class from code; they are not a real fold.

Shared types

import SwiftUI
import Observation

struct Item: Identifiable, Hashable, Codable, Sendable {
    let id: Int
    var title: String
}

/// App-wide: who is signed in. One instance for every window.
@MainActor @Observable
final class Session {
    var userName: String?
}

/// Per window: what this window shows.
@MainActor @Observable
final class LibraryModel {
    var items: [Item] = (1...200).map { Item(id: $0, title: "Item \($0)") }
    var selection: Item.ID?
    var path = NavigationPath()
    var scroll = ScrollPosition(idType: Item.ID.self)
    var sheetItem: Item?
    var itemToDelete: Item?
    var details: [Item.ID: String] = [:]     // loaded once per item, kept across folds
}

Who owns what

  • App-wide objects (session, stores, upload service) live in the App and go down through .environment.
  • Per-window objects live in the window's root view. On the inner display people can open more than one window, so a selection shared by every window would be wrong.
  • The root view never changes identity. Switching between signed-in and signed-out content is a data change, so a new identity there is correct. Switching on layout is not.
import SwiftUI

@main
struct LibraryApp: App {
    @State private var session = Session()           // shared by every window

    var body: some Scene {
        WindowGroup {
            WindowRoot()
                .environment(session)
        }
    }
}

struct WindowRoot: View {
    @Environment(Session.self) private var session
    @State private var model = LibraryModel()        // one per window

    var body: some View {
        Group {
            if session.userName == nil {
                Button("Sign in") { session.userName = "Sam" }  // your sign-in flow
            } else {
                Text("Library")                                 // your signed-in root
            }
        }
        .environment(model)
    }
}

Apple's guide to this ownership model: Managing model data in your app. WWDC23 Discover Observation in SwiftUI explains @Observable, and WWDC21 Demystify SwiftUI explains view identity.

Pick a layout without a branch

Decide from the size class when you can. When you need the real space, measure a frame that fills the offered space, not the content, so the measurement can't feed back into the layout.

import SwiftUI

struct MediaRow: View {
    @State private var isWide = false     // layout only; losing it costs nothing

    var body: some View {
        let layout = isWide
            ? AnyLayout(HStackLayout(alignment: .top))
            : AnyLayout(VStackLayout(alignment: .leading))
        layout {
            Image(systemName: "photo")
            Text("Caption")
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)   // measure the space, not the content
        .onGeometryChange(for: Bool.self) { proxy in
            proxy.size.width > proxy.size.height
        } action: { wide in
            isWide = wide
        }
    }
}
  • onGeometryChange(for:of:action:) reports the value only when it changes.
  • A custom Layout type is another branch-free option. duo-layout has one that splits at the fold.
  • ViewThatFits is fine for labels and icons. Don't put @State, a text field or a scroll view inside it; read state from the model instead.

Restore after a relaunch

A fold keeps the window alive, so the model is enough. @SceneStorage covers the other case: the system ends the app in the background and later brings the window back (SceneStorage, Restoring your app's state with SwiftUI).

import SwiftUI

struct RestoringRoot: View {
    @State private var model = LibraryModel()
    @SceneStorage("library.selection") private var savedSelection: Int?
    @SceneStorage("library.path") private var savedPath: Data?
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        NavigationStack(path: $model.path) {
            List(model.items) { item in
                NavigationLink(item.title, value: item.id)
            }
            .navigationDestination(for: Item.ID.self) { Text("Item \($0)") }
        }
        .environment(model)
        .task { restore() }                       // runs once: this view never changes identity
        .onChange(of: scenePhase) { _, phase in
            if phase == .background { save() }
        }
    }

    private func restore() {
        model.selection = savedSelection
        if let savedPath,
           let saved = try? JSONDecoder().decode(NavigationPath.CodableRepresentation.self, from: savedPath) {
            model.path = NavigationPath(saved)
        }
    }

    private func save() {
        savedSelection = model.selection
        if let codable = model.path.codable {   // nil when a pushed value isn't Codable
            savedPath = try? JSONEncoder().encode(codable)
        }
    }
}
  • Apple's SceneStorage page says: keep the data small, don't store sensitive data, and expect the system to choose when it saves. Data goes away when the scene is destroyed.
  • Two @SceneStorage properties with the same key in one scene share a value. Measured: a value written in one size-class branch was read back by the view in the other branch.
  • NavigationPath.codable is nil if any pushed value isn't Codable (codable).
  • Save on .background, not .inactive. Measured on the simulator (v0.2, duo-scenes): a fold kept the scene active, with no .inactive. A real device is unverified, so don't depend on it.

Scroll position

Store the position as an item ID in the model and attach it to a ScrollView whose identity never changes. Change columns inside it, not around it.

import SwiftUI

struct GalleryView: View {
    @Environment(LibraryModel.self) private var model
    @Environment(\.horizontalSizeClass) private var sizeClass

    var body: some View {
        @Bindable var model = model
        ScrollView {
            LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: sizeClass == .compact ? 2 : 4)) {
                ForEach(model.items) { item in
                    Text(item.title).frame(height: 80)
                }
            }
            .scrollTargetLayout()                      // the views ScrollPosition can name
        }
        .scrollPosition($model.scroll, anchor: .top)
        .toolbar {
            Button("Top") { model.scroll.scrollTo(edge: .top) }
        }
    }
}
  • Read the current item with model.scroll.viewID(type: Item.ID.self) (ScrollPosition).
  • Before iOS 18, use scrollPosition(id:anchor:) with an Item.ID? in the model.
  • Measured: an ID position kept item 150 at the top through 4 → 1 → 4 columns. A point position (scrollTo(y:)) kept the offset and showed other items.
  • Measured: onScrollTargetVisibilityChange(idType:threshold:_:) reported no IDs after the column count changed, while viewID stayed right. Restore from the position, not from that callback.
  • Measured: scrollPosition(id:) did not scroll a List. A List keeps its own offset while its identity is stable, and inside a collapsing NavigationSplitView it came back at the same offset. To jump a List to an item, use ScrollViewReader.

Text, focus and selection

import SwiftUI

struct MessageDraft: Equatable {
    var subject = ""
    var message = ""
}

enum DraftField: Hashable { case subject, message }

@MainActor @Observable
final class ComposeModel {
    var draft = MessageDraft()
    var messageSelection: TextSelection?
}

struct ComposeView: View {
    @Bindable var model: ComposeModel
    @FocusState private var focus: DraftField?     // view state: keep this view's identity stable
    @Environment(\.horizontalSizeClass) private var sizeClass

    var body: some View {
        let layout = sizeClass == .compact
            ? AnyLayout(VStackLayout(alignment: .leading))
            : AnyLayout(HStackLayout(alignment: .top))
        layout {
            TextField("Subject", text: $model.draft.subject)
                .focused($focus, equals: .subject)
            TextEditor(text: $model.draft.message, selection: $model.messageSelection)
                .focused($focus, equals: .message)
        }
    }
}
  • FocusState can't live in a model. It belongs to a view, so the field and that view must keep their identity.
  • Measured: focus survived two size-class flips when the field kept its identity, and became nil when the field sat in an if/else.
  • TextSelection and the selection: initializers of TextField and TextEditor need iOS 18.
  • Unverified: what the software keyboard does when the window moves to the other display. Run the Device Hub pass with the keyboard up.

Form input

Bind every field to one draft struct in the model. Validate in the model, not in onAppear or onDisappear.

import SwiftUI

struct AddressForm: Equatable {
    var name = ""
    var street = ""
    var city = ""
    var isComplete: Bool { !name.isEmpty && !street.isEmpty && !city.isEmpty }
}

@MainActor @Observable
final class CheckoutModel {
    var address = AddressForm()
}

struct AddressFields: View {
    @Bindable var model: CheckoutModel
    var body: some View {
        Form {
            TextField("Name", text: $model.address.name)
            TextField("Street", text: $model.address.street)
            TextField("City", text: $model.address.city)
        }
    }
}

Playback

import SwiftUI
import AVKit

@MainActor @Observable
final class PlaybackModel {
    let player = AVPlayer()
    private(set) var currentURL: URL?

    func play(_ url: URL) {
        guard url != currentURL else { return }      // a fold never restarts the same item
        currentURL = url
        player.replaceCurrentItem(with: AVPlayerItem(url: url))
        player.play()
    }
}

struct PlayerScreen: View {
    let playback: PlaybackModel                      // owned by the window root
    let url: URL
    @Environment(\.horizontalSizeClass) private var sizeClass

    var body: some View {
        let layout = sizeClass == .compact ? AnyLayout(VStackLayout()) : AnyLayout(HStackLayout())
        layout {
            VideoPlayer(player: playback.player)
            Text("Up next")
        }
        .task(id: url) { playback.play(url) }       // keyed on the data; a repeat is a no-op
    }
}
  • VideoPlayer is only a view of the player. Rebuilding it doesn't touch playback. Measured: playback ran on through every flip.
  • Apple's ArrangementView page uses an overlay arrangement for player controls over video. It takes the player view once, so the same rule holds. See duo-layout.

Uploads and downloads

Start long work from an action, inside a service object that outlives every view. SwiftUI cancels a .task when its view goes away or changes identity (task(id:name:priority:file:line:_:)).

import SwiftUI

@MainActor @Observable
final class UploadCenter {
    enum Status: Equatable { case running, done, failed }
    private(set) var uploads: [UUID: Status] = [:]
    private var tasks: [UUID: Task<Void, Never>] = [:]

    @discardableResult
    func start(file: URL, to endpoint: URL) -> UUID {
        let id = UUID()
        uploads[id] = .running
        tasks[id] = Task {
            var request = URLRequest(url: endpoint)
            request.httpMethod = "POST"
            do {
                _ = try await URLSession.shared.upload(for: request, fromFile: file)
                uploads[id] = .done
            } catch {
                uploads[id] = .failed
            }
            tasks[id] = nil
        }
        return id
    }

    func cancel(_ id: UUID) { tasks[id]?.cancel() }
}

struct AttachButton: View {
    @Environment(UploadCenter.self) private var uploads
    let file: URL
    let endpoint: URL

    var body: some View {
        Button("Upload") { uploads.start(file: file, to: endpoint) }   // an action, not .task
    }
}
  • Put UploadCenter in the App, next to the session, so every window sees the same uploads.
  • For uploads that must go on while the app is in the background, use a session made with background(withIdentifier:).

Loading without reloading

import SwiftUI

struct ItemDetail: View {
    @Environment(LibraryModel.self) private var model
    let item: Item

    var body: some View {
        Text(model.details[item.id] ?? "Loading…")
            .task(id: item.id) {
                guard model.details[item.id] == nil else { return }  // cached: a fold shows it at once
                model.details[item.id] = await fetchDetail(item.id)
            }
    }
}

func fetchDetail(_ id: Item.ID) async -> String {
    "Detail for item \(id)"   // your network call
}
  • Key .task(id:) on the data the view shows. If the view is rebuilt anyway, the cache in the model makes the rerun free.

Sheets, alerts and sign-in

import SwiftUI

struct LibraryShell: View {
    @Environment(LibraryModel.self) private var model

    var body: some View {
        @Bindable var model = model
        Text("Screen with layout switches")            // your content
            .sheet(item: $model.sheetItem) { item in
                Text(item.title)
            }
            .alert("Delete this item?", item: $model.itemToDelete) { item in
                Button("Delete", role: .destructive) {
                    model.items.removeAll { $0.id == item.id }
                }
            }
    }
}
  • Attach presentations at a view that never changes identity. Measured: a sheet attached inside an if/else closed on a flip while its isPresented binding stayed true.
  • sheet(item:onDismiss:content:) and alert(_:item:actions:) take the item from the model. The alert and confirmationDialog item overloads are new in the iOS 27 SDK and also build for older targets (API index).
  • Keep the session in the app-level Session object and tokens in Keychain Services. Present sign-in from the window root. Unverified: whether a web sign-in sheet in flight survives a fold.

Panes that hide

When the phone closes, extra panes leave the view tree:

  • a NavigationSplitView collapses to one stack (measured: the sidebar's onDisappear ran on collapse and its onAppear on expand);
  • a split ArrangementView shows only its primary view when it can't split, for example closed and held sideways (duo-layout measured this), so the secondary view's own state goes;
  • your own "only when regular" panes go away.

Anything a hidden pane must remember goes in the model: its scroll position, its draft, its selection.

Popping a stack while the phone opens

A screen pushed on the closed phone often moves into a second pane when the phone opens, so the stack pops and the screen under it comes back. Measured on the iPhone Duo simulator (iOS 27.1, 20 October 2026) in recipe-finance-dashboard and recipe-shop:

  • Popped at once, during the unfold animation, the screen that came back kept the closed phone's geometry: x = −2 pt, a right safe-area inset of 42 pt instead of 84 pt, and no margins. Its close button ended up under the vertical bar. It stayed so until relaunch; a new .id and animations on or off made no difference.
  • Popped after the animation, it matched a fresh launch on the open phone.

So when the phone opens on a pushed screen, switch to two panes after the animation. At launch, and with nothing pushed, switch at once. The 600 ms wait is a measured workaround, not an Apple rule, and unverified on a device. It applies to layouts you switch yourself; NavigationSplitView's own collapse and expand were not tested for it.

import Observation
import SwiftUI

@Observable
final class PaneModel {
    var selection: Int?     // something is pushed while this is set
    var isTwoPane = false   // the layout reads this, never the size class
}

struct TwoPaneSwitch: ViewModifier {
    @Environment(\.horizontalSizeClass) private var widthClass
    let model: PaneModel
    @State private var paneChange: Task<Void, Never>?

    func body(content: Content) -> some View {
        content.onChange(of: widthClass, initial: true) { oldValue, newValue in
            let isTwoPane = newValue == .regular
            paneChange?.cancel()
            // Opening on a pushed screen pops it: wait out the unfold animation first.
            if isTwoPane, oldValue != newValue, !model.isTwoPane, model.selection != nil {
                paneChange = Task {
                    try? await Task.sleep(for: .milliseconds(600))
                    guard !Task.isCancelled else { return }
                    model.isTwoPane = true
                }
            } else {
                model.isTwoPane = isTwoPane
            }
        }
    }
}

The recipes' RootView.swift uses the same switch with their own models.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts1mo3 checks · Risk SAFE
  • Gen Agent Trust Hub1mo

    This skill provides technical guidance and code patterns for managing application state on the iPhone Duo (foldable) platform. It contains documentation and code snippets for SwiftUI and UIKit developers.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW · No issues

Signed by skilld at cffd95d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated last month
Other metadata
metadata
{
  "verified": "Xcode 27.1 (27A9275), iOS 27.1 simulator, device: no",
  "sources": "https://github.com/Kimeur/duology/blob/main/reference/sources.md"
}

README badge

README badge for kimeur/duology/duo-continuity