Newer Older
61 lines | 1.698kb
Bogdan Timofte authored 2 weeks ago
1
//
Bogdan Timofte authored a week ago
2
//  RSSIView.swift
Bogdan Timofte authored 2 weeks ago
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

            
12
struct RSSIView: View {
13

            
14
    var RSSI: Int
Bogdan Timofte authored 2 weeks ago
15
    private let barThresholds = [-100, -85, -70, -55]
16
    private let barHeights: [CGFloat] = [5, 8, 11, 14]
Bogdan Timofte authored 2 weeks ago
17

            
18
    var body: some View {
Bogdan Timofte authored 2 weeks ago
19
        HStack(alignment: .bottom, spacing: 2) {
20
            ForEach(Array(barHeights.enumerated()), id: \.offset) { index, height in
21
                RoundedRectangle(cornerRadius: 1.2, style: .continuous)
22
                    .stroke(barStrokeColor(for: index), lineWidth: 1)
23
                    .background(
24
                        RoundedRectangle(cornerRadius: 1.2, style: .continuous)
25
                            .fill(index < activeBarCount ? clr() : Color.clear)
26
                    )
27
                    .frame(width: 3, height: height)
Bogdan Timofte authored 2 weeks ago
28
            }
29
        }
Bogdan Timofte authored 2 weeks ago
30
        .frame(width: 18, height: 16, alignment: .bottom)
31
    }
32

            
33
    private var activeBarCount: Int {
34
        barThresholds.reduce(0) { count, threshold in
35
            count + (RSSI >= threshold ? 1 : 0)
36
        }
37
    }
38

            
39
    private func barStrokeColor(for index: Int) -> Color {
40
        index < activeBarCount ? clr().opacity(0.65) : Color.secondary.opacity(0.35)
Bogdan Timofte authored 2 weeks ago
41
    }
42

            
43
    private func clr() -> Color {
44
        switch RSSI {
45
        case let x where x < -100:
46
            return .red
47
        case let x where x < -80:
48
            return .orange
49
        default:
50
            return .green
51
        }
52
    }
53

            
54
}
55

            
56

            
Bogdan Timofte authored a week ago
57
struct RSSIView_Previews: PreviewProvider {
Bogdan Timofte authored 2 weeks ago
58
    static var previews: some View {
Bogdan Timofte authored 2 weeks ago
59
        RSSIView(RSSI: -80).frame(width: 20, height: 20, alignment: .center)
Bogdan Timofte authored 2 weeks ago
60
    }
61
}