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] = memberHasChatRepository(
      appId: appId,
    )!
        .listenTo(member.documentID, (value) {
      var hasUnread = false;
      if (value != null) {
        hasUnread = value.hasUnread!;
      }
      if (!c.isCompleted) {
        stateConditionMemberHasUnreadChat[app.documentID] = hasUnread;
        // 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: conditionMemberHasUnreadChat,
              value: hasUnread),
          PackageConditionDetails(
              packageName: packageName,
              conditionName: conditionMemberAllHaveBeenRead,
              value: !hasUnread),
        ]);
      } else {
        // subsequent calls we get this trigger, it's when the date has changed. Now add the event to the bloc
        if (hasUnread != stateConditionMemberHasUnreadChat[appId]) {
          stateConditionMemberHasUnreadChat[app.documentID] = hasUnread;
          accessBloc.add(UpdatePackageConditionEvent(
              app, this, conditionMemberHasUnreadChat, hasUnread));
          accessBloc.add(UpdatePackageConditionEvent(
              app, this, conditionMemberAllHaveBeenRead, !hasUnread));
        }
      }
    });
    return c.future;
  } else {
    stateConditionMemberHasUnreadChat[app.documentID] = false;
    return Future.value([
      PackageConditionDetails(
          packageName: packageName,
          conditionName: conditionMemberHasUnreadChat,
          value: false),
      PackageConditionDetails(
          packageName: packageName,
          conditionName: conditionMemberAllHaveBeenRead,
          value: false),
    ]);
  }
}