USB-Meter / USB Meter / Views / Meter / MeterSettingsView.swift
Newer Older
183 lines | 6.622kb
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 && meter.supportsManualTemperatureUnitSelection {
Bogdan Timofte authored 2 weeks ago
37
                    settingsCard(title: "Meter Temperature Unit", tint: .orange) {
38
                        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.")
Bogdan Timofte authored 2 weeks ago
39
                            .font(.footnote)
40
                            .foregroundColor(.secondary)
41
                        Picker("", selection: $meter.tc66TemperatureUnitPreference) {
42
                            ForEach(TemperatureUnitPreference.allCases) { unit in
43
                                Text(unit.title).tag(unit)
44
                            }
45
                        }
46
                        .pickerStyle(SegmentedPickerStyle())
47
                    }
Bogdan Timofte authored 2 weeks ago
48
                }
Bogdan Timofte authored 2 weeks ago
49

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

            
65
                        ControlView(showsHeader: false)
66
                    }
67
                }
68

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

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

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

            
128
struct EditNameView: View {
129

            
130
    @EnvironmentObject private var meter: Meter
131

            
132
    @Binding var editingName: Bool
133
    @State var newName: String
134

            
135
    var body: some View {
136
        TextField("Name", text: self.$newName, onCommit: {
137
            self.meter.name = self.newName
138
            self.editingName = false
139
        })
140
            .textFieldStyle(RoundedBorderTextFieldStyle())
141
            .lineLimit(1)
142
            .disableAutocorrection(true)
143
            .multilineTextAlignment(.center)
144
    }
145
}
146

            
147
struct EditScreenTimeoutView: View {
148

            
149
    @EnvironmentObject private var meter: Meter
150

            
151
    var body: some View {
152
        Picker("", selection: self.$meter.screenTimeout ) {
153
            Text("1").tag(1)
154
            Text("2").tag(2)
155
            Text("3").tag(3)
156
            Text("4").tag(4)
157
            Text("5").tag(5)
158
            Text("6").tag(6)
159
            Text("7").tag(7)
160
            Text("8").tag(8)
161
            Text("9").tag(9)
162
            Text("Off").tag(0)
163
        }
164
        .pickerStyle( SegmentedPickerStyle() )
165
    }
166
}
167

            
168
struct EditScreenBrightnessView: View {
169

            
170
    @EnvironmentObject private var meter: Meter
171

            
172
    var body: some View {
173
        Picker("", selection: self.$meter.screenBrightness ) {
174
            Text("0").tag(0)
175
            Text("1").tag(1)
176
            Text("2").tag(2)
177
            Text("3").tag(3)
178
            Text("4").tag(4)
179
            Text("5").tag(5)
180
        }
181
        .pickerStyle( SegmentedPickerStyle() )
182
    }
183
}