Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LocalPackage/Sources/DataSource/Entities/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public struct AppState: Sendable {
public var runnerBundleLists = AsyncStreamBundle<[RunnerBundle]>()
public var runnerBundles = AsyncStreamBundle<RunnerBundle>()
public var runnerSpeeds = AsyncStreamBundle<Float>()
public var settingsResets = AsyncStreamBundle<Void>()

init(
name: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public struct UserDefaultsRepository: Sendable {
}
}

public func resetToDefaults() {
userDefaultsClient.removeObject(.runnerID)
userDefaultsClient.removeObject(.speedDecreasesUnderLoad)
userDefaultsClient.removeObject(.isFlippedHorizontally)
userDefaultsClient.removeObject(.updateInterval)
userDefaultsClient.removeObject(.systemMetricsConfiguration)
userDefaultsClient.removeObject(.showsMetricsBar)
userDefaultsClient.removeObject(.metricsBarConfiguration)
}

private func showAllData() {
guard let dict = userDefaultsClient.persistentDomain(Bundle.main.bundleIdentifier!) else {
return
Expand Down
32 changes: 32 additions & 0 deletions LocalPackage/Sources/Model/Stores/GeneralSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,34 @@ import Observation

@MainActor @Observable
public final class GeneralSettings: Composable {
private let appStateClient: AppStateClient
private let launchAtLoginRepository: LaunchAtLoginRepository
private let userDefaultsRepository: UserDefaultsRepository
private let logService: LogService
private let systemMetricsService: SystemMetricsService
private let runnerService: RunnerService

public var updateInterval: UpdateInterval
public var launchesAtLogin: Bool
public var showingResetConfirmationDialog: Bool
public let action: (Action) async -> Void

public init(
_ appDependencies: AppDependencies,
updateInterval: UpdateInterval? = nil,
launchesAtLogin: Bool? = nil,
showingResetConfirmationDialog: Bool = false,
action: @escaping (Action) async -> Void = { _ in }
) {
self.appStateClient = appDependencies.appStateClient
self.launchAtLoginRepository = .init(appDependencies.smAppServiceClient)
self.userDefaultsRepository = .init(appDependencies.userDefaultsClient)
self.logService = .init(appDependencies)
self.systemMetricsService = .init(appDependencies)
self.runnerService = .init(appDependencies)
self.updateInterval = updateInterval ?? userDefaultsRepository.updateInterval
self.launchesAtLogin = launchesAtLogin ?? launchAtLoginRepository.isEnabled
self.showingResetConfirmationDialog = showingResetConfirmationDialog
self.action = action
}

Expand All @@ -65,12 +72,37 @@ public final class GeneralSettings: Composable {
case let .failure(.switchFailed(value)):
launchesAtLogin = value
}

case .resetToDefaultsButtonTapped:
showingResetConfirmationDialog = true

case .resetToDefaultsCancelled:
showingResetConfirmationDialog = false

case .resetToDefaultsConfirmed:
showingResetConfirmationDialog = false
userDefaultsRepository.resetToDefaults()
updateInterval = userDefaultsRepository.updateInterval
systemMetricsService.stopMonitoring()
systemMetricsService.startMonitoring()
do {
try runnerService.update(runner: .default)
} catch {
logService.critical(.unknown(error))
}
let cpuInfo = systemMetricsService.currentSystemInfoBundle.cpuInfo
runnerService.updateRunnerSpeed(from: cpuInfo)
systemMetricsService.emitConfigurationChange()
appStateClient.send(\.settingsResets, ())
}
}

public enum Action: Sendable {
case task(String)
case updateIntervalChanged(UpdateInterval)
case launchAtLoginToggleSwitched(Bool)
case resetToDefaultsButtonTapped
case resetToDefaultsCancelled
case resetToDefaultsConfirmed
}
}
6 changes: 6 additions & 0 deletions LocalPackage/Sources/Model/Stores/MetricsBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public final class MetricsBar: Composable {
self?.updateMetricsBarConfiguration()
}
}
group.addImmediateTask {
let stream = appStateClient.withLock(\.settingsResets.stream)
for await _ in stream {
self?.updateMetricsBarConfiguration()
}
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions LocalPackage/Sources/Model/Stores/MetricsBarSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ public final class MetricsBarSettings: Composable {
customMetricsSources = userDefaultsRepository.customMetricsConfiguration.sources
task?.cancel()
task = Task.immediate { [weak self, appStateClient] in
let stream = appStateClient.withLock(\.customMetricsConfigurationChanges.stream)
for await _ in stream {
self?.updateCustomMetricsConfiguration()
await withTaskGroup { group in
group.addImmediateTask {
let stream = appStateClient.withLock(\.customMetricsConfigurationChanges.stream)
for await _ in stream {
self?.updateCustomMetricsConfiguration()
}
}
group.addImmediateTask {
let stream = appStateClient.withLock(\.settingsResets.stream)
for await _ in stream {
self?.updateCustomMetricsConfiguration()
}
}
}
}

Expand Down
23 changes: 20 additions & 3 deletions LocalPackage/Sources/Model/Stores/MetricsSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,23 @@ public final class MetricsSettings: Composable {
switch action {
case let .task(screenName):
logService.notice(.screenView(name: screenName))
showsMetricsBar = userDefaultsRepository.showsMetricsBar
refreshSystemMetricsConfiguration()
task?.cancel()
task = Task.immediate { [weak self, appStateClient] in
let stream = appStateClient.withLock(\.systemMetricsConfigurationChanges.stream)
for await _ in stream {
self?.refreshSystemMetricsConfiguration()
await withTaskGroup { group in
group.addImmediateTask {
let stream = appStateClient.withLock(\.systemMetricsConfigurationChanges.stream)
for await _ in stream {
self?.refreshSystemMetricsConfiguration()
}
}
group.addImmediateTask {
let stream = appStateClient.withLock(\.settingsResets.stream)
for await _ in stream {
self?.resetToDefaults()
}
}
}
}

Expand Down Expand Up @@ -141,6 +153,11 @@ public final class MetricsSettings: Composable {
systemMetricsConfiguration = userDefaultsRepository.systemMetricsConfiguration
}

private func resetToDefaults() {
showsMetricsBar = userDefaultsRepository.showsMetricsBar
refreshSystemMetricsConfiguration()
}

public enum Action: Sendable {
case task(String)
case onDisappear
Expand Down
20 changes: 20 additions & 0 deletions LocalPackage/Sources/Model/Stores/RunnerSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public final class RunnerSettings: Composable {
switch action {
case let .task(screenName):
logService.notice(.screenView(name: screenName))
speedDecreasesUnderLoad = userDefaultsRepository.speedDecreasesUnderLoad
isFlippedHorizontally = userDefaultsRepository.isFlippedHorizontally
if let runnerBundle = appStateClient.withLock(\.runnerBundles.latestValue) {
currentRunner = runnerBundle.runner
}
Expand All @@ -91,6 +93,12 @@ public final class RunnerSettings: Composable {
self?.update(runnerBundleList: value)
}
}
group.addImmediateTask {
let stream = appStateClient.withLock(\.settingsResets.stream)
for await _ in stream {
self?.resetToDefaults()
}
}
}
}

Expand Down Expand Up @@ -136,6 +144,18 @@ public final class RunnerSettings: Composable {
self.runnerBundleList = runnerBundleList
}

private func resetToDefaults() {
speedDecreasesUnderLoad = userDefaultsRepository.speedDecreasesUnderLoad
isFlippedHorizontally = userDefaultsRepository.isFlippedHorizontally
do {
try runnerService.update(runner: .default)
} catch {
logService.critical(.unknown(error))
}
let cpuInfo = systemMetricsService.currentSystemInfoBundle.cpuInfo
runnerService.updateRunnerSpeed(from: cpuInfo)
}

public enum Action: Sendable {
case task(String)
case onDisappear
Expand Down
Loading