// // RSSIView.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 private let barThresholds = [-100, -85, -70, -55] private let barHeights: [CGFloat] = [5, 8, 11, 14] var body: some View { HStack(alignment: .bottom, spacing: 2) { ForEach(Array(barHeights.enumerated()), id: \.offset) { index, height in RoundedRectangle(cornerRadius: 1.2, style: .continuous) .stroke(barStrokeColor(for: index), lineWidth: 1) .background( RoundedRectangle(cornerRadius: 1.2, style: .continuous) .fill(index < activeBarCount ? clr() : Color.clear) ) .frame(width: 3, height: height) } } .frame(width: 18, height: 16, alignment: .bottom) } private var activeBarCount: Int { barThresholds.reduce(0) { count, threshold in count + (RSSI >= threshold ? 1 : 0) } } private func barStrokeColor(for index: Int) -> Color { index < activeBarCount ? clr().opacity(0.65) : Color.secondary.opacity(0.35) } 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 RSSIView_Previews: PreviewProvider { static var previews: some View { RSSIView(RSSI: -80).frame(width: 20, height: 20, alignment: .center) } }