Skip to content

Commit 73fd0f7

Browse files
committed
feat: implement #42
1 parent 4dccf56 commit 73fd0f7

6 files changed

+76
-0
lines changed

src/ui-mapbox/layers/layer-factory.android.ts

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ export class Layer implements LayerCommon {
3434
public getFilter(): any[] {
3535
return FilterParser.toJson(this.instance.getFilter());
3636
}
37+
38+
public setProperty(name: string, value: any) {
39+
const properties = PropertyParser.parsePropertiesForLayer({ [name]: value });
40+
this.instance.setProperties(properties);
41+
}
42+
43+
public getProperty(name: string): any {
44+
return PropertyParser.propertyValueFromLayer(this.instance, name);
45+
}
3746
}
3847

3948
export class LayerFactory {

src/ui-mapbox/layers/layer-factory.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ export declare class Layer implements LayerCommon {
1414
getNativeInstance(): any;
1515
setFilter(filter: any[]): void;
1616
getFilter(): any[];
17+
setProperty(name: string, value: any): void;
18+
getProperty(name: string): any;
1719
}

src/ui-mapbox/layers/layer-factory.ios.ts

+13
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@ export class Layer implements LayerCommon {
9090
getFilter(): any[] {
9191
return FilterParser.toJson(this.instance.predicate);
9292
}
93+
94+
setProperty(name: string, value: any) {
95+
const properties = PropertyParser.parsePropertiesForLayer({ [name]: value });
96+
for (const propKey in properties) {
97+
if (Object.prototype.hasOwnProperty.call(properties, propKey)) {
98+
this.instance[propKey] = properties[propKey];
99+
}
100+
}
101+
}
102+
103+
getProperty(name: string): any {
104+
return PropertyParser.propertyValueFromLayer(this.instance, name);
105+
}
93106
}

src/ui-mapbox/layers/parser/property-parser.android.ts

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ function toCamelCase(s) {
44
return s.replace(/([-_][a-z])/gi, ($1) => $1.toUpperCase().replace('-', '').replace('_', ''));
55
}
66

7+
function toPascalCase(s) {
8+
return s.replace(/(^[a-z]|[-_][a-z])/gi, ($1) => $1.toUpperCase().replace('-', '').replace('_', ''));
9+
}
10+
711
const Expression = com.mapbox.mapboxsdk.style.expressions.Expression;
812
function transformValue(key, value) {
913
let nValue = value;
@@ -36,4 +40,28 @@ export class PropertyParser {
3640

3741
return nProperties;
3842
}
43+
44+
static propertyValueFromLayer(layer, key: string): any {
45+
const getterMethodName = `get${toPascalCase(key)}`;
46+
47+
let nValue: com.mapbox.mapboxsdk.style.layers.PropertyValue<any>;
48+
try {
49+
nValue = layer[getterMethodName]();
50+
} catch (e) {
51+
// native method seems not exist
52+
return null;
53+
}
54+
55+
if (!nValue || nValue.isNull()) {
56+
return null;
57+
}
58+
59+
if (nValue.isExpression()) {
60+
return JSON.parse(nValue.getExpression().toString());
61+
} else if (!!nValue.getColorInt()) {
62+
return new Color(nValue.getColorInt().intValue());
63+
} else {
64+
return nValue.getValue();
65+
}
66+
}
3967
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export declare class PropertyParser {
22
static parsePropertiesForLayer(propertiesObject: any): any;
3+
static propertyValueFromLayer(layer, key: string): any;
34
}

src/ui-mapbox/layers/parser/property-parser.ios.ts

+23
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,27 @@ export class PropertyParser {
7878

7979
return nProperties;
8080
}
81+
82+
static propertyValueFromLayer(layer, key: string): any {
83+
const actualKey = keysMap[key] || toCamelCase(key);
84+
const nValue: NSExpression = layer[actualKey];
85+
86+
if (!nValue) {
87+
return null;
88+
}
89+
90+
if (nValue.expressionType === NSExpressionType.ConstantValueExpressionType) {
91+
if (nValue.constantValue instanceof UIColor) {
92+
return Color.fromIosColor(nValue.constantValue);
93+
} else {
94+
return nValue.constantValue;
95+
}
96+
} else {
97+
const expressionObj = (nValue as any).mgl_jsonExpressionObject;
98+
const data = NSJSONSerialization.dataWithJSONObjectOptionsError(expressionObj, 0);
99+
const expression: any = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding);
100+
101+
return JSON.parse(expression);
102+
}
103+
}
81104
}

0 commit comments

Comments
 (0)