Skip to content

Commit

Permalink
Merge branch 'main' into new_docs_nextra
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-Sander authored Aug 23, 2024
2 parents be55416 + 85b9317 commit 96ab739
Show file tree
Hide file tree
Showing 28 changed files with 723 additions and 201 deletions.
23 changes: 20 additions & 3 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 Expand Up @@ -742,11 +752,11 @@ class ConnectedCourse extends Equatable {
});
}

class Weight extends Equatable {
class Weight {
final num asFactor;
num get asPercentage => asFactor * 100;
@override
List<Object?> get props => [asFactor];

bool get isNegative => asFactor < 0;

const Weight.percent(num percent) : asFactor = percent / 100;
const Weight.factor(this.asFactor);
Expand All @@ -756,4 +766,11 @@ class Weight extends Equatable {
String toString() {
return 'Weight($asFactor / $asPercentage%)';
}

@override
bool operator ==(Object other) =>
identical(this, other) || other is Weight && other.asFactor == asFactor;

@override
int get hashCode => asFactor.hashCode;
}
31 changes: 24 additions & 7 deletions app/lib/grades/grades_service/src/grades_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ class FirestoreGradesStateRepository extends GradesStateRepository {
?.gradeComposition
.gradeWeights
.map((key, value) =>
MapEntry(key, value.toWeight()))[dto.id] ??
const Weight.factor(1),
MapEntry(key, value.toNonNegativeWeight()))[dto.id] ??
NonNegativeWeight.factor(1),
);
},
).toIList();
Expand Down Expand Up @@ -259,14 +259,16 @@ class FirestoreGradesStateRepository extends GradesStateRepository {
isFinalGradeTypeOverridden:
termSubject.finalGradeType != subTerm.finalGradeTypeId,
gradeTypeWeightings: termSubject.gradeComposition.gradeTypeWeights
.map((key, value) => MapEntry(GradeTypeId(key), value.toWeight()))
.map((key, value) =>
MapEntry(GradeTypeId(key), value.toNonNegativeWeight()))
.toIMap(),
gradeTypeWeightingsFromTerm: subTerm.gradeTypeWeights
.map((key, value) => MapEntry(GradeTypeId(key), value.toWeight()))
.map((key, value) =>
MapEntry(GradeTypeId(key), value.toNonNegativeWeight()))
.toIMap(),
weightingForTermGrade:
subTerm.subjectWeights[subject.id.value]?.toWeight() ??
const Weight.factor(1),
subTerm.subjectWeights[subject.id.value]?.toNonNegativeWeight() ??
NonNegativeWeight.factor(1),
grades: grades
.where((grade) =>
grade.subjectId == subject.id && grade.termId.value == termId)
Expand All @@ -287,14 +289,15 @@ 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(),
name: dto.displayName,
// Change both to num
gradeTypeWeightings: dto.gradeTypeWeights
.map((key, value) =>
MapEntry(GradeTypeId(key), value.toWeight()))
MapEntry(GradeTypeId(key), value.toNonNegativeWeight()))
.toIMap(),
),
)
Expand Down Expand Up @@ -384,6 +387,13 @@ class WeightDto {
);
}

NonNegativeWeight toNonNegativeWeight() {
return switch (type) {
_WeightNumberType.factor => NonNegativeWeight.factor(value),
_WeightNumberType.percent => NonNegativeWeight.percent(value),
};
}

Weight toWeight() {
return switch (type) {
_WeightNumberType.factor => Weight.factor(value),
Expand Down Expand Up @@ -436,6 +446,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 +457,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 +471,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 +488,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 +507,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
20 changes: 14 additions & 6 deletions app/lib/grades/grades_service/src/grades_service_internal.dart
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 @@ -215,7 +216,7 @@ class _GradesServiceInternal {
newTerm = newTerm.addSubject(subject);
}

newTerm = newTerm.changeWeighting(id, weight);
newTerm = newTerm.changeWeighting(id, weight.toNonNegativeWeightOrThrow());
_updateTerm(newTerm);
}

Expand All @@ -233,8 +234,8 @@ class _GradesServiceInternal {
required GradeTypeId gradeType,
required Weight weight,
}) {
final newTerm = _term(termId)
.changeWeightingOfGradeTypeInSubject(id, gradeType, weight);
final newTerm = _term(termId).changeWeightingOfGradeTypeInSubject(
id, gradeType, weight.toNonNegativeWeightOrThrow());
_updateTerm(newTerm);
}

Expand Down Expand Up @@ -325,17 +326,24 @@ class _GradesServiceInternal {
.subjects
.where((element) => element.grades.any((grade) => grade.id == id))
.first;
final newTerm = _term(termId).changeWeightOfGrade(id, subject.id, weight);
final newTerm = _term(termId).changeWeightOfGrade(
id, subject.id, weight.toNonNegativeWeightOrThrow());

_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,
required Weight weight}) {
final newTerm =
_term(termId).changeWeightingOfGradeType(gradeType, weight: weight);
final newTerm = _term(termId).changeWeightingOfGradeType(gradeType,
weight: weight.toNonNegativeWeightOrThrow());
_updateTerm(newTerm);
}

Expand Down
Loading

0 comments on commit 96ab739

Please sign in to comment.