Skip to content

Commit

Permalink
Merge pull request #132 from Solvro/feat/sci-clubs-new-sorting
Browse files Browse the repository at this point in the history
Feat/ new sci clubs sorting rules
  • Loading branch information
simon-the-shark authored Aug 4, 2024
2 parents 30f49e2 + 91e474c commit 1b1cd52
Showing 1 changed file with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "package:collection/collection.dart";
import "package:riverpod_annotation/riverpod_annotation.dart";

import "../../../../../api_base/watch_query_adapter.dart";
Expand All @@ -7,7 +8,6 @@ import "getScienceClubs.graphql.dart";
part "science_clubs_repository.g.dart";

typedef ScienceClub = Query$GetScienceClubs$Scientific_Circles;
// just alias for shorter type name

@riverpod
Stream<List<ScienceClub?>?> scienceClubsRepository(
Expand All @@ -17,20 +17,37 @@ Stream<List<ScienceClub?>?> scienceClubsRepository(
WatchOptions$Query$GetScienceClubs(eagerlyFetchResults: true),
TtlKey.scienceClubsRepository,
);
yield* stream.map(
(event) => event?.Scientific_Circles.sortBySourceTypes().toList(),
);
yield* stream.map((event) => event?.Scientific_Circles.sortBySourceTypes());
}

extension IsSolvroX on ScienceClub {
bool get isSolvro => name.contains("Solvro");
}

extension SortBySourceTypeX on Iterable<ScienceClub> {
Iterable<ScienceClub> _filter(String source) {
return where((element) => element.source == source);
Iterable<ScienceClub> _filterByType(
String source, {
bool includeSolvro = false,
}) {
return where(
(element) =>
element.source == source && (includeSolvro || !element.isSolvro),
);
}

Iterable<ScienceClub> sortBySourceTypes() {
final p0 = _filter("manual_entry");
final p1 = _filter("aktywni_website");
final p2 = _filter("student_department");
return p0.followedBy(p1).followedBy(p2).toList();
List<ScienceClub> sortBySourceTypes() {
final solvro = firstWhereOrNull((element) => element.isSolvro);
final manualSource = _filterByType("manual_entry").toList()..shuffle();
final activeWebSource = _filterByType("aktywni_website").toList()
..shuffle();
final studentDepartmentSource = _filterByType("student_department").toList()
..shuffle();

return [
if (solvro != null) solvro,
...manualSource,
...activeWebSource,
...studentDepartmentSource,
];
}
}

0 comments on commit 1b1cd52

Please sign in to comment.