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.
177 lines
5.1 KiB
177 lines
5.1 KiB
//
|
|
// CountdownView.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 25/01/2023.
|
|
//
|
|
|
|
import SwiftUI
|
|
import WidgetKit
|
|
import CoreData
|
|
|
|
struct SingleTimerView: View {
|
|
|
|
@Environment(\.widgetFamily) var family: WidgetFamily
|
|
|
|
var timer: AbstractTimer
|
|
|
|
var body: some View {
|
|
VStack {
|
|
HStack {
|
|
VStack(alignment: .leading) {
|
|
Text(timer.displayName.uppercased())
|
|
if let countdown = timer as? Countdown {
|
|
Text(countdown.duration.minuteSecond)
|
|
}
|
|
}
|
|
Spacer()
|
|
}
|
|
Spacer()
|
|
}
|
|
.padding()
|
|
.monospaced()
|
|
.foregroundColor(Color.white)
|
|
.font(self.font)
|
|
.widgetURL(timer.url)
|
|
}
|
|
|
|
private var font: Font {
|
|
switch family {
|
|
case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
|
|
return .body
|
|
case .accessoryCircular:
|
|
return .footnote
|
|
default:
|
|
return .body
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct LockScreenCountdownView: View {
|
|
|
|
@Environment(\.widgetFamily) var family: WidgetFamily
|
|
|
|
var timer: AbstractTimer
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text(timer.displayName.uppercased())
|
|
if let countdown = timer as? Countdown {
|
|
Text(countdown.duration.minuteSecond)
|
|
}
|
|
}
|
|
.monospaced()
|
|
.foregroundColor(Color.white)
|
|
.font(self.font)
|
|
.widgetURL(timer.url)
|
|
}
|
|
|
|
private var font: Font {
|
|
switch family {
|
|
case .systemSmall, .systemMedium, .systemLarge, .systemExtraLarge:
|
|
return .body
|
|
case .accessoryCircular:
|
|
return Font.system(.callout, weight: .medium)
|
|
default:
|
|
return .body
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct MultiCountdownView: View {
|
|
|
|
@Environment(\.widgetFamily) var family: WidgetFamily
|
|
|
|
private let columns: [GridItem] = [
|
|
GridItem(spacing: 10.0),
|
|
GridItem(spacing: 10.0),
|
|
]
|
|
|
|
var timers: [AbstractTimer]
|
|
|
|
var body: some View {
|
|
|
|
if timers.isEmpty {
|
|
VoidView()
|
|
} else {
|
|
|
|
LazyVGrid(
|
|
columns: columns,
|
|
spacing: 10.0
|
|
) {
|
|
|
|
ForEach(timers) { timer in
|
|
|
|
Link(destination: timer.url) {
|
|
HStack {
|
|
|
|
VStack(alignment: .leading) {
|
|
Spacer()
|
|
Text(timer.displayName.uppercased())
|
|
if let countdown = timer as? Countdown {
|
|
Text(countdown.duration.minuteSecond)
|
|
}
|
|
Spacer()
|
|
}
|
|
Spacer()
|
|
}
|
|
.padding(.horizontal)
|
|
.font(.callout)
|
|
.background(Image(timer.imageName))
|
|
.foregroundColor(.white)
|
|
.monospaced()
|
|
.cornerRadius(16.0)
|
|
}
|
|
|
|
}
|
|
}.padding()
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
SingleTimerView(timer: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .systemSmall)).background(.black)
|
|
LockScreenCountdownView(timer: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .accessoryRectangular))
|
|
LockScreenCountdownView(timer: Countdown.fake(context: PersistenceController.preview.container.viewContext)).previewContext(WidgetPreviewContext(family: .accessoryCircular))
|
|
MultiCountdownView(timers: 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)
|
|
}
|
|
}
|
|
|
|
}
|
|
|