You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LeCountdown/LaunchWidget/LaunchWidgetLiveActivity.swift

121 lines
3.6 KiB

//
// LaunchWidgetLiveActivity.swift
// LaunchWidget
//
// Created by Laurent Morvillier on 25/01/2023.
//
import ActivityKit
import WidgetKit
import SwiftUI
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 {
var body: some WidgetConfiguration {
ActivityConfiguration(for: LaunchWidgetAttributes.self) { context in
// let range = Date()...context.attributes.date
// Lock screen/banner UI goes here
HStack {
Text(context.attributes.name.uppercased())
Spacer()
Text(context.attributes.date, style: .timer)
.font(.title)
// if Date() < context.attributes.date {
// Text(context.attributes.date, style: .timer)
// } else {
// GreenCheckmarkView()
// }
// if context.attributes.endDate > self.model.now {
// Text(context.attributes.endDate, style: .timer)
// .monospaced()
// } else {
// Text("It's time!")
// }
}.padding()
.monospaced()
.foregroundColor(.white)
.activityBackgroundTint(Color(white: 0.2))
.activitySystemActionForegroundColor(.white)
} dynamicIsland: { context in
DynamicIsland {
// Expanded UI goes here. Compose the expanded UI through
// various regions, like leading/trailing/center/bottom
DynamicIslandExpandedRegion(.leading) {
Text(context.attributes.name.uppercased())
.monospaced()
}
DynamicIslandExpandedRegion(.trailing) {
Text(context.attributes.date, style: .timer)
.monospaced()
}
DynamicIslandExpandedRegion(.bottom) {
Button {
self._stop()
} label: {
Text("Stop")
}
}
} compactLeading: {
Text("L")
} compactTrailing: {
Text("T")
} minimal: {
Text("Min")
}
.widgetURL(URL(string: context.attributes.id))
.keylineTint(Color.red)
}
}
fileprivate func _stop() {
}
}
struct LaunchWidgetLiveActivity_Previews: PreviewProvider {
static let attributes = LaunchWidgetAttributes(
id: "",
name: "Tea",
date: Date().addingTimeInterval(3600.0))
static let contentState = LaunchWidgetAttributes.ContentState(ended: false)
static var previews: some View {
attributes
.previewContext(contentState, viewKind: .dynamicIsland(.compact))
.previewDisplayName("Island Compact")
attributes
.previewContext(contentState, viewKind: .dynamicIsland(.expanded))
.previewDisplayName("Island Expanded")
attributes
.previewContext(contentState, viewKind: .dynamicIsland(.minimal))
.previewDisplayName("Minimal")
attributes
.previewContext(contentState, viewKind: .content)
.previewDisplayName("Notification")
}
}