Newer Older
165 lines | 6.49kb
Bogdan Timofte authored a week ago
1
//
2
//  MeterSettingsTabView.swift
3
//  USB Meter
4
//
5

            
6
import SwiftUI
7

            
8
struct MeterSettingsTabView: View {
9
    @EnvironmentObject private var meter: Meter
10

            
11
    let isMacIPadApp: Bool
12
    let onBackToHome: () -> Void
13

            
14
    @State private var editingName = false
15
    @State private var editingScreenTimeout = false
16
    @State private var editingScreenBrightness = false
17

            
18
    var body: some View {
19
        VStack(spacing: 0) {
20
            if isMacIPadApp {
21
                settingsMacHeader
22
            }
23
            ScrollView {
24
                VStack(spacing: 14) {
25
                    settingsCard(title: "Name", tint: meter.color) {
26
                        HStack {
27
                            Spacer()
28
                            if !editingName {
29
                                Text(meter.name)
30
                                    .foregroundColor(.secondary)
31
                            }
32
                            ChevronView(rotate: $editingName)
33
                        }
34
                        if editingName {
35
                            EditNameView(editingName: $editingName, newName: meter.name)
36
                        }
37
                    }
38

            
39
                    if meter.operationalState == .dataIsAvailable && meter.supportsManualTemperatureUnitSelection {
40
                        settingsCard(title: "Meter Temperature Unit", tint: .orange) {
41
                            Text("TC66 temperature is shown as degrees without assuming Celsius or Fahrenheit. Keep this matched to the unit configured on the device so you can interpret the reading correctly.")
42
                                .font(.footnote)
43
                                .foregroundColor(.secondary)
44
                            Picker("", selection: $meter.tc66TemperatureUnitPreference) {
45
                                ForEach(TemperatureUnitPreference.allCases) { unit in
46
                                    Text(unit.title).tag(unit)
47
                                }
48
                            }
49
                            .pickerStyle(SegmentedPickerStyle())
50
                        }
51
                    }
52

            
53
                    if meter.operationalState == .dataIsAvailable {
54
                        settingsCard(
55
                            title: meter.reportsCurrentScreenIndex ? "Screen Controls" : "Page Controls",
56
                            tint: .indigo
57
                        ) {
58
                            if meter.reportsCurrentScreenIndex {
59
                                Text("Use these controls when you want to change the screen shown on the device without crowding the main meter view.")
60
                                    .font(.footnote)
61
                                    .foregroundColor(.secondary)
62
                            } else {
63
                                Text("Use these controls when you want to switch device pages without crowding the main meter view.")
64
                                    .font(.footnote)
65
                                    .foregroundColor(.secondary)
66
                            }
67

            
68
                            ControlView(showsHeader: false)
69
                        }
70
                    }
71

            
72
                    if meter.operationalState == .dataIsAvailable && meter.supportsUMSettings {
73
                        settingsCard(title: "Screen Timeout", tint: .purple) {
74
                            HStack {
75
                                Spacer()
76
                                if !editingScreenTimeout {
77
                                    Text(meter.screenTimeout > 0 ? "\(meter.screenTimeout) Minutes" : "Off")
78
                                        .foregroundColor(.secondary)
79
                                }
80
                                ChevronView(rotate: $editingScreenTimeout)
81
                            }
82
                            if editingScreenTimeout {
83
                                EditScreenTimeoutView()
84
                            }
85
                        }
86

            
87
                        settingsCard(title: "Screen Brightness", tint: .yellow) {
88
                            HStack {
89
                                Spacer()
90
                                if !editingScreenBrightness {
91
                                    Text("\(meter.screenBrightness)")
92
                                        .foregroundColor(.secondary)
93
                                }
94
                                ChevronView(rotate: $editingScreenBrightness)
95
                            }
96
                            if editingScreenBrightness {
97
                                EditScreenBrightnessView()
98
                            }
99
                        }
100
                    }
101
                }
102
                .padding()
103
            }
104
            .background(
105
                LinearGradient(
106
                    colors: [meter.color.opacity(0.14), Color.clear],
107
                    startPoint: .topLeading,
108
                    endPoint: .bottomTrailing
109
                )
110
                .ignoresSafeArea()
111
            )
112
        }
113
    }
114

            
115
    private var settingsMacHeader: some View {
116
        HStack(spacing: 12) {
117
            Button(action: onBackToHome) {
118
                HStack(spacing: 4) {
119
                    Image(systemName: "chevron.left")
120
                        .font(.body.weight(.semibold))
121
                    Text("Back")
122
                }
123
                .foregroundColor(.accentColor)
124
            }
125
            .buttonStyle(.plain)
126

            
127
            Text("Meter Settings")
128
                .font(.headline)
129
                .lineLimit(1)
130

            
131
            Spacer()
132

            
133
            if meter.operationalState > .notPresent {
134
                RSSIView(RSSI: meter.btSerial.averageRSSI)
135
                    .frame(width: 18, height: 18)
136
            }
137
        }
138
        .padding(.horizontal, 16)
139
        .padding(.vertical, 10)
140
        .background(
141
            Rectangle()
142
                .fill(.ultraThinMaterial)
143
                .ignoresSafeArea(edges: .top)
144
        )
145
        .overlay(alignment: .bottom) {
146
            Rectangle()
147
                .fill(Color.secondary.opacity(0.12))
148
                .frame(height: 1)
149
        }
150
    }
151

            
152
    private func settingsCard<Content: View>(
153
        title: String,
154
        tint: Color,
155
        @ViewBuilder content: () -> Content
156
    ) -> some View {
157
        VStack(alignment: .leading, spacing: 12) {
158
            Text(title)
159
                .font(.headline)
160
            content()
161
        }
162
        .padding(18)
163
        .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24)
164
    }
165
}