actionOnEvent method

Future<bool> actionOnEvent(
  1. EventNotificationModel event,
  2. ATKEY_TYPE_ENUM keyType, {
  3. bool? isAccepted,
  4. bool? isSharing,
  5. bool? isExited,
  6. bool? isCancelled,
})

Processes any kind of update in an event and notifies creator/members

Implementation

Future<bool> actionOnEvent(
    EventNotificationModel event, ATKEY_TYPE_ENUM keyType,
    {bool? isAccepted,
    bool? isSharing,
    bool? isExited,
    bool? isCancelled}) async {
  var eventData = EventNotificationModel.fromJson(jsonDecode(
      EventNotificationModel.convertEventNotificationToJson(event)));

  try {
    var atkeyMicrosecondId =
        eventData.key!.split('createevent-')[1].split('@')[0];

    var currentAtsign =
        AtEventNotificationListener().atClientInstance!.currentAtSign!;

    eventData.isUpdate = true;
    if (eventData.atsignCreator!.toLowerCase() ==
        currentAtsign.toLowerCase()) {
      eventData.isSharing =
          // ignore: prefer_if_null_operators
          isSharing != null ? isSharing : eventData.isSharing;
      if (isSharing == false) {
        eventData.lat = null;
        eventData.long = null;
      }

      if (isCancelled == true) {
        eventData.isCancelled = true;
      }
    } else {
      eventData.group!.members!.forEach((member) {
        if (member.atSign![0] != '@') member.atSign = '@' + member.atSign!;
        if (currentAtsign[0] != '@') currentAtsign = '@' + currentAtsign;
        if (member.atSign!.toLowerCase() == currentAtsign.toLowerCase()) {
          member.tags!['isAccepted'] =
              // ignore: prefer_if_null_operators
              isAccepted != null ? isAccepted : member.tags!['isAccepted'];
          member.tags!['isSharing'] =
              // ignore: prefer_if_null_operators
              isSharing != null ? isSharing : member.tags!['isSharing'];
          member.tags!['isExited'] =
              // ignore: prefer_if_null_operators
              isExited != null ? isExited : member.tags!['isExited'];

          if (isSharing == false || isExited == true) {
            member.tags!['lat'] = null;
            member.tags!['long'] = null;
          }

          if (isExited == true) {
            member.tags!['isAccepted'] = false;
            member.tags!['isSharing'] = false;
          }
        }
      });
    }

    var key = formAtKey(keyType, atkeyMicrosecondId, eventData.atsignCreator,
        currentAtsign, event)!;

    // TODO : Check whther key is correct
    print('key $key');

    var notification =
        EventNotificationModel.convertEventNotificationToJson(eventData);
    var result = await atClientInstance!
        .put(key, notification, isDedicated: MixedConstants.isDedicated);

    if (MixedConstants.isDedicated) {
      await SyncSecondary().callSyncSecondary(SyncOperation.syncSecondary);
    }
    // if key type is createevent, we have to notify all members
    if (keyType == ATKEY_TYPE_ENUM.CREATEEVENT) {
      mapUpdatedEventDataToWidget(eventData);

      var allAtsignList = <String?>[];
      eventData.group!.members!.forEach((element) {
        allAtsignList.add(element.atSign);
      });

      key.sharedWith = jsonEncode(allAtsignList);
      await SyncSecondary().callSyncSecondary(
        SyncOperation.notifyAll,
        atKey: key,
        notification: notification,
        operation: OperationEnum.update,
        isDedicated: MixedConstants.isDedicated,
      );
    } else {
      ///  update pending status if receiver, add more if checks like already responded
      if (result) {
        updatePendingStatus(eventData);
      }
      notifyListeners();
    }

    return result;
  } catch (e) {
    print('error in updating event $e');
    return false;
  }
}