Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  ProgressBarView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 5/5/22.
6
//
7
 
8
import SwiftUI
9
 
10
struct ProgressBar: View {
11
    private let value: Double
12
    private let maxValue: Double
13
    private let backgroundEnabled: Bool
14
    private let backgroundColor: Color
15
    private let foregroundColor: Color
16
 
17
    init(value: Double,
18
         maxValue: Double,
19
         backgroundEnabled: Bool = true,
20
         backgroundColor: Color = Color(UIColor(red: 245/255,
21
                                                green: 245/255,
22
                                                blue: 245/255,
23
                                                alpha: 1.0)),
24
         foregroundColor: Color = Color.black) {
25
        self.value = value
26
        self.maxValue = maxValue
27
        self.backgroundEnabled = backgroundEnabled
28
        self.backgroundColor = backgroundColor
29
        self.foregroundColor = foregroundColor
30
    }
31
 
32
    var body: some View {
33
        // 1
34
        ZStack {
35
            // 2
36
            GeometryReader { geometryReader in
37
                // 3
38
                if self.backgroundEnabled {
39
                    Capsule()
40
                        .foregroundColor(self.backgroundColor) // 4
41
                }
42
 
43
                Capsule()
44
                    .frame(width: self.progress(value: self.value,
45
                                                maxValue: self.maxValue,
46
                                                width: geometryReader.size.width)) // 5
47
                    .foregroundColor(self.foregroundColor) // 6
48
                    .animation(.easeIn) // 7
49
            }
50
        }
51
    }
52
 
53
    private func progress(value: Double,
54
                          maxValue: Double,
55
                          width: CGFloat) -> CGFloat {
56
        let percentage = value / maxValue
57
        return width *  CGFloat(percentage)
58
    }
59
}
60
 
61
 
62
 
63
struct ProgressBar_Previews: PreviewProvider {
64
    static var previews: some View {
65
        ProgressBar(value: 50, maxValue: 100)
66
    }
67
}
68