PolylineUpdates.from constructor

PolylineUpdates.from(
  1. Set<Polyline> previous,
  2. Set<Polyline> current
)

通过polyline的前后更新集合构造一个polylineUpdates

Implementation

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

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

  final Map<String, Polyline> previousPolylines = keyByPolylineId(previous);
  final Map<String, Polyline> currentPolylines = keyByPolylineId(current);

  final Set<String> prevPolylineIds = previousPolylines.keys.toSet();
  final Set<String> currentPolylineIds = currentPolylines.keys.toSet();

  Polyline idToCurrentPolyline(String id) {
    return currentPolylines[id]!;
  }

  final Set<String> _polylineIdsToRemove =
      prevPolylineIds.difference(currentPolylineIds);

  final Set<Polyline> _polylinesToAdd = currentPolylineIds
      .difference(prevPolylineIds)
      .map(idToCurrentPolyline)
      .toSet();

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

  final Set<Polyline> _polylinesToChange = currentPolylineIds
      .intersection(prevPolylineIds)
      .map(idToCurrentPolyline)
      .where(hasChanged)
      .toSet();

  polylinesToAdd = _polylinesToAdd;
  polylineIdsToRemove = _polylineIdsToRemove;
  polylinesToChange = _polylinesToChange;
}