Skip to content

Commit

Permalink
Batch create, batch delete symbols [only batch actions on native sides]
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasia Chechulina committed Jun 9, 2020
1 parent e3c4bc2 commit d71c4a3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,7 @@ private CameraPosition getCameraPosition() {
}

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

private void removeSymbol(String symbolId) {
final SymbolController symbolController = symbols.remove(symbolId);
if (symbolController != null) {
symbolController.remove(symbolManager);
}
return new SymbolBuilder();
}

private SymbolController symbol(String symbolId) {
Expand Down Expand Up @@ -558,15 +551,6 @@ public void onError(@NonNull String message) {
break;
}
case "symbol#add": {
final SymbolBuilder symbolBuilder = newSymbolBuilder();
Convert.interpretSymbolOptions(call.argument("options"), symbolBuilder);
final Symbol symbol = symbolBuilder.build();
final String symbolId = String.valueOf(symbol.getId());
symbols.put(symbolId, new SymbolController(symbol, true, this));
result.success(symbolId);
break;
}
case "symbol#addAll": {
List<String> newSymbolIds = new ArrayList<String>();
final List<Object> options = call.argument("options");
List<SymbolOptions> symbolOptionsList = new ArrayList<SymbolOptions>();
Expand All @@ -591,12 +575,6 @@ public void onError(@NonNull String message) {
break;
}
case "symbol#remove": {
final String symbolId = call.argument("symbol");
removeSymbol(symbolId);
result.success(null);
break;
}
case "symbol#removeAll": {
final ArrayList<String> symbolIds = call.argument("symbols");
SymbolController symbolController;

Expand Down
8 changes: 1 addition & 7 deletions android/src/main/java/com/mapbox/mapboxgl/SymbolBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions;

class SymbolBuilder implements SymbolOptionsSink {
private final SymbolManager symbolManager;
private final SymbolOptions symbolOptions;
private static boolean customImage;

SymbolBuilder(SymbolManager symbolManager) {
this.symbolManager = symbolManager;
SymbolBuilder() {
this.symbolOptions = new SymbolOptions();
}

Symbol build() {
return symbolManager.create(symbolOptions);
}

public SymbolOptions getSymbolOptions(){
return this.symbolOptions;
}
Expand Down
36 changes: 23 additions & 13 deletions example/lib/place_symbol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,19 @@ class PlaceSymbolBodyState extends State<PlaceSymbolBody> {
}

void _add(String iconImage) {
controller.addSymbol(_getSymbolOptions(iconImage, _symbolCount));
setState(() {
_symbolCount += 1;
});
List<int> availableNumbers = Iterable<int>.generate(12).toList();
controller.symbols.forEach(
(s) => availableNumbers.removeWhere((i) => i == s.data['count'])
);
if (availableNumbers.isNotEmpty) {
controller.addSymbol(
_getSymbolOptions(iconImage, availableNumbers.first),
{'count': availableNumbers.first}
);
setState(() {
_symbolCount += 1;
});
}
}

SymbolOptions _getSymbolOptions(String iconImage, int symbolCount){
Expand All @@ -106,17 +115,18 @@ class PlaceSymbolBodyState extends State<PlaceSymbolBody> {
}

Future<void> _addAll(String iconImage) async {
int symbolsToAddAmount = 12 - _symbolCount;
if (symbolsToAddAmount > 0) {
final List<SymbolOptions> symbolOptionsList = Iterable<SymbolOptions>.generate(
symbolsToAddAmount,
(i) => _getSymbolOptions(
iconImage,
_symbolCount + i
)
List<int> symbolsToAddNumbers = Iterable<int>.generate(12).toList();
controller.symbols.forEach(
(s) => symbolsToAddNumbers.removeWhere((i) => i == s.data['count'])
);

if (symbolsToAddNumbers.isNotEmpty) {
final List<SymbolOptions> symbolOptionsList = symbolsToAddNumbers.map(
(i) => _getSymbolOptions(iconImage, i)
).toList();
controller.addSymbols(
symbolOptionsList
symbolOptionsList,
symbolsToAddNumbers.map((i) => {'count': i}).toList()
);

setState(() {
Expand Down
24 changes: 0 additions & 24 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,6 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
case "symbol#add":
guard let symbolAnnotationController = symbolAnnotationController else { return }
guard let arguments = methodCall.arguments as? [String: Any] else { return }


if let options = arguments["options"] as? [String: Any],
let symbol = getSymbolForOptions(options: options) {
symbolAnnotationController.addStyleAnnotation(symbol)
result(symbol.identifier)
} else {
result(nil)
}
case "symbol#addAll":
guard let symbolAnnotationController = symbolAnnotationController else { return }
guard let arguments = methodCall.arguments as? [String: Any] else { return }

if let options = arguments["options"] as? [[String: Any]] {
var symbols: [MGLSymbolStyleAnnotation] = [];
Expand Down Expand Up @@ -201,18 +189,6 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
}
result(nil)
case "symbol#remove":
guard let symbolAnnotationController = symbolAnnotationController else { return }
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let symbolId = arguments["symbol"] as? String else { return }

for symbol in symbolAnnotationController.styleAnnotations(){
if symbol.identifier == symbolId {
symbolAnnotationController.removeStyleAnnotation(symbol)
break;
}
}
result(nil)
case "symbol#removeAll":
guard let symbolAnnotationController = symbolAnnotationController else { return }
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let symbolIds = arguments["symbols"] as? [String] else { return }
Expand Down
13 changes: 10 additions & 3 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,26 @@ class MapboxMapController extends ChangeNotifier {
}


Future<List<Symbol>> addSymbols(List<SymbolOptions> options) async {
Future<List<Symbol>> addSymbols(List<SymbolOptions> options, [List<Map> data]) async {
final List<SymbolOptions> effectiveOptions = options.map(
(o) => SymbolOptions.defaultOptions.copyWith(o)
).toList();

final List<dynamic> symbolIds = await _channel.invokeMethod(
'symbol#addAll',
'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)))
(i, id) => MapEntry(
i,
Symbol(
id,
effectiveOptions.elementAt(i),
data != null && data.length > i ? data.elementAt(i) : null
)
)
).values.toList();

symbols.forEach((s) => _symbols[s.id] = s);
Expand Down

0 comments on commit d71c4a3

Please sign in to comment.