Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  FiveStarView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 7/27/22.
6
//
7
 
8
import SwiftUI
9
 
10
 
11
public struct FiveStarView: View {
12
    var rating: Decimal
13
    var color: Color
14
    var backgroundColor: Color
15
 
16
 
17
    public init(
18
        rating: Decimal,
19
        color: Color = .red,
20
        backgroundColor: Color = .gray
21
    ) {
22
        self.rating = rating
23
        self.color = color
24
        self.backgroundColor = backgroundColor
25
    }
26
 
27
 
28
    public var body: some View {
29
        ZStack {
30
            BackgroundStars(backgroundColor)
31
            ForegroundStars(rating: rating, color: color)
32
        }
33
    }
34
}
35
 
36
 
37
struct RatingStar: View {
38
    var rating: CGFloat
39
    var color: Color
40
    var index: Int
41
 
42
 
43
    var maskRatio: CGFloat {
44
        let mask = rating - CGFloat(index)
45
 
46
        switch mask {
47
        case 1...: return 1
48
        case ..<0: return 0
49
        default: return mask
50
        }
51
    }
52
 
53
 
54
    init(rating: Decimal, color: Color, index: Int) {
55
 
56
        self.rating = CGFloat(Double(rating.description) ?? 0)
57
        self.color = color
58
        self.index = index
59
    }
60
 
61
 
62
    var body: some View {
63
        GeometryReader { star in
64
            StarImage()
65
                .foregroundColor(self.color)
66
                .mask(
67
                    Rectangle()
68
                        .size(
69
                            width: star.size.width * self.maskRatio,
70
                            height: star.size.height
71
                        )
72
 
73
                )
74
        }
75
    }
76
}
77
 
78
 
79
private struct StarImage: View {
80
 
81
 
82
    var body: some View {
83
        Image(systemName: "star.fill")
84
            .resizable()
85
            .aspectRatio(contentMode: .fill)
86
 
87
    }
88
}
89
 
90
 
91
private struct BackgroundStars: View {
92
    var color: Color
93
 
94
 
95
    init(_ color: Color) {
96
        self.color = color
97
    }
98
 
99
 
100
    var body: some View {
101
        HStack {
102
            ForEach(0..<5) { _ in
103
                StarImage()
104
            }
105
        }.foregroundColor(color)
106
    }
107
}
108
 
109
 
110
private struct ForegroundStars: View {
111
    var rating: Decimal
112
    var color: Color
113
 
114
 
115
    init(rating: Decimal, color: Color) {
116
        self.rating = rating
117
        self.color = color
118
    }
119
 
120
 
121
    var body: some View {
122
        HStack {
123
            ForEach(0..<5) { index in
124
                RatingStar(
125
                    rating: self.rating,
126
                    color: self.color,
127
                    index: index
128
                )
129
            }
130
        }
131
    }
132
}
133
 
134
 
135
struct FiveStarView_Previews: PreviewProvider {
136
 
137
 
138
    static var previews: some View {
139
        VStack {
140
            FiveStarView(rating: 4.5)
141
                .frame(minWidth: 1, idealWidth: 100, maxWidth: 150, minHeight: 1, idealHeight: 30, maxHeight: 50, alignment: .center)
142
 
143
        }
144
    }
145
}