← 返回 Skills 市场
soponcd

Swiftui View Refactor

作者 soponcd · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
133
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install timeflow-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...
使用说明 (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.
安全使用建议
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.
功能分析
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.
能力评估
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.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install timeflow-swiftui-view-refactor
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /timeflow-swiftui-view-refactor 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
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.
元数据
Slug timeflow-swiftui-view-refactor
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

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... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 133 次。

如何安装 Swiftui View Refactor?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install timeflow-swiftui-view-refactor」即可一键安装,无需额外配置。

Swiftui View Refactor 是免费的吗?

是的,Swiftui View Refactor 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Swiftui View Refactor 支持哪些平台?

Swiftui View Refactor 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Swiftui View Refactor?

由 soponcd(@soponcd)开发并维护,当前版本 v1.0.0。

💬 留言讨论