This file contains hidden or 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
struct Lights: View { | |
init(_ rootHandler: @escaping (Message) -> ()) { | |
self.rootHandler = rootHandler | |
} | |
var body: some View { | |
VStack { | |
VStack { | |
NavigationView { | |
List { | |
ForEach(Array(viewState.lights.enumerated()), id:\.offset) { LightCell(light: $1, rootHandler:rootHandler) } |
This file contains hidden or 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
public struct ContentView: View { | |
public init(viewState: ViewState, rootHandler: @escaping (Message) -> ()) { | |
self.viewState = viewState | |
self.rootHandler = rootHandler | |
} | |
public var body: some View { | |
TabView(selection:$tab) { | |
Dashboard(rootHandler).tabItem { Label(name(for:0), systemImage:imageName(for:0)) }.tag(0) | |
Lights (rootHandler).tabItem { Label(name(for:1), systemImage:imageName(for:1)) }.tag(1) | |
Rooms (rootHandler).tabItem { Label(name(for:2), systemImage:imageName(for:2)) }.tag(2) |
This file contains hidden or 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
public | |
final class ViewState: ObservableObject { | |
@Published public var lights : [Light ] = [] | |
@Published public var rooms : [Room ] = [] | |
@Published public var favorites : [Favorite] = [] | |
public init(store:Store<AppState,AppState.Change>) { | |
store.updated { self.process(state(in:store)) } | |
process(store.state()) | |
} | |
This file contains hidden or 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
fileprivate var store : Store = createDiskStore() | |
fileprivate var viewState : ViewState = ViewState(store: store) | |
fileprivate var rootHandler: (Message) -> () = createAppDomain( | |
store : store, | |
receivers : [viewState.handle(msg:)], | |
rootHandler: { rootHandler($0) } | |
) | |
@main | |
final | |
class BrighterHueApp: App { |
This file contains hidden or 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
.by(.adding(.light(<aLight>))) | |
.by(.adding(.favorite(<aFavorite>))) | |
.by(.replacing(.lights(<aListOfLights>))) | |
.by(.replacing(.light(<aLight>))) | |
.by(.replacing(.rooms(<aListOfRooms>))) | |
.by(.removing(.favorite(<aFavorite>))) |
This file contains hidden or 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
struct Dimmer: UseCase { | |
enum Request { case increase(Light.Value, by:Light.Value.Increment, on:Light ) | |
case decrease(Light.Value, by:Light.Value.Increment, on:Light ) } | |
enum Response { case increase(Light.Value, by:Light.Value.Increment, on:Light, Outcome) | |
case decrease(Light.Value, by:Light.Value.Increment, on:Light, Outcome) } | |
init(lightsStack: LightsStack, store: Store, responder: @escaping (Response) -> ()) { | |
self.lightsStack = lightsStack | |
self.store = store | |
self.respond = responder | |
} |
This file contains hidden or 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
let light = Light(id:"01", name:"01") | |
.alter( | |
by: | |
.setting(.brightness (to:0.5 )), | |
.setting(.hue (to:0.5 )), | |
.setting(.saturation (to:0.5 )), | |
.setting(.temperature(to:200.mk)), | |
.turning(.it(.on)) | |
) |
This file contains hidden or 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
typealias Access<S> = ( ) -> S | |
typealias Change<C> = ( C... ) -> () | |
typealias Reset = ( ) -> () | |
typealias Callback = ( @escaping () -> () ) -> () | |
typealias Destroy = ( ) -> () | |
typealias Store<S,C> = ( | |
state: Access<S>, | |
change: Change<C>, | |
reset: Reset, |
This file contains hidden or 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
func createDiskStore( | |
pathInDocs p: String , | |
fileManager fm: FileManager = .default | |
) -> Store<AppState, AppState.Change> | |
{ | |
var state = loadAppStateFromStore(pathInDocuments:p,fileManager:fm) { didSet { callbacks.forEach { $0() } } } | |
var callbacks: [() -> ()] = [] | |
return ( | |
state : { state }, | |
change : { state = state.alter($0) ; persistStore(pathInDocuments:p, state:state, fileManager:fm) }, |
This file contains hidden or 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
func createAdder(x:Int) -> (Int) -> Int { | |
var value = x | |
return { | |
value = value + $0 | |
return value | |
} | |
} | |
let adder = createAdder(x: 5) | |
print(adder(4)) // -> 9 |