feat: full local assets / album sync
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// ignore_for_file: avoid-unsafe-collection-methods
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
class CollectionUtil {
|
||||
const CollectionUtil();
|
||||
|
||||
@@ -13,28 +15,33 @@ class CollectionUtil {
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
/// Find the difference between the two sorted lists [first] and [second]
|
||||
/// Find the difference between the two lists [first] and [second]
|
||||
/// Results are passed as callbacks back to the caller during the comparison
|
||||
static bool diffSortedLists<T>(
|
||||
static FutureOr<bool> diffLists<T>(
|
||||
List<T> first,
|
||||
List<T> second, {
|
||||
required Comparator<T> compare,
|
||||
required bool Function(T a, T b) both,
|
||||
required void Function(T a) onlyFirst,
|
||||
required void Function(T b) onlySecond,
|
||||
}) {
|
||||
required int Function(T a, T b) compare,
|
||||
required FutureOr<bool> Function(T a, T b) both,
|
||||
required FutureOr<void> Function(T a) onlyFirst,
|
||||
required FutureOr<void> Function(T b) onlySecond,
|
||||
}) async {
|
||||
first.sort(compare);
|
||||
first.uniqueConsecutive(compare);
|
||||
second.sort(compare);
|
||||
second.uniqueConsecutive(compare);
|
||||
|
||||
bool diff = false;
|
||||
int i = 0, j = 0;
|
||||
|
||||
for (; i < first.length && j < second.length;) {
|
||||
final int order = compare(first[i], second[j]);
|
||||
if (order == 0) {
|
||||
diff |= both(first[i++], second[j++]);
|
||||
diff |= await both(first[i++], second[j++]);
|
||||
} else if (order < 0) {
|
||||
onlyFirst(first[i++]);
|
||||
await onlyFirst(first[i++]);
|
||||
diff = true;
|
||||
} else if (order > 0) {
|
||||
onlySecond(second[j++]);
|
||||
await onlySecond(second[j++]);
|
||||
diff = true;
|
||||
}
|
||||
}
|
||||
@@ -50,3 +57,19 @@ class CollectionUtil {
|
||||
return diff;
|
||||
}
|
||||
}
|
||||
|
||||
extension _ListExtension<T> on List<T> {
|
||||
List<T> uniqueConsecutive(int Function(T a, T b) compare) {
|
||||
int i = 1, j = 1;
|
||||
for (; i < length; i++) {
|
||||
if (compare(this[i - 1], this[i]) != 0) {
|
||||
if (i != j) {
|
||||
this[j] = this[i];
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
length = length == 0 ? 0 : j;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user