|
|
|
@ -12,20 +12,50 @@ import SwiftUI |
|
|
|
struct LaunchWidgetAttributes: ActivityAttributes { |
|
|
|
struct LaunchWidgetAttributes: ActivityAttributes { |
|
|
|
public struct ContentState: Codable, Hashable { |
|
|
|
public struct ContentState: Codable, Hashable { |
|
|
|
// Dynamic stateful properties about your activity go here! |
|
|
|
// Dynamic stateful properties about your activity go here! |
|
|
|
var value: Int |
|
|
|
var ended: Bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Fixed non-changing properties about your activity go here! |
|
|
|
// Fixed non-changing properties about your activity go here! |
|
|
|
|
|
|
|
var id: String |
|
|
|
var name: String |
|
|
|
var name: String |
|
|
|
|
|
|
|
var endDate: Date |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct LiveActivityView: View { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var name: String |
|
|
|
|
|
|
|
var endDate: Date |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var body: some View { |
|
|
|
|
|
|
|
HStack { |
|
|
|
|
|
|
|
Text(name) |
|
|
|
|
|
|
|
Spacer() |
|
|
|
|
|
|
|
Text(endDate, style: .timer) |
|
|
|
|
|
|
|
.monospaced() |
|
|
|
|
|
|
|
}.padding() |
|
|
|
|
|
|
|
.font(.title) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct LaunchWidgetLiveActivity: Widget { |
|
|
|
struct LaunchWidgetLiveActivity: Widget { |
|
|
|
var body: some WidgetConfiguration { |
|
|
|
var body: some WidgetConfiguration { |
|
|
|
ActivityConfiguration(for: LaunchWidgetAttributes.self) { context in |
|
|
|
ActivityConfiguration(for: LaunchWidgetAttributes.self) { context in |
|
|
|
|
|
|
|
|
|
|
|
// Lock screen/banner UI goes here |
|
|
|
// Lock screen/banner UI goes here |
|
|
|
VStack { |
|
|
|
HStack { |
|
|
|
Text("Hello") |
|
|
|
Text(context.attributes.name) |
|
|
|
} |
|
|
|
Spacer() |
|
|
|
|
|
|
|
if !context.state.ended { |
|
|
|
|
|
|
|
Text(context.attributes.endDate, style: .timer) |
|
|
|
|
|
|
|
.monospaced() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Text("It's time!") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}.padding() |
|
|
|
|
|
|
|
.font(.title) |
|
|
|
.activityBackgroundTint(Color.cyan) |
|
|
|
.activityBackgroundTint(Color.cyan) |
|
|
|
.activitySystemActionForegroundColor(Color.black) |
|
|
|
.activitySystemActionForegroundColor(Color.black) |
|
|
|
|
|
|
|
|
|
|
|
@ -34,14 +64,18 @@ struct LaunchWidgetLiveActivity: Widget { |
|
|
|
// Expanded UI goes here. Compose the expanded UI through |
|
|
|
// Expanded UI goes here. Compose the expanded UI through |
|
|
|
// various regions, like leading/trailing/center/bottom |
|
|
|
// various regions, like leading/trailing/center/bottom |
|
|
|
DynamicIslandExpandedRegion(.leading) { |
|
|
|
DynamicIslandExpandedRegion(.leading) { |
|
|
|
Text("Leading") |
|
|
|
Text(context.attributes.name) |
|
|
|
} |
|
|
|
} |
|
|
|
DynamicIslandExpandedRegion(.trailing) { |
|
|
|
DynamicIslandExpandedRegion(.trailing) { |
|
|
|
Text("Trailing") |
|
|
|
Text(context.attributes.endDate, style: .timer) |
|
|
|
|
|
|
|
.monospaced() |
|
|
|
} |
|
|
|
} |
|
|
|
DynamicIslandExpandedRegion(.bottom) { |
|
|
|
DynamicIslandExpandedRegion(.bottom) { |
|
|
|
Text("Bottom") |
|
|
|
Button { |
|
|
|
// more content |
|
|
|
self._stop() |
|
|
|
|
|
|
|
} label: { |
|
|
|
|
|
|
|
Text("Stop") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} compactLeading: { |
|
|
|
} compactLeading: { |
|
|
|
Text("L") |
|
|
|
Text("L") |
|
|
|
@ -50,15 +84,24 @@ struct LaunchWidgetLiveActivity: Widget { |
|
|
|
} minimal: { |
|
|
|
} minimal: { |
|
|
|
Text("Min") |
|
|
|
Text("Min") |
|
|
|
} |
|
|
|
} |
|
|
|
.widgetURL(URL(string: "http://www.apple.com")) |
|
|
|
.widgetURL(URL(string: context.attributes.id)) |
|
|
|
.keylineTint(Color.red) |
|
|
|
.keylineTint(Color.red) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _stop() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct LaunchWidgetLiveActivity_Previews: PreviewProvider { |
|
|
|
struct LaunchWidgetLiveActivity_Previews: PreviewProvider { |
|
|
|
static let attributes = LaunchWidgetAttributes(name: "Me") |
|
|
|
|
|
|
|
static let contentState = LaunchWidgetAttributes.ContentState(value: 3) |
|
|
|
static let attributes = LaunchWidgetAttributes( |
|
|
|
|
|
|
|
id: "", |
|
|
|
|
|
|
|
name: "Tea", |
|
|
|
|
|
|
|
endDate: Date().addingTimeInterval(3600.0)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static let contentState = LaunchWidgetAttributes.ContentState(ended: false) |
|
|
|
|
|
|
|
|
|
|
|
static var previews: some View { |
|
|
|
static var previews: some View { |
|
|
|
attributes |
|
|
|
attributes |
|
|
|
|