prepareLocationDataAndSend method

Future<void> prepareLocationDataAndSend(
  1. EventNotificationModel _storedEventNotificationModel,
  2. LatLng _myLocation
)

Implementation

Future<void> prepareLocationDataAndSend(
    EventNotificationModel _storedEventNotificationModel,
    LatLng _myLocation) async {
  late EventNotificationModel _eventNotificationModel;

  /// To get updated event data
  for (var i = 0;
      i < EventKeyStreamService().allEventNotifications.length;
      i++) {
    if (EventKeyStreamService()
            .allEventNotifications[i]
            .eventNotificationModel!
            .key ==
        _storedEventNotificationModel.key) {
      _eventNotificationModel = EventKeyStreamService()
          .allEventNotifications[i]
          .eventNotificationModel!;
      break;
    }
  }

  // ignore: unnecessary_null_comparison
  if (_eventNotificationModel == null) {
    return;
  }

  if (_eventNotificationModel.isCancelled == true) {
    return;
  }

  if (_eventNotificationModel.atsignCreator ==
      AtEventNotificationListener().currentAtSign) {
    var _from = _eventNotificationModel.event!.startTime!;
    var _to = _eventNotificationModel.event!.endTime;

    if ((DateTime.now().difference(_from) > Duration(seconds: 0)) &&
        (_to!.difference(DateTime.now()) > Duration(seconds: 0))) {
      var _event = EventNotificationModel.fromJson(jsonDecode(
          EventNotificationModel.convertEventNotificationToJson(
              _eventNotificationModel)));

      _event.lat = _myLocation.latitude;
      _event.long = _myLocation.longitude;

      await EventKeyStreamService()
          .actionOnEvent(_event, ATKEY_TYPE_ENUM.CREATEEVENT);
    }
  } else {
    late var currentGroupMember;

    /// TODO: Optimise this, dont do this on every loop. Do only once
    _eventNotificationModel.group!.members!.forEach((groupMember) {
      // sending location to other group members
      if (groupMember.atSign == AtEventNotificationListener().currentAtSign) {
        currentGroupMember = groupMember;
      }
    });

    var _from = startTimeEnumToTimeOfDay(
        currentGroupMember.tags['shareFrom'].toString(),
        _eventNotificationModel.event!.startTime)!;
    var _to = endTimeEnumToTimeOfDay(
        currentGroupMember.tags['shareTo'].toString(),
        _eventNotificationModel.event!.endTime);

    if ((DateTime.now().difference(_from) > Duration(seconds: 0)) &&
        (_to!.difference(DateTime.now()) > Duration(seconds: 0))) {
      var _data = EventMemberLocation(
          fromAtSign: AtEventNotificationListener().currentAtSign,
          receiver: _eventNotificationModel.atsignCreator,
          key: _eventNotificationModel.key,
          lat: _myLocation.latitude,
          long: _myLocation.longitude);

      var atkeyMicrosecondId =
          _eventNotificationModel.key!.split('-')[1].split('@')[0];

      var atKey = newAtKey(
          5000,
          '${MixedConstants.EVENT_MEMBER_LOCATION_KEY}-$atkeyMicrosecondId',
          _eventNotificationModel.atsignCreator);

      try {
        await AtEventNotificationListener().atClientInstance!.put(
            atKey,
            EventMemberLocation.convertLocationNotificationToJson(
              _data,
            ),
            isDedicated: MixedConstants.isDedicated);
      } catch (e) {
        print('error in sending location: $e');
      }
    }
  }
}