Skip to content

Instantly share code, notes, and snippets.

@timdonnelly
Created June 4, 2019 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdonnelly/2cc58168d581ac0891716a43105a0f25 to your computer and use it in GitHub Desktop.
Save timdonnelly/2cc58168d581ac0891716a43105a0f25 to your computer and use it in GitHub Desktop.
TodoStore
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