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.
92 lines
2.6 KiB
92 lines
2.6 KiB
//
|
|
// CountdownView.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 25/01/2023.
|
|
//
|
|
|
|
import SwiftUI
|
|
import WidgetKit
|
|
import CoreData
|
|
|
|
struct SingleCountdownView: View {
|
|
|
|
@Environment(\.widgetFamily) var family: WidgetFamily
|
|
|
|
var countdown: Countdown
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text(countdown.name ?? "")
|
|
Text(countdown.duration.minuteSecond)
|
|
}.foregroundColor(Color.white)
|
|
.font(self.font)
|
|
.widgetURL(countdown.url)
|
|
}
|
|
|
|
private var font: Font {
|
|
switch family {
|
|
case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
|
|
return .title2
|
|
default:
|
|
return .body
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct MultiCountdownView: View {
|
|
|
|
@Environment(\.widgetFamily) var family: WidgetFamily
|
|
|
|
var countdowns: [Countdown]
|
|
|
|
var body: some View {
|
|
|
|
if countdowns.isEmpty {
|
|
VoidView()
|
|
} else {
|
|
HStack {
|
|
ForEach(countdowns) { countdown in
|
|
|
|
Link(destination: countdown.url) {
|
|
VStack {
|
|
Text(countdown.name ?? "")
|
|
Text(countdown.duration.minuteSecond)
|
|
}
|
|
.font(self.font)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
|
|
}
|
|
}.frame(maxWidth: .infinity)
|
|
}
|
|
|
|
}
|
|
|
|
private var font: Font {
|
|
switch family {
|
|
case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
|
|
return .title2
|
|
default:
|
|
return .body
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct CountdownView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
|
|
SingleCountdownView(countdown: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .systemSmall))
|
|
SingleCountdownView(countdown: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .accessoryRectangular))
|
|
MultiCountdownView(countdowns: self.countdowns(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .systemMedium))
|
|
}
|
|
|
|
static func countdowns(context: NSManagedObjectContext) -> [Countdown] {
|
|
return (0..<4).map { _ in
|
|
Countdown.fake(context: context)
|
|
}
|
|
}
|
|
|
|
}
|
|
|