twigs-ios/Twigs/Views/ProgressView.swift

61 lines
2 KiB
Swift
Raw Normal View History

//
// ProgressView.swift
// Twigs
//
// Created by Billy Brawner on 10/20/19.
// Copyright © 2019 William Brawner. All rights reserved.
//
import SwiftUI
struct ProgressView: View {
2021-09-21 01:59:13 +00:00
var value: Float
var maxValue: Float = 100.0
var progressTintColor: Color = .blue
2021-09-21 01:59:13 +00:00
var progressBarHeight: Float = 20
var progressBarCornerRadius: Float = 4.0
2022-05-19 02:05:03 +00:00
@State
var progressBarWidth: CGFloat = 0.0
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .leading) {
Rectangle()
.opacity(0.1)
Rectangle()
2021-09-21 01:59:13 +00:00
.frame(width: getProgressBarWidth(geometry: geometry))
.opacity(0.5)
.background(self.progressTintColor)
2022-05-19 02:05:03 +00:00
.animation(.default, value: progressBarWidth)
.onAppear {
self.progressBarWidth = getProgressBarWidth(geometry: geometry)
}
2021-09-21 01:59:13 +00:00
}.frame(height: CGFloat(self.progressBarHeight))
.cornerRadius(CGFloat(self.progressBarCornerRadius))
}
}
private func getProgressBarWidth(geometry: GeometryProxy) -> CGFloat {
let frame = geometry.frame(in: .global)
return max(0, frame.size.width * min(CGFloat(value / maxValue), CGFloat(1)))
}
}
struct ProgressView_Previews: PreviewProvider {
static var previews: some View {
2021-09-21 01:59:13 +00:00
VStack {
Text("0%")
ProgressView(value: 1.0, maxValue: 0.0, progressTintColor: .red)
Text("25%")
ProgressView(value: 1.0, maxValue: 4.0, progressTintColor: .green)
Text("50%")
ProgressView(value: 1.0, maxValue: 2.0, progressTintColor: .blue)
Text("66%")
ProgressView(value: 2.0, maxValue: 3.0, progressTintColor: .orange)
Text("150%")
ProgressView(value: 150.0, maxValue: 100.0, progressTintColor: .purple)
}.padding(50)
}
}