OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 library todomvc.todo_model; | |
6 | |
7 // Very simple model for a collection of TODO items. | |
8 | |
9 class Item { | |
10 String title; | |
11 bool _done = false; | |
12 int _id; | |
13 | |
14 static int id_pool = 0; | |
15 | |
16 Item(this.title) { | |
17 _id = id_pool++; | |
18 } | |
19 | |
20 bool get done => _done; | |
21 void complete() { _done = true; } | |
22 void uncomplete() { _done = false; } | |
23 int get id => _id; | |
24 } | |
25 | |
26 class TodoModel { | |
27 Map<int, Item> todos; | |
28 | |
29 TodoModel() : todos = new Map<int, Item>(); | |
30 | |
31 void createItem(String title) { | |
32 assert(title.isNotEmpty); | |
33 Item item = new Item(title); | |
34 todos.putIfAbsent( item.id, () => item ); | |
35 } | |
36 | |
37 void deleteItem(int id) { | |
38 if (todos.containsKey(id)) { | |
39 todos.remove(id); | |
40 } | |
41 } | |
42 | |
43 void completeItem(int id) { | |
44 if (todos.containsKey(id)) { | |
45 todos[id].complete(); | |
46 } | |
47 } | |
48 | |
49 void uncompleteItem(int id) { | |
50 if (todos.containsKey(id)) { | |
51 todos[id].uncomplete(); | |
52 } | |
53 } | |
54 | |
55 void clearItems() { | |
56 List<int> toDelete = new List<int>(); | |
57 | |
58 todos.forEach((k,v) { | |
59 if (v.done) { | |
60 toDelete.add(k); | |
61 } | |
62 }); | |
63 | |
64 toDelete.forEach((key) { | |
65 todos.remove(key); | |
66 }); | |
67 } | |
68 | |
69 } | |
OLD | NEW |