1 contributor
81 lines | 2.245kb
//
//  SwiftUIView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 14/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI


struct RSSIView: View {
    
    var RSSI: Int
    
    var body: some View {
        GeometryReader { box in
            HStack (alignment: VerticalAlignment.bottom, spacing: 3) {
                ZStack {
                    Rectangle().stroke()
                    if self.RSSI >= -110 {
                        Rectangle()
                            .foregroundColor(self.clr())
                    }
                }
                .frame(height: box.size.height * 0.2)
                ZStack {
                    Rectangle().stroke()
                    if self.RSSI >= -100 {
                        Rectangle()
                        .foregroundColor(self.clr())
                    }
                }
                .frame(height: box.size.height * 0.4)
                ZStack {
                    Rectangle().stroke()
                    if self.RSSI >= -80 {
                        Rectangle()
                        .foregroundColor(self.clr())
                    }
                }
                .frame(height: box.size.height * 0.6)
                ZStack {
                    Rectangle().stroke()
                    if self.RSSI >= -60 {
                        Rectangle()
                        .foregroundColor(self.clr())
                    }
                }
                .frame(height: box.size.height * 0.8)
                ZStack {
                    Rectangle().stroke()
                    if self.RSSI >= -50 {
                        Rectangle()
                        .foregroundColor(self.clr())
                    }
                }
                .frame(height: box.size.height/1)
            }
        }
    }

    private func clr() -> Color {
        switch RSSI {
        case let x where x < -100:
            return .red
        case let x where x < -80:
            return .orange
        default:
            return .green
        }
    }

}


struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        RSSIView(RSSI: -80).frame(width: 64, height: 64, alignment: .center)
    }
}