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/LeCountdown/Views/LiveTimerListView.swift

249 lines
7.0 KiB

//
// LiveTimerView.swift
// LeCountdown
//
// Created by Laurent Morvillier on 03/02/2023.
//
import SwiftUI
class LiveStopwatchModel: ObservableObject {
@Published var endDate: Date? = nil
func stop(_ stopwatch: Stopwatch) {
let now = Date()
self.endDate = now
Conductor.maestro.stopStopwatch(stopwatch)
}
}
fileprivate let liveViewSize: CGFloat = 70.0
fileprivate let timerFontSize: CGFloat = 32.0
fileprivate let actionButtonFontSize: CGFloat = 36.0
struct TimeView: View {
var text: String
var body: some View {
Text(self.text)
.font(.system(size: timerFontSize, weight: .medium))
.minimumScaleFactor(0.1)
}
}
struct LiveStopwatchView: View {
@Environment(\.managedObjectContext) private var viewContext
@EnvironmentObject var conductor: Conductor
@StateObject var model: LiveStopwatchModel = LiveStopwatchModel()
@State var stopwatch: Stopwatch
var date: Date
var body: some View {
let running = (self.model.endDate == nil)
HStack {
VStack(alignment: .leading) {
if running {
TimelineView(.periodic(from: self.date, by: 0.01)) { context in
TimeView(text: self._formattedDuration(date: context.date))
}
} else {
let duration = self.model.endDate?.timeIntervalSince(self.date) ?? 0.0
TimeView(text: duration.hourMinuteSecondHS)
}
Text(stopwatch.displayName.uppercased())
}
Spacer()
Group {
if !running {
GreenCheckmarkView()
} else {
Image(systemName: "stop.circle.fill")
.foregroundColor(.accentColor)
}
}.font(.system(size: actionButtonFontSize))
}
.monospaced()
.frame(height: liveViewSize)
.contentShape(Rectangle())
.onTapGesture {
self._actionHandler()
}
}
fileprivate func _actionHandler() {
withAnimation {
if self.model.endDate == nil {
self.model.stop(stopwatch)
} else {
self._dismiss()
}
}
}
fileprivate func _dismiss() {
conductor.removeLiveTimer(id: self.stopwatch.stringId)
}
fileprivate func _formattedDuration(date: Date) -> String {
let duration = date.timeIntervalSince(self.date)
return duration.hourMinuteSecondHS
}
}
struct LiveCountdownView: View {
@Environment(\.managedObjectContext) private var viewContext
@EnvironmentObject var conductor: Conductor
@State var countdown: Countdown
var date: Date
var body: some View {
TimelineView(.periodic(from: self.date, by: 0.01)) { context in
HStack {
let running = self.date > context.date
let cancelled = self.conductor.cancelledCountdowns.contains(where: { $0 == self.countdown.stringId })
VStack(alignment: .leading) {
if cancelled {
TimeView(text: NSLocalizedString("Cancelled", comment: ""))
} else if running {
TimeView(text: self._formattedDuration(date: context.date))
} else {
TimeView(text: self.date.formatted(date: .omitted, time: .shortened))
}
Text(self.countdown.displayName.uppercased())
}
Spacer()
Group {
if cancelled {
Image(systemName: "xmark.circle").foregroundColor(.accentColor)
} else if !running {
GreenCheckmarkView()
} else {
Image(systemName: "xmark.circle.fill").foregroundColor(.accentColor)
}
}.font(.system(size: actionButtonFontSize))
}
}
.contentShape(Rectangle())
.onTapGesture {
self._actionHandler()
}
.frame(height: liveViewSize)
.monospaced()
}
fileprivate func _actionHandler() {
withAnimation {
if conductor.currentCountdowns[self.countdown.stringId] != nil {
self._cancelCountdown()
} else {
self._dismiss()
}
}
}
fileprivate func _dismiss() {
conductor.removeLiveTimer(id: self.countdown.stringId)
}
fileprivate func _formattedDuration(date: Date) -> String {
let duration = self.date.timeIntervalSince(date)
return duration.minuteSecond
}
fileprivate func _cancelCountdown() {
Conductor.maestro.cancelCountdown(id: self.countdown.stringId)
}
}
struct LiveTimerListView: View {
@Environment(\.managedObjectContext) private var viewContext
@EnvironmentObject var conductor: Conductor
var body: some View {
VStack {
ForEach(conductor.liveTimers) { liveTimer in
if let timer: AbstractTimer = liveTimer.timer(context: self.viewContext) {
switch timer {
case let cd as Countdown:
LiveCountdownView(countdown: cd, date: liveTimer.date)
case let sw as Stopwatch:
LiveStopwatchView(stopwatch: sw, date: liveTimer.date)
default:
Text("unmanaged timer: \(timer)")
}
}
}
}.padding()
}
fileprivate func _columnCount() -> Int {
#if os(iOS)
if UIDevice.isPhoneIdiom {
return 18
} else {
return 3
}
#else
return 3
#endif
}
fileprivate func _columns() -> [GridItem] {
return (0..<self._columnCount()).map { _ in GridItem(spacing: 20.0) }
}
}
struct LiveTimerView_Previews: PreviewProvider {
static var previews: some View {
// LiveTimerListView().environmentObject(Conductor.maestro)
Group {
VStack(spacing: 20.0) {
LiveCountdownView(countdown: Countdown.fake(context: PersistenceController.preview.container.viewContext), date: Date().addingTimeInterval(3600.0))
.environmentObject(Conductor.maestro)
LiveStopwatchView(stopwatch: Stopwatch.fake(context: PersistenceController.preview.container.viewContext), date: Date())
.environmentObject(Conductor.maestro)
}
}.padding()
}
}