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 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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ final class TimerStateManager: ObservableObject {
|
||||
}
|
||||
|
||||
private func buildInitialStates(
|
||||
configurations: [TimerIdentifier: TimerConfiguration],
|
||||
configurations: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)],
|
||||
userTimers: [UserTimer]
|
||||
) -> [TimerIdentifier: TimerState] {
|
||||
var newStates: [TimerIdentifier: TimerState] = [:]
|
||||
@@ -101,7 +101,7 @@ final class TimerStateManager: ObservableObject {
|
||||
}
|
||||
|
||||
private func buildUpdatedStates(
|
||||
configurations: [TimerIdentifier: TimerConfiguration],
|
||||
configurations: [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)],
|
||||
userTimers: [UserTimer]
|
||||
) -> [TimerIdentifier: TimerState] {
|
||||
var newStates: [TimerIdentifier: TimerState] = [:]
|
||||
|
||||
@@ -22,15 +22,16 @@ struct TimerConfigurationHelper {
|
||||
}
|
||||
}
|
||||
|
||||
func configurations() -> [TimerIdentifier: TimerConfiguration] {
|
||||
var configurations: [TimerIdentifier: TimerConfiguration] = [:]
|
||||
for timerType in TimerType.allCases {
|
||||
let intervalSeconds = settingsProvider.timerIntervalMinutes(for: timerType) * 60
|
||||
configurations[.builtIn(timerType)] = TimerConfiguration(
|
||||
enabled: settingsProvider.isTimerEnabled(for: timerType),
|
||||
intervalSeconds: intervalSeconds
|
||||
)
|
||||
func configuration(for identifier: TimerIdentifier) -> (enabled: Bool, intervalSeconds: Int)? {
|
||||
switch identifier {
|
||||
case .builtIn(let type):
|
||||
let intervalSeconds = settingsProvider.timerIntervalMinutes(for: type) * 60
|
||||
return (enabled: settingsProvider.isTimerEnabled(for: type), intervalSeconds: intervalSeconds)
|
||||
case .user(let id):
|
||||
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
|
||||
stop()
|
||||
stateManager.initializeTimers(
|
||||
using: configurationHelper.configurations(),
|
||||
using: timerConfigurations(),
|
||||
userTimers: settingsProvider.settings.userTimers
|
||||
)
|
||||
scheduler.start()
|
||||
@@ -110,7 +110,7 @@ class TimerEngine: ObservableObject {
|
||||
private func updateConfigurations() {
|
||||
logDebug("Updating timer configurations")
|
||||
stateManager.updateConfigurations(
|
||||
using: configurationHelper.configurations(),
|
||||
using: timerConfigurations(),
|
||||
userTimers: settingsProvider.settings.userTimers
|
||||
)
|
||||
}
|
||||
@@ -209,8 +209,14 @@ class TimerEngine: ObservableObject {
|
||||
return stateManager.isTimerPaused(identifier)
|
||||
}
|
||||
|
||||
private func timerConfigurations() -> [TimerIdentifier: TimerConfiguration] {
|
||||
configurationHelper.configurations()
|
||||
private func timerConfigurations() -> [TimerIdentifier: (enabled: Bool, intervalSeconds: Int)] {
|
||||
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