Skip to content

Instantly share code, notes, and snippets.

@ruan65
Created March 15, 2019 22:40
Show Gist options
  • Save ruan65/edd7a450849d8609ceb5caebf1ddfd83 to your computer and use it in GitHub Desktop.
Save ruan65/edd7a450849d8609ceb5caebf1ddfd83 to your computer and use it in GitHub Desktop.
import 'package:bloc/bloc.dart';
import 'package:tps_mobile_combined_maximo_app/data/model/user_state/member_user_state.dart';
import 'package:tps_mobile_combined_maximo_app/data/repositories/local_storage.dart';
import 'package:tps_mobile_combined_maximo_app/global/bloc/export.dart';
class GlobalBloc extends Bloc<GlobalEvent, GlobalState> {
@override
GlobalState get initialState => GlobalStateUnauthenticated();
@override
Stream<GlobalState> mapEventToState(
GlobalState currentState,
GlobalEvent event,
) async* {
if(event is GlobalMessageEvent) {
yield GlobalMessageState(event.message);
}
if(event is GlobalAvoidDistinctEvent) {
yield GlobalStateAvoidDistinct();
}
if(event is AppStartedGlobalEvent) {
yield GlobalStateCheckingAuthentication();
}
if(event is AuthenticatedGlobalEvent) {
yield GlobalStateAuthenticated();
}
if(event is UnauthenticatedGlobalEvent) {
yield GlobalStateUnauthenticated();
LocalStorage.eraseUserState();
}
}
void authenticated() => this.dispatch(AuthenticatedGlobalEvent());
void unAuthenticated() => this.dispatch(UnauthenticatedGlobalEvent());
Future<MemberUserState> currentUser() async {
return await LocalStorage.readUserState();
}
void globalMessage(GlobalMessage message) {
dispatch(GlobalMessageEvent(message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment