Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy weights from term to subject for subjects settings page #1477

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:flutter/material.dart';

import 'package:sharezone/grades/grades_service/grades_service.dart';
import 'package:sharezone/grades/pages/subject_settings_page/subject_settings_page_view.dart';
import 'package:cloud_firestore_helper/cloud_firestore_helper.dart';

class SubjectSettingsPageController extends ChangeNotifier {
final GradesService gradesService;
Expand All @@ -35,7 +36,14 @@ class SubjectSettingsPageController extends ChangeNotifier {
_finalGradeTypeDisplayName = _getFinalGradeTypeDisplayName(finalGradeType);
_finalGradeTypeIcon = _getFinalGradeTypeIcon(finalGradeType);
_selectableGradeTypes = gradesService.getPossibleGradeTypes();
_weights = subject.gradeTypeWeights;

if (subject.weightType == WeightType.inheritFromTerm) {
// We show the weights from the term, but we need to copy them into the
// subject if the user changes them.
_weights = _getTerm()!.gradeTypeWeightings;
} else {
_weights = subject.gradeTypeWeights;
}

state = SubjectSettingsLoaded(view);
}
Expand Down Expand Up @@ -95,30 +103,65 @@ class SubjectSettingsPageController extends ChangeNotifier {
);
}

void setGradeWeight({
/// Copies the weights from the term to the subject if the subject is set to
/// inherit the weights from the term.
///
/// This method is called before changing the weights of a subject.
Future<void> _maybeCopyWeightsFromTerm() async {
if (_getSubject()!.weightType != WeightType.inheritFromTerm) {
// Subject has its own weights, no need to copy from term.
return;
}

// In the constructor, we already copied the weights from the term. But they
// were only copied into the state. Now we need to copy them into the
// database.
for (final gradeType in _weights.keys) {
gradesService.changeGradeTypeWeightForSubject(
id: subjectId,
termId: termId,
gradeType: gradeType,
weight: _weights[gradeType]!,
);
await waitForFirestoreWriteLimit();
}

gradesService.changeSubjectWeightTypeSettings(
id: subjectId,
termId: termId,
perGradeType: WeightType.perGradeType,
);
await waitForFirestoreWriteLimit();
}

Future<void> setGradeWeight({
required GradeTypeId gradeTypeId,
required Weight weight,
}) {
}) async {
await _maybeCopyWeightsFromTerm();

gradesService.changeGradeTypeWeightForSubject(
id: subjectId,
termId: termId,
gradeType: gradeTypeId,
weight: weight,
);
_selectableGradeTypes = gradesService.getPossibleGradeTypes();
_weights = _getSubject()!.gradeTypeWeights;
_weights = _weights.add(gradeTypeId, weight);
state = SubjectSettingsLoaded(view);
notifyListeners();
}

void removeGradeType(GradeTypeId gradeTypeId) {
Future<void> removeGradeType(GradeTypeId gradeTypeId) async {
await _maybeCopyWeightsFromTerm();

gradesService.removeGradeTypeWeightForSubject(
id: subjectId,
termId: termId,
gradeType: gradeTypeId,
);
_selectableGradeTypes = gradesService.getPossibleGradeTypes();
_weights = _getSubject()!.gradeTypeWeights;
_weights = _weights.remove(gradeTypeId);
state = SubjectSettingsLoaded(view);
notifyListeners();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class TermSettingsPageController extends ChangeNotifier {
gradeType: gradeTypeId,
weight: weight,
);
_weights = _getTerm()!.gradeTypeWeightings;
_weights = _weights.add(gradeTypeId, weight);
state = TermSettingsLoaded(view);
notifyListeners();
}
Expand All @@ -149,7 +149,7 @@ class TermSettingsPageController extends ChangeNotifier {
termId: termId,
gradeType: gradeTypeId,
);
_weights = _getTerm()!.gradeTypeWeightings;
_weights = _weights.remove(gradeTypeId);
state = TermSettingsLoaded(view);
notifyListeners();
}
Expand Down
13 changes: 13 additions & 0 deletions lib/cloud_firestore_helper/lib/src/cloud_firestore_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ DateTime? dateTimeFromTimestampOrNull(dynamic timestamp) {
final microsecondsSinceEpoch = timestamp.microsecondsSinceEpoch;
return DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch);
}

/// Firestore had a soft limit of 1 write per second per document. However,
/// this limit isn't mentioned in the documentation anymore. We still keep
/// the delay to be on the safe side.
///
/// See:
/// * https://stackoverflow.com/q/74454570
/// * https://firebase.google.com/docs/firestore/best-practices#updates_to_a_single_document
Future<void> waitForFirestoreWriteLimit({
Duration delay = const Duration(milliseconds: 200),
}) async {
await Future.delayed(delay);
}
Loading