locationDataUpdated method

bool locationDataUpdated({
  1. required double latitude,
  2. required double longitude,
  3. double? lastLatitude,
  4. double? lastLongitude,
})

Function called when new location data is available. @param latitude The new latitude. @param longitude The new longitude. @param lastLatitude The last latitude seen by MBAutomation. @param lastLongitude The last longitude seen by MBAutomation. @returns If this trigger has changed.

Implementation

bool locationDataUpdated({
  required double latitude,
  required double longitude,
  double? lastLatitude,
  double? lastLongitude,
}) {
  if (completionDate != null) {
    return false;
  }

  double currentDistanceFromCenter = _calculateDistance(
    latitude,
    longitude,
    this.latitude,
    this.longitude,
  ).abs();

  bool isInside = currentDistanceFromCenter <= radius;
  bool locationTriggerSatisfied = false;
  if (isInside) {
    if (lastLatitude != null && lastLongitude != null) {
      double lastLocationDistanceFromCenter = _calculateDistance(
        lastLatitude,
        lastLongitude,
        this.latitude,
        this.longitude,
      ).abs();
      bool lastLocationIsInside = lastLocationDistanceFromCenter <= radius;
      locationTriggerSatisfied = !lastLocationIsInside;
    } else {
      locationTriggerSatisfied = true;
    }
  }

  if (locationTriggerSatisfied) {
    if (afterDays == 0) {
      completionDate = DateTime.now();
      return true;
    } else {
      completionDate = DateTime.now().add(Duration(days: afterDays));
      return false;
    }
  }
  return false;
}