Skip to content

Commit

Permalink
Remove Validator from HomeworkDialogBloc. (#1120)
Browse files Browse the repository at this point in the history
I didn't feel like they provided that much value so I removed them.
  • Loading branch information
Jonas-Sander authored Oct 9, 2023
1 parent f74feaf commit d8b21f1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 83 deletions.
52 changes: 30 additions & 22 deletions app/lib/blocs/homework/homework_dialog_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'dart:async';

import 'package:analytics/analytics.dart';
import 'package:bloc_base/bloc_base.dart';
import 'package:files_basics/local_file.dart';
Expand All @@ -17,16 +19,14 @@ import 'package:sharezone/markdown/markdown_analytics.dart';
import 'package:sharezone/util/api.dart';
import 'package:sharezone/util/next_lesson_calculator/next_lesson_calculator.dart';
import 'package:sharezone_common/helper_functions.dart';
import 'package:sharezone_common/homework_validators.dart';
import 'package:sharezone_common/validators.dart';
import 'package:time/time.dart';
import 'package:user/user.dart';

extension on DateTime {
Time toTime() => Time(hour: hour, minute: minute);
}

class HomeworkDialogBloc extends BlocBase with HomeworkValidators {
class HomeworkDialogBloc extends BlocBase {
List<CloudFile> initialCloudFiles = [];
final _titleSubject = BehaviorSubject<String?>();
final _courseSegmentSubject = BehaviorSubject<Course>();
Expand Down Expand Up @@ -79,7 +79,8 @@ class HomeworkDialogBloc extends BlocBase with HomeworkValidators {
_localFilesSubject.valueOrNull != null &&
_localFilesSubject.valueOrNull!.isNotEmpty;

Stream<String> get title => _titleSubject.stream.transform(validateTitle);
Stream<String> get title =>
_titleSubject.stream.transform(_validateTitleTransformer);
Stream<DateTime> get todoUntil => _todoUntilSubject;
Stream<String> get description => _descriptionSubject;
Stream<Course> get courseSegment => _courseSegmentSubject;
Expand Down Expand Up @@ -199,26 +200,31 @@ class HomeworkDialogBloc extends BlocBase with HomeworkValidators {
});
}

final _validateTitleTransformer =
StreamTransformer<String?, String>.fromHandlers(
handleData: (title, sink) {
if (isEmptyOrNull(title)) {
sink.addError(EmptyTitleException());
} else {
sink.add(title!);
}
});

void validateInputOrThrow() {
final validatorTitle = NotEmptyOrNullValidator(_titleSubject.valueOrNull);
if (!validatorTitle.isValid()) {
_titleSubject.addError(
TextValidationException(HomeworkValidators.emptyTitleUserMessage));
throw InvalidTitleException();
bool isNullOrEmpty(String? s) => s == null || s.isEmpty;

// This should be the same behavior as for [_validateTitleTransformer]
// above.
if (isNullOrEmpty(_titleSubject.valueOrNull)) {
throw EmptyTitleException();
}

final validatorCourse = NotNullValidator(_courseSegmentSubject.valueOrNull);
if (!validatorCourse.isValid()) {
_courseSegmentSubject.addError(
TextValidationException(HomeworkValidators.emptyCourseUserMessage));
throw InvalidCourseException();
if (_courseSegmentSubject.valueOrNull == null) {
throw EmptyCourseException();
}

final validatorTodoUntil = NotNullValidator(_todoUntilSubject.valueOrNull);
if (!validatorTodoUntil.isValid()) {
_todoUntilSubject.addError(
TextValidationException(HomeworkValidators.emptyDueDateUserMessage));
throw InvalidTodoUntilException();
if (_todoUntilSubject.valueOrNull == null) {
throw EmptyTodoUntilException();
}
}

Expand Down Expand Up @@ -303,11 +309,13 @@ class HomeworkDialogBloc extends BlocBase with HomeworkValidators {
}
}

class InvalidTitleException implements Exception {}
sealed class InvalidHomeworkInputException implements Exception {}

class EmptyTitleException implements InvalidHomeworkInputException {}

class InvalidCourseException implements Exception {}
class EmptyCourseException implements InvalidHomeworkInputException {}

class InvalidTodoUntilException implements Exception {}
class EmptyTodoUntilException implements InvalidHomeworkInputException {}

class UserInput {
final String? title, description;
Expand Down
40 changes: 21 additions & 19 deletions app/lib/pages/homework/homework_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import 'package:sharezone/timetable/src/edit_time.dart';
import 'package:sharezone/util/next_lesson_calculator/next_lesson_calculator.dart';
import 'package:sharezone/widgets/material/list_tile_with_description.dart';
import 'package:sharezone/widgets/material/save_button.dart';
import 'package:sharezone_common/homework_validators.dart';
import 'package:sharezone_common/validators.dart';
import 'package:sharezone_utils/platform.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';
import 'package:time/time.dart';
Expand Down Expand Up @@ -182,6 +180,15 @@ class _MobileDivider extends StatelessWidget {
}
}

class _ErrorStrings {
static const String emptyTitle =
"Bitte gib einen Titel für die Hausaufgabe an!";
static const String emptyCourse =
"Bitte gib einen Kurs für die Hausaufgabe an!";
static const String emptyTodoUntil =
"Bitte gib ein Fälligkeitsdatum für die Hausaufgabe an!";
}

class _SaveButton extends StatelessWidget {
const _SaveButton({Key? key, this.editMode = false}) : super(key: key);

Expand All @@ -197,23 +204,13 @@ class _SaveButton extends StatelessWidget {
if (!context.mounted) return;
hideSendDataToFrankfurtSnackBar(context);
Navigator.pop(context, true);
} on InvalidTitleException {
showSnackSec(
text: TextValidationException(HomeworkValidators.emptyTitleUserMessage)
.toString(),
context: context,
);
} on InvalidCourseException {
} on InvalidHomeworkInputException catch (e) {
showSnackSec(
text: TextValidationException(HomeworkValidators.emptyCourseUserMessage)
.toString(),
context: context,
);
} on InvalidTodoUntilException {
showSnackSec(
text:
TextValidationException(HomeworkValidators.emptyDueDateUserMessage)
.toString(),
text: switch (e) {
EmptyTitleException() => _ErrorStrings.emptyTitle,
EmptyCourseException() => _ErrorStrings.emptyCourse,
EmptyTodoUntilException() => _ErrorStrings.emptyTodoUntil
},
context: context,
);
} on Exception catch (e) {
Expand Down Expand Up @@ -338,11 +335,16 @@ class _TitleField extends StatelessWidget {
child: StreamBuilder<String>(
stream: bloc.title,
builder: (context, snapshot) {
final errorText = switch (snapshot.error) {
EmptyTitleException => _ErrorStrings.emptyTitle,
_ => snapshot.error?.toString(),
};

return _TitleFieldBase(
prefilledTitle: prefilledTitle,
focusNode: focusNode,
onChanged: bloc.changeTitle,
errorText: snapshot.error?.toString(),
errorText: errorText,
);
}),
);
Expand Down
11 changes: 0 additions & 11 deletions lib/sharezone_common/lib/homework_validators.dart

This file was deleted.

31 changes: 0 additions & 31 deletions lib/sharezone_common/lib/src/validators/homework_validators.dart

This file was deleted.

0 comments on commit d8b21f1

Please sign in to comment.