forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiIterator.dart
70 lines (62 loc) · 1.6 KB
/
BiIterator.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// @dart = 2.9
part of swarmlib;
/**
* An iterator that allows the user to move forward and backward though
* a set of items. (Bi-directional)
*/
class BiIterator<E> {
/**
* Provides forward and backward iterator functionality to keep track
* which item is currently selected.
*/
ObservableValue<int> currentIndex;
/**
* The collection of items we will be iterating through.
*/
List<E> list;
BiIterator(this.list, [List<ChangeListener> oldListeners = null])
: currentIndex = new ObservableValue<int>(0) {
if (oldListeners != null) {
currentIndex.listeners = oldListeners;
}
}
/**
* Returns the next section from the sections, given the current
* position. Returns the last source if there is no next section.
*/
E next() {
if (currentIndex.value < list.length - 1) {
currentIndex.value += 1;
}
return list[currentIndex.value];
}
/**
* Returns the current Section (page in the UI) that the user is
* looking at.
*/
E get current {
return list[currentIndex.value];
}
/**
* Returns the previous section from the sections, given the current
* position. Returns the front section if we are already at the front of
* the list.
*/
E previous() {
if (currentIndex.value > 0) {
currentIndex.value -= 1;
}
return list[currentIndex.value];
}
/**
* Move the iterator pointer over so that it points to a given list item.
*/
void jumpToValue(E val) {
for (int i = 0; i < list.length; i++) {
if (identical(list[i], val)) {
currentIndex.value = i;
break;
}
}
}
}