AGENT SKILL

SwiftUI 架构设计与状态管理

swiftui-architecture

用于构建 SwiftUI 视图、管理状态、设置共享服务或进行架构决策;不适用于 UIKit 或遗留模式;提供不依赖 ViewModel 的纯 SwiftUI 数据流,使用 @State、@Binding、@Observable 和 @Environment。

License 许可
MIT
所属仓库
mintuz/skills
最近更新
2026-11-13
Stars
★ 35
Forks
7

01 前言 Front Matter

name
swiftui-architecture
description
WHEN building SwiftUI views, managing state, setting up shared services, or making architectural decisions; NOT for UIKit or legacy patterns; provides pure SwiftUI data flow without ViewModels using @State, @Binding, @Observable, and @Environment.

02 资源文件 11

03 SKILL.md 正文

Modern SwiftUI Architecture

Pure SwiftUI architecture without ViewModels. The references hold patterns, examples, and edge cases.

Scope

This skill covers SwiftUI views and their data flow.

When asked to restructure UIKit or AppKit code, say that this guidance does not cover that code and leave its internals alone. Answer at the boundary:

  • Wrap an existing view controller in UIViewControllerRepresentable to show it in SwiftUI.
  • Wrap an existing view in UIViewRepresentable only when a UIView is already separated from its controller.
  • Use UIHostingController for the opposite direction, to show a SwiftUI view inside UIKit.
  • On macOS, use NSViewControllerRepresentable, NSViewRepresentable, and NSHostingController in the same three roles.

Keep the wrapped code's state where it already lives. Pass the SwiftUI side's values into the wrapper. Send the wrapper's events back out through a callback.

Start Here

  • State management: references/state-management.md
  • Observable services: references/observable-patterns.md
  • Async work: references/async-patterns.md
  • Navigation: references/navigation-patterns.md
  • UI components: references/lists.md, references/scrollview.md, references/forms.md, references/grids.md, references/sheets.md, references/tabs.md
  • Anti-patterns: references/anti-patterns.md

Typical Flow

  1. Give every mutable value and dependency one owner and lifetime (references/state-management.md, principle-derive-from-one-owner).
    • Keep a value in @State in the view that uses it.
    • Lift it to the nearest common ancestor when sibling views read it. Pass it down as an argument or a @Binding.
    • Move it into a shared @Observable service only when it must outlive that ancestor, when it is domain state that unrelated features read, or when passing it down would thread it through views that do not use it. A value in a service takes the service's lifetime, so it stays set after the screen closes (principle-narrow-state-lifetime).
    • Retain each shared service once with @State. Inject each service separately through the environment. Mutate shared state through actions on the service.
  2. Keep views declarative; put business logic in injected services (references/observable-patterns.md, principle-keep-the-core-pure).
  3. Give every async path one owner and one lifetime (references/async-patterns.md).
    • Use .task for work that starts with the view.
    • Use .task(id:) for work that must restart when an input changes. SwiftUI cancels the running task when that identity changes and when the view disappears. Put any debounce inside the task.
    • Start no input-driven work from onChange with an unstructured Task.
    • For user-triggered work, hold the Task in @State and cancel the previous one before starting the next. State what happens to an in-flight task when the view disappears.
    • Tie each result to the input that produced it, so a superseded result never renders as the current one.
    • Check Task.isCancelled immediately before every write to view state that follows an await: a cancelled task keeps running until it checks.
    • Treat cancellation as a non-error outcome. Let a retry re-enter the same path.
  4. Wire navigation with NavigationStack and typed destinations. Follow references/navigation-patterns.md when the project already depends on the AppRouter package.
  5. Build UI with the matching component guides. Audit every decision against references/anti-patterns.md.

Finish when every value and service has one stated owner, data flows down and actions up, and every async path has explicit cancellation and terminal-state behaviour.

04 同仓库更多 Skills

mintuz
mintuz/skills

lens-review

USE WHEN reviewing uncommitted work, a branch diff or a pull request through several installed skills, each run as its own review lens; NOT WHEN you need one cold second opinion (double-check), a verdict against a specification (acceptance-review) or the built-in fixed-lens /code-review. Runs each lens in an isolated read-only agent, has a separate agent try to refute every finding, and returns one severity-ranked report.

★ 35
mintuz
mintuz/skills

XState 状态机建模

xstate

当 React 状态因外部响应而变化——异步提交、进行中或加载标志、重试、取消、轮询、乐观更新、过期响应——或因与可中断的过程(如拖拽或手势)交互而变化时使用;或在构建、审查、测试或可视化 XState 5 actors 和 @xstate/react 集成时使用;不适用于单个事件即可完全设定的值,如文本草稿、焦点标志或简单的开关切换;建模类型化生命周期并渲染合法的 Mermaid stateDiagram-v2 图表。

软件开发工程师前端开发与界面实现
★ 35
mintuz
mintuz/skills

网页界面设计优化

web-design

在优化 UI 布局、排版、色彩或细节打磨时使用;不涉及代码实现;提供简洁的原则,以实现有意图、易读的设计。

界面设计师用户体验设计
★ 35
mintuz
mintuz/skills

Tailwind CSS 样式开发

tailwind

在编写、修复或审查使用 Tailwind CSS 的项目中的样式时使用,即便只是改一个类名,或请求中未提及 Tailwind:颜色、焦点环、间距、由 prop、map 或 CVA 变体选择的类名、Tailwind 样式表或 tailwind.config 中的主题令牌与深色模式、配置或升级(如不确定项目是否使用 Tailwind,先检查 package.json 或主样式表);不适用于在没有 Tailwind 的项目中编写纯 CSS 或 Sass;产出的类名能被 Tailwind 识别,语法适配已安装的主版本,具有可见的焦点指示器,且不引入未要求的依赖或杜撰的品牌值。

软件开发工程师前端开发与界面实现
★ 35
mintuz
mintuz/skills

代码重构规划

refactoring

当你需要制定计划来重构、重组或清理正常工作的代码,且测试结果已知(全绿或已记录的失败集合)时使用;不适用于新功能交付或任何行为变更;以可逆的基线安全性和按优先级排序的改进为核心。

软件开发工程师设计评审与架构优化
★ 35
mintuz
mintuz/skills

react

USE WHEN changing the logic, data, props, state, Effects or rendered text of a React component, hook or page, even a one-line change; NOT WHEN the request is styling only (class names, Tailwind, CSS, dark mode, spacing, shadows) or tests only, or when it models submit, in-flight, loading, retry or failure states, even for a named React form (xstate covers that, including its React wiring); applies React 19 composition, state, Effect, structure, API and performance rules with only the project's installed libraries.

★ 35