All skills
dpearson2699 avatar

/swiftui-patterns

@cf3fe87

Builds and reviews SwiftUI views with modern MV architecture, state, composition, isolated previews, and migration guidance. Covers @Observable ownership, @State/@Bindable/@Environment wiring, view decomposition, ViewModifiers, environment values, .task loading, iOS 26+ handoffs, Writing Tools, clipboard availability, and performance. Use when structuring SwiftUI state, managing @Observable, composing views, previewing meaningful UI states, or correcting SwiftUI patterns.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swiftui-patterns

This session only. Nothing lands on disk.

referencesview-refactoring.md

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

Behavior-Preserving View Refactoring

Load this reference when restructuring an existing SwiftUI view without an explicit request to change its interface or behavior.

Contents

Pin the Contract

A structure-only refactor preserves:

  • layout, presentation, and navigation behavior;
  • accessibility labels, values, actions, focus order, and identifiers;
  • state ownership, bindings, and stable view identity;
  • side-effect timing and cancellation behavior;
  • domain behavior, error handling, and persistence semantics.

Record intentional changes separately. Do not let cleanup silently become a visual redesign, navigation rewrite, or business-rule change.

Use the extraction signals in the main skill. Dedicated View types are the default for sections with substantial layout or branching, their own state or lifecycle, narrower dependencies, independent preview value, or enough complexity to obscure the parent's data flow. Keep only genuinely small stateless fragments as computed some View properties. Extensions and // MARK: headings organize a file; they do not create view boundaries. Do not replace one oversized body with a screen-sized extension made of computed some View fragments.

Keep Body Declarative

Move non-trivial button actions and lifecycle closures to small named methods so body reads as UI:

Button("Save", action: save)
    .disabled(isSaving)

.task(id: searchText) {
    await reload(for: searchText)
}

private func save() {
    Task { await editorService.validateAndSave(draft) }
}

private func reload(for query: String) async {
    results = await searchClient.results(for: query)
}

The view methods remain thin orchestration points. They may update view-owned loading, selection, error, or presentation state around a service/model call.

Preserve the Business Boundary

Validation, persistence, networking, retry/caching rules, and reusable loading policy belong in services or models. Do not merely move a large inline closure into an equally large private view method and call the refactor complete.

Keep reusable domain errors and their recovery policy with the service or model that defines the operation. The view may translate those errors into presentation state, but it should not become the owner of domain failure semantics.

Pass extracted subviews only the values, bindings, and actions they need. Preserve the existing environment-versus-initializer ownership decision unless changing that boundary is part of the request. If a child needs a cohesive feature-scoped observable model, pass that model explicitly instead of replacing its interface with many unrelated closures.

Verify the Refactor

After each meaningful extraction:

  1. Build the affected target and fix compiler errors before continuing.
  2. Run existing unit, snapshot, and UI tests that cover the screen.
  3. Render independently useful previews with deterministic fixtures and every required dependency installed.
  4. Exercise the original interaction flow, including loading, error, cancellation, save/dismiss timing, navigation, focus, and accessibility behavior.
  5. Review the diff for accidental constants, modifier-order, identity, or task-lifetime changes.

Preserve existing preview-rendering and snapshot-test CI gates. Compilation alone does not prove that extracted views still compose or render correctly.

A cleaner file that changes behavior, weakens a required dependency, or no longer builds is not a successful refactor.

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill is a comprehensive collection of SwiftUI development patterns for iOS, covering modern architecture, state management, and UI composition. It provides best practices for Swift 6 and modern platform integrations without any detected security risks.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    1/5 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

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

Last checked against GitHub 2 days ago.

Steadyupdated 3 months ago
  • Performance
  • swiftui
  • ios
  • state-management
  • architecture
  • view-composition
  • observable
  • async-loading
  • environment
  • swift

README badge

README badge for dpearson2699/swift-ios-skills/swiftui-patterns

Guides SwiftUI view architecture with the Model-View pattern, @Observable state management, view composition, environment wiring, and async data loading for iOS 17+. Use when structuring a SwiftUI app, managing @Observable ownership, decomposing views, or applying state synchronization patterns like .task and @Bindable.

Generated from the current SKILL.md.

Does this skill cover navigation patterns like NavigationStack and tabs?
No. Navigation patterns are covered in the dedicated swiftui-navigation skill. This skill focuses on state management, view composition, and environment wiring.
What iOS versions does this skill target?
iOS 26+ with Swift 6.3, with most patterns backward-compatible to iOS 17 unless explicitly noted (e.g., Writing Tools require iOS 18+).
Does this cover layout and component patterns like grids and lists?
No. Detailed layout, container, and component patterns are covered in the swiftui-layout-components skill.
Should I use @Observable with @MainActor?
Yes. Always annotate @Observable view model classes with @MainActor to ensure UI-bound state updates on the main thread and comply with Swift 6 concurrency safety.
When should I use a view model versus the MV pattern?
Default to the MV pattern where views are lightweight state expressions and models own business logic. Only introduce view models if the existing codebase already uses them.

Generated from the current SKILL.md. These answers refresh after source changes.