PolygonUpdates.from constructor

PolygonUpdates.from(
  1. Set<Polygon> previous,
  2. Set<Polygon> current
)

通过Polygon的前后更新集合构造一个PolygonUpdates

Implementation

PolygonUpdates.from(Set<Polygon> previous, Set<Polygon> current) {
  // ignore: unnecessary_null_comparison
  if (previous == null) {
    previous = Set<Polygon>.identity();
  }

  // ignore: unnecessary_null_comparison
  if (current == null) {
    current = Set<Polygon>.identity();
  }

  final Map<String, Polygon> previousPolygons = keyByPolygonId(previous);
  final Map<String, Polygon> currentPolygons = keyByPolygonId(current);

  final Set<String> prevPolygonIds = previousPolygons.keys.toSet();
  final Set<String> currentPolygonIds = currentPolygons.keys.toSet();

  Polygon idToCurrentPolygon(String id) {
    return currentPolygons[id]!;
  }

  final Set<String> _polygonIdsToRemove =
      prevPolygonIds.difference(currentPolygonIds);

  final Set<Polygon> _polygonsToAdd = currentPolygonIds
      .difference(prevPolygonIds)
      .map(idToCurrentPolygon)
      .toSet();

  bool hasChanged(Polygon current) {
    final Polygon previous = previousPolygons[current.id]!;
    return current != previous;
  }

  final Set<Polygon> _polygonsToChange = currentPolygonIds
      .intersection(prevPolygonIds)
      .map(idToCurrentPolygon)
      .where(hasChanged)
      .toSet();

  polygonsToAdd = _polygonsToAdd;
  polygonIdsToRemove = _polygonIdsToRemove;
  polygonsToChange = _polygonsToChange;
}