Skip to content

Commit

Permalink
Merge pull request #55 from PAException/bug/substitute-settings
Browse files Browse the repository at this point in the history
Fixed substitute settings bug - same classes multiple times
  • Loading branch information
PAException authored Apr 23, 2024
2 parents c6f99e6 + 6623115 commit 2decea3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@ import 'package:engelsburg_planer/src/backend/database/state/user_state.dart';
import 'package:engelsburg_planer/src/utils/global_context.dart';
import 'package:provider/provider.dart';

/// Stores the settings of the substitutes (filter and password).
class SubstituteSettings {

/// The password to access the substitutes
String? password;

/// Whether to filter by classes or not
bool byClasses;
List<String> classes;

/// The classes to filter by if [byClasses] is true
Set<String> classes;

/// Whether to filter by teachers or not
bool byTeacher;
List<String> teacher;

/// The teachers to filter by if [byTeacher] is true
Set<String> teacher;

//TODO
/// Whether to filter by timetable or not
bool byTimetable;

SubstituteSettings({
Expand All @@ -35,9 +46,9 @@ class SubstituteSettings {
factory SubstituteSettings.empty() {
var config = globalContext().read<AppConfigState>();
bool byClasses = false;
List<String> classes = [];
Set<String> classes = {};
bool byTeacher = false;
List<String> teacher = [];
Set<String> teacher = {};
bool byTimetable = false;

if (config.userType == UserType.student) {
Expand Down Expand Up @@ -98,23 +109,26 @@ class SubstituteSettings {
const DocumentReference<SubstituteSettings>(
"substitute_settings", SubstituteSettings.fromJson);

/// Converts serialized substitute settings to a [SubstituteSettings] object.
/// If the JSON is empty, an empty [SubstituteSettings] object is created
/// with default settings.
factory SubstituteSettings.fromJson(Map<String, dynamic> json) => json.isEmpty
? SubstituteSettings.empty()
: SubstituteSettings(
password: json["password"],
byClasses: json["byClasses"] ?? false,
classes: List.of(json["classes"] ?? []).cast<String>().toList(),
classes: Set.of(json["classes"] ?? []).cast<String>().toSet(),
byTeacher: json["byTeacher"] ?? false,
teacher: List.of(json["teacher"] ?? []).cast<String>().toList(),
teacher: Set.of(json["teacher"] ?? []).cast<String>().toSet(),
byTimetable: json["byTimetable"] ?? false,
);

Map<String, dynamic> toJson() => {
"password": password,
"byClasses": byClasses,
"classes": classes,
"classes": classes.toList(),
"byTeacher": byTeacher,
"teacher": teacher,
"teacher": teacher.toList(),
"byTimetable": byTimetable,
};
}
6 changes: 2 additions & 4 deletions lib/src/view/pages/substitute/content/substitute_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ class _SubstituteTabState extends State<SubstituteTab>
itemBuilder: (context, doc, substituteSettings) {
var request = getSubstitutes(
substituteSettings.password!,
classes:
substituteSettings.byClasses ? substituteSettings.classes : [],
teacher:
substituteSettings.byTeacher ? substituteSettings.teacher : [],
classes: substituteSettings.byClasses ? substituteSettings.classes.toList() : [],
teacher: substituteSettings.byTeacher ? substituteSettings.teacher.toList() : [],
).build();

_executedRequest ??= request.api((json) => Substitutes.fromJson(json));
Expand Down

0 comments on commit 2decea3

Please sign in to comment.