Skip to content

Commit

Permalink
feat(Configuration): migrate Configuration to modern TCA
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudjenny committed Apr 6, 2023
1 parent d0d1213 commit 2d59735
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 44 deletions.
10 changes: 3 additions & 7 deletions telltime/TellTime/AppCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TTSCore

struct AppState: Equatable {
var date: Date = Date()
var configuration = ConfigurationState()
var configuration = Configuration.State()
var tts = TTS.State()
var speechRecognizer = SpeechRecognizer.State()
var isAboutPresented = false
Expand All @@ -18,7 +18,7 @@ struct AppState: Equatable {
enum AppAction: Equatable {
case setDate(Date)
case setRandomDate
case configuration(ConfigurationAction)
case configuration(Configuration.Action)
case tts(TTS.Action)
case speechRecognizer(SpeechRecognizer.Action)
case appStarted
Expand All @@ -36,11 +36,7 @@ struct AppEnvironment {
}

let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
configurationReducer.pullback(
state: \.configuration,
action: /AppAction.configuration,
environment: { _ in ConfigurationEnvironment() }
),
AnyReducer(Configuration()).pullback(state: \.configuration, action: /AppAction.configuration, environment: { $0 }),
AnyReducer(TTS()).pullback(state: \.tts, action: /AppAction.tts, environment: { $0 }),
AnyReducer(SpeechRecognizer()).pullback(
state: \.speechRecognizer,
Expand Down
73 changes: 36 additions & 37 deletions telltime/TellTime/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
import SwiftClockUI
import ComposableArchitecture
import SwiftClockUI

struct ConfigurationState: Equatable {
var clock = ClockConfiguration()
var clockStyle: ClockStyle = .classic
var isPresented = false
}

enum ConfigurationAction: Equatable {
case setMinuteIndicatorsShown(Bool)
case setHourIndicatorsShown(Bool)
case setLimitedHoursShown(Bool)
case setClockStyle(ClockStyle)
case present
case hide
}
struct Configuration: ReducerProtocol {
struct State: Equatable {
var clock = ClockConfiguration()
var clockStyle: ClockStyle = .classic
var isPresented = false
}

struct ConfigurationEnvironment { }
enum Action: Equatable {
case setMinuteIndicatorsShown(Bool)
case setHourIndicatorsShown(Bool)
case setLimitedHoursShown(Bool)
case setClockStyle(ClockStyle)
case present
case hide
}

typealias ConfigurationReducer = Reducer<ConfigurationState, ConfigurationAction, ConfigurationEnvironment>
let configurationReducer = ConfigurationReducer { state, action, _ in
switch action {
case let .setMinuteIndicatorsShown(isShown):
state.clock.isMinuteIndicatorsShown = isShown
return .none
case let .setHourIndicatorsShown(isShown):
state.clock.isHourIndicatorsShown = isShown
return .none
case let .setLimitedHoursShown(isShown):
state.clock.isLimitedHoursShown = isShown
return .none
case let .setClockStyle(clockStyle):
state.clockStyle = clockStyle
return .none
case .present:
state.isPresented = true
return .none
case .hide:
state.isPresented = false
return .none
func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case let .setMinuteIndicatorsShown(isShown):
state.clock.isMinuteIndicatorsShown = isShown
return .none
case let .setHourIndicatorsShown(isShown):
state.clock.isHourIndicatorsShown = isShown
return .none
case let .setLimitedHoursShown(isShown):
state.clock.isLimitedHoursShown = isShown
return .none
case let .setClockStyle(clockStyle):
state.clockStyle = clockStyle
return .none
case .present:
state.isPresented = true
return .none
case .hide:
state.isPresented = false
return .none
}
}
}

0 comments on commit 2d59735

Please sign in to comment.