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.
85 lines
2.8 KiB
85 lines
2.8 KiB
//
|
|
// TimerRouter.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 02/02/2023.
|
|
//
|
|
|
|
import Foundation
|
|
import NotificationCenter
|
|
|
|
enum TimerRouterError: Error {
|
|
case notificationAuthorizationMissing
|
|
case unmanagedTimer(_ timer: AbstractTimer)
|
|
}
|
|
|
|
class TimerRouter {
|
|
|
|
static func performAction(timer: AbstractTimer) async throws -> Bool {
|
|
|
|
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Error>) in
|
|
self.performAction(timer: timer) { result in
|
|
switch result {
|
|
case .success:
|
|
continuation.resume(returning: true)
|
|
case .failure(let failure):
|
|
continuation.resume(throwing: failure)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
static func performAction(timer: AbstractTimer, handler: @escaping (Result<Void, Error>) -> Void) {
|
|
switch timer {
|
|
case let countdown as Countdown:
|
|
self._launchCountdown(countdown.stringId, handler: handler)
|
|
case let stopwatch as Stopwatch:
|
|
self._startStopwatch(stopwatch.stringId, handler: handler)
|
|
case let alarm as Alarm:
|
|
self._scheduleAlarm(alarm, handler: handler)
|
|
default:
|
|
handler(Result.failure(TimerRouterError.unmanagedTimer(timer)))
|
|
print("missing launcher for \(self)")
|
|
}
|
|
|
|
}
|
|
|
|
fileprivate static func _launchCountdown(_ countdownId: TimerID, handler: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard let countdown = IntentDataProvider.main.timer(id: countdownId) as? Countdown else {
|
|
handler(.failure(AppError.timerNotFound(id: countdownId)))
|
|
return
|
|
}
|
|
|
|
CountdownScheduler.master.scheduleIfPossible(countdown: countdown) { result in
|
|
switch result {
|
|
case .success:
|
|
handler(.success(Void()))
|
|
case .failure(let failure):
|
|
handler(.failure(failure))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
fileprivate static func _scheduleAlarm(_ alarm: Alarm, handler: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
}
|
|
|
|
fileprivate static func _startStopwatch(_ stopwatchId: TimerID, handler: @escaping (Result<Void, Error>) -> Void) {
|
|
Conductor.maestro.startStopwatch(stopwatchId)
|
|
handler(.success(Void()))
|
|
}
|
|
|
|
fileprivate static func _stopStopwatch(_ stopwatch: Stopwatch) {
|
|
Conductor.maestro.stopStopwatch(stopwatch)
|
|
}
|
|
|
|
}
|
|
|