more refactors and logs page handling

This commit is contained in:
shenlong-tanwen
2024-10-23 02:30:46 +05:30
parent 8f47645cdb
commit a0afea04d8
90 changed files with 2386 additions and 584 deletions
+20 -22
View File
@@ -2,7 +2,7 @@
import 'dart:async';
class CollectionUtil {
abstract final class CollectionUtil {
const CollectionUtil();
static int compareToNullable<T extends Comparable>(T? a, T? b) {
@@ -18,22 +18,22 @@ class CollectionUtil {
/// Find the difference between the two lists [first] and [second]
/// Results are passed as callbacks back to the caller during the comparison
static FutureOr<bool> diffLists<T>(
List<T> first,
List<T> second, {
List<T> firstList,
List<T> secondList, {
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);
final first =
_uniqueConsecutive(List.of(firstList)..sort(compare), compare);
final second =
_uniqueConsecutive(List.of(secondList)..sort(compare), compare);
bool diff = false;
int i = 0, j = 0;
for (; i < first.length && j < second.length;) {
while (i < first.length && j < second.length) {
final int order = compare(first[i], second[j]);
if (order == 0) {
diff |= await both(first[i++], second[j++]);
@@ -49,27 +49,25 @@ class CollectionUtil {
diff |= i < first.length || j < second.length;
for (; i < first.length; i++) {
onlyFirst(first[i]);
await onlyFirst(first[i]);
}
for (; j < second.length; j++) {
onlySecond(second[j]);
await onlySecond(second[j]);
}
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++;
}
List<T> _uniqueConsecutive<T>(List<T> list, int Function(T a, T b) compare) {
if (list.isEmpty) return list;
List<T> unique = [];
unique.add(list.first);
for (int i = 1; i < list.length; i++) {
if (compare(list[i], list[i - 1]) != 0) {
unique.add(list[i]);
}
length = length == 0 ? 0 : j;
return this;
}
return unique;
}