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] = memberCartRepository(appId: appId)!.listen((list) {
      var cartHasItems = (list.isNotEmpty) &&
          (list.first!.cartItems != null) &&
          (list.first!.cartItems!.isNotEmpty);
      if (!c.isCompleted) {
        stateConditionCartsHasItems[appId] = cartHasItems;
        // 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: conditionCartsHasItems,
              value: cartHasItems)
        ]);
      } else {
        // subsequent calls we get this trigger, it's when the date has changed. Now add the event to the bloc
        if (cartHasItems != stateConditionCartsHasItems[appId]) {
          stateConditionCartsHasItems[appId] = cartHasItems;
          accessBloc.add(UpdatePackageConditionEvent(
              app, this, conditionCartsHasItems, cartHasItems));
        }
      }
    }, eliudQuery: getCartQuery(appId, member.documentID));
    return c.future;
  } else {
    stateConditionCartsHasItems[appId] = false;
    return Future.value([
      PackageConditionDetails(
          packageName: packageName,
          conditionName: conditionCartsHasItems,
          value: false)
    ]);
  }
}