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

Continue refactoring homework dialog. #1113

Merged
merged 17 commits into from
Oct 5, 2023
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
52 changes: 29 additions & 23 deletions app/lib/blocs/homework/homework_dialog_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,8 @@ class HomeworkDialogBloc extends BlocBase with HomeworkValidators {

Future<void> _loadInitialCloudFiles(
String courseID, String homeworkID) async {
List<CloudFile> cloudFiles = await api.api.fileSharing.cloudFilesGateway
.filesStreamAttachment(courseID, homeworkID)
.first;
final cloudFiles =
await api.loadCloudFiles(courseId: courseID, homeworkId: homeworkID);
_cloudFilesSubject.sink.add(cloudFiles);
initialCloudFiles.addAll(cloudFiles);
}
Expand Down Expand Up @@ -337,23 +336,30 @@ class UserInput {
}

class HomeworkDialogApi {
final SharezoneGateway api;
final SharezoneGateway _api;

HomeworkDialogApi(this._api);

HomeworkDialogApi(this.api);
Future<List<CloudFile>> loadCloudFiles(
{required String courseId, required String homeworkId}) {
return _api.fileSharing.cloudFilesGateway
.filesStreamAttachment(courseId, homeworkId)
.first;
}

Future<HomeworkDto> create(UserInput userInput) async {
final localFiles = userInput.localFiles;
final course = userInput.course!;
final authorReference = api.references.users.doc(api.user.authUser!.uid);
final authorName = (await api.user.userStream.first)!.name;
final authorID = api.user.authUser!.uid;
final typeOfUser = (await api.user.userStream.first)!.typeOfUser;
final authorReference = _api.references.users.doc(_api.user.authUser!.uid);
final authorName = (await _api.user.userStream.first)!.name;
final authorID = _api.user.authUser!.uid;
final typeOfUser = (await _api.user.userStream.first)!.typeOfUser;

final attachments = await api.fileSharing.uploadAttachments(
final attachments = await _api.fileSharing.uploadAttachments(
localFiles, userInput.course!.id, authorReference.id, authorName);

final homework = HomeworkDto.create(
courseReference: api.references.getCourseReference(course.id),
courseReference: _api.references.getCourseReference(course.id),
courseID: course.id)
.copyWith(
subject: course.subject,
Expand All @@ -379,11 +385,11 @@ class HomeworkDialogApi {
);

if (userInput.private!) {
await api.homework.addPrivateHomework(homework, false,
attachments: attachments, fileSharingGateway: api.fileSharing);
await _api.homework.addPrivateHomework(homework, false,
attachments: attachments, fileSharingGateway: _api.fileSharing);
} else {
await api.homework.addHomeworkToCourse(homework,
attachments: attachments, fileSharingGateway: api.fileSharing);
await _api.homework.addHomeworkToCourse(homework,
attachments: attachments, fileSharingGateway: _api.fileSharing);
}

// If the homework will be added to the create, the wrong homework object will be return (the new forUsers map is missing)
Expand All @@ -393,17 +399,17 @@ class HomeworkDialogApi {
Future<HomeworkDto> edit(HomeworkDto oldHomework, UserInput userInput,
{List<CloudFile> removedCloudFiles = const []}) async {
List<String> attachments = oldHomework.attachments.toList();
final editorName = (await api.user.userStream.first)!.name;
final editorID = api.user.authUser!.uid;
final editorName = (await _api.user.userStream.first)!.name;
final editorID = _api.user.authUser!.uid;

for (int i = 0; i < removedCloudFiles.length; i++) {
attachments.remove(removedCloudFiles[i].id);
api.fileSharing.removeReferenceData(removedCloudFiles[i].id!,
_api.fileSharing.removeReferenceData(removedCloudFiles[i].id!,
ReferenceData(type: ReferenceType.blackboard, id: oldHomework.id));
}

final localFiles = userInput.localFiles;
final newAttachments = await api.fileSharing.uploadAttachments(
final newAttachments = await _api.fileSharing.uploadAttachments(
localFiles,
oldHomework.courseReference!.id,
editorID,
Expand All @@ -423,11 +429,11 @@ class HomeworkDialogApi {

final hasAttachments = attachments.isNotEmpty;
if (hasAttachments) {
await api.homework.addPrivateHomework(homework, true,
attachments: newAttachments, fileSharingGateway: api.fileSharing);
await _api.homework.addPrivateHomework(homework, true,
attachments: newAttachments, fileSharingGateway: _api.fileSharing);
} else {
api.homework.addPrivateHomework(homework, true,
attachments: newAttachments, fileSharingGateway: api.fileSharing);
_api.homework.addPrivateHomework(homework, true,
attachments: newAttachments, fileSharingGateway: _api.fileSharing);
}
return homework;
}
Expand Down
24 changes: 24 additions & 0 deletions app/lib/pages/homework/homework_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ class _HomeworkDialogState extends State<HomeworkDialog> {
}
}

class HwDialogKeys {
static const Key titleTextField = Key("title-field");
static const Key courseTile = Key("course-tile");
static const Key todoUntilTile = Key("todo-until-tile");
static const Key submissionTile = Key("submission-tile");
static const Key submissionTimeTile = Key("submission-time-tile");
static const Key descriptionField = Key("description-field");
static const Key addAttachmentTile = Key("add-attachment-tile");
static const Key notifyCourseMembersTile = Key("notify-course-members-tile");
static const Key isPrivateTile = Key("is-private-tile");
static const Key saveButton = Key("save-button");
}

class __HomeworkDialog extends StatefulWidget {
const __HomeworkDialog({Key? key, this.homework, this.bloc})
: super(key: key);
Expand Down Expand Up @@ -274,6 +287,7 @@ class _TodoUntilPicker extends StatelessWidget {
color: snapshot.hasError ? Colors.red : null,
),
child: DatePicker(
key: HwDialogKeys.todoUntilTile,
padding: const EdgeInsets.all(12),
selectedDate: snapshot.data,
selectDate: bloc.changeTodoUntil,
Expand Down Expand Up @@ -326,6 +340,7 @@ class _AppBar extends StatelessWidget {
tooltip: "Schließen",
),
_SaveButton(
key: HwDialogKeys.saveButton,
oldHomework: oldHomework,
editMode: editMode,
),
Expand Down Expand Up @@ -367,6 +382,7 @@ class _TitleField extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
PrefilledTextField(
key: HwDialogKeys.titleTextField,
prefilledText: prefilledTitle,
focusNode: focusNode,
cursorColor: Colors.white,
Expand Down Expand Up @@ -406,6 +422,7 @@ class _CourseTile extends StatelessWidget {
top: false,
bottom: false,
child: CourseTile(
key: HwDialogKeys.courseTile,
courseStream: bloc.courseSegment,
onChanged: bloc.changeCourseSegment,
editMode: editMode,
Expand All @@ -432,6 +449,7 @@ class _SendNotification extends StatelessWidget {
builder: (context, snapshot) {
final sendNotification = snapshot.data ?? false;
return ListTileWithDescription(
key: HwDialogKeys.notifyCourseMembersTile,
leading: const Icon(Icons.notifications_active),
title: Text(
"Kursmitglieder ${editMode ? "über die Änderungen " : ""}benachrichtigen"),
Expand Down Expand Up @@ -472,6 +490,7 @@ class _DescriptionField extends StatelessWidget {
ListTile(
leading: const Icon(Icons.subject),
title: PrefilledTextField(
key: HwDialogKeys.descriptionField,
prefilledText: oldDescription,
maxLines: null,
scrollPadding: const EdgeInsets.all(16.0),
Expand Down Expand Up @@ -505,6 +524,7 @@ class _AttachFile extends StatelessWidget {
top: false,
bottom: false,
child: AttachFile(
key: HwDialogKeys.addAttachmentTile,
addLocalFileToBlocMethod: (localFile) => bloc.addLocalFile(localFile),
removeLocalFileFromBlocMethod: (localFile) =>
bloc.removeLocalFile(localFile),
Expand Down Expand Up @@ -552,6 +572,7 @@ class _SubmissionsSwitch extends StatelessWidget {
final time = snapshot.data?.submissionTime;

return _SubmissionsSwitchBase(
key: HwDialogKeys.submissionTile,
isWidgetEnabled: isEnabled,
submissionsEnabled: withSubmissions,
onChanged: (newVal) => bloc.changeWithSubmissions(newVal),
Expand Down Expand Up @@ -597,6 +618,7 @@ class _SubmissionsSwitchBase extends StatelessWidget {
duration: const Duration(milliseconds: 200),
child: submissionsEnabled
? ListTile(
key: HwDialogKeys.submissionTimeTile,
title: const Text("Abgabe-Uhrzeit"),
onTap: () async {
await hideKeyboardWithDelay(context: context);
Expand Down Expand Up @@ -642,6 +664,7 @@ class _PrivateHomeworkSwitch extends StatelessWidget {
stream: bloc.private,
builder: (context, snapshot) {
return _PrivateHomeworkSwitchBase(
key: HwDialogKeys.isPrivateTile,
isPrivate: snapshot.data ?? false,
onChanged: editMode ? null : bloc.changePrivate,
);
Expand All @@ -656,6 +679,7 @@ class _PrivateHomeworkSwitchBase extends StatelessWidget {
const _PrivateHomeworkSwitchBase({
required this.isPrivate,
this.onChanged,
super.key,
});

final bool isPrivate;
Expand Down
Loading