Exemplo n.º 1
0
        private TodoListAppViewModel Connect(IStore <TodoList> store, TodoListAppViewModel connectible)
        {
            return(store.Connect <TodoList, TodoListAppViewModel>(
                       mapStateToProps: (state, props) =>
            {
                props.CanClickShowCompleted = state.Filter != VisibilityFilter.ShowCompletedItems;
                props.CanClickShowActive = state.Filter != VisibilityFilter.ShowActiveItems;
                props.CanClickShowAll = state.Filter != VisibilityFilter.ShowAllItems;
                props.HasTodoItems = state.Todos.Any();
                props.ClearButtonIsVisible = state.Todos.Any(x => x.Value.Completed);
                props.CheckToggleAll = !state.Todos.Any(x => x.Value.Completed == false);
                props.ItemsLeft = state.Todos.Count(x => x.Value.Completed == false);
                props.NewTodo = "";

                var visibleTodos = GetFilteredTodos(state.Todos, state.EditingTodos, state.Filter);

                props.Todos.Patch(visibleTodos);
            },
                       mapDispatchToProps: (dispatcher, state, props) =>
            {
                props.DispatchAdd = (newTodo) =>
                {
                    int id = state.Todos.Values.Select(x => x.Id).DefaultIfEmpty(0).Max() + 1;
                    dispatcher.DispatchAsync(new AddTodo(new TodoItem(id, newTodo, false)));
                };
                props.DispatchClearCompleted = () => dispatcher.DispatchAsync(new RemoveCompleted());
                props.DispatchCompleteAll = () =>
                {
                    if (state.Todos.Any(x => !x.Value.Completed))
                    {
                        dispatcher.DispatchAsync(new CompleteAllTodos());
                    }
                };
                props.DispatchShowActive = () => dispatcher.DispatchAsync(new ChangeVisibilityFilter(VisibilityFilter.ShowActiveItems));
                props.DispatchShowAll = () => dispatcher.DispatchAsync(new ChangeVisibilityFilter(VisibilityFilter.ShowAllItems));
                props.DispatchShowCompleted = () => dispatcher.DispatchAsync(new ChangeVisibilityFilter(VisibilityFilter.ShowCompletedItems));
                props.DispatchActivateAll = () =>
                {
                    if (state.Todos.All(x => x.Value.Completed))
                    {
                        dispatcher.DispatchAsync(new MarkAllTodosAsActive());
                    }
                };

                foreach (var todoItemProps in props.Todos)
                {
                    todoItemProps.DispatchShowTodoEditor = (todoItem) => dispatcher.DispatchAsync(new ShowTodoEditor(todoItem));
                    todoItemProps.DispatchHideTodoEditor = (id) => dispatcher.DispatchAsync(new HideTodoEditor(id));
                    todoItemProps.DispatchToggleComplete = (id) => dispatcher.DispatchAsync(new ToggleComplete(id));
                    todoItemProps.DispatchUpdateTodoText = (id, text) => dispatcher.DispatchAsync(new UpdateTodoText(id, text));
                    todoItemProps.DispatchRemoveTodo = (id) => dispatcher.DispatchAsync(new RemoveTodo(id));
                }
            })(connectible));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Create a new instance of ShellViewModel.
 /// </summary>
 /// <param name="store"></param>
 public ShellViewModel(IStore <TodoList> store)
 {
     _store      = store;
     TodoListApp = Connect(_store, new TodoListAppViewModel());
 }