28 lines
1.0 KiB
Swift
28 lines
1.0 KiB
Swift
import SwiftUI
|
|
|
|
/// Four square blocks fill from bottom to top; partial blocks retain the actual fraction.
|
|
struct SegmentedCoreBar: View {
|
|
let value: Double?
|
|
let color: Color
|
|
|
|
static func portions(_ value: Double?, count: Int = 4) -> [Double] {
|
|
guard count > 0 else { return [] }
|
|
let fraction = value.flatMap { $0.isFinite ? min(1, max(0, $0)) : nil } ?? 0
|
|
return (0..<count).map { min(1, max(0, fraction * Double(count) - Double($0))) }
|
|
}
|
|
|
|
var body: some View {
|
|
let fills = Self.portions(value)
|
|
VStack(spacing: 2) {
|
|
ForEach(Array(fills.indices.reversed()), id: \.self) { index in
|
|
ZStack(alignment: .bottom) {
|
|
Rectangle().fill(color.opacity(0.15))
|
|
Rectangle().fill(color).frame(height: 6 * fills[index])
|
|
}.frame(width: 6, height: 6).clipped()
|
|
}
|
|
}
|
|
.accessibilityHidden(true)
|
|
.transaction { $0.animation = nil; $0.disablesAnimations = true }
|
|
}
|
|
}
|