getAndSubscribe method

  1. @override
Future<List<PackageConditionDetails>>? getAndSubscribe(
  1. AccessBloc accessBloc,
  2. AppModel app,
  3. MemberModel? member,
  4. bool isOwner,
  5. bool? isBlocked,
  6. PrivilegeLevel? privilegeLevel,
)
override

Implementation

@override
Future<List<PackageConditionDetails>>? getAndSubscribe(
    AccessBloc accessBloc,
    AppModel app,
    MemberModel? member,
    bool isOwner,
    bool? isBlocked,
    PrivilegeLevel? privilegeLevel) {
  var appId = app.documentID;
  subscription[appId]?.cancel();
  if (member != null) {
    final c = Completer<List<PackageConditionDetails>>();
    subscription[appId] =
        accessRepository(appId: appId)!.listenTo(member.documentID, (value) {
      if (value != null) {
        var newPrivilegeInfo = PrivilegeInfo(
            value.privilegeLevel ?? PrivilegeLevel.noPrivilege,
            value.blocked ?? false);
        if (!c.isCompleted) {
          // the first time we ignore because a) privilege is handled by the calling mechanism b) the condition itself is determined below
          statePRIVILEGE[appId] = newPrivilegeInfo;
        } else {
          // subsequent calls we get this trigger, it's when the date has changed. Now add the event to the bloc
          if (newPrivilegeInfo != statePRIVILEGE[appId]) {
            statePRIVILEGE[appId] = newPrivilegeInfo;
            accessBloc.add(PrivilegeChangedEvent(
                app, newPrivilegeInfo.privilege, newPrivilegeInfo.blocked));
          }
        }
      }
    });
  } else {
    statePRIVILEGE[appId] = null;
  }
  return Future.value([
    PackageConditionDetails(
        packageName: packageName,
        conditionName: mustBeLoggedIn,
        value: (member != null))
  ]);
}