summaryrefslogtreecommitdiff
path: root/Hutch/Views/Home/HomeSectionView.swift
blob: 5f04d58493bf631f56c58e7ab07d1bd792fddef9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import SwiftUI

struct HomeSectionView<Accessory: View, Content: View>: View {
    let title: String
    @Binding var isExpanded: Bool
    let accessory: Accessory
    let content: Content

    init(
        _ title: String,
        isExpanded: Binding<Bool>,
        @ViewBuilder accessory: () -> Accessory,
        @ViewBuilder content: () -> Content
    ) {
        self.title = title
        self._isExpanded = isExpanded
        self.accessory = accessory()
        self.content = content()
    }

    var body: some View {
        Section {
            if isExpanded {
                content
            }
        } header: {
            HomeSectionHeader(
                title: title,
                isExpanded: $isExpanded,
                accessory: accessory
            )
        }
    }
}

private struct HomeSectionHeader<Accessory: View>: View {
    let title: String
    @Binding var isExpanded: Bool
    let accessory: Accessory

    var body: some View {
        HStack(spacing: 12) {
            Button {
                isExpanded.toggle()
            } label: {
                HStack(spacing: 8) {
                    Image(systemName: isExpanded ? "chevron.down" : "chevron.right")
                        .font(.caption.weight(.semibold))
                        .foregroundStyle(.secondary)
                    Text(title)
                }
                .contentShape(Rectangle())
            }
            .buttonStyle(.plain)
            .accessibilityLabel(isExpanded ? "Collapse \(title)" : "Expand \(title)")

            Spacer(minLength: 8)

            accessory
        }
        .textCase(nil)
    }
}