minor simplification
This commit is contained in:
@@ -1,23 +0,0 @@
|
|||||||
//
|
|
||||||
// TimerConfiguration.swift
|
|
||||||
// Gaze
|
|
||||||
//
|
|
||||||
// Created by Mike Freno on 1/7/26.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct TimerConfiguration: Codable, Equatable, Hashable, Sendable {
|
|
||||||
var enabled: Bool
|
|
||||||
var intervalSeconds: Int
|
|
||||||
|
|
||||||
init(enabled: Bool = true, intervalSeconds: Int) {
|
|
||||||
self.enabled = enabled
|
|
||||||
self.intervalSeconds = intervalSeconds
|
|
||||||
}
|
|
||||||
|
|
||||||
var intervalMinutes: Int {
|
|
||||||
get { intervalSeconds / 60 }
|
|
||||||
set { intervalSeconds = newValue * 60 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,11 +13,11 @@ final class TimerStateManager: ObservableObject {
|
|||||||
@Published private(set) var timerStates: [TimerIdentifier: TimerState] = [:]
|
@Published private(set) var timerStates: [TimerIdentifier: TimerState] = [:]
|
||||||
@Published private(set) var activeReminder: ReminderEvent?
|
@Published private(set) var activeReminder: ReminderEvent?
|
||||||
|
|
||||||
func initializeTimers(using configurations: [TimerIdentifier: TimerConfiguration], userTimers: [UserTimer]) {
|
func initializeTimers(using configurations: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)], userTimers: [UserTimer]) {
|
||||||
timerStates = buildInitialStates(configurations: configurations, userTimers: userTimers)
|
timerStates = buildInitialStates(configurations: configurations, userTimers: userTimers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateConfigurations(using configurations: [TimerIdentifier: TimerConfiguration], userTimers: [UserTimer]) {
|
func updateConfigurations(using configurations: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)], userTimers: [UserTimer]) {
|
||||||
timerStates = buildUpdatedStates(configurations: configurations, userTimers: userTimers)
|
timerStates = buildUpdatedStates(configurations: configurations, userTimers: userTimers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ final class TimerStateManager: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func buildInitialStates(
|
private func buildInitialStates(
|
||||||
configurations: [TimerIdentifier: TimerConfiguration],
|
configurations: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)],
|
||||||
userTimers: [UserTimer]
|
userTimers: [UserTimer]
|
||||||
) -> [TimerIdentifier: TimerState] {
|
) -> [TimerIdentifier: TimerState] {
|
||||||
var newStates: [TimerIdentifier: TimerState] = [:]
|
var newStates: [TimerIdentifier: TimerState] = [:]
|
||||||
@@ -101,7 +101,7 @@ final class TimerStateManager: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func buildUpdatedStates(
|
private func buildUpdatedStates(
|
||||||
configurations: [TimerIdentifier: TimerConfiguration],
|
configurations: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)],
|
||||||
userTimers: [UserTimer]
|
userTimers: [UserTimer]
|
||||||
) -> [TimerIdentifier: TimerState] {
|
) -> [TimerIdentifier: TimerState] {
|
||||||
var newStates: [TimerIdentifier: TimerState] = [:]
|
var newStates: [TimerIdentifier: TimerState] = [:]
|
||||||
|
|||||||
@@ -22,15 +22,16 @@ struct TimerConfigurationHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func configurations() -> [TimerIdentifier: TimerConfiguration] {
|
func configuration(for identifier: TimerIdentifier) -> (enabled: Bool, intervalSeconds: Int)? {
|
||||||
var configurations: [TimerIdentifier: TimerConfiguration] = [:]
|
switch identifier {
|
||||||
for timerType in TimerType.allCases {
|
case .builtIn(let type):
|
||||||
let intervalSeconds = settingsProvider.timerIntervalMinutes(for: timerType) * 60
|
let intervalSeconds = settingsProvider.timerIntervalMinutes(for: type) * 60
|
||||||
configurations[.builtIn(timerType)] = TimerConfiguration(
|
return (enabled: settingsProvider.isTimerEnabled(for: type), intervalSeconds: intervalSeconds)
|
||||||
enabled: settingsProvider.isTimerEnabled(for: timerType),
|
case .user(let id):
|
||||||
intervalSeconds: intervalSeconds
|
guard let userTimer = settingsProvider.settings.userTimers.first(where: { $0.id == id }), userTimer.enabled else {
|
||||||
)
|
return nil
|
||||||
|
}
|
||||||
|
return (enabled: true, intervalSeconds: userTimer.intervalMinutes * 60)
|
||||||
}
|
}
|
||||||
return configurations
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ class TimerEngine: ObservableObject {
|
|||||||
// Initial start - create all timer states
|
// Initial start - create all timer states
|
||||||
stop()
|
stop()
|
||||||
stateManager.initializeTimers(
|
stateManager.initializeTimers(
|
||||||
using: configurationHelper.configurations(),
|
using: timerConfigurations(),
|
||||||
userTimers: settingsProvider.settings.userTimers
|
userTimers: settingsProvider.settings.userTimers
|
||||||
)
|
)
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
@@ -110,7 +110,7 @@ class TimerEngine: ObservableObject {
|
|||||||
private func updateConfigurations() {
|
private func updateConfigurations() {
|
||||||
logDebug("Updating timer configurations")
|
logDebug("Updating timer configurations")
|
||||||
stateManager.updateConfigurations(
|
stateManager.updateConfigurations(
|
||||||
using: configurationHelper.configurations(),
|
using: timerConfigurations(),
|
||||||
userTimers: settingsProvider.settings.userTimers
|
userTimers: settingsProvider.settings.userTimers
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -209,8 +209,14 @@ class TimerEngine: ObservableObject {
|
|||||||
return stateManager.isTimerPaused(identifier)
|
return stateManager.isTimerPaused(identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func timerConfigurations() -> [TimerIdentifier: TimerConfiguration] {
|
private func timerConfigurations() -> [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)] {
|
||||||
configurationHelper.configurations()
|
var configs: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)] = [:]
|
||||||
|
for timerType in TimerType.allCases {
|
||||||
|
if let config = configurationHelper.configuration(for: .builtIn(timerType)) {
|
||||||
|
configs[.builtIn(timerType)] = config
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return configs
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user