getAndSubscribe method
Future<List<PackageConditionDetails> > ?
getAndSubscribe(
- AccessBloc accessBloc,
- AppModel app,
- MemberModel? member,
- bool isOwner,
- bool? isBlocked,
- 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] =
corerepo.accessRepository(appId: appId)!.listen((list) {
var valueHasNoMembershipYet = list.isEmpty ||
(list.first == null) ||
((list.first!.blocked == null) || (!list.first!.blocked!)) &&
((list.first!.privilegeLevel == null) ||
(list.first!.privilegeLevel == PrivilegeLevel.noPrivilege));
if (!c.isCompleted) {
// the first time we get this trigger, it's upon entry of the getAndSubscribe. Now we simply return the value
stateMemberHasNoMembershipYet[appId] = valueHasNoMembershipYet;
c.complete([
PackageConditionDetails(
packageName: packageName,
conditionName: memberHasNoMembershipYet,
value: valueHasNoMembershipYet)
]);
} else {
// subsequent calls we get this trigger, it's when the date has changed. Now add the event to the bloc
if (valueHasNoMembershipYet != stateMemberHasNoMembershipYet[appId]) {
stateMemberHasNoMembershipYet[appId] = valueHasNoMembershipYet;
noMembershipYet(accessBloc, app, valueHasNoMembershipYet);
}
var first = list.first;
if (first == null) {
accessBloc.add(PrivilegeChangedEvent(
app,
PrivilegeLevel.noPrivilege,
false,
));
} else {
accessBloc.add(PrivilegeChangedEvent(
app,
first.privilegeLevel ?? PrivilegeLevel.noPrivilege,
first.blocked ?? false,
));
}
}
}, eliudQuery: getAccessQuery(appId, member.documentID));
return c.future;
} else {
stateMemberHasNoMembershipYet[appId] = false;
return Future.value([
PackageConditionDetails(
packageName: packageName,
conditionName: memberHasNoMembershipYet,
value: false)
]);
}
}