filter method

List<MoodleCourse> filter({
  1. bool? enabled,
  2. String? name,
  3. String? shortname,
  4. int? id,
  5. List<int> ids = const <int>[],
})

Filters the courses based on the given properties.

All properties are optional and will be ignored if not provided.

Implementation

List<MoodleCourse> filter({bool? enabled, String? name, String? shortname, int? id, List<int> ids = const <int>[]}) {
  if (!state.hasData) {
    log('Cannot filter courses: No data available.');

    return [];
  }

  final courses = state.requireData;

  return courses.where((element) {
    if (id != null && element.id != id) return false;
    if (enabled != null && element.enabled != enabled) return false;
    if (name != null && !element.name.containsIgnoreCase(name)) return false;
    if (shortname != null && !element.shortname.containsIgnoreCase(shortname)) return false;
    if (ids.isNotEmpty && !ids.contains(element.id)) return false;

    return true;
  }).toList();
}