USB-Meter / USB Meter / Views / Meter / MeterSettingsView.swift
Newer Older
201 lines | 7.147kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  SettingsView.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 14/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10

            
11
struct MeterSettingsView: View {
12

            
13
    @EnvironmentObject private var meter: Meter
14

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

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

            
Bogdan Timofte authored 2 weeks ago
36
                if meter.operationalState == .dataIsAvailable {
Bogdan Timofte authored 2 weeks ago
37
                    settingsCard(title: "Device Info", tint: .blue) {
Bogdan Timofte authored 2 weeks ago
38
                        DeviceInfoRow(label: "Advertised Model", value: meter.modelString)
39
                        DeviceInfoRow(label: "Displayed Model", value: meter.deviceModelSummary)
Bogdan Timofte authored 2 weeks ago
40
                        DeviceInfoRow(label: "Working Voltage", value: meter.documentedWorkingVoltage)
Bogdan Timofte authored 2 weeks ago
41
                        DeviceInfoRow(label: "Temperature Unit", value: meter.temperatureUnitDescription)
Bogdan Timofte authored 2 weeks ago
42
                        if meter.modelNumber != 0 {
43
                            DeviceInfoRow(label: "Model Code", value: "\(meter.modelNumber)")
44
                        }
45
                        if !meter.firmwareVersion.isEmpty {
46
                            DeviceInfoRow(label: "Firmware", value: meter.firmwareVersion)
47
                        }
48
                        if meter.serialNumber != 0 {
49
                            DeviceInfoRow(label: "Serial", value: "\(meter.serialNumber)")
50
                        }
51
                        if meter.bootCount != 0 {
52
                            DeviceInfoRow(label: "Boot Count", value: "\(meter.bootCount)")
53
                        }
54
                    }
Bogdan Timofte authored 2 weeks ago
55
                }
Bogdan Timofte authored 2 weeks ago
56

            
Bogdan Timofte authored 2 weeks ago
57
                if meter.operationalState == .dataIsAvailable && meter.supportsManualTemperatureUnitSelection {
Bogdan Timofte authored 2 weeks ago
58
                    settingsCard(title: "Temperature Unit", tint: .orange) {
Bogdan Timofte authored 2 weeks ago
59
                        Text("TC66 reports temperature using the unit selected on the device. Keep this setting matched to the meter.")
60
                            .font(.footnote)
61
                            .foregroundColor(.secondary)
62
                        Picker("", selection: $meter.tc66TemperatureUnitPreference) {
63
                            ForEach(TemperatureUnitPreference.allCases) { unit in
64
                                Text(unit.title).tag(unit)
65
                            }
66
                        }
67
                        .pickerStyle(SegmentedPickerStyle())
68
                    }
Bogdan Timofte authored 2 weeks ago
69
                }
Bogdan Timofte authored 2 weeks ago
70

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

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

            
115
    private func settingsCard<Content: View>(
116
        title: String,
117
        tint: Color,
118
        @ViewBuilder content: () -> Content
119
    ) -> some View {
120
        VStack(alignment: .leading, spacing: 12) {
121
            Text(title)
122
                .font(.headline)
123
            content()
124
        }
125
        .padding(18)
126
        .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24)
127
    }
Bogdan Timofte authored 2 weeks ago
128
}
129

            
Bogdan Timofte authored 2 weeks ago
130
private struct DeviceInfoRow: View {
131
    let label: String
132
    let value: String
133

            
134
    var body: some View {
135
        HStack {
136
            Text(label)
137
            Spacer()
138
            Text(value)
139
                .foregroundColor(.secondary)
140
                .multilineTextAlignment(.trailing)
141
        }
142
        .font(.footnote)
143
    }
144
}
145

            
Bogdan Timofte authored 2 weeks ago
146
struct EditNameView: View {
147

            
148
    @EnvironmentObject private var meter: Meter
149

            
150
    @Binding var editingName: Bool
151
    @State var newName: String
152

            
153
    var body: some View {
154
        TextField("Name", text: self.$newName, onCommit: {
155
            self.meter.name = self.newName
156
            self.editingName = false
157
        })
158
            .textFieldStyle(RoundedBorderTextFieldStyle())
159
            .lineLimit(1)
160
            .disableAutocorrection(true)
161
            .multilineTextAlignment(.center)
162
    }
163
}
164

            
165
struct EditScreenTimeoutView: View {
166

            
167
    @EnvironmentObject private var meter: Meter
168

            
169
    var body: some View {
170
        Picker("", selection: self.$meter.screenTimeout ) {
171
            Text("1").tag(1)
172
            Text("2").tag(2)
173
            Text("3").tag(3)
174
            Text("4").tag(4)
175
            Text("5").tag(5)
176
            Text("6").tag(6)
177
            Text("7").tag(7)
178
            Text("8").tag(8)
179
            Text("9").tag(9)
180
            Text("Off").tag(0)
181
        }
182
        .pickerStyle( SegmentedPickerStyle() )
183
    }
184
}
185

            
186
struct EditScreenBrightnessView: View {
187

            
188
    @EnvironmentObject private var meter: Meter
189

            
190
    var body: some View {
191
        Picker("", selection: self.$meter.screenBrightness ) {
192
            Text("0").tag(0)
193
            Text("1").tag(1)
194
            Text("2").tag(2)
195
            Text("3").tag(3)
196
            Text("4").tag(4)
197
            Text("5").tag(5)
198
        }
199
        .pickerStyle( SegmentedPickerStyle() )
200
    }
201
}