Skip to content

Commit

Permalink
Enable changing display type of weights (factor or percentage) (#1712)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-Sander authored Aug 21, 2024
1 parent c9c0aac commit abb86c1
Show file tree
Hide file tree
Showing 28 changed files with 490 additions and 97 deletions.
10 changes: 10 additions & 0 deletions app/lib/grades/grades_service/grades_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class TermRef {
void removeGradeTypeWeight(GradeTypeId gradeType) {
_service.removeGradeTypeWeightForTerm(termId: id, gradeType: gradeType);
}

void changeWeightDisplayType(WeightDisplayType weightDisplayType) {
_service.changeWeightDisplayTypeForTerm(
termId: id, weightDisplayType: weightDisplayType);
}
}

class TermSubjectRef {
Expand Down Expand Up @@ -552,6 +557,7 @@ class TermResult extends Equatable {
final String name;
final GradeType finalGradeType;
final IMap<GradeTypeId, Weight> gradeTypeWeightings;
final WeightDisplayType weightDisplayType;

SubjectResult subject(SubjectId id) {
final subject = subjects.firstWhere((element) => element.id == id);
Expand All @@ -567,6 +573,7 @@ class TermResult extends Equatable {
required this.isActiveTerm,
required this.finalGradeType,
required this.gradeTypeWeightings,
required this.weightDisplayType,
});

@override
Expand All @@ -579,6 +586,7 @@ class TermResult extends Equatable {
name,
finalGradeType,
gradeTypeWeightings,
weightDisplayType,
];
}

Expand Down Expand Up @@ -663,6 +671,8 @@ extension on GradeInput {
}
}

enum WeightDisplayType { percent, factor }

enum WeightType {
perGrade,
perGradeType,
Expand Down
8 changes: 8 additions & 0 deletions app/lib/grades/grades_service/src/grades_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ class FirestoreGradesStateRepository extends GradesStateRepository {
createdOn: dto.createdOn?.toDate(),
gradingSystem: dto.gradingSystem.toGradingSystemModel(),
isActiveTerm: data['currentTerm'] == dto.id,
weightDisplayType: dto.weightDisplayType,
subjects: termSubjects
.where((subject) => subject.termId.value == dto.id)
.toIList(),
Expand Down Expand Up @@ -436,6 +437,7 @@ class TermDto {
final Timestamp? createdOn;
final GradingSystem gradingSystem;
final Map<_SubjectId, WeightDto> subjectWeights;
final WeightDisplayType weightDisplayType;
final Map<_GradeTypeId, WeightDto> gradeTypeWeights;
final List<TermSubjectDto> subjects;
final _GradeTypeId finalGradeTypeId;
Expand All @@ -446,6 +448,7 @@ class TermDto {
required this.createdOn,
required this.gradingSystem,
required this.subjectWeights,
required this.weightDisplayType,
required this.gradeTypeWeights,
required this.finalGradeTypeId,
required this.subjects,
Expand All @@ -459,6 +462,7 @@ class TermDto {
finalGradeTypeId: term.finalGradeType.value,
gradingSystem: term.gradingSystem.spec.gradingSystem,
subjects: term.subjects.map(TermSubjectDto.fromSubject).toList(),
weightDisplayType: term.weightDisplayType,
subjectWeights: Map.fromEntries(term.subjects.map((subject) =>
MapEntry(subject.id.value, subject.weightingForTermGrade.toDto()))),
gradeTypeWeights: term.gradeTypeWeightings
Expand All @@ -475,6 +479,9 @@ class TermDto {
gradingSystem:
GradingSystem.values.byName(data['gradingSystem'] as String),
subjectWeights: data['subjectWeights'].toWeightsDtoMap(),
weightDisplayType: data['weightDisplayType'] is String
? WeightDisplayType.values.byName(data['weightDisplayType'] as String)
: WeightDisplayType.factor,
gradeTypeWeights: data['gradeTypeWeights'].toWeightsDtoMap(),
subjects: (data['subjects'] as Map)
.mapTo((_, sub) => TermSubjectDto.fromData(sub))
Expand All @@ -491,6 +498,7 @@ class TermDto {
if (createdOn == null) 'createdOn': FieldValue.serverTimestamp(),
'gradingSystem': gradingSystem.name,
'subjectWeights': subjectWeights.toWeightDataMap(),
'weightDisplayType': weightDisplayType.name,
'gradeTypeWeights': gradeTypeWeights.toWeightDataMap(),
'subjects': subjects
.map((subject) => MapEntry(subject.id, subject.toData()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class _GradesServiceInternal {
? term.gradingSystem.toGradeResult(term.tryGetTermGrade()!)
: null,
gradeTypeWeightings: term.gradeTypeWeightings,
weightDisplayType: term.weightDisplayType,
subjects: term.subjects
.map(
(subject) => SubjectResult(
Expand Down Expand Up @@ -330,6 +331,12 @@ class _GradesServiceInternal {
_updateTerm(newTerm);
}

void changeWeightDisplayTypeForTerm(
{required TermId termId, required WeightDisplayType weightDisplayType}) {
final newTerm = _term(termId).changeWeightDisplayType(weightDisplayType);
_updateTerm(newTerm);
}

void changeGradeTypeWeightForTerm(
{required TermId termId,
required GradeTypeId gradeType,
Expand Down
10 changes: 10 additions & 0 deletions app/lib/grades/grades_service/src/term.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class TermModel extends Equatable {
final GradeTypeId finalGradeType;
final bool isActiveTerm;
final String name;
final WeightDisplayType weightDisplayType;

@override
List<Object?> get props => [
id,
createdOn,
subjects,
gradeTypeWeightings,
weightDisplayType,
gradingSystem,
finalGradeType,
isActiveTerm,
Expand All @@ -42,12 +44,14 @@ class TermModel extends Equatable {
this.createdOn,
this.subjects = const IListConst([]),
this.gradeTypeWeightings = const IMapConst({}),
this.weightDisplayType = WeightDisplayType.factor,
});

const TermModel.internal(
this.id,
this.subjects,
this.gradeTypeWeightings,
this.weightDisplayType,
this.finalGradeType,
this.isActiveTerm,
this.name,
Expand Down Expand Up @@ -93,6 +97,7 @@ class TermModel extends Equatable {
TermId? id,
IList<SubjectModel>? subjects,
IMap<GradeTypeId, Weight>? gradeTypeWeightings,
WeightDisplayType? weightDisplayType,
GradeTypeId? finalGradeType,
bool? isActiveTerm,
String? name,
Expand All @@ -103,6 +108,7 @@ class TermModel extends Equatable {
id ?? this.id,
subjects ?? this.subjects,
gradeTypeWeightings ?? this.gradeTypeWeightings,
weightDisplayType ?? this.weightDisplayType,
finalGradeType ?? this.finalGradeType,
isActiveTerm ?? this.isActiveTerm,
name ?? this.name,
Expand Down Expand Up @@ -322,6 +328,10 @@ class TermModel extends Equatable {
bool containsGrade(GradeId id) {
return subjects.any((s) => s.hasGrade(id));
}

TermModel changeWeightDisplayType(WeightDisplayType weightDisplayType) {
return _copyWith(weightDisplayType: weightDisplayType);
}
}

class SubjectModel extends Equatable {
Expand Down
Loading

0 comments on commit abb86c1

Please sign in to comment.