Skip to content

Commit ca2e365

Browse files
committed
feat: add ui of widgets
1 parent 3eb89fb commit ca2e365

File tree

5 files changed

+91
-120
lines changed

5 files changed

+91
-120
lines changed

Diff for: TrackerApp/TrackerApp.xcodeproj/project.pbxproj

-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
B1CDF5252C6575C40005EF4C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B1CDF5242C6575C40005EF4C /* Assets.xcassets */; };
2525
B1CDF5292C6575C40005EF4C /* TrackerWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = B1CDF5152C6575C30005EF4C /* TrackerWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
2626
B1CDF5302C6585970005EF4C /* TrackerWidgetBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1CDF52E2C6585960005EF4C /* TrackerWidgetBundle.swift */; };
27-
B1CDF5312C6585970005EF4C /* TrackerWidgetLiveActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1CDF52F2C6585960005EF4C /* TrackerWidgetLiveActivity.swift */; };
2827
/* End PBXBuildFile section */
2928

3029
/* Begin PBXContainerItemProxy section */
@@ -88,7 +87,6 @@
8887
B1CDF5242C6575C40005EF4C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
8988
B1CDF5262C6575C40005EF4C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9089
B1CDF52E2C6585960005EF4C /* TrackerWidgetBundle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackerWidgetBundle.swift; sourceTree = "<group>"; };
91-
B1CDF52F2C6585960005EF4C /* TrackerWidgetLiveActivity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackerWidgetLiveActivity.swift; sourceTree = "<group>"; };
9290
/* End PBXFileReference section */
9391

9492
/* Begin PBXFrameworksBuildPhase section */
@@ -226,7 +224,6 @@
226224
isa = PBXGroup;
227225
children = (
228226
B1CDF52E2C6585960005EF4C /* TrackerWidgetBundle.swift */,
229-
B1CDF52F2C6585960005EF4C /* TrackerWidgetLiveActivity.swift */,
230227
B1CDF5202C6575C30005EF4C /* TrackerWidget.swift */,
231228
B1CDF5242C6575C40005EF4C /* Assets.xcassets */,
232229
B1CDF5262C6575C40005EF4C /* Info.plist */,
@@ -427,7 +424,6 @@
427424
files = (
428425
B1CDF5302C6585970005EF4C /* TrackerWidgetBundle.swift in Sources */,
429426
B1CDF5212C6575C30005EF4C /* TrackerWidget.swift in Sources */,
430-
B1CDF5312C6585970005EF4C /* TrackerWidgetLiveActivity.swift in Sources */,
431427
);
432428
runOnlyForDeploymentPostprocessing = 0;
433429
};

Diff for: TrackerApp/TrackerWidget/TrackerWidget.swift

+91-35
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ struct ConfigurationAppIntent: WidgetConfigurationIntent {
1919
@Parameter(title: "Element ID", default: "ct")
2020
var elementId: String
2121

22-
@Parameter(title: "Element Type", default: "div")
22+
@Parameter(title: "Element Type", optionsProvider: ElementTypeOptionsProvider())
2323
var elementType: String
2424

25+
var price: String = ""
26+
27+
var cents: String = ""
28+
29+
var discount: String = ""
30+
2531
struct ElementTypeOptionsProvider: DynamicOptionsProvider {
32+
2633
func results() async throws -> [String] {
27-
["Div", "Span", "Paragraphe"]
34+
["span", "div", "p"]
35+
}
36+
37+
func defaultResult() async -> String? {
38+
"span"
2839
}
2940
}
3041
}
@@ -48,17 +59,7 @@ struct Provider: AppIntentTimelineProvider {
4859
}
4960

5061
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
6263
let entry = TrackingEntry(date: .now, configuration: configuration)
6364
return Timeline(entries: [entry], policy: .after(nextUpdateTime))
6465
}
@@ -81,36 +82,90 @@ struct TrackerWidgetEntryView : View {
8182
.fontWeight(.heavy)
8283
Spacer()
8384

85+
Rectangle()
86+
.fill(Color.white.opacity(0.1))
87+
.frame(height: 1)
88+
.frame(maxWidth: .infinity)
89+
.padding(.bottom, 5)
8490
HStack(alignment: .top, spacing: 0) {
85-
Text("12")
91+
Text(entry.configuration.price)
8692
.font(.largeTitle.bold())
87-
Text("99")
93+
.contentTransition(.numericText())
94+
Text(entry.configuration.cents)
8895
.font(.headline.bold())
96+
.contentTransition(.numericText())
8997
.offset(CGSize(width: 0.0, height: 5.0))
9098
Text(" $ ")
9199
.font(.largeTitle)
92100
}
93101
}
94102

95103
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:
96148
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)")
106153
.font(.largeTitle.bold())
107-
Text("99")
154+
.contentTransition(.numericText())
155+
Text(entry.configuration.cents)
108156
.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))
112159
}
113160
}
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+
114169
default:
115170
EmptyView()
116171
}
@@ -135,28 +190,29 @@ struct TrackerWidget: Widget {
135190
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in
136191
TrackerWidgetEntryView(entry: entry)
137192
}
193+
.supportedFamilies([.systemSmall, .systemMedium, .accessoryInline, .accessoryRectangular])
138194
}
139195
}
140196

141197
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")
144200
intent.elementId = "ct"
145201
intent.elementType = "span"
146202
return intent
147203
}
148204

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")
151207
intent.elementId = "cta"
152208
intent.elementType = "span"
153209
return intent
154210
}
155211
}
156212

157-
#Preview(as: .systemMedium) {
213+
#Preview(as: .accessoryRectangular) {
158214
TrackerWidget()
159215
} 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)
162218
}

Diff for: TrackerApp/TrackerWidget/TrackerWidgetBundle.swift

-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ import SwiftUI
1212
struct TrackerWidgetBundle: WidgetBundle {
1313
var body: some Widget {
1414
TrackerWidget()
15-
TrackerWidgetLiveActivity()
1615
}
1716
}

Diff for: TrackerApp/TrackerWidget/TrackerWidgetLiveActivity.swift

-80
This file was deleted.

0 commit comments

Comments
 (0)