# Swiftui View Refactor

SwiftUI view refactor/review: layout, DI, Observation, view models.

- Repository: https://github.com/steipete/agent-scripts (7431 stars)
- Category: Mobile
- License: MIT
- Last updated: 2026-10-30
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add steipete/agent-scripts --skill swiftui-view-refactor -g -a claude-code -y`
Codex: `npx skills add steipete/agent-scripts --skill swiftui-view-refactor -g -a codex -y`
Cursor: `npx skills add steipete/agent-scripts --skill swiftui-view-refactor -g -a cursor -y`
Gemini CLI: `npx skills add steipete/agent-scripts --skill swiftui-view-refactor -g -a gemini-cli -y`

## Description

SwiftUI view refactor/review: layout, DI, Observation, view models.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# SwiftUI View Refactor

_Attribution: copied from @Dimillian’s `Dimillian/Skills` (2025-12-31)._

## Overview
Apply a consistent structure and dependency pattern to SwiftUI views, with a focus on ordering, Model-View (MV) patterns, careful view model handling, and correct Observation usage.

## Core Guidelines

### 1) View ordering (top → bottom)
- Environment
- `private`/`public` `let`
- `@State` / other stored properties
- computed `var` (non-view)
- `init`
- `body`
- computed view builders / other view helpers
- helper / async functions

### 2) Prefer MV (Model-View) patterns
- Default to MV: Views are lightweight state expressions; models/services own business logic.
- Favor `@State`, `@Environment`, `@Query`, and `task`/`onChange` for orchestration.
- Inject services and shared models via `@Environment`; keep views small and composable.
- Split large views into subviews rather than introducing a view model.

### 3) Split large bodies and view properties
- If `body` grows beyond a screen or has multiple logical sections, split it into smaller subviews.
- Extract large computed view properties (`var header: some View { ... }`) into dedicated `View` types when they carry state or complex branching.
- It's fine to keep related subviews as computed view properties in the same file; extract to a standalone `View` struct only when it structurally makes sense or when reuse is intended.
- Prefer passing small inputs (data, bindings, callbacks) over reusing the entire parent view state.

Example (extracting a section):

```swift
var body: some View {
    VStack(alignment: .leading, spacing: 16) {
        HeaderSection(title: title, isPinned: isPinned)
        DetailsSection(details: details)
        ActionsSection(onSave: onSave, onCancel: onCancel)
    }
}
```

Example (long body → shorter body + computed views in the same file):

```swift
var body: some View {
    List {
        header
        filters
        results
        footer
    }
}

The rest: https://github.com/steipete/agent-scripts/blob/HEAD/skills/swiftui-view-refactor/SKILL.md
