@@ -19,12 +19,23 @@ struct ConfigurationAppIntent: WidgetConfigurationIntent {
19
19
@Parameter ( title: " Element ID " , default: " ct " )
20
20
var elementId : String
21
21
22
- @Parameter ( title: " Element Type " , default : " div " )
22
+ @Parameter ( title: " Element Type " , optionsProvider : ElementTypeOptionsProvider ( ) )
23
23
var elementType : String
24
24
25
+ var price : String = " "
26
+
27
+ var cents : String = " "
28
+
29
+ var discount : String = " "
30
+
25
31
struct ElementTypeOptionsProvider : DynamicOptionsProvider {
32
+
26
33
func results( ) async throws -> [ String ] {
27
- [ " Div " , " Span " , " Paragraphe " ]
34
+ [ " span " , " div " , " p " ]
35
+ }
36
+
37
+ func defaultResult( ) async -> String ? {
38
+ " span "
28
39
}
29
40
}
30
41
}
@@ -48,17 +59,7 @@ struct Provider: AppIntentTimelineProvider {
48
59
}
49
60
50
61
func timeline( for configuration: ConfigurationAppIntent , in context: Context ) async -> Timeline < TrackingEntry > {
51
- var entries : [ TrackingEntry ] = [ ]
52
-
53
- // Generate a timeline consisting of five entries an hour apart, starting from the current date.
54
- let currentDate = Date ( )
55
- for hourOffset in 0 ..< 5 {
56
- let entryDate = Calendar . current. date ( byAdding: . hour, value: hourOffset, to: currentDate) !
57
- let entry = TrackingEntry ( date: entryDate, configuration: configuration)
58
- entries. append ( entry)
59
- }
60
-
61
- let nextUpdateTime : Date = . now. addingTimeInterval ( 10 ) // TODO: Extend time
62
+ let nextUpdateTime : Date = . now. addingTimeInterval ( 3 ) // TODO: Extend time
62
63
let entry = TrackingEntry ( date: . now, configuration: configuration)
63
64
return Timeline ( entries: [ entry] , policy: . after( nextUpdateTime) )
64
65
}
@@ -81,36 +82,90 @@ struct TrackerWidgetEntryView : View {
81
82
. fontWeight ( . heavy)
82
83
Spacer ( )
83
84
85
+ Rectangle ( )
86
+ . fill ( Color . white. opacity ( 0.1 ) )
87
+ . frame ( height: 1 )
88
+ . frame ( maxWidth: . infinity)
89
+ . padding ( . bottom, 5 )
84
90
HStack ( alignment: . top, spacing: 0 ) {
85
- Text ( " 12 " )
91
+ Text ( entry . configuration . price )
86
92
. font ( . largeTitle. bold ( ) )
87
- Text ( " 99 " )
93
+ . contentTransition ( . numericText( ) )
94
+ Text ( entry. configuration. cents)
88
95
. font ( . headline. bold ( ) )
96
+ . contentTransition ( . numericText( ) )
89
97
. offset ( CGSize ( width: 0.0 , height: 5.0 ) )
90
98
Text ( " $ " )
91
99
. font ( . largeTitle)
92
100
}
93
101
}
94
102
95
103
case . systemMedium:
104
+ ZStack {
105
+ VStack ( alignment: . leading, spacing: 5 ) {
106
+ Text ( " You Next Five Moves " )
107
+ . font ( . callout)
108
+ . frame ( maxWidth: . infinity, alignment: . leading)
109
+ Text ( " Current Price " )
110
+ . font ( . title2)
111
+ . fontWeight ( . heavy)
112
+ . frame ( maxWidth: . infinity, alignment: . leading)
113
+ Spacer ( )
114
+
115
+ HStack ( alignment: . center, spacing: 0 ) {
116
+ Text ( " \( entry. configuration. discount) % " )
117
+ . font ( . title)
118
+ . contentTransition ( . numericText( ) )
119
+ . foregroundColor ( . green)
120
+ . padding ( . trailing, 5 )
121
+ VStack ( spacing: 5 ) {
122
+ Image ( systemName: " arrowtriangle.up.fill " )
123
+ . resizable ( )
124
+ . foregroundColor ( . green)
125
+ . frame ( width: 10 , height: 5 )
126
+ Image ( systemName: " arrowtriangle.down.fill " )
127
+ . resizable ( )
128
+ . foregroundColor ( . white. opacity ( 0.5 ) )
129
+ . frame ( width: 10 , height: 5 )
130
+ }
131
+ Spacer ( )
132
+ HStack ( alignment: . top, spacing: 0 ) {
133
+ Text ( entry. configuration. price)
134
+ . font ( . largeTitle. bold ( ) )
135
+ . contentTransition ( . numericText( ) )
136
+ Text ( entry. configuration. cents)
137
+ . font ( . headline. bold ( ) )
138
+ . contentTransition ( . numericText( ) )
139
+ . offset ( CGSize ( width: 0.0 , height: 5.0 ) )
140
+ Text ( " $ " )
141
+ . font ( . largeTitle)
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ case . accessoryRectangular:
96
148
VStack ( alignment: . leading, spacing: 5 ) {
97
- Text ( " Your Amazon article " )
98
- . font ( . footnote)
99
- Text ( " Price " )
100
- . font ( . title2)
101
- . fontWeight ( . heavy)
102
- Spacer ( )
103
-
104
- HStack ( alignment: . top, spacing: 0 ) {
105
- Text ( " 12 " )
149
+ Text ( " Tracked Price is " )
150
+ . fontWeight ( . bold)
151
+ HStack ( alignment: . center, spacing: 0 ) {
152
+ Text ( " $ \( entry. configuration. price) " )
106
153
. font ( . largeTitle. bold ( ) )
107
- Text ( " 99 " )
154
+ . contentTransition ( . numericText( ) )
155
+ Text ( entry. configuration. cents)
108
156
. font ( . headline. bold ( ) )
109
- . offset ( CGSize ( width: 0.0 , height: 5.0 ) )
110
- Text ( " $ " )
111
- . font ( . largeTitle)
157
+ . contentTransition ( . numericText( ) )
158
+ . offset ( CGSize ( width: 0.0 , height: - 10.0 ) )
112
159
}
113
160
}
161
+ . frame ( maxWidth: . infinity, alignment: . leading)
162
+
163
+ case . accessoryInline:
164
+ VStack ( alignment: . leading, spacing: 5 ) {
165
+ Text ( " Tracked Price is $ \( entry. configuration. price) , \( entry. configuration. cents) " )
166
+ }
167
+ . frame ( maxWidth: . infinity, alignment: . leading)
168
+
114
169
default :
115
170
EmptyView ( )
116
171
}
@@ -135,28 +190,29 @@ struct TrackerWidget: Widget {
135
190
AppIntentConfiguration ( kind: kind, intent: ConfigurationAppIntent . self, provider: Provider ( ) ) { entry in
136
191
TrackerWidgetEntryView ( entry: entry)
137
192
}
193
+ . supportedFamilies ( [ . systemSmall, . systemMedium, . accessoryInline, . accessoryRectangular] )
138
194
}
139
195
}
140
196
141
197
extension ConfigurationAppIntent {
142
- fileprivate static var clock : ConfigurationAppIntent {
143
- let intent = ConfigurationAppIntent ( )
198
+ fileprivate static var log1 : ConfigurationAppIntent {
199
+ let intent = ConfigurationAppIntent ( price : " 12 " , cents : " 99 " , discount : " -13 " )
144
200
intent. elementId = " ct "
145
201
intent. elementType = " span "
146
202
return intent
147
203
}
148
204
149
- fileprivate static var timeZone : ConfigurationAppIntent {
150
- let intent = ConfigurationAppIntent ( )
205
+ fileprivate static var log2 : ConfigurationAppIntent {
206
+ let intent = ConfigurationAppIntent ( price : " 13 " , cents : " 79 " , discount : " 0 " )
151
207
intent. elementId = " cta "
152
208
intent. elementType = " span "
153
209
return intent
154
210
}
155
211
}
156
212
157
- #Preview( as: . systemMedium ) {
213
+ #Preview( as: . accessoryRectangular ) {
158
214
TrackerWidget ( )
159
215
} timeline: {
160
- TrackingEntry ( date: . now, configuration: . clock )
161
- // TrackingEntry(date: .now, configuration: .timeZone )
216
+ TrackingEntry ( date: . now, configuration: . log1 )
217
+ TrackingEntry ( date: . now, configuration: . log2 )
162
218
}
0 commit comments