addPolygonGeofence method

Future<bool> addPolygonGeofence(
  1. PolygonGeofence polygon
)

Adds a polygon geofence.

Throws ArgumentError if the polygon is invalid. Returns true if added successfully, false if identifier already exists.

Implementation

Future<bool> addPolygonGeofence(PolygonGeofence polygon) async {
  if (!polygon.isValid) {
    throw ArgumentError('Invalid polygon geofence: ${polygon.identifier}. '
        'Must have non-empty identifier and at least 3 valid vertices.');
  }

  if (_polygons.containsKey(polygon.identifier)) {
    debugPrint(
        '[PolygonGeofenceService] Polygon already exists: ${polygon.identifier}');
    return false;
  }

  _polygons[polygon.identifier] = polygon;
  _insideState[polygon.identifier] = false;
  await _persist();

  debugPrint('[PolygonGeofenceService] Added polygon: ${polygon.identifier} '
      '(${polygon.vertices.length} vertices)');
  return true;
}