forked from bitgapp/eqMac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationState.swift
More file actions
68 lines (56 loc) · 1.77 KB
/
Copy pathApplicationState.swift
File metadata and controls
68 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// State.swift
// eqMac
//
// Created by Roman Kisil on 25/06/2018.
// Copyright © 2018 Roman Kisil. All rights reserved.
//
import Foundation
import ReSwift
import SwiftyUserDefaults
import BetterCodable
protocol State: Codable, DefaultsSerializable {}
fileprivate struct VolumeDefault: DefaultCodableStrategy {
static var defaultValue = VolumeState()
}
struct ApplicationState: State {
var settings = SettingsState()
var ui = UIState()
var effects = EffectsState()
@DefaultCodable<VolumeDefault> var volume = VolumeDefault.value
@DefaultTrue var enabled = true
static func load () -> ApplicationState {
guard let stateData = UserDefaults.standard.data(forKey: "state") else {
return ApplicationState()
}
guard let state = ({ () -> ApplicationState? in
if Constants.DEBUG && false {
return try! JSONDecoder().decode(ApplicationState.self, from: stateData)
} else {
return try? JSONDecoder().decode(ApplicationState.self, from: stateData)
}
})() else {
return ApplicationState()
}
return state
}
}
enum ApplicationAction: Action {
case setEnabled(Bool)
}
func ApplicationStateReducer(action: Action, state: ApplicationState?) -> ApplicationState {
var state = state ?? ApplicationState()
state.settings = SettingsStateReducer(action: action, state: state.settings)
state.ui = UIStateReducer(action: action, state: state.ui)
state.effects = EffectsStateReducer(action: action, state: state.effects)
state.volume = VolumeStateReducer(action: action, state: state.volume)
switch action as? ApplicationAction {
case .setEnabled(let enabled)?:
state.enabled = enabled
case .none:
break
}
Application.newState(state) // Notify
Storage[.state] = state // Store
return state
}