2020-09-07 15:13:39 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:hive/hive.dart';
|
2020-08-27 16:54:34 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
2020-09-07 15:13:39 +00:00
|
|
|
mixin Keyable {
|
|
|
|
dynamic keyIndex;
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:35:23 +00:00
|
|
|
void connectWithTransform<T extends Keyable, Y extends Keyable>(
|
2020-08-31 08:44:58 +00:00
|
|
|
ObservableList<T> source, ObservableList<Y> dest, Y Function(T) transform,
|
|
|
|
{bool Function(T) filter}) {
|
|
|
|
source.observe((ListChange<T> change) {
|
2020-09-07 15:13:39 +00:00
|
|
|
change.elementChanges.forEach((change) {
|
|
|
|
switch (change.type) {
|
|
|
|
case OperationType.add:
|
|
|
|
if (filter?.call(change.newValue as T) ?? true) {
|
|
|
|
dest.add(transform(change.newValue as T));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OperationType.remove:
|
|
|
|
// Hive could has equal index and key
|
|
|
|
dest.removeWhere(
|
|
|
|
(elem) => elem.keyIndex == (change.oldValue.key ?? change.index));
|
|
|
|
break;
|
|
|
|
case OperationType.update:
|
|
|
|
for (var i = 0; i < dest.length; i++) {
|
|
|
|
final item = dest[i];
|
|
|
|
|
|
|
|
if (item.keyIndex == change.newValue.key) {
|
|
|
|
dest[i] = transform(change.newValue as T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2020-08-27 16:54:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:35:23 +00:00
|
|
|
void connectMapToListWithTransform<T extends Keyable, Y extends Keyable>(
|
|
|
|
ObservableMap<dynamic, T> source,
|
|
|
|
ObservableList<Y> dest,
|
|
|
|
Y Function(T) transform,
|
|
|
|
{bool Function(T) filter}) {
|
|
|
|
source.observe((MapChange<dynamic, T> change) {
|
|
|
|
switch (change.type) {
|
|
|
|
case OperationType.add:
|
|
|
|
if (filter?.call(change.newValue) ?? true) {
|
|
|
|
dest.add(transform(change.newValue));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OperationType.remove:
|
|
|
|
// Hive could has equal index and key
|
|
|
|
dest.removeWhere(
|
|
|
|
(elem) => elem.keyIndex == (change.key ?? change.newValue.keyIndex));
|
|
|
|
break;
|
|
|
|
case OperationType.update:
|
|
|
|
for (var i = 0; i < dest.length; i++) {
|
|
|
|
final item = dest[i];
|
|
|
|
|
|
|
|
if (item.keyIndex == change.key) {
|
|
|
|
dest[i] = transform(change.newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:13:39 +00:00
|
|
|
void connect<T extends Keyable>(
|
|
|
|
ObservableList<T> source, ObservableList<T> dest) {
|
2020-08-31 08:44:58 +00:00
|
|
|
source.observe((ListChange<T> change) {
|
2020-09-07 15:13:39 +00:00
|
|
|
source.observe((ListChange<T> change) {
|
|
|
|
change.elementChanges.forEach((change) {
|
|
|
|
switch (change.type) {
|
|
|
|
case OperationType.add:
|
|
|
|
// if (filter?.call(change.newValue as T) ?? true) {
|
|
|
|
dest.add(change.newValue as T);
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
case OperationType.remove:
|
|
|
|
// Hive could has equal index and key
|
|
|
|
dest.removeWhere((elem) =>
|
|
|
|
elem.keyIndex == (change.oldValue.key ?? change.index));
|
|
|
|
break;
|
|
|
|
case OperationType.update:
|
|
|
|
for (var i = 0; i < dest.length; i++) {
|
|
|
|
final item = dest[i];
|
|
|
|
|
|
|
|
if (item.keyIndex == change.newValue.key) {
|
|
|
|
dest[i] = change.newValue as T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamSubscription<BoxEvent> bindBox<T extends Keyable>(
|
|
|
|
Box<T> source, ObservableList<T> dest) {
|
|
|
|
return source.watch().listen((event) {
|
|
|
|
if (event.deleted) {
|
|
|
|
dest.removeWhere((el) => el.keyIndex == event.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
final dynamic value = event.value;
|
|
|
|
|
|
|
|
if (value is T) {
|
|
|
|
final elIndex = dest.indexWhere((el) => el.keyIndex == value.keyIndex);
|
|
|
|
|
|
|
|
if (elIndex > -1) {
|
|
|
|
dest[elIndex] = value;
|
|
|
|
} else {
|
|
|
|
dest.add(value);
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 16:54:34 +00:00
|
|
|
});
|
|
|
|
}
|