55// Created by Stijn van de Water on 18/07/2024.
66//
77
8- import WidgetKit
9- import SwiftUI
108import addy_shared
11-
12-
13-
9+ import SwiftUI
10+ import WidgetKit
1411
1512private func getUserResource( ) -> UserResource ? {
1613 return CacheHelper . getBackgroundServiceCacheUserResource ( )
17-
1814}
1915
2016private func getMostActiveAliasesData( ) -> [ Aliases ] ? {
@@ -23,28 +19,28 @@ private func getMostActiveAliasesData() -> [Aliases]? {
2319}
2420
2521struct Provider : AppIntentTimelineProvider {
26- func placeholder( in context : Context ) -> SimpleEntry {
22+ func placeholder( in _ : Context ) -> SimpleEntry {
2723 SimpleEntry ( date: Date ( ) , configuration: ConfigurationAppIntent ( ) )
2824 }
29-
30- func snapshot( for configuration: ConfigurationAppIntent , in context : Context ) async -> SimpleEntry {
25+
26+ func snapshot( for configuration: ConfigurationAppIntent , in _ : Context ) async -> SimpleEntry {
3127 SimpleEntry ( date: Date ( ) , configuration: configuration)
3228 }
33-
34- func timeline( for configuration: ConfigurationAppIntent , in context : Context ) async -> Timeline < SimpleEntry > {
29+
30+ func timeline( for configuration: ConfigurationAppIntent , in _ : Context ) async -> Timeline < SimpleEntry > {
3531 var entries : [ SimpleEntry ] = [ ]
36-
32+
3733 // Generate a timeline consisting of five entries an hour apart, starting from the current date.
3834 let currentDate = Date ( )
3935 for hourOffset in 0 ..< 5 {
4036 let entryDate = Calendar . current. date ( byAdding: . hour, value: hourOffset, to: currentDate) !
4137 let entry = SimpleEntry ( date: entryDate, configuration: configuration)
4238 entries. append ( entry)
4339 }
44-
40+
4541 return Timeline ( entries: entries, policy: . atEnd)
4642 }
47-
43+
4844 // func relevances() async -> WidgetRelevances<ConfigurationAppIntent> {
4945 // // Generate a list containing the contexts this widget is relevant in.
5046 // }
@@ -55,25 +51,22 @@ struct SimpleEntry: TimelineEntry {
5551 let configuration : ConfigurationAppIntent
5652}
5753
58- struct AddyStatisticWidgetEntryView : View {
54+ struct AddyStatisticWidgetEntryView : View {
5955 var entry : Provider . Entry
6056 @Environment ( \. widgetFamily) var family
61-
57+
6258 var body : some View {
63-
6459 if let userResource = getUserResource ( ) {
65-
6660 switch family {
6761 case . accessoryCircular:
68-
6962 Gauge (
70- value: Double ( userResource. bandwidth) , in: 0 ... Double ( userResource. bandwidth_limit) ,
63+ value: Double ( userResource. bandwidth) , in: 0 ... Double ( userResource. bandwidth_limit) ,
7164 label: { Text ( String ( localized: " widget_1_bandwidth_gauge " ) ) } ,
72- currentValueLabel: { Text ( String ( userResource. bandwidth/ 1024 / 1024 ) ) } ,
65+ currentValueLabel: { Text ( String ( userResource. bandwidth / 1024 / 1024 ) ) } ,
7366 minimumValueLabel: { Text ( " 0 " ) } ,
74- maximumValueLabel: { Text ( userResource. bandwidth_limit == 0 ? " ∞ " : String ( userResource. bandwidth_limit/ 1024 / 1024 ) ) }
67+ maximumValueLabel: { Text ( userResource. bandwidth_limit == 0 ? " ∞ " : String ( userResource. bandwidth_limit / 1024 / 1024 ) ) }
7568 ) . gaugeStyle ( . accessoryCircular)
76-
69+
7770 case . accessoryRectangular:
7871 VStack ( alignment: . leading) {
7972 Label {
@@ -95,29 +88,30 @@ struct AddyStatisticWidgetEntryView : View {
9588 }
9689 Text ( String ( localized: " monthly_bandwidth " ) ) . frame ( maxHeight: . infinity)
9790 Gauge (
98- value: Double ( userResource. bandwidth) , in: 0 ... Double ( userResource. bandwidth_limit) ,
91+ value: Double ( userResource. bandwidth) , in: 0 ... Double ( userResource. bandwidth_limit) ,
9992 label: { Text ( String ( localized: " widget_1_bandwidth_gauge " ) ) } ,
100- currentValueLabel: { Text ( String ( userResource. bandwidth/ 1024 / 1024 ) ) } ,
93+ currentValueLabel: { Text ( String ( userResource. bandwidth / 1024 / 1024 ) ) } ,
10194 minimumValueLabel: { Text ( " 0 " ) } ,
102- maximumValueLabel: { Text ( userResource. bandwidth_limit == 0 ? " ∞ " : String ( userResource. bandwidth_limit/ 1024 / 1024 ) ) }
95+ maximumValueLabel: { Text ( userResource. bandwidth_limit == 0 ? " ∞ " : String ( userResource. bandwidth_limit / 1024 / 1024 ) ) }
10396 ) . gaugeStyle ( . accessoryLinear)
104-
10597 }
98+
10699 case . accessoryInline:
107100 Text ( String ( format: String ( localized: " widget_1_inline_text " ) , " \( userResource. total_emails_forwarded) " ) ) . frame ( maxHeight: . infinity)
108101 . contentTransition ( . numericText( ) )
109102 . animation ( . spring( duration: 0.2 ) , value: userResource. total_emails_forwarded)
110- //case .systemSmall:
111- // Fall back to default for unknown sizes
103+
104+ // case .systemSmall:
105+ // Fall back to default for unknown sizes
112106 case . systemLarge:
113107 largeWidgetSize ( userResource: userResource, entry: entry)
114-
108+
115109 case . systemExtraLarge:
116110 mediumWidgetSize ( userResource: userResource, entry: entry)
117-
111+
118112 case . systemMedium:
119113 mediumWidgetSize ( userResource: userResource, entry: entry)
120-
114+
121115 default :
122116 VStack {
123117 Image ( " AddyLogo " ) . apply {
@@ -133,11 +127,8 @@ struct AddyStatisticWidgetEntryView : View {
133127 . animation ( . spring( duration: 0.2 ) , value: userResource. total_emails_forwarded) . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
134128 Text ( " emails_forwarded " ) . minimumScaleFactor ( 0.1 ) . lineLimit ( 1 ) . frame ( maxWidth: . infinity, alignment: . leading) . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
135129 }
136-
137130 }
138-
139-
140-
131+
141132 } else {
142133 ContentUnavailableView {
143134 Text ( String ( localized: " app_not_setup " ) ) . minimumScaleFactor ( 0.1 ) . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
@@ -146,14 +137,12 @@ struct AddyStatisticWidgetEntryView : View {
146137 }
147138 }
148139 }
149-
150140}
151141
152-
153142struct mediumWidgetSize : View {
154143 var userResource : UserResource
155144 var entry : Provider . Entry
156-
145+
157146 var body : some View {
158147 HStack ( alignment: . top) {
159148 VStack ( spacing: 4 ) {
@@ -182,7 +171,7 @@ struct mediumWidgetSize: View {
182171 Text ( " replied " ) . minimumScaleFactor ( 0.1 ) . lineLimit ( 1 )
183172 } . frame ( maxWidth: . infinity, alignment: . leading)
184173 } . frame ( maxWidth: . infinity) . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
185-
174+
186175 VStack {
187176 Image ( " AddyLogo " ) . apply {
188177 if entry. configuration. colorfulBackground {
@@ -199,7 +188,7 @@ struct mediumWidgetSize: View {
199188struct largeWidgetSize : View {
200189 var userResource : UserResource
201190 var entry : Provider . Entry
202-
191+
203192 var body : some View {
204193 VStack {
205194 HStack ( alignment: . center) {
@@ -212,107 +201,95 @@ struct largeWidgetSize: View {
212201 $0. resizable ( ) . scaledToFit ( ) . frame ( maxHeight: 30 )
213202 }
214203 } . widgetAccentable ( )
215-
216-
204+
217205 } . frame ( maxWidth: . infinity, minHeight: 30 )
218-
206+
219207 VStack ( spacing: 0 ) {
220-
221208 if let aliases = getMostActiveAliasesData ( ) {
222- ForEach ( Array ( aliases. enumerated ( ) ) , id: \. 1 ) { ( index, alias) in
209+ ForEach ( Array ( aliases. enumerated ( ) ) , id: \. 1 ) { index, alias in
223210 AliasWidgetRowView ( alias: alias, entry: entry)
224-
211+
225212 // Show divider for all but the last item
226213 if index < aliases. count - 1 {
227214 Divider ( ) . background ( entry. configuration. colorfulBackground ? Color . white. opacity ( 0.1 ) : Color . gray. opacity ( 0.1 ) )
228215 }
229216 }
230217 }
231-
232-
233218 }
234219 . background ( ContainerRelativeShape ( ) . fill ( . white. opacity ( 0.15 ) ) )
235220 . frame ( maxHeight: . infinity)
236221 }
237222 }
238223}
239224
240-
241-
242225struct AliasWidgetRowView : View {
243226 @State var alias : Aliases
244227 @State var aliasDescription : String = " "
245228 var entry : Provider . Entry
246-
247-
229+
248230 var body : some View {
249231 Link ( destination: URL ( string: " addyio://alias/ \( alias. id) " ) !) {
250- HStack ( ) {
232+ HStack {
251233 VStack ( alignment: . leading) {
252234 Text ( SettingsManager ( encrypted: true ) . getSettingsBool ( key: . privacyMode) ? String ( localized: " alias_hidden " ) : alias. email)
253235 . font ( . system( size: 16 ) )
254236 . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
255237 . lineLimit ( 1 )
256238 . frame ( maxWidth: . infinity, alignment: . leading)
257-
239+
258240 Text ( aliasDescription)
259241 . font ( . system( size: 13 ) )
260242 . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
261243 . lineLimit ( 1 )
262244 . frame ( maxWidth: . infinity, alignment: . leading)
263245 . onAppear {
264246 if let description = alias. description {
265- aliasDescription = String ( format: String ( localized: " s_s_s " ) ,
266- description,
267- String ( format: NSLocalizedString ( " created_at_s " , comment: " " ) ,
268- DateTimeUtils . convertStringToLocalTimeZoneString ( alias. created_at) ) ,
269- String ( format: String ( localized: " updated_at_s " ) ,
270- DateTimeUtils . convertStringToLocalTimeZoneString ( alias. updated_at) ) )
247+ aliasDescription = String ( format: String ( localized: " s_s_s " ) ,
248+ description,
249+ String ( format: NSLocalizedString ( " created_at_s " , comment: " " ) ,
250+ DateTimeUtils . convertStringToLocalTimeZoneString ( alias. created_at) ) ,
251+ String ( format: String ( localized: " updated_at_s " ) ,
252+ DateTimeUtils . convertStringToLocalTimeZoneString ( alias. updated_at) ) )
271253 } else {
272- aliasDescription = String ( format: String ( localized: " s_s " ) ,
273- String ( format: NSLocalizedString ( " created_at_s " , comment: " " ) ,
274- DateTimeUtils . convertStringToLocalTimeZoneString ( alias. created_at) ) ,
275- String ( format: String ( localized: " updated_at_s " ) ,
276- DateTimeUtils . convertStringToLocalTimeZoneString ( alias. updated_at) ) )
254+ aliasDescription = String ( format: String ( localized: " s_s " ) ,
255+ String ( format: NSLocalizedString ( " created_at_s " , comment: " " ) ,
256+ DateTimeUtils . convertStringToLocalTimeZoneString ( alias. created_at) ) ,
257+ String ( format: String ( localized: " updated_at_s " ) ,
258+ DateTimeUtils . convertStringToLocalTimeZoneString ( alias. updated_at) ) )
277259 }
278260 }
279261 }
280-
262+
281263 Image ( systemName: " chevron.right " )
282264 . foregroundStyle ( entry. configuration. colorfulBackground ? . white : . revertedNightMode)
283265 }
284266 . frame ( maxHeight: . infinity)
285267 . padding ( . horizontal)
286-
287268 }
288-
289269 }
290-
291270}
292271
293272struct AddyStatisticWidget : Widget {
294273 let kind : String = " AddyStatisticWidget "
295-
274+
296275 var body : some WidgetConfiguration {
297276 AppIntentConfiguration ( kind: kind, intent: ConfigurationAppIntent . self, provider: Provider ( ) ) { entry in
298277 AddyStatisticWidgetEntryView ( entry: entry)
299278 . containerBackground ( entry. configuration. colorfulBackground ? LinearGradient (
300279 gradient: Gradient ( colors: [
301280 Color ( red: 0.30 , green: 0.60 , blue: 0.71 ) ,
302- Color ( red: 0.24 , green: 0.28 , blue: 0.51 )
281+ Color ( red: 0.24 , green: 0.28 , blue: 0.51 ) ,
303282 ] ) ,
304283 startPoint: . top,
305284 endPoint: . bottom
306285 ) : LinearGradient (
307286 gradient: Gradient ( colors: [
308287 Color ( UIColor . tertiarySystemFill) ,
309- Color ( UIColor . tertiarySystemFill)
288+ Color ( UIColor . tertiarySystemFill) ,
310289 ] ) ,
311290 startPoint: . top,
312291 endPoint: . bottom
313292 ) , for: . widget)
314-
315-
316293 }
317294 . configurationDisplayName ( String ( localized: " widget_1_name " ) )
318295 . description ( String ( localized: " widget_1_description " ) )
@@ -324,28 +301,26 @@ struct AddyStatisticWidget: Widget {
324301 . systemExtraLarge,
325302 . accessoryInline,
326303 . accessoryCircular,
327- . accessoryRectangular
304+ . accessoryRectangular,
328305 ]
329306 )
330307 }
331308}
332309
333- extension ConfigurationAppIntent {
334- fileprivate static var plain : ConfigurationAppIntent {
310+ private extension ConfigurationAppIntent {
311+ static var plain : ConfigurationAppIntent {
335312 let intent = ConfigurationAppIntent ( )
336313 intent. colorfulBackground = false
337314 return intent
338315 }
339-
340- fileprivate static var colorful : ConfigurationAppIntent {
316+
317+ static var colorful : ConfigurationAppIntent {
341318 let intent = ConfigurationAppIntent ( )
342319 intent. colorfulBackground = true
343320 return intent
344321 }
345322}
346323
347-
348-
349324#Preview( as: . systemSmall) {
350325 AddyStatisticWidget ( )
351326} timeline: {
0 commit comments