Langsung ke konten
Skillpedia
Website & UI (frontend)48k · #5915

swiftui-view-refactor

Fungsinya

Tool ini membantu merapikan kode SwiftUI Anda dengan memecah layar besar menjadi bagian-bagian yang lebih kecil dan mudah dikelola. Tool ini memastikan alur data berjalan dengan jelas dan dapat diprediksi.

Kapan dipakai

  • ✓Saat sebuah layar di aplikasi SwiftUI Anda menjadi terlalu panjang dan membingungkan.
  • ✓Saat Anda perlu membuat bagian layar yang bisa digunakan ulang.
  • ✓Untuk membuat alur data aplikasi lebih jelas dan mudah dipahami.

Link skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/swiftui-view-refactor/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/swiftui-view-refactor/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit →

Contoh permintaan ke AI

“Tolong refaktor layar SwiftUI ini agar dipecah menjadi komponen-komponen yang lebih kecil dan terorganisir.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Tool ini menganalisis layar SwiftUI Anda dan menyarankan cara memecahnya menjadi komponen yang lebih kecil. Tool ini juga membantu memastikan data diteruskan secara terorganisir di antara komponen-komponen tersebut.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

SwiftUI View Refactor

Overview

Refactor SwiftUI views toward small, explicit, stable view types. Default to vanilla SwiftUI: local state in the view, shared dependencies in the environment, business logic in services/models, and view models only when the request or existing code clearly requires one.

When to Use

  • When cleaning up a large SwiftUI view or splitting long body implementations.
  • When you need smaller subviews, explicit dependency injection, or better Observation usage.

Core Guidelines

1) View ordering (top → bottom)

  • Enforce this ordering unless the existing file has a stronger local convention you must preserve.
  • Environment
  • private/public let
  • @State / other stored properties
  • computed var (non-view)
  • init
  • body
  • computed view builders / other view helpers
  • helper / async functions

2) Default to MV, not MVVM

  • Views should be lightweight state expressions and orchestration points, not containers for business logic.
  • Favor @State, @Environment, @Query, .task, .task(id:), and onChange before reaching for a view model.
  • Inject services and shared models via @Environment; keep domain logic in services/models, not in the view body.
  • Do not introduce a view model just to mirror local view state or wrap environment dependencies.
  • If a screen is getting large, split the UI into subviews before inventing a new view model layer.

3) Strongly prefer dedicated subview types over computed some View helpers

  • Flag body properties that are longer than roughly one screen or contain multiple logical sections.
  • Prefer extracting dedicated View types for non-trivial sections, especially when they have state, async work, branching, or deserve their own preview.
  • Keep computed some View helpers rare and small. Do not build an entire screen out of private var header: some View-style fragments.
  • Pass small, explicit inputs (data, bindings, callbacks) into extracted subviews instead of handing down the entire parent state.
  • If an extracted subview becomes reusable or independently meaningful, move it to its own file.

Prefer:

var body: some View {
    List {
        HeaderSection(title: title, subtitle: subtitle)
        FilterSection(
            filterOptions: filterOptions,
            selectedFilter: $selectedFilter
        )
        ResultsSection(items: filteredItems)
        FooterSection()
    }
}

private struct HeaderSection: View {
    let title: String
    let subtitle: String

    var body: some View {
        VStack(alignment: .leading, spacing: 6) {
            Text(title).font(.title2)
            Text(subtitle).font(.subheadline)
        }
    }
}

private struct FilterSection: View {
    let filterOptions: [FilterOption]
    @Binding var selectedFilter: FilterOption

    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack {
                ForEach(filterOptions, id: \.self) { option in
                    FilterChip(option: option, isSelected: option == selectedFilter)
                        .onTapGesture { selectedFilter = option }
                }
            }
        }
    }
}

Avoid:

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)
    }
}

3b) Extract actions and side effects out of body

  • Do not keep non-trivial button actions inline in the view body.
  • Do not bury business logic inside .task, .onAppear, .onChange, or .refreshable.
  • Prefer calling small private methods from the view, and move real business logic into services/models.
  • The body should read like UI, not like a view controller.
Button("Save", action: save)
    .disabled(isSaving)

.task(id: searchText) {
    await reload(for: searchText)
}

private func save() {
    Task { await saveAsync() }
}

private func reload(for searchText: String) async {
    guard !searchText.isEmpty else {
        results = []
        return
    }
    await searchService.search(searchText)
}

4) Keep a stable view tree (avoid top-level conditional view swapping)

  • Avoid body or computed views that return completely different root branches via if/else.

…

Pertanyaan umum

Mengapa layar besar perlu dipecah?

Layar yang besar sulit dibaca, dipelihara, dan di-debug. Memecahnya menjadi bagian yang lebih kecil membuat kode lebih teratur dan lebih mudah dikerjakan.

Apa maksud 'explicit data flow'?

Artinya jelas dari mana data berasal dan ke mana data pergi, sehingga kode lebih mudah diprediksi dan lebih kecil kemungkinan menimbulkan error.

Apakah saya perlu menggunakan 'view model'?

Belum tentu. Tool ini lebih memilih cara lain untuk mengatur kode sebelum memperkenalkan 'view model', kecuali memang benar-benar dibutuhkan.

Detail

Deskripsi asli dari pembuat

Refactor SwiftUI views into smaller components with stable, explicit data flow.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#5915
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa