AccessBloc constructor

AccessBloc(
  1. GlobalKey<NavigatorState> navigatorKey
)

Implementation

AccessBloc(this.navigatorKey) : super(UndeterminedAccessState()) {
  on<AccessInitEvent>((event, emit) async {
    var usr = repos.AbstractMainRepositorySingleton.singleton
        .userRepository()!
        .currentSignedinUser();

    if (usr == null) {
      await StyleRegistry.registry()
          .addApp(null, event.app, () => _currentStyleChanged(event.app));
      _listenToApp(event.app.documentID, null);
      emit(await LoggedOut.getLoggedOut2(this, event.app,
          playstoreApp: event.playstoreApp));
    } else {
      var member = await firebaseToMemberModel(usr);
      _listenToMember(member);
      await StyleRegistry.registry()
          .addApp(member, event.app, () => _currentStyleChanged(event.app));

      _listenToApp(event.app.documentID, member);
      var newState = await LoggedIn.getLoggedIn2(
          this, usr, member, event.app, LoggedIn.getSubscriptions(member),
          playstoreApp: event.playstoreApp);

      // auto subscribe when no policies in place
      newState = await acceptPoliciesIfNone(newState, event.app, member);

      _listenToMemberClaims(member.documentID, newState);
      emit(newState);
    }
  });
  on<GoHome>((event, emit) async {
    var theState = state as AccessDetermined;
    if (event.isProcessing()) {
      var appId = event.app.documentID;
      var app = theState.getApp(appId);
      var homePage = (event.redetermine && (app != null))
          ? await theState.reterminedHomePageForAppId(app)
          : theState.homePageForAppId(appId);
      gotoPage(true, event.app.documentID, homePage?.documentID,
          errorString:
              'Homepage not set correct for app ${event.app.documentID}');
      emit(theState.asNotProcessing());
    } else {
      add(event.asProcessing());
      emit(theState.asProcessing());
    }
  });
  on<LogoutEvent>((event, emit) async {
    var theState = state as AccessDetermined;
    _stopListenToMember();
    _stopListenToMemberClaims();
    if (event.isProcessing()) {
      await repos.AbstractMainRepositorySingleton.singleton
          .userRepository()!
          .signOut();
      var toEmit = await LoggedOut.getLoggedOut(this,
          theState.apps.map((determinedApp) => determinedApp.app).toList(),
          playstoreApp: theState.playstoreApp);
      var homePage = toEmit.homePageForAppId(event.app.documentID);
      gotoPage(true, event.app.documentID, homePage?.documentID,
          errorString:
              'Homepage not set correct for app ${event.app.documentID}');
      emit(toEmit);
    } else {
      add(event.asProcessing());
      emit(theState.asProcessing());
    }
  });

  on<DismissTempMessage>((event, emit) async {
    var theState = state as AccessDetermined;
    emit(theState.clearTempMessage());
  });

  on<ShowTempMessage>((event, emit) async {
    var theState = state as AccessDetermined;
    emit(theState.withTempMessage(event.tempMessage));
  });

  on<LoginEvent>((event, emit) async {
    var theState = state as AccessDetermined;
    if (event.isProcessing()) {
      User? usr;
      try {
        switch (event.loginType) {
          case LoginType.googleLogin:
            usr = await repos.AbstractMainRepositorySingleton.singleton
                .userRepository()!
                .signInWithGoogle();
            break;
          case LoginType.appleLogin:
            usr = await repos.AbstractMainRepositorySingleton.singleton
                .userRepository()!
                .signInWithApple();
            break;
        }
      } catch (exception) {
        print('Exception during signIn: $exception');
        emit(theState
            .asNotProcessing()
            .withTempMessage('Error during signin'));
      }
      if (usr != null) {
        var member = await firebaseToMemberModel(usr);

        var toEmit = await LoggedIn.getLoggedIn(
            this,
            usr,
            member,
            theState.apps.map((determinedApp) => determinedApp.app).toList(),
            null,
            LoggedIn.getSubscriptions(member),
            playstoreApp: theState.playstoreApp);

        // auto subscribe when no policies in place
        toEmit = await acceptPoliciesIfNone(toEmit, event.app, member);

        _resetAccessListeners(
            theState.apps.map((e) => e.app.documentID).toList(),
            member.documentID);

        _listenToMember(member);
        _listenToMemberClaims(member.documentID, toEmit);
        emit(toEmit);
        if (event.actions != null) {
          event.actions!.runTheAction();
        } else {
          var homePage = toEmit.homePageForAppId(event.app.documentID);
          gotoPage(true, event.app.documentID, homePage?.documentID,
              errorString:
                  'Homepage not set correct for app ${event.app.documentID}');
        }
      }
    } else {
      add(event.asProcessing());
      emit(theState.asProcessing());
    }
  });

  on<SwitchAppWithIDEvent>((event, emit) async {
    var theState = state as AccessDetermined;
    if (event.isProcessing()) {
      var app = await _fetchApp(event.appId);
      await StyleRegistry.registry()
          .addApp(state.getMember(), app, () => _currentStyleChanged(app));
      var newState = await theState.asNotProcessing().addApp(this, app);
      var homePage = newState.homePageForAppId(event.appId);
      gotoPage(false, event.appId, homePage?.documentID,
          errorString: 'Homepage not set correct for app ${event.appId}');

      _listenToApp(event.appId, theState.getMember());
      emit(newState);
    } else {
      add(event.asProcessing());
      emit(theState.asProcessing());
    }
  });

  on<MemberUpdatedEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      var newState = theState.updateMember(event.member);
      emit(newState);
    }
  });

  on<AppUpdatedEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      var newState = await theState.updateApp2(this, event.app);
      await StyleRegistry.registry().addApp(state.getMember(), event.app,
          () => _currentStyleChanged(event.app));
      emit(newState);
    }
  });

  on<RefreshAppEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      var newState = theState.newVersion();
      emit(newState);
    }
  });

  on<GotoPageEvent>((event, emit) async {
    // NAVIGATION-USING-BLOC: Navigation within the context of using bloc should use BlocListener. However, there are issues with that, see : https://github.com/felangel/bloc/issues/2938
    // When this would get resolved, then we can use theState.switchPage(page, parameters: event.parameters)
    // and remove the navigation from here:
    gotoPage(false, event.app.documentID, event.pageId,
        parameters: event.parameters, errorString: 'Page not does not exist');
  });

  on<OpenDialogEvent>((event, emit) async {
    // See comment @ GotoPageEvent
    // We should use theState.openDialog(`dialog, parameters: event.parameters);
  });

  on<UpdatePackageConditionEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      var newState = theState.withDifferentPackageCondition(
          event.app.documentID,
          event.package,
          event.packageCondition,
          event.condition);
      emit(newState);
    }
  });

  on<PrivilegeChangedEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      emit(await theState.withOtherPrivilege(
          this, event.app, event.privilege, event.blocked));
    }
  });

  on<AcceptedMembershipEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      if (theState is LoggedIn) {
        var theMember = theState.getMember();
        if (theMember != null) {
          await LoggedIn.acceptMembership(theMember, event.app);
          var newState = theState
              .withSubscriptions(LoggedIn.getSubscriptions(theMember));
          if (newState.postLoginAction != null) {
            newState.postLoginAction!.runTheAction();
          }
          emit(newState);
        }
      }
    }
  });

  on<AccessUpdatedEvent>((event, emit) async {
    if (state is AccessDetermined) {
      var theState = state as AccessDetermined;
      emit(await theState.withNewAccess(this, event.access));
    }
  });
}