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] =
        notificationRepository(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) {
        stateConditionMemberHasUnreadNotifications[appId] = value;
        // the first time we get this trigger, it's upon entry of the getAndSubscribe. Now we simply return the value
        c.complete([
          PackageConditionDetails(
              packageName: packageName,
              conditionName: conditionMemberHasUnreadNotifications,
              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 != stateConditionMemberHasUnreadNotifications[appId]) {
          stateConditionMemberHasUnreadNotifications[appId] = value;
          accessBloc.add(UpdatePackageConditionEvent(
              app, this, conditionMemberHasUnreadNotifications, value));
        }
      }
    }, eliudQuery: getOpenNotificationsQuery(appId, member.documentID));
    return c.future;
  } else {
    return Future.value([
      PackageConditionDetails(
          packageName: packageName,
          conditionName: conditionMemberHasUnreadNotifications,
          value: false)
    ]);
  }
}