Skip to content

Commit

Permalink
Upgrade bloc library in hausaufgabenheft_logik package. (#738)
Browse files Browse the repository at this point in the history
Upgrade the `bloc` package from `^4.0.0` to `^8.1.2`.    
This enables us to make the `hausaufgabenheft_logik` package null-safe
afterwards (#191).

Note: Before there was an abstract "interface" for some blocs in our
code, which I removed because of class types issues (Bloc was `Stream`
before but isn't after upgrading) and also because it's unnecessary.
Thus I also renamed some bloc files which git seems to interpret as
deleting old bloc files and creating new ones.

Closes #729
  • Loading branch information
Jonas-Sander authored Aug 17, 2023
1 parent 54619c5 commit c642a78
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class _SortButton extends StatelessWidget {
final bloc = BlocProvider.of<HomeworkPageBloc>(context);

return StreamBuilder<Success>(
stream: bloc.whereType<Success>(),
stream: bloc.stream.whereType<Success>(),
builder: (context, snapshot) {
final currentSort = snapshot?.data?.open?.sorting;
return Padding(
Expand Down
4 changes: 2 additions & 2 deletions app/lib/homework/student/student_homework_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class StudentHomeworkBody extends StatelessWidget {
final bloc = BlocProvider.of<HomeworkPageBloc>(context);
bloc.add(LoadHomeworks());
return StreamBuilder<HomeworkPageState>(
stream: bloc,
initialData: Uninitialized(),
stream: bloc.stream,
initialData: bloc.state,
builder: (context, snapshot) {
final state = snapshot.hasData ? snapshot.data : Uninitialized();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TeacherSortButton extends StatelessWidget {
final bloc = BlocProvider.of<TeacherHomeworkPageBloc>(context);

return StreamBuilder<Success>(
stream: bloc.whereType<Success>(),
stream: bloc.stream.whereType<Success>(),
builder: (context, snapshot) {
final currentSort = snapshot?.data?.open?.sorting;
return Padding(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/homework/teacher/teacher_homework_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class TeacherHomeworkBody extends StatelessWidget {
final bloc = BlocProvider.of<TeacherHomeworkPageBloc>(context);
bloc.add(LoadHomeworks());
return StreamBuilder<TeacherHomeworkPageState>(
stream: bloc,
stream: bloc.stream,
initialData: Uninitialized(),
builder: (context, snapshot) {
final state = snapshot.hasData ? snapshot.data : Uninitialized();
Expand Down
4 changes: 2 additions & 2 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 21 additions & 12 deletions app/test/homework/teacher/teacher_homework_page_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'dart:async';
import 'dart:collection';

import 'package:analytics/analytics.dart';
Expand Down Expand Up @@ -34,6 +33,14 @@ class MockTeacherHomeworkPageBloc extends TeacherHomeworkPageBloc {

final receivedEvents = <TeacherHomeworkPageEvent>[];

MockTeacherHomeworkPageBloc() : super() {
on<TeacherHomeworkPageEvent>((event, emit) {
if (_queuedStates.isNotEmpty) {
emit(_queuedStates.removeFirst());
}
});
}

void emitNewState(TeacherHomeworkPageState state) {
_queuedStates.add(state);
add(LoadHomeworks());
Expand All @@ -44,17 +51,6 @@ class MockTeacherHomeworkPageBloc extends TeacherHomeworkPageBloc {
receivedEvents.add(event);
super.onEvent(event);
}

@override
Stream<TeacherHomeworkPageState> mapEventToState(
TeacherHomeworkPageEvent event) async* {
if (_queuedStates.isNotEmpty) {
yield _queuedStates.removeFirst();
}
}

@override
TeacherHomeworkPageState initialState = Uninitialized();
}

enum HomeworkTab { open, archived }
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/abgabe/abgabe_client_lib/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lib/firebase_hausaufgabenheft_logik/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletedHomeworksViewBlocState>,
Sink<CompletedHomeworksViewBlocEvent> {}
export 'events.dart';
export 'states.dart';

class CompletedHomeworksViewBloc extends Bloc<CompletedHomeworksViewBlocEvent,
CompletedHomeworksViewBlocState> implements bloc_base.BlocBase {
final lazy_loading.LazyLoadingCompletedHomeworksBloc
_lazyLoadingCompletedHomeworksBloc;
final CompletedHomeworkListViewFactory _completedHomeworkListViewFactory;
final int nrOfInitialCompletedHomeworksToLoad;
StreamSubscription _streamSubscription;
Stream<lazy_loading.Success> _lazyLoadingSuccessStates;

CompletedHomeworksViewBloc(this._lazyLoadingCompletedHomeworksBloc,
this._completedHomeworkListViewFactory,
{this.nrOfInitialCompletedHomeworksToLoad = 8})
: super(Loading()) {
_lazyLoadingSuccessStates = _lazyLoadingCompletedHomeworksBloc.stream
.whereType<lazy_loading.Success>();

on<StartTransformingHomeworks>((event, emit) {
_lazyLoadingCompletedHomeworksBloc.add(
lazy_loading.LoadCompletedHomeworks(
nrOfInitialCompletedHomeworksToLoad));

_streamSubscription = _lazyLoadingSuccessStates.listen((state) {
add(_Transform(state));
});
});
on<AdvanceCompletedHomeworks>((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<Object> get props => [successState];
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<LazyLoadingCompletedHomeworksBlocState>,
Sink<LazyLoadingCompletedHomeworksEvent> {}
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<LoadCompletedHomeworks>(
(event, emit) {
_lazyLoadingController =
_homeworkRepository.getLazyLoadingCompletedHomeworksController(
event.numberOfHomeworksToLoad);
_lazyLoadingController.results.listen((res) => add(
_Yield(Success(
HomeworkList(res.homeworks),
loadedAllHomeworks: !res.moreHomeworkAvailable,
)),
));
},
);
on<AdvanceCompletedHomeworks>(
(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<Object> get props => [payload];

@override
String toString() {
return '_Yield(payload: $payload)';
}
}
Loading

0 comments on commit c642a78

Please sign in to comment.