Swiftui view refactor

From steipete/agent-scripts · View interactive version

Swiftui view refactor is an AI agent skill from the GitHub repository steipete/agent-scripts. SwiftUI view refactor/review: layout, DI, Observation, view models.

How to use this skill

Skill file

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)

2) Prefer MV (Model-View) patterns

3) Split large bodies and view properties

Example (extracting a section):

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):

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

private var header: some View {
    VStack(alignment: .leading, spacing: 6) {
        Text(title).font(.title2)
        Text(subtitle).font(.subheadline)
    }
}

private var filters: some View {
    ScrollView(.horizontal, showsIndicators: false) {
        HStack {
            ForEach(filterOptions, id: \.self) { option in
                FilterChip(option: option, isSelected: option == selectedFilter)
                    .onTapGesture { selectedFilter = option }
            }
        }
    }
}

Example (extracting a complex computed view):

private var header: some View {
    HeaderSection(title: title, subtitle: subtitle, status: status)
}

private struct HeaderSection: View {
    let title: String
    let subtitle: String?
    let status: Status

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            Text(title).font(.headline)
            if let subtitle { Text(subtitle).font(.subheadline) }
            StatusBadge(status: status)
        }
    }
}

4) View model handling (only if already present)

Example (Observation-based):

@State private var viewModel: SomeViewModel

init(dependency: Dependency) {
    _viewModel = State(initialValue: SomeViewModel(dependency: dependency))
}

5) Observation usage

Workflow

1) Reorder the view to match the ordering rules.

2) Favor MV: move lightweight orchestration into the view using @State, @Environment, @Query, task, and onChange.

3) If a view model exists, replace optional view models with a non-optional @State view model initialized in init by passing dependencies from the view.

4) Confirm Observation usage: @State for root @Observable view models, no redundant wrappers.

5) Keep behavior intact: do not change layout or business logic unless requested.

Notes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.