-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from uhooi/feature/set_default_times
Set default times
- Loading branch information
Showing
10 changed files
with
241 additions
and
18 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
LokiPackage/Sources/Data/Sakatsu/DefaultSaunaSetRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import UserDefaultsCore | ||
|
||
public protocol DefaultSaunaSetRepository { | ||
func defaultSaunaSet() throws -> SaunaSet | ||
func saveDefaultSaunaSet(_ defaultSaunaSet: SaunaSet) throws | ||
} | ||
|
||
public struct DefaultSaunaSetUserDefaultsClient { | ||
public static let shared: Self = .init() | ||
private static let defaultSaunaSetKey = "defaultSaunaSet" | ||
|
||
private let userDefaultsClient = UserDefaultsClient.shared | ||
|
||
private init() {} | ||
} | ||
|
||
extension DefaultSaunaSetUserDefaultsClient: DefaultSaunaSetRepository { | ||
public func defaultSaunaSet() throws -> SaunaSet { | ||
do { | ||
return try userDefaultsClient.object(forKey: Self.defaultSaunaSetKey) | ||
} catch UserDefaultsError.missingValue { | ||
return .init() | ||
} catch { | ||
throw error | ||
} | ||
} | ||
|
||
public func saveDefaultSaunaSet(_ defaultSaunaSet: SaunaSet) throws { | ||
try userDefaultsClient.set(defaultSaunaSet, forKey: Self.defaultSaunaSetKey) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
LokiPackage/Sources/Features/Sakatsu/SakatsuSettings/SakatsuSettingsScreen.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
LokiPackage/Sources/Features/Sakatsu/SakatsuSettings/SakatsuSettingsViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import Foundation | ||
import Combine | ||
import SakatsuData | ||
|
||
// MARK: UI state | ||
|
||
struct SakatsuSettingsUiState { | ||
var defaultSaunaSet: SaunaSet = .init() | ||
var sakatsuSettingsError: SakatsuSettingsError? = nil | ||
} | ||
|
||
// MARK: - Error | ||
|
||
enum SakatsuSettingsError: LocalizedError { | ||
case defaultSaunaSetFetchFailed(localizedDescription: String) | ||
case defaultSaunaSetSaveFailed(localizedDescription: String) | ||
|
||
var errorDescription: String? { | ||
switch self { | ||
case let .defaultSaunaSetFetchFailed(localizedDescription): | ||
return localizedDescription | ||
case let .defaultSaunaSetSaveFailed(localizedDescription): | ||
return localizedDescription | ||
} | ||
} | ||
} | ||
|
||
// MARK: - View model | ||
|
||
@MainActor | ||
final class SakatsuSettingsViewModel< | ||
Repository: DefaultSaunaSetRepository, | ||
Validator: SakatsuValidatorProtocol | ||
>: ObservableObject { | ||
@Published private(set) var uiState: SakatsuSettingsUiState | ||
|
||
private let repository: Repository | ||
private let validator: Validator | ||
|
||
init( | ||
repository: Repository = DefaultSaunaSetUserDefaultsClient.shared, | ||
validator: Validator = SakatsuValidator() | ||
) { | ||
self.uiState = SakatsuSettingsUiState() | ||
self.repository = repository | ||
self.validator = validator | ||
refreshDefaultSaunaSet() | ||
} | ||
|
||
private func refreshDefaultSaunaSet() { | ||
do { | ||
uiState.defaultSaunaSet = try repository.defaultSaunaSet() | ||
} catch { | ||
uiState.sakatsuSettingsError = .defaultSaunaSetFetchFailed(localizedDescription: error.localizedDescription) | ||
} | ||
} | ||
|
||
private func saveDefaultSaunaSet() { | ||
do { | ||
try repository.saveDefaultSaunaSet(uiState.defaultSaunaSet) | ||
} catch { | ||
uiState.sakatsuSettingsError = .defaultSaunaSetSaveFailed(localizedDescription: error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Event handler | ||
|
||
extension SakatsuSettingsViewModel { | ||
func onDefaultSaunaTimeChange(defaultSaunaTime: TimeInterval?) { | ||
guard validator.validate(saunaTime: defaultSaunaTime) else { | ||
return | ||
} | ||
uiState.defaultSaunaSet.sauna.time = defaultSaunaTime | ||
saveDefaultSaunaSet() | ||
} | ||
|
||
func onDefaultCoolBathTimeChange(defaultCoolBathTime: TimeInterval?) { | ||
guard validator.validate(coolBathTime: defaultCoolBathTime) else { | ||
return | ||
} | ||
uiState.defaultSaunaSet.coolBath.time = defaultCoolBathTime | ||
saveDefaultSaunaSet() | ||
} | ||
|
||
func onDefaultRelaxationTimeChange(defaultRelaxationTime: TimeInterval?) { | ||
guard validator.validate(relaxationTime: defaultRelaxationTime) else { | ||
return | ||
} | ||
uiState.defaultSaunaSet.relaxation.time = defaultRelaxationTime | ||
saveDefaultSaunaSet() | ||
} | ||
|
||
func onErrorAlertDismiss() { | ||
uiState.sakatsuSettingsError = nil | ||
} | ||
} |