Created
June 4, 2019 21:09
-
-
Save timdonnelly/2cc58168d581ac0891716a43105a0f25 to your computer and use it in GitHub Desktop.
TodoStore
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
import SwiftUI | |
import Combine | |
struct State { | |
var isCreatingItem: Bool = false | |
var partialItemName: String = "" | |
var todoItems: [TodoItem] = [] | |
} | |
final class TodoStore: BindableObject { | |
private let initialState: State = State() | |
private var subsequentStates: [State] = [] | |
let didChange = PassthroughSubject<Void, Never>() | |
var stateCount: Int { | |
return 1 + subsequentStates.count | |
} | |
var currentStateIndex: Int = 0 { | |
didSet { | |
didChange.send(()) | |
} | |
} | |
var state: State { | |
get { [[initialState], subsequentStates].flatMap({ $0 })[currentStateIndex] } | |
set { | |
subsequentStates.append(newValue) | |
currentStateIndex = stateCount - 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment