krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.2.0: Hutch/Views/Home/HomeSectionView.swift · raw

 1import SwiftUI
 2
 3struct HomeSectionView<Accessory: View, Content: View>: View {
 4    let title: String
 5    @Binding var isExpanded: Bool
 6    let accessory: Accessory
 7    let content: Content
 8
 9    init(
10        _ title: String,
11        isExpanded: Binding<Bool>,
12        @ViewBuilder accessory: () -> Accessory,
13        @ViewBuilder content: () -> Content
14    ) {
15        self.title = title
16        self._isExpanded = isExpanded
17        self.accessory = accessory()
18        self.content = content()
19    }
20
21    var body: some View {
22        Section {
23            if isExpanded {
24                content
25            }
26        } header: {
27            HomeSectionHeader(
28                title: title,
29                isExpanded: $isExpanded,
30                accessory: accessory
31            )
32        }
33    }
34}
35
36private struct HomeSectionHeader<Accessory: View>: View {
37    let title: String
38    @Binding var isExpanded: Bool
39    let accessory: Accessory
40
41    var body: some View {
42        HStack(spacing: 12) {
43            Button {
44                isExpanded.toggle()
45            } label: {
46                HStack(spacing: 8) {
47                    Image(systemName: isExpanded ? "chevron.down" : "chevron.right")
48                        .font(.caption.weight(.semibold))
49                        .foregroundStyle(.secondary)
50                    Text(title)
51                }
52                .contentShape(Rectangle())
53            }
54            .buttonStyle(.plain)
55            .accessibilityLabel(isExpanded ? "Collapse \(title)" : "Expand \(title)")
56
57            Spacer(minLength: 8)
58
59            accessory
60        }
61        .textCase(nil)
62    }
63}