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) {
  String appId = app.documentID;
  subscription[appId]?.cancel();
  if (member != null) {
    final c = Completer<List<PackageConditionDetails>>();
    subscription[appId] =
        followRequestRepository(appId: appId)!.listen((list) {
      // If we have a different set of assignments, i.e. it has assignments were before it didn't or vice versa,
      // then we must inform the AccessBloc, so that it can refresh the state
      var value = list.isNotEmpty;
      if (!c.isCompleted) {
        // the first time we get this trigger, it's upon entry of the getAndSubscribe. Now we simply return the value
        stateConditionMemberHasOpenRequests[appId] = value;
        c.complete([
          PackageConditionDetails(
              packageName: packageName,
              conditionName: conditionMemberHasOpenRequests,
              value: value)
        ]);
      } else {
        // subsequent calls we get this trigger, it's when the date has changed. Now add the event to the bloc
        if (value != stateConditionMemberHasOpenRequests[appId]) {
          stateConditionMemberHasOpenRequests[appId] = value;
          accessBloc.add(UpdatePackageConditionEvent(
              app, this, conditionMemberHasOpenRequests, value));
        }
      }
    }, eliudQuery: getOpenFollowRequestsQuery(appId, member.documentID));
    return c.future;
  } else {
    stateConditionMemberHasOpenRequests[appId] = false;
    return Future.value([
      PackageConditionDetails(
          packageName: packageName,
          conditionName: conditionMemberHasOpenRequests,
          value: false)
    ]);
  }
}