Newer Older
100 lines | 3.02kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  ControlView.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 09/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10

            
11
struct ControlView: View {
12

            
13
    @EnvironmentObject private var meter: Meter
14

            
15
    var body: some View {
Bogdan Timofte authored 2 weeks ago
16
        VStack(alignment: .leading, spacing: 14) {
17
            HStack {
18
                Text("Controls")
19
                    .font(.headline)
20
                Spacer()
21
                Text(meter.reportsCurrentScreenIndex ? "Device Screen" : "Page Controls")
22
                    .font(.caption.weight(.semibold))
23
                    .foregroundColor(.secondary)
24
            }
25

            
26
            HStack(spacing: 12) {
27
                controlButton(
28
                    title: "Prev",
29
                    symbol: "chevron.left",
30
                    tint: .indigo,
31
                    action: { meter.previousScreen() }
32
                )
33

            
Bogdan Timofte authored 2 weeks ago
34
                currentScreenCard
Bogdan Timofte authored 2 weeks ago
35
                .frame(maxWidth: .infinity, minHeight: 92)
36
                .padding(.horizontal, 12)
37
                .meterCard(tint: meter.color, fillOpacity: 0.06, strokeOpacity: 0.10)
38

            
39
                controlButton(
40
                    title: "Next",
41
                    symbol: "chevron.right",
42
                    tint: .indigo,
43
                    action: { meter.nextScreen() }
44
                )
45
            }
46

            
47
            controlButton(
48
                title: "Rotate Screen",
49
                symbol: "rotate.right.fill",
50
                tint: .orange,
51
                compact: false,
52
                action: { meter.rotateScreen() }
53
            )
54
        }
55
    }
56

            
Bogdan Timofte authored 2 weeks ago
57
    @ViewBuilder
58
    private var currentScreenCard: some View {
59
        if meter.reportsCurrentScreenIndex {
60
            Text(meter.currentScreenDescription)
61
                .font(.subheadline.weight(.semibold))
62
                .multilineTextAlignment(.center)
63
        } else {
64
            VStack {
65
                Image(systemName: "questionmark.square.dashed")
66
                    .font(.system(size: 24, weight: .semibold))
67
                    .foregroundColor(.secondary)
68
            }
69
        }
70
    }
71

            
Bogdan Timofte authored 2 weeks ago
72
    private func controlButton(
73
        title: String,
74
        symbol: String,
75
        tint: Color,
76
        compact: Bool = true,
77
        action: @escaping () -> Void
78
    ) -> some View {
79
        Button(action: action) {
80
            VStack(spacing: 10) {
81
                Image(systemName: symbol)
82
                    .font(.system(size: compact ? 18 : 20, weight: .semibold))
83
                Text(title)
84
                    .font(.footnote.weight(.semibold))
85
                    .multilineTextAlignment(.center)
Bogdan Timofte authored 2 weeks ago
86
            }
Bogdan Timofte authored 2 weeks ago
87
            .foregroundColor(tint)
88
            .frame(maxWidth: .infinity, minHeight: compact ? 92 : 68)
89
            .padding(.horizontal, 8)
90
            .meterCard(tint: tint, fillOpacity: 0.10, strokeOpacity: 0.14)
Bogdan Timofte authored 2 weeks ago
91
        }
Bogdan Timofte authored 2 weeks ago
92
        .buttonStyle(.plain)
Bogdan Timofte authored 2 weeks ago
93
    }
94
}
95

            
96
struct ControlView_Previews: PreviewProvider {
97
    static var previews: some View {
98
        ControlView()
99
    }
100
}