DFSegmentBar.swift 1.51 KB
//
//  DFSegmentBar.swift
//  hippo-watch-extension
//

import SwiftUI

struct DFSegmentBar: View {
    let selected: DFStressStatus?
    private let kinds: [DFStressStatus] = [.overloaded, .little, .normal, .excellent]

    var body: some View {
        if selected == nil {
            Capsule()
                .fill(DFWatchFaceColor.grayBar)
        } else {
            GeometryReader { proxy in
                HStack(spacing: 2) {
                    ForEach(kinds, id: \.rawValue) { kind in
                        Capsule()
                            .fill(kind.color)
                            .frame(width: segmentWidth(kind: kind, totalWidth: proxy.size.width))
                            .overlay {
                                if selected == kind {
                                    Capsule()
                                        .stroke(kind.color, lineWidth: 3)
                                        .background(Capsule().fill(Color.black))
                                        .frame(width: 10, height: 10)
                                }
                            }
                    }
                }
            }
        }
    }

    private func segmentWidth(kind: DFStressStatus, totalWidth: CGFloat) -> CGFloat {
        guard let selected else {
            return totalWidth
        }
        let spacing: CGFloat = 6
        let extra: CGFloat = totalWidth * 0.18
        let base = (totalWidth - spacing - extra) / CGFloat(kinds.count)
        return selected == kind ? base + extra : base
    }
}