Skip to content

Commit

Permalink
Merge pull request #120 from Nesteo/pagecontrol-doc
Browse files Browse the repository at this point in the history
Documented and refactored the page control code. Resolves #105
  • Loading branch information
SimonSchwierzeck authored Oct 28, 2019
2 parents d417e9c + 37c9b10 commit e0e3149
Showing 1 changed file with 32 additions and 37 deletions.
69 changes: 32 additions & 37 deletions lib/blocs/pagecontrol_bloc/pagecontrol_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,57 @@ import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:bloc/bloc.dart';
import './pagecontrol.dart';

/// BLoC that handles page control and transitioning.
///
/// *Author: Simon Oyen*
class PageControlBloc extends Bloc<PageControlEvent, PageControlState> {
bool navigationBarEnabled = false;
List<PageControlState> history = new List();
final Map<Type, PageControlState> eventStateMap = {
GoToMapEvent: MapScreenState(),
GoToBoxListEvent: BoxListScreenState(),
GoToBoxInfoEvent: BoxInfoScreenState(),
GoToInspectionEvent: InspectionScreenState(),
GoToNewInspectionEvent: NewInspectionScreenState(),
GoToNewBoxEvent: NewBoxScreenState(),
GoToInspectionListEvent: InspectionListScreenState(),
};

PageControlBloc() : super() {
BackButtonInterceptor.add(myInterceptor);
BackButtonInterceptor.add(interceptFunc);
}
bool myInterceptor(bool stopDefaultButtonEvent) {

/// Is called when the hardware backbutton is pressed
///
/// Returns true when the normal backbutton behaviour should be blocked.
bool interceptFunc(bool stopDefaultButtonEvent) {
this.add(BackButtonEvent());
return true;
}

bool navigationBarEnabled = false;
List<PageControlState> history =
new List(); //state history since starting the app
/// Returns the BLoCs initial state, [LoginScreenState]
@override
PageControlState get initialState => LoginScreenState();

/// Maps received events to corresponding states and handles the navigationbar
@override
Stream<PageControlState> mapEventToState(
PageControlEvent event,
) async* {
if (event is! BackButtonEvent && state != LoginScreenState()) {
history.add(state);
}

if (event is GoToMapEvent) {
navigationBarEnabled = true;
yield MapScreenState();
}

if (event is GoToBoxListEvent) {
navigationBarEnabled = true;
yield BoxListScreenState();
if (event is! BackButtonEvent) {
if (state != LoginScreenState()) {
history.add(state);
}
navigationBarEnabled =
(event is GoToMapEvent || event is GoToBoxListEvent);
}

if (event is GoToNewBoxEvent) {
navigationBarEnabled = false;
yield NewBoxScreenState();
}
PageControlState nextState = eventStateMap[event.runtimeType];

if (event is GoToBoxInfoEvent) {
navigationBarEnabled = false;
yield BoxInfoScreenState();
if (nextState != null) {
yield nextState;
}

if (event is GoToInspectionEvent) {
navigationBarEnabled = false;
yield InspectionScreenState();
}

if (event is GoToNewInspectionEvent) {
navigationBarEnabled = false;
yield NewInspectionScreenState();
}
if (event is GoToInspectionListEvent) {
navigationBarEnabled = false;
yield InspectionListScreenState();
}
if (event is BackButtonEvent) {
if (history.isNotEmpty) {
yield history.last;
Expand Down

0 comments on commit e0e3149

Please sign in to comment.