Engineering
swiftui-view-refactor avatar

swiftui-view-refactor

Refactor SwiftUI views for consistent structure, dependency injection, and Observation usage while adhering to Model-View architectural patterns.

Introduction

This skill provides a systematic approach for standardizing SwiftUI view files, ensuring they remain maintainable, performant, and aligned with modern Swift development practices. It is designed for iOS and macOS developers who want to eliminate architectural clutter, such as unnecessary ViewModels, and instead embrace the power of the @Observable macro, @State, and @Environment for state management. By enforcing a consistent file structure—from property ordering to view builder grouping—the skill helps teams achieve a uniform codebase across large projects.

  • Enforces a strict property and method ordering (Environment, let, @State, computed vars, init, body, helper functions) to improve readability.

  • Promotes Model-View (MV) patterns over complex MVVM, encouraging lightweight views that delegate logic to specialized services or data models.

  • Facilitates the safe handling of dependencies by ensuring view models are non-optional and initialized with injected services via the view's initializer.

  • Provides clear instructions for splitting large view bodies into smaller, composable subviews or dedicated View types to maintain single-responsibility principles.

  • Validates correct Observation usage, ensuring @Observable reference types are properly stored as @State and passed down through the view hierarchy.

  • Includes techniques for managing large files using extension blocks and // MARK: annotations to categorize subviews, actions, and helper logic.

  • Best used when refactoring legacy SwiftUI code or onboarding new features that require strict dependency management.

  • Requires existing SwiftUI views; the skill does not create new business logic but organizes existing structures to be more declarative.

  • Recommended for projects targeting iOS 17+ where the Observation framework is available and preferred over ObservableObject.

  • Constraints: Avoids introducing ViewModels where standard SwiftUI property wrappers like @State or @Query suffice, favoring the 'forget MVVM' philosophy.

  • Inputs: Accepts a SwiftUI file or a request to clean up view layout, dependency injection patterns, or state initialization logic.

Repository Stats

Stars
312
Forks
46
Open Issues
1
Language
Vim Script
Default Branch
main
Sync Status
Idle
Last Synced
Apr 28, 2026, 12:29 PM
View on GitHub