USB-Meter / USB Meter / Views / Sidebar / SidebarList / Sections / ContentSidebarHelpSectionView.swift
Newer Older
141 lines | 5.467kb
Bogdan Timofte authored a week ago
1
//
2
//  ContentSidebarHelpSectionView.swift
3
//  USB Meter
4
//
5

            
6
import SwiftUI
7

            
8
struct ContentSidebarHelpSectionView<BluetoothHelpDestination: View, DeviceHelpDestination: View>: View {
9
    let activeReason: SidebarHelpReason?
10
    let isExpanded: Bool
11
    let bluetoothStatusTint: Color
12
    let bluetoothStatusText: String
13
    let cloudSyncHelpTitle: String
14
    let cloudSyncHelpMessage: String
15
    let onToggle: () -> Void
16
    let onOpenSettings: () -> Void
17
    let bluetoothHelpDestination: BluetoothHelpDestination
18
    let deviceHelpDestination: DeviceHelpDestination
19

            
20
    init(
21
        activeReason: SidebarHelpReason?,
22
        isExpanded: Bool,
23
        bluetoothStatusTint: Color,
24
        bluetoothStatusText: String,
25
        cloudSyncHelpTitle: String,
26
        cloudSyncHelpMessage: String,
27
        onToggle: @escaping () -> Void,
28
        onOpenSettings: @escaping () -> Void,
29
        @ViewBuilder bluetoothHelpDestination: () -> BluetoothHelpDestination,
30
        @ViewBuilder deviceHelpDestination: () -> DeviceHelpDestination
31
    ) {
32
        self.activeReason = activeReason
33
        self.isExpanded = isExpanded
34
        self.bluetoothStatusTint = bluetoothStatusTint
35
        self.bluetoothStatusText = bluetoothStatusText
36
        self.cloudSyncHelpTitle = cloudSyncHelpTitle
37
        self.cloudSyncHelpMessage = cloudSyncHelpMessage
38
        self.onToggle = onToggle
39
        self.onOpenSettings = onOpenSettings
40
        self.bluetoothHelpDestination = bluetoothHelpDestination()
41
        self.deviceHelpDestination = deviceHelpDestination()
42
    }
43

            
44
    var body: some View {
45
        Section(header: Text("Help & Troubleshooting").font(.headline)) {
46
            Button(action: onToggle) {
47
                HStack(spacing: 14) {
48
                    Image(systemName: sectionSymbol)
49
                        .font(.system(size: 18, weight: .semibold))
50
                        .foregroundColor(sectionTint)
51
                        .frame(width: 42, height: 42)
52
                        .background(Circle().fill(sectionTint.opacity(0.18)))
53

            
54
                    Text("Help")
55
                        .font(.headline)
56

            
57
                    Spacer()
58

            
59
                    if let activeReason {
60
                        Text(activeReason.badgeTitle)
61
                            .font(.caption2.weight(.bold))
62
                            .foregroundColor(activeReason.tint)
63
                            .padding(.horizontal, 10)
64
                            .padding(.vertical, 6)
65
                            .background(
66
                                Capsule(style: .continuous)
67
                                    .fill(activeReason.tint.opacity(0.12))
68
                            )
69
                            .overlay(
70
                                Capsule(style: .continuous)
71
                                    .stroke(activeReason.tint.opacity(0.22), lineWidth: 1)
72
                            )
73
                    }
74

            
75
                    Image(systemName: isExpanded ? "chevron.up" : "chevron.down")
76
                        .font(.footnote.weight(.bold))
77
                        .foregroundColor(.secondary)
78
                }
79
                .padding(14)
80
                .meterCard(tint: sectionTint, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 18)
81
            }
82
            .buttonStyle(.plain)
83

            
84
            if isExpanded {
85
                if let activeReason {
86
                    SidebarHelpNoticeCardView(
87
                        reason: activeReason,
88
                        cloudSyncHelpTitle: cloudSyncHelpTitle,
89
                        cloudSyncHelpMessage: cloudSyncHelpMessage
90
                    )
91
                }
92

            
93
                SidebarBluetoothStatusCardView(
94
                    tint: bluetoothStatusTint,
95
                    statusText: bluetoothStatusText
96
                )
97

            
98
                if activeReason == .cloudSyncUnavailable {
99
                    Button(action: onOpenSettings) {
100
                        SidebarLinkCardView(
101
                            title: "Open Settings",
102
                            subtitle: "Check Apple ID, iCloud Drive, and any restrictions affecting Cloud sync.",
103
                            symbol: "gearshape.fill",
104
                            tint: .indigo
105
                        )
106
                    }
107
                    .buttonStyle(.plain)
108
                }
109

            
110
                NavigationLink(destination: bluetoothHelpDestination) {
111
                    SidebarLinkCardView(
112
                        title: "Bluetooth",
113
                        subtitle: "Permissions, adapter state, and connection tips.",
114
                        symbol: "bolt.horizontal.circle.fill",
115
                        tint: bluetoothStatusTint
116
                    )
117
                }
118
                .buttonStyle(.plain)
119

            
120
                NavigationLink(destination: deviceHelpDestination) {
121
                    SidebarLinkCardView(
122
                        title: "Device",
123
                        subtitle: "Quick checks when a meter is not responding as expected.",
124
                        symbol: "questionmark.circle.fill",
125
                        tint: .orange
126
                    )
127
                }
128
                .buttonStyle(.plain)
129
            }
130
        }
131
        .animation(.easeInOut(duration: 0.22), value: isExpanded)
132
    }
133

            
134
    private var sectionTint: Color {
135
        activeReason?.tint ?? .secondary
136
    }
137

            
138
    private var sectionSymbol: String {
139
        activeReason?.symbol ?? "questionmark.circle.fill"
140
    }
141
}