swiftui-view-refactor
What it does
This skill restructures SwiftUI view code so it is easier to read and maintain. It is for iOS developers who have large or messy views.
When to use it
- ✓When a SwiftUI view file has grown large and hard to read.
- ✓When a view's body is too long and should be split into smaller parts.
- ✓When you want a view's state and dependencies set up in a consistent way.
Skill link
https://github.com/steipete/agent-scripts/blob/main/skills/swiftui-view-refactor/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“This SwiftUI screen file is huge. Refactor it into smaller views with a clean structure.”
5 new skills a week. 1 minute to read.
How it works
The agent applies a consistent order to the view's properties and code, splits large bodies into subviews, and favors simple state and environment injection over extra view models.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
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/publiclet@State/ other stored properties- computed
var(non-view) initbody- 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, andtask/onChangefor 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
bodygrows beyond a screen or has multiple logical sections, split it into smaller subviews. - Extract large computed view properties (
var header: some View { ... }) into dedicatedViewtypes 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
Viewstruct 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)
}
}
}
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'sinit. - Avoid
bootstrapIfNeededpatterns.
Example (Observation-based):
@State private var viewModel: SomeViewModel
init(dependency: Dependency) {
_viewModel = State(initialValue: SomeViewModel(dependency: dependency))
}
5) Observation usage
- For
@Observablereference types, store them as@Statein the root view. - Pass observables down explicitly as needed; avoid optional state unless required.
Workflow
- Reorder the view to match the ordering rules.
- Favor MV: move lightweight orchestration into the view using
@State,@Environment,@Query,task, andonChange. - If a view model exists, replace optional view models with a non-optional
@Stateview model initialized ininitby passing dependencies from the view. - Confirm Observation usage:
@Statefor root@Observableview models, no redundant wrappers. - 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
bodyand non-view computed vars aboveinit. - For MV-first guidance and rationale, see
references/mv-patterns.md.
FAQ
Does it add a view model to each screen?
No. The skill favors a lightweight pattern where views stay simple and models own business logic.
Where should properties sit in a view file?
In a fixed order, starting with environment values and then stored properties.
When should a helper become its own view?
When it carries state or complex branching, or when reuse makes structural sense.
Details
Original description by the author
SwiftUI view refactor/review: layout, DI, Observation, view models.
repo: steipete/agent-scripts
Popularity
- GitHub stars
- 7.4k
- Ranking position
- #13251
- License
- MIT
- Updated
- 10/30/2026
Comments
- …
More skills from this repository
beeper
7.4kHelps your agent search your local Beeper chat history to find messages and work out which chat or contact a vague hint refers to.
cloudflare-registrar
7.4kHelps your agent check Cloudflare Registrar domain prices and availability, and register or list domains with a confirmation step.
codexbar
7.4kHelps your agent check how much usage you have left on AI services such as Codex, Claude, and Kimi, including limits, credits, and reset times.
create-cli
7.4kHelps your agent design a command line tool's commands, flags, help text, output, and errors so it works for both people and scripts.
Similar skills
android-clean-architecture
280kMakes your agent organize Android projects efficiently. It structures the code to make maintenance and new feature development easier.
compose-multiplatform-patterns
280kHelps your agent use Compose Multiplatform and Jetpack Compose patterns in KMP projects, covering state, navigation, theming, and performance.
dart-flutter-patterns
280kMakes your agent write code for Dart and Flutter apps. It uses modern patterns to create robust and manageable apps.