Skip to content

Commit

Permalink
Batch create, batch delete symbols [for web platform]
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasia Chechulina committed Jun 9, 2020
1 parent d71c4a3 commit 647e2ac
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 100 deletions.
50 changes: 23 additions & 27 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ private CameraPosition getCameraPosition() {
return trackCameraPosition ? mapboxMap.getCameraPosition() : null;
}

private SymbolBuilder newSymbolBuilder() {
return new SymbolBuilder();
}

private SymbolController symbol(String symbolId) {
final SymbolController symbol = symbols.get(symbolId);
if (symbol == null) {
Expand Down Expand Up @@ -550,31 +546,31 @@ public void onError(@NonNull String message) {
});
break;
}
case "symbol#add": {
List<String> newSymbolIds = new ArrayList<String>();
final List<Object> options = call.argument("options");
List<SymbolOptions> symbolOptionsList = new ArrayList<SymbolOptions>();
if (options != null) {
SymbolBuilder symbolBuilder;
for (Object o : options) {
symbolBuilder = newSymbolBuilder();
Convert.interpretSymbolOptions(o, symbolBuilder);
symbolOptionsList.add(symbolBuilder.getSymbolOptions());
}
if (!symbolOptionsList.isEmpty()) {
List<Symbol> newSymbols = symbolManager.create(symbolOptionsList);
String symbolId;
for (Symbol symbol : newSymbols) {
symbolId = String.valueOf(symbol.getId());
newSymbolIds.add(symbolId);
symbols.put(symbolId, new SymbolController(symbol, true, this));
}
}
case "symbols#addAll": {
List<String> newSymbolIds = new ArrayList<String>();
final List<Object> options = call.argument("options");
List<SymbolOptions> symbolOptionsList = new ArrayList<SymbolOptions>();
if (options != null) {
SymbolBuilder symbolBuilder;
for (Object o : options) {
symbolBuilder = new SymbolBuilder();
Convert.interpretSymbolOptions(o, symbolBuilder);
symbolOptionsList.add(symbolBuilder.getSymbolOptions());
}
result.success(newSymbolIds);
break;
if (!symbolOptionsList.isEmpty()) {
List<Symbol> newSymbols = symbolManager.create(symbolOptionsList);
String symbolId;
for (Symbol symbol : newSymbols) {
symbolId = String.valueOf(symbol.getId());
newSymbolIds.add(symbolId);
symbols.put(symbolId, new SymbolController(symbol, true, this));
}
}
}
result.success(newSymbolIds);
break;
}
case "symbol#remove": {
case "symbols#removeAll": {
final ArrayList<String> symbolIds = call.argument("symbols");
SymbolController symbolController;

Expand Down
61 changes: 16 additions & 45 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,9 @@ class MapboxMapController extends ChangeNotifier {
/// The returned [Future] completes with the added symbol once listeners have
/// been notified.
Future<Symbol> addSymbol(SymbolOptions options, [Map data]) async {
final SymbolOptions effectiveOptions =
SymbolOptions.defaultOptions.copyWith(options);
final symbol =
await MapboxGlPlatform.getInstance(_id).addSymbol(effectiveOptions);
_symbols[symbol.id] = symbol;
notifyListeners();
return symbol;
List<Symbol> result = await addSymbols([options], [data]);

return result.first;
}


Expand All @@ -336,23 +332,7 @@ class MapboxMapController extends ChangeNotifier {
(o) => SymbolOptions.defaultOptions.copyWith(o)
).toList();

final List<dynamic> symbolIds = await _channel.invokeMethod(
'symbol#add',
<String, dynamic>{
'options': effectiveOptions.map((o) => o._toJson()).toList(),
},
);
final List<Symbol> symbols = symbolIds.asMap().map(
(i, id) => MapEntry(
i,
Symbol(
id,
effectiveOptions.elementAt(i),
data != null && data.length > i ? data.elementAt(i) : null
)
)
).values.toList();

final symbols = await MapboxGlPlatform.instance.addSymbols(effectiveOptions, data);
symbols.forEach((s) => _symbols[s.id] = s);
notifyListeners();
return symbols;
Expand Down Expand Up @@ -396,18 +376,18 @@ class MapboxMapController extends ChangeNotifier {
Future<void> removeSymbol(Symbol symbol) async {
assert(symbol != null);
assert(_symbols[symbol.id] == symbol);
await _removeSymbol(symbol.id);
await _removeSymbols([symbol.id]);
notifyListeners();
}

Future<void> removeSymbols(Iterable<Symbol> symbols) async {
assert(symbols.length > 0);
symbols.forEach((s) {
assert(_symbols[s._id] == s);
});
await _removeSymbols(symbols.map((s) => s._id).toList());
notifyListeners();
assert(symbols.length > 0);
symbols.forEach((s) {
assert(_symbols[s.id] == s);
});

await _removeSymbols(symbols.map((s) => s.id));
notifyListeners();
}

/// Removes all [symbols] from the map.
Expand All @@ -419,9 +399,7 @@ class MapboxMapController extends ChangeNotifier {
Future<void> clearSymbols() async {
assert(_symbols != null);
final List<String> symbolIds = List<String>.from(_symbols.keys);
for (String id in symbolIds) {
await _removeSymbol(id);
}
_removeSymbols(symbolIds);
notifyListeners();
}

Expand All @@ -430,16 +408,9 @@ class MapboxMapController extends ChangeNotifier {
///
/// The returned [Future] completes once the symbol has been removed from
/// [_symbols].
Future<void> _removeSymbol(String id) async {
await MapboxGlPlatform.getInstance(_id).removeSymbol(id);
_symbols.remove(id);
}

Future<void> _removeSymbols(List<String> ids) async {
await _channel.invokeMethod('symbol#removeAll', <String, dynamic>{
'symbols': ids,
});
_symbols.removeWhere((k, s) => ids.contains(k));
Future<void> _removeSymbols(Iterable<String> ids) async {
await MapboxGlPlatformm.getInstance(_id).removeSymbols(ids);
_symbols.removeWhere((k, s) => ids.contains(k));
}

/// Adds a line to the map, configured using the specified custom [options].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ abstract class MapboxGlPlatform {
Future<bool> getTelemetryEnabled() async {
throw UnimplementedError('getTelemetryEnabled() has not been implemented.');
}

Future<Symbol> addSymbol(SymbolOptions options, [Map data]) async {
throw UnimplementedError('addSymbol() has not been implemented.');
Future<List<Symbol>> addSymbols(List<SymbolOptions> options, [List<Map> data]) async {
throw UnimplementedError('addSymbols() has not been implemented.');
}

Future<void> updateSymbol(Symbol symbol, SymbolOptions changes) async {
throw UnimplementedError('updateSymbol() has not been implemented.');
}

Future<void> removeSymbol(String symbolId) async {
Future<void> removeSymbols(Iterable<String> symbolsIds) async {
throw UnimplementedError('removeSymbol() has not been implemented.');
}

Expand Down
27 changes: 19 additions & 8 deletions mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,25 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
}

@override
Future<Symbol> addSymbol(SymbolOptions options, [Map data]) async {
final String symbolId = await _channel.invokeMethod(
'symbol#add',
Future<List<Symbol>> addSymbols(List<SymbolOptions> options, [List<Map> data]) async {
final List<dynamic> symbolIds = await _channel.invokeMethod(
'symbols#addAll',
<String, dynamic>{
'options': options.toJson(),
'options': options.map((o) => o.toJson()).toList(),
},
);
return Symbol(symbolId, options, data);
final List<Symbol> symbols = symbolIds.asMap().map(
(i, id) => MapEntry(
i,
Symbol(
id,
options.elementAt(i),
data != null && data.length > i ? data.elementAt(i) : null
)
)
).values.toList();

return symbols;
}

@override
Expand All @@ -212,9 +223,9 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
}

@override
Future<void> removeSymbol(String symbolId) async {
await _channel.invokeMethod('symbol#remove', <String, dynamic>{
'symbol': symbolId,
Future<void> removeSymbols(Iterable<String> ids) async {
await _channel.invokeMethod('symbols#removeAll', <String, dynamic>{
'symbols': ids.toList(),
});
}

Expand Down
20 changes: 17 additions & 3 deletions mapbox_gl_web/lib/src/feature_manager/feature_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@ abstract class FeatureManager<T> {
return '${feature.id}';
}


void updateFeature(Feature feature) {
_features['${feature.id}'] = feature;
_updateSource();
updateFeatures([feature]);
}


void updateFeatures(Iterable<Feature> features) {
features.forEach(
(feature) => _features['${feature.id}'] = feature
);
_updateSource();
}

void remove(String featureId) {
_features.remove(featureId);
removeAll([featureId]);
}

void removeAll(Iterable<String> featuresIds) {
featuresIds.forEach(
(featureId) => _features.remove(featureId)
);
_updateSource();
}

Expand Down
18 changes: 15 additions & 3 deletions mapbox_gl_web/lib/src/feature_manager/symbol_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,21 @@ class SymbolManager extends FeatureManager<SymbolOptions> {

@override
void update(String lineId, SymbolOptions changes) {
Feature olfFeature = getFeature(lineId);
Feature newFeature = Convert.interpretSymbolOptions(changes, olfFeature);
updateFeature(newFeature);
updateAll({lineId: changes});
}


void updateAll(Map<String, SymbolOptions> changesById) {
List<Feature> featuresWithUpdatedOptions = [];
changesById.forEach(
(id, options) => featuresWithUpdatedOptions.add(
Convert.interpretSymbolOptions(
options,
getFeature(id)
)
)
);
updateFeatures(featuresWithUpdatedOptions);
}

@override
Expand Down
36 changes: 26 additions & 10 deletions mapbox_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,31 @@ class MapboxMapController extends MapboxGlPlatform
}

@override
Future<Symbol> addSymbol(SymbolOptions options, [Map data]) async {
String symbolId = symbolManager.add(Feature(
geometry: Geometry(
type: 'Point',
coordinates: [options.geometry.longitude, options.geometry.latitude],
Future<List<Symbol>> addSymbols(List<SymbolOptions> options, [List<Map> data]) async {
Map<String, SymbolOptions> optionsById = Map.fromIterable(
options,
key: (o) => symbolManager.add(
Feature(
geometry: Geometry(
type: 'Point',
coordinates: [o.geometry.longitude, o.geometry.latitude],
),
)
),
));
symbolManager.update(symbolId, options);
return Symbol(symbolId, options, data);
value: (o) => o
);
symbolManager.updateAll(optionsById);

return optionsById.map(
(id, singleOptions) {
int dataIndex = options.indexOf(singleOptions);
Map singleData = data != null && data.length >= dataIndex + 1 ? data[dataIndex] : null;
return MapEntry(
id,
Symbol(id, singleOptions, singleData)
);
}
).values.toList();
}

@override
Expand All @@ -153,8 +169,8 @@ class MapboxMapController extends MapboxGlPlatform
}

@override
Future<void> removeSymbol(String symbolId) async {
symbolManager.remove(symbolId);
Future<void> removeSymbols(Iterable<String> symbolsIds) async {
symbolManager.removeAll(symbolsIds);
}

@override
Expand Down

0 comments on commit 647e2ac

Please sign in to comment.