Engineering
swiftui-view-refactor avatar

swiftui-view-refactor

Refactors SwiftUI views for clean architecture, consistent property ordering, efficient dependency injection, and correct @Observable state management.

Introduction

This skill provides a systematic approach to refactoring and standardizing SwiftUI view files. It is designed for software engineers working on complex iOS/macOS applications who need to maintain code quality, readability, and architectural integrity. The tool enforces specific design patterns such as the Model-View (MV) pattern, discouraging the overuse of ViewModels in favor of native SwiftUI data flow mechanisms like @State, @Environment, @Query, and Observation-based state management.

  • Standardizes code layout by enforcing a strict property ordering: Environment, private constants, @State properties, computed non-view variables, init, body, and view helpers.

  • Promotes the extraction of complex view bodies into smaller, reusable subviews or computed properties to keep files maintainable and components focused.

  • Enforces strict dependency injection practices by ensuring that dependencies are passed through initializers and assigned to non-optional properties.

  • Guides developers on the correct usage of @Observable, specifically ensuring reference types are stored correctly as @State in root views to prevent unnecessary state invalidation.

  • Provides automated guidance on managing large view files, including best practices for using extensions and MARK comments to improve file navigation.

  • Adheres to the 'Forget MVVM' philosophy, ensuring that views remain lightweight declarations of state while offloading business logic to services and domain models.

  • Intended for SwiftUI developers handling professional codebases with growing complexity and UI debt.

  • Input: A SwiftUI view file needing cleanup, reordering, or architectural migration.

  • Output: A refactored Swift source file that aligns with the established ordering rules and Model-View patterns.

  • Constraints: The skill operates on the assumption that UI-specific business logic belongs in domain services; it will not automatically invent ViewModels unless explicitly requested or required by existing code structures. Use this whenever a file exceeds ~300 lines or shows signs of 'Massive View Controller' anti-patterns.

Repository Stats

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