diff --git a/app/lib/homework/student/src/homework_bottom_action_bar.dart b/app/lib/homework/student/src/homework_bottom_action_bar.dart index 17e32e134..d1bdcfa41 100644 --- a/app/lib/homework/student/src/homework_bottom_action_bar.dart +++ b/app/lib/homework/student/src/homework_bottom_action_bar.dart @@ -166,7 +166,7 @@ class _SortButton extends StatelessWidget { final bloc = BlocProvider.of(context); return StreamBuilder( - stream: bloc.whereType(), + stream: bloc.stream.whereType(), builder: (context, snapshot) { final currentSort = snapshot?.data?.open?.sorting; return Padding( diff --git a/app/lib/homework/student/student_homework_page.dart b/app/lib/homework/student/student_homework_page.dart index 65874ef4e..ced440104 100644 --- a/app/lib/homework/student/student_homework_page.dart +++ b/app/lib/homework/student/student_homework_page.dart @@ -88,8 +88,8 @@ class StudentHomeworkBody extends StatelessWidget { final bloc = BlocProvider.of(context); bloc.add(LoadHomeworks()); return StreamBuilder( - stream: bloc, - initialData: Uninitialized(), + stream: bloc.stream, + initialData: bloc.state, builder: (context, snapshot) { final state = snapshot.hasData ? snapshot.data : Uninitialized(); diff --git a/app/lib/homework/teacher/src/teacher_homework_bottom_action_bar.dart b/app/lib/homework/teacher/src/teacher_homework_bottom_action_bar.dart index 756a781ad..9387f39ca 100644 --- a/app/lib/homework/teacher/src/teacher_homework_bottom_action_bar.dart +++ b/app/lib/homework/teacher/src/teacher_homework_bottom_action_bar.dart @@ -67,7 +67,7 @@ class TeacherSortButton extends StatelessWidget { final bloc = BlocProvider.of(context); return StreamBuilder( - stream: bloc.whereType(), + stream: bloc.stream.whereType(), builder: (context, snapshot) { final currentSort = snapshot?.data?.open?.sorting; return Padding( diff --git a/app/lib/homework/teacher/teacher_homework_page.dart b/app/lib/homework/teacher/teacher_homework_page.dart index efd96130d..d5e1c0b38 100644 --- a/app/lib/homework/teacher/teacher_homework_page.dart +++ b/app/lib/homework/teacher/teacher_homework_page.dart @@ -90,7 +90,7 @@ class TeacherHomeworkBody extends StatelessWidget { final bloc = BlocProvider.of(context); bloc.add(LoadHomeworks()); return StreamBuilder( - stream: bloc, + stream: bloc.stream, initialData: Uninitialized(), builder: (context, snapshot) { final state = snapshot.hasData ? snapshot.data : Uninitialized(); diff --git a/app/pubspec.lock b/app/pubspec.lock index 0e2982d75..4de511a58 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -167,10 +167,10 @@ packages: dependency: transitive description: name: bloc - sha256: bc0e8756ffa2ba174a6094b2bd4527af8176dc0deb19addb538ad079a2bac75a + sha256: "3820f15f502372d979121de1f6b97bfcf1630ebff8fe1d52fb2b0bfa49be5b49" url: "https://pub.dev" source: hosted - version: "4.0.0" + version: "8.1.2" bloc_base: dependency: "direct main" description: diff --git a/app/test/homework/teacher/teacher_homework_page_widget_test.dart b/app/test/homework/teacher/teacher_homework_page_widget_test.dart index 4e94a43c1..8c0e3351a 100644 --- a/app/test/homework/teacher/teacher_homework_page_widget_test.dart +++ b/app/test/homework/teacher/teacher_homework_page_widget_test.dart @@ -6,7 +6,6 @@ // // SPDX-License-Identifier: EUPL-1.2 -import 'dart:async'; import 'dart:collection'; import 'package:analytics/analytics.dart'; @@ -34,6 +33,14 @@ class MockTeacherHomeworkPageBloc extends TeacherHomeworkPageBloc { final receivedEvents = []; + MockTeacherHomeworkPageBloc() : super() { + on((event, emit) { + if (_queuedStates.isNotEmpty) { + emit(_queuedStates.removeFirst()); + } + }); + } + void emitNewState(TeacherHomeworkPageState state) { _queuedStates.add(state); add(LoadHomeworks()); @@ -44,17 +51,6 @@ class MockTeacherHomeworkPageBloc extends TeacherHomeworkPageBloc { receivedEvents.add(event); super.onEvent(event); } - - @override - Stream mapEventToState( - TeacherHomeworkPageEvent event) async* { - if (_queuedStates.isNotEmpty) { - yield _queuedStates.removeFirst(); - } - } - - @override - TeacherHomeworkPageState initialState = Uninitialized(); } enum HomeworkTab { open, archived } @@ -303,6 +299,16 @@ void main() { hasLength(1), reason: "The UI should not ask the bloc for new homework when scrolling to the end of the homework list (as all homeworks have already been loaded)."); + + // In the teacher homework page bloc which is just a mock right now we use + // `await Future.delayed(const Duration(milliseconds: 1200))` to simulate + // the loading of homeworks. + // Since testWidgets uses fakeAsync interally and Future.delayed uses a + // Timer internally we need to pump (advance the fakeAsync time) so that + // this test doesn't fail because of this: + // The following assertion was thrown running a test: + // A Timer is still pending even after the widget tree was disposed. + tester.pump(Duration(seconds: 2)); }); testWidgets( @@ -420,6 +426,9 @@ void main() { await tester.pump(); expect(_finders.archivedHomeworkTab.noHomeworkPlaceholder, findsNothing); + + // See test further above for why we need to pump here. + tester.pump(Duration(seconds: 2)); }); }); } diff --git a/lib/abgabe/abgabe_client_lib/pubspec.lock b/lib/abgabe/abgabe_client_lib/pubspec.lock index 0a399e0ae..9b6270cdb 100644 --- a/lib/abgabe/abgabe_client_lib/pubspec.lock +++ b/lib/abgabe/abgabe_client_lib/pubspec.lock @@ -75,10 +75,10 @@ packages: dependency: transitive description: name: bloc - sha256: bc0e8756ffa2ba174a6094b2bd4527af8176dc0deb19addb538ad079a2bac75a + sha256: "3820f15f502372d979121de1f6b97bfcf1630ebff8fe1d52fb2b0bfa49be5b49" url: "https://pub.dev" source: hosted - version: "4.0.0" + version: "8.1.2" bloc_base: dependency: "direct main" description: diff --git a/lib/firebase_hausaufgabenheft_logik/pubspec.lock b/lib/firebase_hausaufgabenheft_logik/pubspec.lock index b50379926..cb3b606eb 100644 --- a/lib/firebase_hausaufgabenheft_logik/pubspec.lock +++ b/lib/firebase_hausaufgabenheft_logik/pubspec.lock @@ -44,10 +44,10 @@ packages: dependency: transitive description: name: bloc - sha256: bc0e8756ffa2ba174a6094b2bd4527af8176dc0deb19addb538ad079a2bac75a + sha256: "3820f15f502372d979121de1f6b97bfcf1630ebff8fe1d52fb2b0bfa49be5b49" url: "https://pub.dev" source: hosted - version: "4.0.0" + version: "8.1.2" bloc_base: dependency: transitive description: diff --git a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart index 570c922e3..57a985486 100644 --- a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart @@ -6,10 +6,69 @@ // // SPDX-License-Identifier: EUPL-1.2 +import 'dart:async'; + +import 'package:bloc/bloc.dart'; +import 'package:bloc_base/bloc_base.dart' as bloc_base; +import 'package:hausaufgabenheft_logik/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart' + as lazy_loading; +import 'package:hausaufgabenheft_logik/src/completed_homeworks/views/completed_homework_list_view_factory.dart'; +import 'package:rxdart/rxdart.dart'; + import 'events.dart'; import 'states.dart'; -abstract class CompletedHomeworksViewBloc - implements - Stream, - Sink {} +export 'events.dart'; +export 'states.dart'; + +class CompletedHomeworksViewBloc extends Bloc implements bloc_base.BlocBase { + final lazy_loading.LazyLoadingCompletedHomeworksBloc + _lazyLoadingCompletedHomeworksBloc; + final CompletedHomeworkListViewFactory _completedHomeworkListViewFactory; + final int nrOfInitialCompletedHomeworksToLoad; + StreamSubscription _streamSubscription; + Stream _lazyLoadingSuccessStates; + + CompletedHomeworksViewBloc(this._lazyLoadingCompletedHomeworksBloc, + this._completedHomeworkListViewFactory, + {this.nrOfInitialCompletedHomeworksToLoad = 8}) + : super(Loading()) { + _lazyLoadingSuccessStates = _lazyLoadingCompletedHomeworksBloc.stream + .whereType(); + + on((event, emit) { + _lazyLoadingCompletedHomeworksBloc.add( + lazy_loading.LoadCompletedHomeworks( + nrOfInitialCompletedHomeworksToLoad)); + + _streamSubscription = _lazyLoadingSuccessStates.listen((state) { + add(_Transform(state)); + }); + }); + on((event, emit) { + _lazyLoadingCompletedHomeworksBloc + .add(lazy_loading.AdvanceCompletedHomeworks(event.advanceBy)); + }); + on<_Transform>((event, emit) { + final success = event.successState; + final listView = _completedHomeworkListViewFactory.create( + success.homeworks, success.loadedAllHomeworks); + emit(Success(listView)); + }); + } + + @override + void dispose() { + _streamSubscription.cancel(); + } +} + +class _Transform extends CompletedHomeworksViewBlocEvent { + final lazy_loading.Success successState; + + _Transform(this.successState); + + @override + List get props => [successState]; +} diff --git a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc_impl.dart b/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc_impl.dart deleted file mode 100644 index 67675ce4f..000000000 --- a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc_impl.dart +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) -// Licensed under the EUPL-1.2-or-later. -// -// You may obtain a copy of the Licence at: -// https://joinup.ec.europa.eu/software/page/eupl -// -// SPDX-License-Identifier: EUPL-1.2 - -import 'dart:async'; - -import 'package:bloc/bloc.dart'; -import 'package:bloc_base/bloc_base.dart'; -import 'package:rxdart/rxdart.dart'; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/events.dart' - as lazy_loading; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart'; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/states.dart' - as lazy_loading; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/views/completed_homework_list_view_factory.dart'; - -import 'completed_homeworks_view_bloc.dart'; -import 'events.dart'; -import 'states.dart'; - -class CompletedHomeworksViewBlocImpl extends Bloc< - CompletedHomeworksViewBlocEvent, CompletedHomeworksViewBlocState> - implements CompletedHomeworksViewBloc, BlocBase { - final LazyLoadingCompletedHomeworksBloc _lazyLoadingCompletedHomeworksBloc; - final CompletedHomeworkListViewFactory _completedHomeworkListViewFactory; - final int nrOfInitialCompletedHomeworksToLoad; - StreamSubscription _streamSubscription; - Stream _lazyLoadingSuccessStates; - - CompletedHomeworksViewBlocImpl(this._lazyLoadingCompletedHomeworksBloc, - this._completedHomeworkListViewFactory, - {this.nrOfInitialCompletedHomeworksToLoad = 8}) { - _lazyLoadingSuccessStates = - _lazyLoadingCompletedHomeworksBloc.whereType(); - } - - @override - CompletedHomeworksViewBlocState get initialState => Loading(); - - @override - Stream mapEventToState( - CompletedHomeworksViewBlocEvent event) async* { - if (event is StartTransformingHomeworks) { - _lazyLoadingCompletedHomeworksBloc.add( - lazy_loading.LoadCompletedHomeworks( - nrOfInitialCompletedHomeworksToLoad)); - - _streamSubscription = _lazyLoadingSuccessStates.listen((state) { - add(_Transform(state)); - }); - } else if (event is AdvanceCompletedHomeworks) { - _lazyLoadingCompletedHomeworksBloc - .add(lazy_loading.AdvanceCompletedHomeworks(event.advanceBy)); - } else if (event is _Transform) { - final success = event.successState; - final listView = _completedHomeworkListViewFactory.create( - success.homeworks, success.loadedAllHomeworks); - yield Success(listView); - } else { - throw UnimplementedError('$event is not implemented'); - } - } - - @override - void dispose() { - _streamSubscription.cancel(); - } -} - -class _Transform extends CompletedHomeworksViewBlocEvent { - final lazy_loading.Success successState; - - _Transform(this.successState); - - @override - List get props => [successState]; -} diff --git a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart index 06626b8a0..3185e489f 100644 --- a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart @@ -6,10 +6,62 @@ // // SPDX-License-Identifier: EUPL-1.2 +import 'package:bloc/bloc.dart'; +import 'package:bloc_base/bloc_base.dart' as bloc_base; + +import '../../data_source/homework_data_source.dart'; +import '../../models/homework_list.dart'; import 'events.dart'; import 'states.dart'; -abstract class LazyLoadingCompletedHomeworksBloc - implements - Stream, - Sink {} +export 'events.dart'; +export 'states.dart'; + +class LazyLoadingCompletedHomeworksBloc extends Bloc< + LazyLoadingCompletedHomeworksEvent, + LazyLoadingCompletedHomeworksBlocState> implements bloc_base.BlocBase { + final HomeworkDataSource _homeworkRepository; + LazyLoadingController _lazyLoadingController; + + LazyLoadingCompletedHomeworksBloc(this._homeworkRepository) + : super(Loading()) { + on( + (event, emit) { + _lazyLoadingController = + _homeworkRepository.getLazyLoadingCompletedHomeworksController( + event.numberOfHomeworksToLoad); + _lazyLoadingController.results.listen((res) => add( + _Yield(Success( + HomeworkList(res.homeworks), + loadedAllHomeworks: !res.moreHomeworkAvailable, + )), + )); + }, + ); + on( + (event, emit) { + assert(_lazyLoadingController != null); + _lazyLoadingController.advanceBy(event.advanceBy); + }, + ); + on<_Yield>( + (event, emit) => emit(event.payload), + ); + } + + @override + void dispose() {} +} + +class _Yield extends LazyLoadingCompletedHomeworksEvent { + final dynamic payload; + _Yield(this.payload) : assert(payload != null); + + @override + List get props => [payload]; + + @override + String toString() { + return '_Yield(payload: $payload)'; + } +} diff --git a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc_impl.dart b/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc_impl.dart deleted file mode 100644 index bc2d32ba6..000000000 --- a/lib/hausaufgabenheft_logik/lib/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc_impl.dart +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) -// Licensed under the EUPL-1.2-or-later. -// -// You may obtain a copy of the Licence at: -// https://joinup.ec.europa.eu/software/page/eupl -// -// SPDX-License-Identifier: EUPL-1.2 - -import 'dart:async'; - -import 'package:bloc/bloc.dart'; -import 'package:bloc_base/bloc_base.dart'; - -import '../../data_source/homework_data_source.dart'; -import '../../models/homework_list.dart'; -import 'events.dart'; -import 'lazy_loading_completed_homeworks_bloc.dart'; -import 'states.dart'; - -class LazyLoadingCompletedHomeworksBlocImpl extends Bloc< - LazyLoadingCompletedHomeworksEvent, - LazyLoadingCompletedHomeworksBlocState> - implements LazyLoadingCompletedHomeworksBloc, BlocBase { - final HomeworkDataSource _homeworkRepository; - LazyLoadingController _lazyLoadingController; - - LazyLoadingCompletedHomeworksBlocImpl(this._homeworkRepository); - - @override - LazyLoadingCompletedHomeworksBlocState get initialState => Loading(); - - @override - Stream mapEventToState( - LazyLoadingCompletedHomeworksEvent event) async* { - if (event is LoadCompletedHomeworks) { - _lazyLoadingController = - _homeworkRepository.getLazyLoadingCompletedHomeworksController( - event.numberOfHomeworksToLoad); - _lazyLoadingController.results.listen((res) => add(_Yield(Success( - HomeworkList(res.homeworks), - loadedAllHomeworks: !res.moreHomeworkAvailable)))); - } else if (event is AdvanceCompletedHomeworks) { - assert(_lazyLoadingController != null); - _lazyLoadingController.advanceBy(event.advanceBy); - } else if (event is _Yield) { - yield event.payload; - } else { - throw UnimplementedError('$event is not implemented'); - } - } - - @override - void dispose() {} -} - -class _Yield extends LazyLoadingCompletedHomeworksEvent { - final dynamic payload; - _Yield(this.payload) : assert(payload != null); - - @override - List get props => [payload]; - - @override - String toString() { - return '_Yield(payload: $payload)'; - } -} diff --git a/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart index 362e6afda..c5937e04a 100644 --- a/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart @@ -9,7 +9,7 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; -import 'package:bloc_base/bloc_base.dart'; +import 'package:bloc_base/bloc_base.dart' as bloc_base; import 'package:common_domain_models/common_domain_models.dart'; import 'package:hausaufgabenheft_logik/color.dart'; import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik_lehrer.dart'; @@ -191,43 +191,33 @@ TeacherHomeworkView randomHomeworkViewWith({ /// UI. The real logic will be implemented in the future. class TeacherHomeworkPageBloc extends Bloc - implements BlocBase { - TeacherHomeworkPageBloc(); - - @override - TeacherHomeworkPageState get initialState => Uninitialized(); - - HomeworkSort _currentSort = HomeworkSort.smallestDateSubjectAndTitle; - _ArchivedHwLazyLoadingState _archivedHwLazyLoadingState = - _ArchivedHwLazyLoadingState.askedForFirstBatch; - - @override - Stream mapEventToState( - TeacherHomeworkPageEvent event) async* { - if (event is OpenHwSortingChanged) { + implements bloc_base.BlocBase { + TeacherHomeworkPageBloc() : super(Uninitialized()) { + on((event, emit) { _currentSort = event.sort; - } - if (event is AdvanceArchivedHomeworks) { - _advanveArchivedHwLazyLoadingState(); + }); + on((event, emit) async { await Future.delayed(const Duration(milliseconds: 1200)); - } - if (event is LoadHomeworks) { + _advanveArchivedHwLazyLoadingState(); + }); + on((event, emit) { // Reset so that we can inspect the lazy loading again when we change // away and back to the homework page again. _archivedHwLazyLoadingState = _ArchivedHwLazyLoadingState.askedForFirstBatch; - } - - // Uncomment to see placeholder (as if the user has no homework) - // yield _States._placeholder; - yield _currentSort == HomeworkSort.smallestDateSubjectAndTitle - ? _States._homeworksAllLoadedSortedByTodoDate( - _archivedHwLazyLoadingState) - : _States._homeworksAllLoadedSortedBySubject( - _archivedHwLazyLoadingState); + _currentSort == HomeworkSort.smallestDateSubjectAndTitle + ? _States._homeworksAllLoadedSortedByTodoDate( + _archivedHwLazyLoadingState) + : _States._homeworksAllLoadedSortedBySubject( + _archivedHwLazyLoadingState); + }); } + HomeworkSort _currentSort = HomeworkSort.smallestDateSubjectAndTitle; + _ArchivedHwLazyLoadingState _archivedHwLazyLoadingState = + _ArchivedHwLazyLoadingState.askedForFirstBatch; + void _advanveArchivedHwLazyLoadingState() { switch (_archivedHwLazyLoadingState) { case _ArchivedHwLazyLoadingState.askedForFirstBatch: diff --git a/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart index dd3573ca5..c1541a906 100644 --- a/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart @@ -13,26 +13,19 @@ import 'package:hausaufgabenheft_logik/src/models/homework_list.dart'; import 'events.dart'; import 'states.dart'; +export 'events.dart'; +export 'states.dart'; + class OpenHomeworkListBloc extends Bloc { final HomeworkDataSource _repository; - OpenHomeworkListBloc(this._repository); - - @override - OpenHomeworkListBlocState get initialState => Uninitialized(); - - @override - Stream mapEventToState( - OpenHomeworkListBlocEvent event) async* { - if (event is LoadHomeworks) { + OpenHomeworkListBloc(this._repository) : super(Uninitialized()) { + on((event, emit) { _repository.openHomeworks .listen((hws) => add(_Yield(Success(HomeworkList(hws))))); - } else if (event is _Yield) { - yield event.payload; - } else { - throw UnimplementedError('$event is not implemented'); - } + }); + on<_Yield>((event, emit) => emit(event.payload)); } } diff --git a/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart index a53740d4a..7a8e0fb2f 100644 --- a/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart @@ -9,48 +9,37 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; -import 'package:bloc_base/bloc_base.dart'; +import 'package:bloc_base/bloc_base.dart' as bloc_base; import 'package:hausaufgabenheft_logik/src/models/homework/homework.dart'; import 'package:hausaufgabenheft_logik/src/models/homework_list.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/sort_and_subcategorization/sort/src/sort.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/views/open_homework_list_view_factory.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/events.dart' - as hws_bloc; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/states.dart' +import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart' as hws_bloc; +import 'package:hausaufgabenheft_logik/src/open_homeworks/views/open_homework_list_view_factory.dart'; import 'events.dart'; import 'states.dart'; +export 'events.dart'; +export 'states.dart'; + class OpenHomeworksViewBloc extends Bloc - implements BlocBase { + implements bloc_base.BlocBase { final OpenHomeworkListViewFactory _listViewFactory; - final OpenHomeworkListBloc _openHomeworksBloc; + final hws_bloc.OpenHomeworkListBloc _openHomeworksBloc; Stream _openHomeworks; StreamSubscription _streamSubscription; HomeworkList _latestHomeworks; Sort _currentSort; - OpenHomeworksViewBloc(this._openHomeworksBloc, this._listViewFactory) { + OpenHomeworksViewBloc(this._openHomeworksBloc, this._listViewFactory) + : super(Uninitialized()) { _latestHomeworks = HomeworkList([]); - _openHomeworks = _openHomeworksBloc.transform(_toHomeworkList); - } - - @override - OpenHomeworksViewBlocState get initialState => Uninitialized(); - - @override - void dispose() { - _streamSubscription.cancel(); - } + _openHomeworks = _openHomeworksBloc.stream.transform(_toHomeworkList); - @override - Stream mapEventToState( - OpenHomeworkViewEvent event) async* { - if (event is LoadHomeworks) { + on((event, emit) { _currentSort = event.sort; _openHomeworksBloc.add(hws_bloc.LoadHomeworks()); _streamSubscription = _openHomeworks.listen((hws) { @@ -58,16 +47,21 @@ class OpenHomeworksViewBloc _latestHomeworks = hws; add(_CreateListView(hws, _currentSort)); }); - } else if (event is SortingChanged) { + }); + on((event, emit) { _currentSort = event.sort; add(_CreateListView(_latestHomeworks, _currentSort)); - } else if (event is _CreateListView) { + }); + on<_CreateListView>((event, emit) { final view = _listViewFactory.create(event.homeworks, event.sort); var success = Success(view); - yield success; - } else { - throw UnimplementedError('$event is not implemented'); - } + emit(success); + }); + } + + @override + void dispose() { + _streamSubscription.cancel(); } } diff --git a/lib/hausaufgabenheft_logik/lib/src/setup/create_homework_page_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/setup/create_homework_page_bloc.dart index 0bd4d18d9..a4a61d01f 100644 --- a/lib/hausaufgabenheft_logik/lib/src/setup/create_homework_page_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/setup/create_homework_page_bloc.dart @@ -11,8 +11,8 @@ import 'package:hausaufgabenheft_logik/src/student_homework_page_bloc/homework_s import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart'; -import '../completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc_impl.dart'; -import '../completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc_impl.dart'; +import '../completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart'; +import '../completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart'; import '../completed_homeworks/views/completed_homework_list_view_factory.dart'; import '../student_homework_page_bloc/student_homework_page_bloc.dart'; import '../models/homework/models_used_by_homework.dart'; @@ -39,8 +39,8 @@ HomeworkPageBloc createHomeworkPageBloc( final completedHomeworkListViewFactory = CompletedHomeworkListViewFactory(viewFactory); final lazyLoadingCompletedHomeworksBloc = - LazyLoadingCompletedHomeworksBlocImpl(dependencies.dataSource); - final completedHomeworksViewBloc = CompletedHomeworksViewBlocImpl( + LazyLoadingCompletedHomeworksBloc(dependencies.dataSource); + final completedHomeworksViewBloc = CompletedHomeworksViewBloc( lazyLoadingCompletedHomeworksBloc, completedHomeworkListViewFactory, nrOfInitialCompletedHomeworksToLoad: config.nrOfInitialCompletedHomeworksToLoad); diff --git a/lib/hausaufgabenheft_logik/lib/src/student_homework_page_bloc/student_homework_page_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/student_homework_page_bloc/student_homework_page_bloc.dart index 0d316e70b..e68a41f56 100644 --- a/lib/hausaufgabenheft_logik/lib/src/student_homework_page_bloc/student_homework_page_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/student_homework_page_bloc/student_homework_page_bloc.dart @@ -9,22 +9,16 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; -import 'package:bloc_base/bloc_base.dart'; +import 'package:bloc_base/bloc_base.dart' as bloc_base; import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik.dart'; import 'package:hausaufgabenheft_logik/src/student_homework_page_bloc/homework_sorting_cache.dart'; import 'package:meta/meta.dart'; import 'package:rxdart/rxdart.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/sort_and_subcategorization/sort/src/homework_sort_enum_sort_object_conversion_extensions.dart'; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart'; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/completed_homeworks_view_bloc/events.dart' - as completed; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/completed_homeworks_view_bloc/states.dart' +import 'package:hausaufgabenheft_logik/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart' as completed; import 'package:hausaufgabenheft_logik/src/homework_completion/homework_page_completion_dispatcher.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/events.dart' - as open; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/states.dart' +import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart' as open; /// This Bloc serves basically only as an interface to the outer world with 2 @@ -35,9 +29,9 @@ import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_blo /// * It caches the last sorting of Homeworks so that it stays consistent /// between visits of the homework page. class HomeworkPageBloc extends Bloc - implements BlocBase { - final OpenHomeworksViewBloc _openHomeworksViewBloc; - final CompletedHomeworksViewBloc _completedHomeworksViewBloc; + implements bloc_base.BlocBase { + final open.OpenHomeworksViewBloc _openHomeworksViewBloc; + final completed.CompletedHomeworksViewBloc _completedHomeworksViewBloc; final HomeworkPageCompletionDispatcher _homeworkCompletionReceiver; final HomeworkSortingCache _homeworkSortingCache; final DateTime Function() _getCurrentDateTime; @@ -46,8 +40,8 @@ class HomeworkPageBloc extends Bloc bool _isClosed = false; HomeworkPageBloc({ - @required OpenHomeworksViewBloc openHomeworksViewBloc, - @required CompletedHomeworksViewBloc completedHomeworksViewBloc, + @required open.OpenHomeworksViewBloc openHomeworksViewBloc, + @required completed.CompletedHomeworksViewBloc completedHomeworksViewBloc, @required HomeworkPageCompletionDispatcher homeworkCompletionReceiver, @required HomeworkSortingCache homeworkSortingCache, @required DateTime Function() getCurrentDateTime, @@ -55,36 +49,34 @@ class HomeworkPageBloc extends Bloc _completedHomeworksViewBloc = completedHomeworksViewBloc, _homeworkSortingCache = homeworkSortingCache, _homeworkCompletionReceiver = homeworkCompletionReceiver, - _getCurrentDateTime = getCurrentDateTime; + _getCurrentDateTime = getCurrentDateTime, + super(Uninitialized()) { + on((event, emit) { + _mapLoadHomeworksToState(); + }); + on((event, emit) { + _mapAdvanceCompletedHomeworks(event); + }); + on((event, emit) { + _mapFilterChangedToState(event); + }); + on((event, emit) { + _mapHomeworkChangedCompletionStatus(event); + }); + on((event, emit) { + _mapHomeworkMarkOverdueToState(event); + }); + on<_Yield>((event, emit) { + emit(event.success); + }); + } Date _getCurrentDate() { return Date.fromDateTime(_getCurrentDateTime()); } - @override - HomeworkPageState get initialState => Uninitialized(); - - @override - Stream mapEventToState(HomeworkPageEvent event) async* { - if (event is LoadHomeworks) { - yield* _mapLoadHomeworksToState(); - } else if (event is AdvanceCompletedHomeworks) { - yield* _mapAdvanceCompletedHomeworks(event); - } else if (event is OpenHwSortingChanged) { - yield* _mapFilterChangedToState(event); - } else if (event is CompletionStatusChanged) { - yield* _mapHomeworkChangedCompletionStatus(event); - } else if (event is CompletedAllOverdue) { - yield* _mapHomeworkMarkOverdueToState(event); - } else if (event is _Yield) { - yield event.success; - } else { - throw UnimplementedError('$event is not implemented.'); - } - } - StreamSubscription _combineLatestSubscription; - Stream _mapLoadHomeworksToState() async* { + Future _mapLoadHomeworksToState() async { _completedHomeworksViewBloc.add(completed.StartTransformingHomeworks()); final sortEnum = await _homeworkSortingCache.getLastSorting( @@ -93,10 +85,10 @@ class HomeworkPageBloc extends Bloc _openHomeworksViewBloc.add(open.LoadHomeworks(sort)); final completedHomeworksSuccessStates = - _completedHomeworksViewBloc.whereType(); + _completedHomeworksViewBloc.stream.whereType(); final openHomeworksSuccessStates = - _openHomeworksViewBloc.whereType(); + _openHomeworksViewBloc.stream.whereType(); _combineLatestSubscription = Rx.combineLatest2( @@ -113,27 +105,24 @@ class HomeworkPageBloc extends Bloc Success _toSuccessState(completed.Success completed, open.Success open) => Success(completed.completedHomeworksView, open.openHomeworkListView); - Stream _mapAdvanceCompletedHomeworks( - AdvanceCompletedHomeworks event) async* { + void _mapAdvanceCompletedHomeworks(AdvanceCompletedHomeworks event) async { _completedHomeworksViewBloc .add(completed.AdvanceCompletedHomeworks(event.advanceBy)); } - Stream _mapFilterChangedToState( - OpenHwSortingChanged event) async* { + Future _mapFilterChangedToState(OpenHwSortingChanged event) async { await _homeworkSortingCache.setLastSorting(event.sort); final newSorting = event.sort.toSortObject(getCurrentDate: _getCurrentDate); _openHomeworksViewBloc.add(open.SortingChanged(newSorting)); } - Stream _mapHomeworkChangedCompletionStatus( - CompletionStatusChanged event) async* { + Future _mapHomeworkChangedCompletionStatus( + CompletionStatusChanged event) async { await _homeworkCompletionReceiver .add(SingleHomeworkCompletionEvent(event.homeworkId, event.newValue)); } - Stream _mapHomeworkMarkOverdueToState( - CompletedAllOverdue event) async* { + Future _mapHomeworkMarkOverdueToState(CompletedAllOverdue event) async { await _homeworkCompletionReceiver.add(AllOverdueHomeworkCompletionEvent()); } diff --git a/lib/hausaufgabenheft_logik/pubspec.lock b/lib/hausaufgabenheft_logik/pubspec.lock index 3c8d313db..3368ba169 100644 --- a/lib/hausaufgabenheft_logik/pubspec.lock +++ b/lib/hausaufgabenheft_logik/pubspec.lock @@ -45,10 +45,10 @@ packages: dependency: "direct main" description: name: bloc - sha256: bc0e8756ffa2ba174a6094b2bd4527af8176dc0deb19addb538ad079a2bac75a + sha256: "3820f15f502372d979121de1f6b97bfcf1630ebff8fe1d52fb2b0bfa49be5b49" url: "https://pub.dev" source: hosted - version: "4.0.0" + version: "8.1.2" bloc_base: dependency: "direct main" description: diff --git a/lib/hausaufgabenheft_logik/pubspec.yaml b/lib/hausaufgabenheft_logik/pubspec.yaml index c5c291698..0799a901b 100644 --- a/lib/hausaufgabenheft_logik/pubspec.yaml +++ b/lib/hausaufgabenheft_logik/pubspec.yaml @@ -18,7 +18,7 @@ environment: dependencies: common_domain_models: path: ../common_domain_models - bloc: ^4.0.0 + bloc: ^8.1.2 rxdart: ^0.27.1 equatable: ^2.0.5 optional: ^6.1.0+1 diff --git a/lib/hausaufgabenheft_logik/test/homework_page_bloc_test.dart b/lib/hausaufgabenheft_logik/test/homework_page_bloc_test.dart index 3865163be..d678fa735 100644 --- a/lib/hausaufgabenheft_logik/test/homework_page_bloc_test.dart +++ b/lib/hausaufgabenheft_logik/test/homework_page_bloc_test.dart @@ -9,12 +9,13 @@ import 'dart:async'; import 'dart:developer'; -import 'package:async/async.dart'; import 'package:bloc/bloc.dart'; import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik.dart'; import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik_setup.dart'; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc_impl.dart'; -import 'package:hausaufgabenheft_logik/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc_impl.dart'; +import 'package:hausaufgabenheft_logik/src/completed_homeworks/completed_homeworks_view_bloc/completed_homeworks_view_bloc.dart' + as completed; +import 'package:hausaufgabenheft_logik/src/completed_homeworks/lazy_loading_completed_homeworks_bloc/lazy_loading_completed_homeworks_bloc.dart' + as lazy_loading; import 'package:hausaufgabenheft_logik/src/completed_homeworks/views/completed_homework_list_view_factory.dart'; import 'package:hausaufgabenheft_logik/src/models/homework_list.dart'; import 'package:hausaufgabenheft_logik/src/student_homework_page_bloc/homework_sorting_cache.dart'; @@ -28,7 +29,7 @@ import 'create_homework_util.dart'; import 'in_memory_repo/in_memory_homework_repository.dart'; void main() { - BlocSupervisor.delegate = VerboseBlocDelegate(); + Bloc.observer = VerboseBlocObserver(); group('GIVEN a Student with Homework WHEN he opens the homework page', () { HomeworkPageBloc bloc; InMemoryHomeworkRepository repository; @@ -62,7 +63,8 @@ void main() { await pumpEventQueue(); bloc.add(CompletionStatusChanged('hw', true)); - Success success = await bloc.firstWhere((element) => element is Success); + Success success = + await bloc.stream.firstWhere((element) => element is Success); expect( success.open.sorting, HomeworkSort.subjectSmallestDateAndTitleSort); @@ -76,7 +78,8 @@ void main() { .setLastSorting(HomeworkSort.subjectSmallestDateAndTitleSort); bloc.add(LoadHomeworks()); - Success success = await bloc.firstWhere((element) => element is Success); + Success success = + await bloc.stream.firstWhere((element) => element is Success); expect( success.open.sorting, HomeworkSort.subjectSmallestDateAndTitleSort); @@ -87,7 +90,8 @@ void main() { () async { await addToRepository([createHomework(title: 'Hallo')]); bloc.add(LoadHomeworks()); - Success success = await bloc.firstWhere((element) => element is Success); + Success success = + await bloc.stream.firstWhere((element) => element is Success); expect(success.open.sorting, HomeworkSort.smallestDateSubjectAndTitle); }); @@ -98,8 +102,7 @@ void main() { bloc.add(LoadHomeworks()); bloc.add( OpenHwSortingChanged(HomeworkSort.subjectSmallestDateAndTitleSort)); - // skip loading and first success (before sorting) - await bloc.skip(2).firstWhere((element) => element is Success); + await bloc.stream.firstWhere((element) => element is Success); expect(await homeworkSortingCache.getLastSorting(), HomeworkSort.subjectSmallestDateAndTitleSort); @@ -132,10 +135,9 @@ void main() { await addToRepository(homeworks); bloc.add(LoadHomeworks()); - await bloc.skip(1).first; bloc.add(OpenHwSortingChanged(HomeworkSort.smallestDateSubjectAndTitle)); - final findState = bloc.firstWhere((state) { + final findState = bloc.stream.firstWhere((state) { if (state is Success) { final hws = state.open.orderedHomeworks; @@ -169,14 +171,11 @@ void main() { await addToRepository(homeworks); - final queue = StreamQueue(bloc); bloc.add(LoadHomeworks()); - - await queue.skip(2); // LoadingHomeworks and firstly loaded homeworks - bloc.add(OpenHwSortingChanged(HomeworkSort.smallestDateSubjectAndTitle)); - Success result = await queue.next; + Success result = + await bloc.stream.where((event) => event is Success).skip(1).first; final notDone = result.open; expect(notDone.sections.length, 5); @@ -235,20 +234,16 @@ void main() { await addToRepository(unsorted); bloc.add(LoadHomeworks()); - // Delay so the SortingChanged-Event is not dispatched before anything was loaded (because of async) - await bloc.skip(1).first; - bloc.add( OpenHwSortingChanged(HomeworkSort.subjectSmallestDateAndTitleSort)); - final findMatchingState = bloc.firstWhere((state) { + final findMatchingState = bloc.stream.firstWhere((state) { if (state is Success) { final sortedOpenHWs = state.open; if (sortedOpenHWs.numberOfHomeworks == unsorted.length) { final orderedIds = sortedOpenHWs.orderedHomeworks.map((hw) => hw.id).toList(); return equals(orderedIds).matches(['1', '2', '3', '4'], {}); - // return listsEqual(orderedIds, ["1", "2", "3", "4"]); } } return false; @@ -263,19 +258,17 @@ void main() { var homework = createHomework(id: 'homeworkId', done: false, title: 'title'); - final queue = StreamQueue(bloc); await addToRepository([homework]); bloc.add(LoadHomeworks()); - await queue.skip(1); // LoadingHomeworks - - final Success uncompleted = await queue.next; + final Success uncompleted = + await bloc.stream.firstWhere((state) => state is Success); expect(uncompleted.open.numberOfHomeworks, 1); expect(uncompleted.completed.numberOfHomeworks, 0); bloc.add(CompletionStatusChanged('homeworkId', true)); - final Success completed = await bloc.firstWhere((state) => + final Success completed = await bloc.stream.firstWhere((state) => state is Success && state.open.numberOfHomeworks == 0 && state.completed.numberOfHomeworks == 1); @@ -290,7 +283,8 @@ void main() { await addToRepository([tomorrow]); bloc.add(LoadHomeworks()); - final Success latest = await bloc.skip(1).first; + final Success latest = + await bloc.stream.firstWhere((state) => state is Success); expect(latest.open.sections.length, 1); }); @@ -312,10 +306,9 @@ void main() { await addToRepository(openHomeworks); bloc.add(LoadHomeworks()); - await bloc.skip(1).first; + await bloc.stream.firstWhere((state) => state is Success); bloc.add(CompletedAllOverdue()); - bloc.listen((e) => log('$e')); - final Success res = await bloc.firstWhere((state) => + final Success res = await bloc.stream.firstWhere((state) => state is Success && state.completed.orderedHomeworks.length == 2 && state.open.orderedHomeworks.length == 1); @@ -335,7 +328,8 @@ void main() { await addToRepository(homeworks); bloc.add(LoadHomeworks()); - final Success res = await bloc.skip(1).first; + final Success res = + await bloc.stream.firstWhere((state) => state is Success); expect(res.open.showCompleteOverdueHomeworkPrompt, true); }); test( @@ -356,7 +350,7 @@ void main() { await addToRepository(homeworks); bloc.add(LoadHomeworks()); - final invalidStates = bloc.where((state) => + final invalidStates = bloc.stream.where((state) => state is Success && state.open.showCompleteOverdueHomeworkPrompt == true); @@ -382,10 +376,6 @@ void main() { (i) => createHomework(id: '$i', done: true, title: title(i))); } - Future getFirstSuccessfullState( - Stream state) async => - await state.firstWhere((state) => state is Success); - test( 'The bloc should give the appropiate status that not all completed homeworks were loaded yet if that is the case', () async { @@ -394,7 +384,8 @@ void main() { await addToRepository(completedHomeworks, repository); bloc.add(LoadHomeworks()); - final state = await getFirstSuccessfullState(bloc); + final Success state = + await bloc.stream.firstWhere((state) => state is Success); expect(state.completed.loadedAllCompletedHomeworks, false); }); @@ -406,7 +397,8 @@ void main() { await addToRepository(completedHomeworks, repository); bloc.add(LoadHomeworks()); - final state = await getFirstSuccessfullState(bloc); + final Success state = + await bloc.stream.firstWhere((state) => state is Success); expect(state.completed.loadedAllCompletedHomeworks, true); }, @@ -420,7 +412,7 @@ void main() { bloc.add(LoadHomeworks()); - final expectedPromise = bloc.firstWhere((state) => + final expectedPromise = bloc.stream.firstWhere((state) => state is Success && state.completed.numberOfHomeworks == 10); expect(expectedPromise, completes); }); @@ -435,7 +427,7 @@ void main() { bloc.add(LoadHomeworks()); bloc.add(AdvanceCompletedHomeworks(10)); - final findExpectedState = bloc.firstWhere((state) => + final findExpectedState = bloc.stream.firstWhere((state) => state is Success && state.completed.numberOfHomeworks == 20); // Skips loading and first Successful state, where the first 10 homeworks were loaded, to get the second successful state. @@ -460,7 +452,7 @@ void main() { bloc.add(AdvanceCompletedHomeworks(5)); bloc.add(AdvanceCompletedHomeworks(5)); - final Success state = await bloc.firstWhere((state) => + final Success state = await bloc.stream.firstWhere((state) => state is Success && state.completed.numberOfHomeworks == 20); final orderedHomeworks = state.completed.orderedHomeworks; @@ -509,15 +501,15 @@ HomeworkPageBloc createBloc( ); } -CompletedHomeworksViewBlocImpl createCompletedHomeworksViewBloc( +completed.CompletedHomeworksViewBloc createCompletedHomeworksViewBloc( StudentHomeworkViewFactory viewFactory, InMemoryHomeworkRepository repository, {int nrOfInitialCompletedHomeworksToLoad}) { final completedHomeworkListViewFactory = CompletedHomeworkListViewFactory(viewFactory); final lazyLoadingCompletedHomeworksBloc = - LazyLoadingCompletedHomeworksBlocImpl(repository); - final completedHomeworksViewBloc = CompletedHomeworksViewBlocImpl( + lazy_loading.LazyLoadingCompletedHomeworksBloc(repository); + final completedHomeworksViewBloc = completed.CompletedHomeworksViewBloc( lazyLoadingCompletedHomeworksBloc, completedHomeworkListViewFactory, nrOfInitialCompletedHomeworksToLoad: nrOfInitialCompletedHomeworksToLoad); return completedHomeworksViewBloc; @@ -550,19 +542,19 @@ class RepositoryHomeworkCompletionDispatcher } } -class VerboseBlocDelegate extends BlocDelegate { +class VerboseBlocObserver extends BlocObserver { @override - void onError(Bloc bloc, Object error, StackTrace stacktrace) { + void onError(BlocBase bloc, Object error, StackTrace stacktrace) { log('${bloc.runtimeType} error: $error, stacktrace: $stacktrace', error: error, stackTrace: stacktrace); super.onError(bloc, error, stacktrace); } - // @override - // void onTransition(Bloc bloc, Transition transition) { - // log("${bloc.runtimeType} transition: $transition"); - // super.onTransition(bloc, transition); - // } + @override + void onTransition(Bloc bloc, Transition transition) { + log("${bloc.runtimeType} transition: $transition"); + super.onTransition(bloc, transition); + } @override void onEvent(Bloc bloc, Object event) { diff --git a/lib/hausaufgabenheft_logik/test/open_homework_view_bloc_test.dart b/lib/hausaufgabenheft_logik/test/open_homework_view_bloc_test.dart index 98aac3eb6..b6cafd12b 100644 --- a/lib/hausaufgabenheft_logik/test/open_homework_view_bloc_test.dart +++ b/lib/hausaufgabenheft_logik/test/open_homework_view_bloc_test.dart @@ -6,20 +6,13 @@ // // SPDX-License-Identifier: EUPL-1.2 -import 'dart:async'; - import 'package:bloc/bloc.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/events.dart' - as list_bloc; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/states.dart' +import 'package:hausaufgabenheft_logik/src/models/homework/homework.dart'; +import 'package:hausaufgabenheft_logik/src/models/homework_list.dart'; +import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_list_bloc/open_homework_list_bloc.dart' as list_bloc; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/events.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/open_homework_view_bloc.dart'; -import 'package:hausaufgabenheft_logik/src/open_homeworks/open_homework_view_bloc/states.dart'; -import 'package:hausaufgabenheft_logik/src/models/homework/homework.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/sort_and_subcategorization/sort/homework_sorts.dart'; -import 'package:hausaufgabenheft_logik/src/models/homework_list.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/views/homework_section_view.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/views/open_homework_list_view.dart'; import 'package:hausaufgabenheft_logik/src/open_homeworks/views/open_homework_list_view_factory.dart'; @@ -44,8 +37,8 @@ void main() { openHomeworksViewBloc .add(LoadHomeworks(SmallestDateSubjectAndTitleSort())); - final Success success = - await openHomeworksViewBloc.firstWhere((state) => state is Success); + final Success success = await openHomeworksViewBloc.stream + .firstWhere((state) => state is Success); expect(success.openHomeworkListView.sections, sections); }); }); @@ -67,16 +60,13 @@ class MockOpenHomeworkListViewFactory implements OpenHomeworkListViewFactory { } class MockOpenHomeworkListBloc extends Bloc implements OpenHomeworkListBloc { + list_bloc.OpenHomeworkListBlocState> + implements list_bloc.OpenHomeworkListBloc { var homeworkListToReturn = HomeworkList([]); - @override - list_bloc.OpenHomeworkListBlocState get initialState => - list_bloc.Success(homeworkListToReturn); - - @override - Stream mapEventToState( - list_bloc.OpenHomeworkListBlocEvent event) { - return Stream.value(list_bloc.Success(homeworkListToReturn)); + MockOpenHomeworkListBloc() : super(list_bloc.Uninitialized()) { + on((event, emit) { + emit(list_bloc.Success(homeworkListToReturn)); + }); } }