Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add ListExtension and UserExtension for enhanced list and user functionalities

AmanRajSinghMourya3805cddebaee
diff --git a/mobile/apps/locker/lib/extensions/list.dart b/mobile/apps/locker/lib/extensions/list.dart
new file mode 100644
index 0000000000..a64d7d6140
--- /dev/null
+++ b/mobile/apps/locker/lib/extensions/list.dart
@@ -0,0 +1,38 @@
+extension ListExtension<E> on List<E> {
+ List<List<E>> chunks(int chunkSize) {
+ final List<List<E>> result = <List<E>>[];
+ for (var i = 0; i < length; i += chunkSize) {
+ result.add(
+ sublist(i, i + chunkSize > length ? length : i + chunkSize),
+ );
+ }
+ return result;
+ }
+
+ // splitMatch, based on the matchFunction, split the input list in two
+ // lists. result.matched contains items which matched and result.unmatched
+ // contains remaining items.
+ ListMatch<E> splitMatch(bool Function(E e) matchFunction) {
+ final listMatch = ListMatch<E>();
+ for (final element in this) {
+ if (matchFunction(element)) {
+ listMatch.matched.add(element);
+ } else {
+ listMatch.unmatched.add(element);
+ }
+ }
+ return listMatch;
+ }
+
+ Iterable<E> interleave(E separator) sync* {
+ for (int i = 0; i < length; i++) {
+ yield this[i];
+ if (i < length) yield separator;
+ }
+ }
+}
+
+class ListMatch<T> {
+ List<T> matched = <T>[];
+ List<T> unmatched = <T>[];
+}
diff --git a/mobile/apps/locker/lib/extensions/user_extension.dart b/mobile/apps/locker/lib/extensions/user_extension.dart
new file mode 100644
index 0000000000..0e0f037146
--- /dev/null
+++ b/mobile/apps/locker/lib/extensions/user_extension.dart
@@ -0,0 +1,12 @@
+import "package:locker/services/collections/models/user.dart";
+
+extension UserExtension on User {
+ //Some initial users have name in name field.
+ String? get displayName =>
+ // ignore: deprecated_member_use_from_same_package
+ ((name?.isEmpty ?? true) ? null : name);
+
+ String get nameOrEmail {
+ return email.substring(0, email.indexOf("@"));
+ }
+}