Skip to content

Instantly share code, notes, and snippets.

View vikingosegundo's full-sized avatar

Manuel Meyer vikingosegundo

  • Groningen, Netherlands
View GitHub Profile
func turn (_ light:Light, _ onOrOff:Turn ) { rootHandler( .lighting(.turn(light,onOrOff)) ) }
func apply ( values:Light.Values, on light:Light) { rootHandler( .lighting(.apply(.values(values),on:light)) ) }
func increase( value :Light.Value, by inc :Light.Value.Increment, on light:Light) { rootHandler( .lighting(.increase(value,by:inc,on:light)) ) }
func decrease( value :Light.Value, by dec :Light.Value.Increment, on light:Light) { rootHandler( .lighting(.decrease(value,by:dec,on:light)) ) }
import SwiftUI
import BrighterModel
final class ViewState: ObservableObject {
@Published var lights : [ Light ] = []
@Published var rooms : [ Room ] = []
@Published var favorites: [ Favorite ] = []
init(store: Store) {
store.updated { self.process(state(in: store)) }
// store changes
store.change(.by(.adding (.favorite(fav))))
store.change(.by(.removing(.favorite(fav))))
store.change(.by(.replacing(.light(with:light))))
// listen for updates
store.updated { self.process(state(in: store)) }
import Foundation.NSFileManager
func createDiskStore(
pathInDocs p: String = "state.json",
fileManager fm: FileManager = .default
) -> Store
{
var state = loadAppStateFromStore(pathInDocuments: p, fileManager: fm) { didSet { callbacks.forEach { $0() } } }
var callbacks: [() -> ()] = []
return (
typealias Access = ( ) -> AppState
typealias Change = ( AppState.Change... ) -> ()
typealias Reset = ( ) -> ()
typealias Callback = ( @escaping () -> () ) -> ()
typealias Destroy = ( ) -> ()
typealias Store = ( state: Access, change: Change, reset: Reset, updated: Callback, destroy: Destroy )
func change(_ state:AppState, _ change:[AppState.Change] ) -> AppState { state.alter(change) }
struct AppState: Codable {
enum Change: Equatable {
case by(_By); enum _By: Equatable {
case adding(_Add); enum _Add: Equatable {
case light(Light)
case favorite(Favorite) }
case replacing(_Replace); enum _Replace: Equatable {
case lights(with:[Light])
//MARK: - Message
enum Message {
case lighting(_Lighting)
case dashboard(_Dashboard)
case logging(_Logging)
}
//MARK: - Common Lighting DSL Elements
enum Outcome { case succeeded, failed(Error?) }
enum Items { case lights, rooms }
func newLight() -> 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))
)
}
import SwiftUI
import BrighterUI
import BrighterModel
@main
final
class BrighterHueApp: App {
init() {
rootHandler(.lighting(.load(.lights)))
struct Light:Codable, Equatable, Identifiable {
enum Change {
case renaming(_RenameIt); enum _RenameIt { case it (to:String) }
case turning (_TurnIt ); enum _TurnIt { case it (Turn) }
case adding (_Add ); enum _Add { case mode (Light.Mode) }
case toggling(_Toggle ); enum _Toggle { case display(to:Interface) }
case setting (_Set ); enum _Set {
case hue (to:Double )
case saturation (to:Double )
case brightness (to:Double )