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.
121 lines
4.0 KiB
121 lines
4.0 KiB
//
|
|
// LaunchWidget.swift
|
|
// LaunchWidget
|
|
//
|
|
// Created by Laurent Morvillier on 25/01/2023.
|
|
//
|
|
|
|
import WidgetKit
|
|
import SwiftUI
|
|
import Intents
|
|
|
|
struct Provider: IntentTimelineProvider {
|
|
func placeholder(in context: Context) -> SimpleEntry {
|
|
SimpleEntry(id: "", name: "Tea", duration: 4 * 60.0, date: Date(), configuration: SelectCountdownIntent())
|
|
}
|
|
|
|
func getSnapshot(for configuration: SelectCountdownIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
|
|
|
|
guard let cp = configuration.countdown,
|
|
let identifier = cp.identifier,
|
|
let countdown = IntentDataProvider.main.countdown(id: identifier) else {
|
|
print("WARNING PLACEHOLDER!")
|
|
|
|
let entry = SimpleEntry(id: "",
|
|
name: "Not found",
|
|
duration: 0.0,
|
|
date: Date(),
|
|
configuration: configuration)
|
|
|
|
completion(entry)
|
|
return
|
|
}
|
|
|
|
let entry = SimpleEntry(id: identifier,
|
|
name: countdown.name,
|
|
duration: countdown.duration,
|
|
date: Date(),
|
|
configuration: configuration)
|
|
completion(entry)
|
|
|
|
}
|
|
|
|
func getTimeline(for configuration: SelectCountdownIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
|
|
|
|
getSnapshot(for: configuration, in: context) { entry in
|
|
let timeline = Timeline(entries: [entry], policy: .atEnd)
|
|
completion(timeline)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
struct SimpleEntry: TimelineEntry {
|
|
let id: String
|
|
let name: String?
|
|
let duration: Double
|
|
|
|
let date: Date
|
|
let configuration: SelectCountdownIntent
|
|
}
|
|
|
|
struct CountdownWidgetView: View {
|
|
|
|
let id: String
|
|
let name: String?
|
|
let duration: Double
|
|
|
|
var body: some View {
|
|
CountdownView(name: name, duration: duration).widgetURL(URL(string: id))
|
|
}
|
|
|
|
}
|
|
|
|
struct LaunchWidgetEntryView : View {
|
|
|
|
@Environment(\.widgetFamily) var family: WidgetFamily
|
|
|
|
var entry: Provider.Entry
|
|
|
|
@ViewBuilder
|
|
var body: some View {
|
|
switch family {
|
|
case .systemSmall: CountdownWidgetView(id: entry.id, name: entry.name, duration: entry.duration)
|
|
|
|
// case .systemMedium: GameStatusWithLastTurnResult(gameStatus)
|
|
// case .systemLarge: GameStatusWithStatistics(gameStatus)
|
|
// case .systemExtraLarge: GameStatusWithStatisticsExtraLarge(gameStatus)
|
|
// case .accessoryCircular: HealthLevelCircular(selectedCharacter)
|
|
// case .accessoryRectangular: HealthLevelRectangular(selectedCharacter)
|
|
// case .accessoryInline: HealthLevelInline(selectedCharacter)
|
|
default: CountdownWidgetView(id: entry.id, name: entry.name, duration: entry.duration)
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
struct LaunchWidget: Widget {
|
|
let kind: String = "com.staxriver.launch-widget"
|
|
|
|
var body: some WidgetConfiguration {
|
|
IntentConfiguration(kind: kind,
|
|
intent: SelectCountdownIntent.self,
|
|
provider: Provider()) { entry in
|
|
LaunchWidgetEntryView(entry: entry)
|
|
}
|
|
.configurationDisplayName("Launch Widget")
|
|
.description("Select and launch your countdowns")
|
|
.supportedFamilies([.systemSmall, .accessoryCircular])
|
|
}
|
|
}
|
|
|
|
struct LaunchWidget_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
LaunchWidgetEntryView(entry: SimpleEntry(id: "", name: "Tea", duration: 3 * 60.0, date: Date(), configuration: SelectCountdownIntent()))
|
|
.previewContext(WidgetPreviewContext(family: .systemSmall))
|
|
LaunchWidgetEntryView(entry: SimpleEntry(id: "", name: "Tea", duration: 3 * 60.0, date: Date(), configuration: SelectCountdownIntent()))
|
|
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
|
|
|
|
}
|
|
}
|
|
|