#54 snippet group

This commit is contained in:
lollipopkit
2023-07-07 20:37:50 +08:00
parent d8155e7771
commit bb50fbc589
12 changed files with 223 additions and 82 deletions

View File

@@ -1,9 +1,15 @@
import 'package:toolbox/core/persistant_store.dart';
typedef Order<T> = List<T>;
typedef _OnMove<T> = void Function(Order<T>);
extension OrderX<T> on Order<T> {
void move(int oldIndex, int newIndex, StoreProperty<List<T>> property) {
void move(
int oldIndex,
int newIndex, {
StoreProperty<List<T>>? property,
_OnMove<T>? onMove,
}) {
if (oldIndex == newIndex) return;
if (oldIndex < newIndex) {
newIndex -= 1;
@@ -11,7 +17,8 @@ extension OrderX<T> on Order<T> {
final item = this[oldIndex];
removeAt(oldIndex);
insert(newIndex, item);
property.put(this);
property?.put(this);
onMove?.call(this);
}
void update(T id, T newId) {
@@ -24,11 +31,16 @@ extension OrderX<T> on Order<T> {
return indexOf(id);
}
void moveById(T oid, T nid, StoreProperty<List<T>> property) {
void moveById(
T oid,
T nid, {
StoreProperty<List<T>>? property,
_OnMove<T>? onMove,
}) {
final index = indexOf(oid);
if (index == -1) return;
final newIndex = indexOf(nid);
if (newIndex == -1) return;
move(index, newIndex, property);
move(index, newIndex, property: property, onMove: onMove);
}
}