-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAdditionalExtensions.cs
307 lines (270 loc) · 9.5 KB
/
AdditionalExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
namespace MapboxMaui;
using System.Collections;
using CoreLocation;
using Foundation;
using MapboxMaui.Expressions;
using MapboxMaui.Offline;
using MapboxMaui.Styles;
using MapboxCoreMaps;
using MapboxMapsObjC;
using Microsoft.Maui.Controls.Compatibility.Platform.iOS;
using Microsoft.Maui.Platform;
using System.Runtime.InteropServices;
public static partial class AdditionalExtensions
{
internal static NSDictionary<NSString, TMBExpression> ToNative(this IDictionary<string, DslExpression> dictionary)
{
var list = new NSMutableDictionary<NSString, TMBExpression>();
foreach (var item in dictionary)
{
list[item.Key] = item.Value.ToPlatformValue();
}
return new NSDictionary<NSString, TMBExpression>(list.Keys, list.Values);
}
internal static TMBValue ToTMBValue<T>(this PropertyValue<T> propertyValue)
{
if (propertyValue.Expression != null)
{
var nativeExpression = propertyValue.Expression.ToPlatformValue();
return TMBValue.FromExpression(nativeExpression);
}
if (propertyValue.Value is Color color)
{
return TMBValue.FromConstant(color.ToPlatform());
}
return TMBValue.FromConstant(propertyValue.Value.Wrap());
}
internal static NSNumber[] ToPlatform(this double[] values, bool defaultToEmpty = false)
{
if (defaultToEmpty && values == null)
{
return Array.Empty<NSNumber>();
}
return values.Select(NSNumber.FromDouble).ToArray();
}
internal static NSNumber ToPlatform(this double? value)
=> value.HasValue
? NSNumber.FromDouble(value.Value)
: null;
internal static double[] ToDoubles(this NSNumber[] values, bool defaultToEmpty = false)
{
if (defaultToEmpty && values == null)
{
return Array.Empty<double>();
}
return values.Select(x => x.DoubleValue).ToArray();
}
internal static MBMStylePackLoadOptions ToNative(this StylePackLoadOptions options)
{
var metadata = new NSMutableDictionary();
if (options.Metadata != null)
{
foreach (var item in options.Metadata)
{
metadata[new NSString(item.Key)] = item.Value.Wrap();
}
}
return new MBMStylePackLoadOptions(
options.Mode.HasValue
? NSNumber.FromInt32((int)options.Mode)
: null,
metadata,
NSNumber.FromBoolean(options.AcceptsExpired)
);
}
internal static TMBTerrain ToPlatformValue(
this Terrain xvalue
)
{
var result = new TMBTerrain(xvalue.SourceId);
switch (xvalue.Exaggeration.Value)
{
case DslExpression expression:
result.Exaggeration = TMBValue.FromExpression(expression.ToPlatformValue());
break;
case double doubleValue:
result.Exaggeration = TMBValue.FromConstant(doubleValue.Wrap());
break;
}
return result;
}
internal static TMBLayerPosition ToPlatformValue(
this LayerPosition xvalue,
NSObject arg
)
{
var layerPositionType = xvalue.Enum switch
{
LayerPositionEnum.Above => TMBLayerPositionType.Above,
LayerPositionEnum.Below => TMBLayerPositionType.Below,
LayerPositionEnum.At => TMBLayerPositionType.At,
_ => (TMBLayerPositionType?)null
};
return layerPositionType.HasValue
? new TMBLayerPosition(layerPositionType.Value, arg)
: null;
}
internal static TMBStyleTransition ToPlatformValue(this StyleTransition xvalue)
{
return new TMBStyleTransition(xvalue.Duration, xvalue.Delay);
}
internal static NSObject Wrap(this object xvalue)
{
var result = xvalue switch
{
bool value => NSNumber.FromBoolean(value),
int value => NSNumber.FromLong(value),
long value => NSNumber.FromLong((nint)value),
float value => NSNumber.FromDouble(value),
double value => NSNumber.FromDouble(value),
string value => new NSString(value),
Color value => new NSString(value.ToRgbaString()),
INamedString value => new NSString(value.Value),
IPropertyValue value => value.Value is DslExpression expression1
? expression1.Wrap()
: value.Value.Wrap(),
_ => (NSObject)null
};
if (result != null) return result;
if (xvalue is DslExpression expression)
{
return NSArray.FromNSObjects(
expression
.ToObjects()
.Select(x => x.Wrap())
.ToArray()
);
}
if (xvalue is PromoteId promoteId)
{
return string.IsNullOrWhiteSpace(promoteId.StringValue)
? promoteId.KeyValues.Wrap()
: promoteId.StringValue.Wrap();
}
if (xvalue is ResolvedImage resolvedImage)
{
return resolvedImage.Id.Wrap();
}
if (xvalue is IDictionary<string, object> dict)
{
var list = new NSMutableDictionary<NSString, NSObject>();
foreach (var item in dict)
{
list[item.Key] = item.Value.Wrap();
}
return new NSDictionary<NSString, NSObject>(list.Keys, list.Values);
}
if (xvalue is IEnumerable objects)
{
var list = new List<NSObject>();
foreach (var item in objects)
{
list.Add(item.Wrap());
}
return NSArray.FromNSObjects(list.ToArray());
}
throw new NotSupportedException($"Invalue property type: {xvalue?.GetType()} | {xvalue}");
}
internal static NSDictionary<NSString, NSObject> ToPlatformValue(
this MapboxLayer xvalue
)
{
var properties = new NSMutableDictionary<NSString, NSObject>();
foreach (var property in xvalue.Properties)
{
var propertyValue = property.Value.Wrap();
properties[property.Key] = propertyValue;
}
return new NSDictionary<NSString, NSObject>(
properties.Keys,
properties.Values
);
}
internal static NSDictionary<NSString, NSObject> ToPlatformValue(
this MapboxSource source
)
{
// TODO Add volatile properties
var properties = new NSMutableDictionary<NSString, NSObject>();
foreach (var property in source.Properties)
{
var xvalue = property.Value.Wrap();
properties[property.Key] = xvalue;
}
return new NSDictionary<NSString, NSObject>(
properties.Keys,
properties.Values
);
}
public static TMBOrnamentVisibility ToNative(this OrnamentVisibility value)
{
return value switch
{
OrnamentVisibility.Adaptive => TMBOrnamentVisibility.Adaptive,
OrnamentVisibility.Visible => TMBOrnamentVisibility.Visible,
_ => TMBOrnamentVisibility.Hidden,
};
}
public static string ToNative(this MapboxStyle mapboxStyle)
{
return mapboxStyle.Value;
}
public static MBMMapDebugOptions ToNative(this DebugOption option)
{
return option switch
{
DebugOption.TileBorders => MBMMapDebugOptions.TileBorders,
DebugOption.ParseStatus => MBMMapDebugOptions.ParseStatus,
DebugOption.Timestamps => MBMMapDebugOptions.Timestamps,
DebugOption.Collision => MBMMapDebugOptions.Collision,
DebugOption.StencilClip => MBMMapDebugOptions.StencilClip,
DebugOption.DepthBuffer => MBMMapDebugOptions.DepthBuffer,
_ => MBMMapDebugOptions.ModelBounds,
};
}
public static IList<MBMMapDebugOptions> ToNative(this IEnumerable<DebugOption> options)
{
return options
.Select(x => x.ToNative())
.ToList();
}
public static TMBCameraOptions ToNative(this CameraOptions cameraOptions)
{
CLLocationCoordinate2D center = cameraOptions.Center is not null
? new CLLocationCoordinate2D(
cameraOptions.Center.Latitude,
cameraOptions.Center.Longitude)
: new CLLocationCoordinate2D(0, 0);
UIKit.UIEdgeInsets padding = cameraOptions.Padding.HasValue
? new UIKit.UIEdgeInsets(
(float)cameraOptions.Padding.Value.Top,
(float)cameraOptions.Padding.Value.Left,
(float)cameraOptions.Padding.Value.Bottom,
(float)cameraOptions.Padding.Value.Right)
: UIKit.UIEdgeInsets.Zero;
var anchor = cameraOptions.Anchor is not null
? new CoreGraphics.CGPoint(
cameraOptions.Anchor.Value.X,
cameraOptions.Anchor.Value.Y
)
: CoreGraphics.CGPoint.Empty;
var zoom = cameraOptions.Zoom.HasValue
? cameraOptions.Zoom.Value
: 14f;
var bearing = cameraOptions.Bearing.HasValue
? cameraOptions.Bearing.Value
: 0f;
var pitch = cameraOptions.Pitch.HasValue
? cameraOptions.Pitch.Value
: 0f;
return new TMBCameraOptions(
center,
padding,
anchor,
zoom,
bearing,
pitch
);
}
}