Skip to content

Instantly share code, notes, and snippets.

@RPallas92
Last active January 3, 2018 15:48
Show Gist options
  • Save RPallas92/5ff087d230897288834bb7159ff31c1c to your computer and use it in GitHub Desktop.
Save RPallas92/5ff087d230897288834bb7159ff31c1c to your computer and use it in GitHub Desktop.
enum Event {
case addTodo(Todo)
case toggleTodo(Todo)
case setVisibilityFilter(VisibilityFilter)
case storeTodos()
case todosStored(Bool)
}
struct State {
var todos: [Todo]
var visibilityFilter: VisibilityFilter
var mustStoreTodos: Bool
}
func reduce(state: State, event: Event) -> State {
switch event {
case .addTodo(let todo):
var newState = state
newState.todos = state.todos + [todo]
return newState
case .toggleTodo(let todo):
var newState = state
newState.todos = state.todos.map { t in
var newTodo = t
if(todo.text == t.text) {
newTodo.completed = !t.completed
}
return newTodo
}
return newState
case .setVisibilityFilter(let filter):
var newState = state
newState.visibilityFilter = filter
return newState
}
case .storeTodos():
var newState = state
newState.mustStoreTodos = true
return newState
}
case .todosStored(let successfulStored):
var newState = state
newState.mustStoreTodos = false
return newState
}
}
func storeUserTodos(todos:[Todo]) -> AsyncResult<Context, Event, GenericError> {
return storeUserTodosUseCase()
.mapTT { Event.meetupsStored($0) }
}
let feedback = Feedback.react({storeUserTodos($0.todos) }, when: { $0.mustStoreTodos})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment