← Back to Skills Marketplace
soponcd

Swiftui View Refactor

by soponcd · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
133
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install timeflow-swiftui-view-refactor
Description
Refactor and review SwiftUI view files for consistent structure, dependency injection, and Observation usage. Use when asked to clean up a SwiftUI view’s lay...
README (SKILL.md)

SwiftUI View Refactor

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

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

3b) Keep a stable view tree (avoid top-level conditional view swapping)

  • Avoid patterns where a computed view (or body) returns completely different root branches using if/else.
  • Prefer a single stable base view, and place conditions inside sections/modifiers (overlay, opacity, disabled, toolbar, row content, etc.).
  • Root-level branch swapping can cause identity churn, broader invalidation, and extra recomputation in SwiftUI.

Prefer:

var body: some View {
    List {
        documentsListContent
    }
    .toolbar {
        if canEdit {
            editToolbar
        }
    }
}

Avoid:

var documentsListView: some View {
    if canEdit {
        editableDocumentsList
    } else {
        readOnlyDocumentsList
    }
}

4) View model handling (only if already present)

  • Do not introduce a view model unless the request or existing code clearly calls for one.
  • If a view model exists, make it non-optional when possible.
  • Pass dependencies to the view via init, then pass them into the view model in the view's init.
  • Avoid bootstrapIfNeeded patterns.

Example (Observation-based):

@State private var viewModel: SomeViewModel

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

5) Observation usage

  • For @Observable reference types, store them as @State in the root view.
  • Pass observables down explicitly as needed; avoid optional state unless required.

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. Ensure stable view structure: avoid top-level if-based branch swapping; move conditions to localized sections/modifiers.
  4. 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.
  5. Confirm Observation usage: @State for root @Observable view models, no redundant wrappers.
  6. Keep behavior intact: do not change layout or business logic unless requested.

Notes

  • Prefer small, explicit helpers over large conditional blocks.
  • Keep computed view builders below body and non-view computed vars above init.
  • For MV-first guidance and rationale, see references/mv-patterns.md.

Large-view handling

  • When a SwiftUI view file exceeds ~300 lines, split it using extensions to group related helpers. Move async functions and helper functions into dedicated private extensions, separated with // MARK: - comments that describe their purpose (e.g., // MARK: - Actions, // MARK: - Subviews, // MARK: - Helpers). Keep the main struct focused on stored properties, init, and body, with view-building computed vars also grouped via marks when the file is long.
Usage Guidance
This skill is a coherent, instruction-only refactoring guide for SwiftUI files and doesn't request secrets or install code. However, it may be used by the agent to rewrite your source files — before using it: (1) run it only on code you can restore, (2) ensure automatic edits are shown as diffs/PRs so you can review changes, and (3) confirm whether the agent will attempt to read or fetch the referenced 'references/mv-patterns.md' (the skill bundle does not include that file). If you want extra safety, make a backup or work on a branch/clone when applying refactors.
Capability Analysis
Type: OpenClaw Skill Name: timeflow-swiftui-view-refactor Version: 1.0.0 The skill bundle contains purely instructional markdown for refactoring SwiftUI code according to specific architectural patterns (MV, Observation, and view ordering). There is no executable code, no network activity, and no evidence of prompt injection or malicious intent in SKILL.md or _meta.json.
Capability Assessment
Purpose & Capability
The name and description (SwiftUI view refactor) match the SKILL.md content: guidance on ordering, MV patterns, view-model handling, and Observation usage. There are no unrelated environment variables, binaries, or install steps requested.
Instruction Scope
The SKILL.md contains only coding/refactor guidelines and examples and does not instruct the agent to read environment variables, secrets, or external endpoints. It does reference a local path ('references/mv-patterns.md') and truncates at the end, which suggests supporting docs may be expected but are not present in the skill bundle — the agent may assume or attempt to fetch missing references, so confirm what the agent will actually execute and whether it will modify files in your repo.
Install Mechanism
No install spec and no code files are included (instruction-only). This minimizes disk writes or fetched code at install time.
Credentials
The skill requests no environment variables, credentials, or config paths — proportionate to a static refactoring guideline.
Persistence & Privilege
always is false and model invocation is allowed (default). The skill does not request persistent privileges or modify other skills; any edits it makes would be to source files, which is expected for a refactoring helper.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install timeflow-swiftui-view-refactor
  3. After installation, invoke the skill by name or use /timeflow-swiftui-view-refactor
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
Initial release of SwiftUI View Refactor guidelines. - Standardizes SwiftUI view file structure and ordering (environments, properties, init, body, helpers). - Emphasizes Model-View (MV) patterns: minimize view models, promote lightweight orchestration. - Outlines when and how to split large bodies and computed views into subviews or dedicated types. - Advises against top-level conditional swaps for stable identity and view trees. - Provides strict handling of view models and @Observable usage for dependency injection. - Includes workflow and formatting practices for large SwiftUI view files.
Metadata
Slug timeflow-swiftui-view-refactor
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is Swiftui View Refactor?

Refactor and review SwiftUI view files for consistent structure, dependency injection, and Observation usage. Use when asked to clean up a SwiftUI view’s lay... It is an AI Agent Skill for Claude Code / OpenClaw, with 133 downloads so far.

How do I install Swiftui View Refactor?

Run "/install timeflow-swiftui-view-refactor" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Swiftui View Refactor free?

Yes, Swiftui View Refactor is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Swiftui View Refactor support?

Swiftui View Refactor is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Swiftui View Refactor?

It is built and maintained by soponcd (@soponcd); the current version is v1.0.0.

💬 Comments