Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (69 sloc) 1.89 KB
import 'package:mobx/mobx.dart';
part 'todos.g.dart';
class Todo = TodoBase with _$Todo;
abstract class TodoBase with Store {
TodoBase(this.description);
@observable
String description = '';
@observable
bool done = false;
}
enum VisibilityFilter { all, pending, completed }
class TodoList = TodoListBase with _$TodoList;
abstract class TodoListBase with Store {
@observable
ObservableList<Todo> todos = ObservableList<Todo>();
@observable
VisibilityFilter filter = VisibilityFilter.all;
@observable
String currentDescription = '';
@computed
ObservableList<Todo> get pendingTodos =>
ObservableList.of(todos.where((todo) => todo.done != true));
@computed
ObservableList<Todo> get completedTodos =>
ObservableList.of(todos.where((todo) => todo.done == true));
@computed
bool get hasCompletedTodos => completedTodos.isNotEmpty;
@computed
bool get hasPendingTodos => pendingTodos.isNotEmpty;
@computed
String get itemsDescription =>
'${pendingTodos.length} pending, ${completedTodos.length} completed';
@computed
ObservableList<Todo> get visibleTodos {
switch (filter) {
case VisibilityFilter.pending:
return pendingTodos;
case VisibilityFilter.completed:
return completedTodos;
default:
return todos;
}
}
@action
void addTodo(String description) {
final todo = Todo(description);
todos.add(todo);
currentDescription = '';
}
@action
void removeTodo(Todo todo) {
todos.removeWhere((x) => x == todo);
}
@action
void changeDescription(String description) =>
currentDescription = description;
@action
void changeFilter(VisibilityFilter filter) => this.filter = filter;
@action
void removeCompleted() {
todos.removeWhere((todo) => todo.done);
}
@action
void markAllAsCompleted() {
for (final todo in todos) {
todo.done = true;
}
}
}
You can’t perform that action at this time.