Permalink
Browse files

feature: Better list syncronisation for MaterialRepeat

  • Loading branch information...
MikeMitterer committed Mar 9, 2017
1 parent d8ed17b commit 511a9510699cd7f607d7f38b8cd9a91d2daeea35
Showing with 52 additions and 15 deletions.
  1. +3 −2 README.md
  2. +34 −9 lib/src/observable/ObservableList.dart
  3. +15 −4 lib/src/template/components/MaterialRepeat.dart
View
@@ -1,5 +1,6 @@
# Material Design Lite for Dart
> A GUI library and framework to develop web apps in Dart.
# Material Design for Dart
> A User Interface Library / GUI-Framework to
> develop *__Progressive Web Apps (PWA)__* in Dart.
# Bug in Dart 1.21.1
> Due to a bug in Dart 1.21.1 all MDL-Dialog (AlertDialog, ConfirmDialog, ... SnackBar and Notification)
@@ -37,7 +37,10 @@ class ListChangedEvent<T> {
/// It is also set on INSERT. It defines the previous items a the given index
final T prevItem;
ListChangedEvent(this.changetype,{ final T this.item, final this.prevItem });
/// Index in der Original-Liste (_innerList)
final int index;
ListChangedEvent(this.changetype,{ final T this.item, final this.prevItem, final this.index: -1 });
}
/// List that sends [ListChangeEvents] to the listener if this list changes
@@ -131,7 +134,8 @@ class ObservableList<T> extends ListBase<T> {
void operator []=(int index, T value) {
_fire(new ListChangedEvent<T>(ListChangeType.UPDATE,
item: value,
prevItem: _innerList[index]));
prevItem: _innerList[index],
index: index ));
_innerList[index] = value;
}
@@ -140,13 +144,20 @@ class ObservableList<T> extends ListBase<T> {
void add(final T value) {
_innerList.add(value);
_fire(new ListChangedEvent<T>(ListChangeType.ADD,item: value));
_fire(new ListChangedEvent<T>(
ListChangeType.ADD,
item: value,
index: _innerList.indexOf(value)));
}
void addAll(Iterable<T> all) {
_innerList.addAll(all);
all.forEach((final element) {
_fire(new ListChangedEvent<T>(ListChangeType.ADD,item: element));
_fire(new ListChangedEvent<T>(
ListChangeType.ADD,
item: element,
index: _innerList.indexOf(element)));
});
}
@@ -166,13 +177,20 @@ class ObservableList<T> extends ListBase<T> {
} else {
if(index == 0) {
_fire(new ListChangedEvent<T>(ListChangeType.INSERT,item: element));
_fire(new ListChangedEvent<T>(
ListChangeType.INSERT,
item: element,
index: index ));
_innerList.insert(index,element);
} else {
_fire(new ListChangedEvent<T>(ListChangeType.INSERT,
item: element, prevItem: _innerList[index]));
_fire(new ListChangedEvent<T>(
ListChangeType.INSERT,
item: element,
prevItem: _innerList[index],
index: index));
_innerList.insert(index,element);
}
@@ -189,14 +207,21 @@ class ObservableList<T> extends ListBase<T> {
void removeRange(int start, int end) {
RangeError.checkValidRange(start, end, this.length);
for(int index = start;index < end;index++) {
_fire(new ListChangedEvent<T>(ListChangeType.REMOVE,item: _innerList[index] ));
_fire(new ListChangedEvent<T>(
ListChangeType.REMOVE,
item: _innerList[index],
index: index ));
}
_innerList.removeRange(start,end);
}
@override
bool remove(final Object element) {
_fire(new ListChangedEvent<T>(ListChangeType.REMOVE,item: element as T ));
_fire(new ListChangedEvent<T>(
ListChangeType.REMOVE,
item: element as T,
index: _innerList.indexOf(element as T) ));
return _innerList.remove(element);
}
@@ -370,10 +370,15 @@ class MaterialRepeat extends MdlTemplateComponent {
break;
case ListChangeType.UPDATE:
int index = -1;
// Index wird aus der Original-Liste mitgeliefert (wenn nicht gesetzt dann -1)
int index = event.index;
try {
final Map itemToRemove = _getItemFromInternalList(event.prevItem);
index = _items.indexOf(itemToRemove);
if(index == -1 || index >= _items.length) {
final Map itemToRemove = _getItemFromInternalList(event.prevItem);
index = _items.indexOf(itemToRemove);
}
final dom.HtmlElement child = element.children[index];
@@ -385,6 +390,10 @@ class MaterialRepeat extends MdlTemplateComponent {
_items[index] = { itemName: event.item};
}
else {
//final Map itemToRemove = _getItemFromInternalList(event.prevItem);
final Map itemToRemove = _items[index];
index = _items.indexOf(itemToRemove);
// _logger.fine("Index to remove: ${index}");
final int indexRemoved = await remove(itemToRemove);
// _logger.fine("Index removed: ${indexRemoved}/${_items.length}");
@@ -409,7 +418,9 @@ class MaterialRepeat extends MdlTemplateComponent {
}
}
on StateError catch (e, stacktrace) {
_logger.shout("_getItemFromInternalList(${event.prevItem}) produced '$e' (Index: $index)",
_logger.shout(
"_getItemFromInternalList(${event.prevItem}) produced '$e' "
"(Index: $index/${_items.length})",
stacktrace);
}

0 comments on commit 511a951

Please sign in to comment.