11import SwiftUI
22
3+ /// A SwiftUI component that displays a circular progress.
34public struct SUCircularProgress : View {
5+ // MARK: - Properties
6+
7+ /// A model that defines the appearance properties.
48 public var model : CircularProgressVM
9+
10+ /// The current progress value.
511 public var currentValue : CGFloat
612
13+ // MARK: - Initializer
14+
15+ /// Initializer.
16+ /// - Parameters:
17+ /// - currentValue: Current progress.
18+ /// - model: A model that defines the appearance properties.
719 public init (
8- model : CircularProgressVM = . init ( ) ,
9- currentValue : CGFloat = 0
20+ currentValue : CGFloat = 0 ,
21+ model : CircularProgressVM = . init ( )
1022 ) {
11- self . model = model
1223 self . currentValue = currentValue
24+ self . model = model
1325 }
1426
27+ // MARK: - Body
28+
1529 public var body : some View {
1630 let normalized = self . model. progress ( for: currentValue)
1731
1832 switch self . model. style {
1933 case . light:
2034 ZStack {
35+ // Background part
2136 Path { path in
2237 path. addArc (
2338 center: self . model. center,
@@ -31,8 +46,12 @@ public struct SUCircularProgress: View {
3146 self . model. color. main. color. opacity ( 0.3 ) ,
3247 lineWidth: self . model. circularLineWidth
3348 )
34- . frame ( width: self . model. preferredSize. width, height: self . model. preferredSize. height)
49+ . frame (
50+ width: self . model. preferredSize. width,
51+ height: self . model. preferredSize. height
52+ )
3553
54+ // Foreground part
3655 Path { path in
3756 path. addArc (
3857 center: self . model. center,
@@ -51,8 +70,12 @@ public struct SUCircularProgress: View {
5170 )
5271 )
5372 . rotationEffect ( . degrees( - 90 ) )
54- . frame ( width: self . model. preferredSize. width, height: self . model. preferredSize. height)
73+ . frame (
74+ width: self . model. preferredSize. width,
75+ height: self . model. preferredSize. height
76+ )
5577
78+ // Optional label
5679 if let label = self . model. label {
5780 Text ( label)
5881 . font ( self . model. titleFont. font)
@@ -62,6 +85,7 @@ public struct SUCircularProgress: View {
6285
6386 case . striped:
6487 ZStack {
88+ // Striped background part
6589 Path { path in
6690 path. addArc (
6791 center: self . model. center,
@@ -71,7 +95,10 @@ public struct SUCircularProgress: View {
7195 clockwise: false
7296 )
7397 }
74- . trim ( from: self . model. backgroundArcStart ( for: normalized) , to: self . model. backgroundArcEnd ( for: normalized) )
98+ . trim (
99+ from: self . model. backgroundArcStart ( for: normalized) ,
100+ to: self . model. backgroundArcEnd ( for: normalized)
101+ )
75102 . stroke (
76103 . clear,
77104 style: StrokeStyle (
@@ -92,7 +119,10 @@ public struct SUCircularProgress: View {
92119 clockwise: false
93120 )
94121 }
95- . trim ( from: self . model. backgroundArcStart ( for: normalized) , to: self . model. backgroundArcEnd ( for: normalized) )
122+ . trim (
123+ from: self . model. backgroundArcStart ( for: normalized) ,
124+ to: self . model. backgroundArcEnd ( for: normalized)
125+ )
96126 . stroke (
97127 style: StrokeStyle (
98128 lineWidth: self . model. circularLineWidth,
@@ -102,9 +132,12 @@ public struct SUCircularProgress: View {
102132 }
103133 }
104134 . rotationEffect ( . degrees( - 90 ) )
105- . frame ( width: self . model. preferredSize. width,
106- height: self . model. preferredSize. height)
135+ . frame (
136+ width: self . model. preferredSize. width,
137+ height: self . model. preferredSize. height
138+ )
107139
140+ // Foreground part
108141 Path { path in
109142 path. addArc (
110143 center: self . model. center,
@@ -114,7 +147,10 @@ public struct SUCircularProgress: View {
114147 clockwise: false
115148 )
116149 }
117- . trim ( from: self . model. progressArcStart ( for: normalized) , to: self . model. progressArcEnd ( for: normalized) )
150+ . trim (
151+ from: self . model. progressArcStart ( for: normalized) ,
152+ to: self . model. progressArcEnd ( for: normalized)
153+ )
118154 . stroke (
119155 self . model. color. main. color,
120156 style: StrokeStyle (
@@ -123,9 +159,12 @@ public struct SUCircularProgress: View {
123159 )
124160 )
125161 . rotationEffect ( . degrees( - 90 ) )
126- . frame ( width: self . model. preferredSize. width,
127- height: self . model. preferredSize. height)
162+ . frame (
163+ width: self . model. preferredSize. width,
164+ height: self . model. preferredSize. height
165+ )
128166
167+ // Optional label
129168 if let label = self . model. label {
130169 Text ( label)
131170 . font ( self . model. titleFont. font)
@@ -136,6 +175,8 @@ public struct SUCircularProgress: View {
136175 }
137176}
138177
178+ // MARK: - Helpers
179+
139180struct StripesShapeCircularProgress : Shape , @unchecked Sendable {
140181 var model : CircularProgressVM
141182
0 commit comments