@@ -5,15 +5,20 @@ import android.graphics.Bitmap
5
5
import com.google.gson.Gson
6
6
import com.mapbox.bindgen.Expected
7
7
import com.mapbox.bindgen.None
8
+ import com.mapbox.bindgen.Value
9
+ import com.mapbox.common.TileRegionError
8
10
import com.mapbox.geojson.*
9
11
import com.mapbox.maps.EdgeInsets
12
+ import com.mapbox.maps.StylePackError
13
+ import com.mapbox.maps.extension.style.expressions.dsl.generated.min
10
14
import com.mapbox.maps.extension.style.layers.properties.generated.ProjectionName
11
15
import com.mapbox.maps.extension.style.light.LightPosition
12
16
import com.mapbox.maps.extension.style.light.generated.ambientLight
13
17
import com.mapbox.maps.extension.style.light.generated.directionalLight
14
18
import com.mapbox.maps.extension.style.light.generated.flatLight
15
19
import com.mapbox.maps.extension.style.projection.generated.Projection
16
20
import com.mapbox.maps.extension.style.types.StyleTransition
21
+ import com.mapbox.maps.logE
17
22
import com.mapbox.maps.mapbox_maps.pigeons.*
18
23
import org.json.JSONArray
19
24
import org.json.JSONObject
@@ -28,6 +33,14 @@ fun GlyphsRasterizationMode.toGlyphsRasterizationMode(): com.mapbox.maps.GlyphsR
28
33
GlyphsRasterizationMode .IDEOGRAPHS_RASTERIZED_LOCALLY -> com.mapbox.maps.GlyphsRasterizationMode .IDEOGRAPHS_RASTERIZED_LOCALLY
29
34
}
30
35
}
36
+
37
+ fun com.mapbox.maps.GlyphsRasterizationMode.toFLTGlyphsRasterizationMode (): GlyphsRasterizationMode {
38
+ return when (this ) {
39
+ com.mapbox.maps.GlyphsRasterizationMode .NO_GLYPHS_RASTERIZED_LOCALLY -> GlyphsRasterizationMode .NO_GLYPHS_RASTERIZED_LOCALLY
40
+ com.mapbox.maps.GlyphsRasterizationMode .IDEOGRAPHS_RASTERIZED_LOCALLY -> GlyphsRasterizationMode .IDEOGRAPHS_RASTERIZED_LOCALLY
41
+ com.mapbox.maps.GlyphsRasterizationMode .ALL_GLYPHS_RASTERIZED_LOCALLY -> GlyphsRasterizationMode .ALL_GLYPHS_RASTERIZED_LOCALLY
42
+ }
43
+ }
31
44
fun GlyphsRasterizationOptions.toGlyphsRasterizationOptions (): com.mapbox.maps.GlyphsRasterizationOptions {
32
45
return com.mapbox.maps.GlyphsRasterizationOptions .Builder ()
33
46
.rasterizationMode(rasterizationMode.toGlyphsRasterizationMode())
@@ -528,10 +541,198 @@ fun Bitmap.toMbxImage(): MbxImage {
528
541
return MbxImage (width.toLong(), height.toLong(), outputStream.toByteArray())
529
542
}
530
543
544
+ fun StylePackLoadOptions.toStylePackLoadOptions (): com.mapbox.maps.StylePackLoadOptions {
545
+ return com.mapbox.maps.StylePackLoadOptions .Builder ()
546
+ .glyphsRasterizationMode(glyphsRasterizationMode?.toGlyphsRasterizationMode())
547
+ .metadata(metadata?.toValue())
548
+ .acceptExpired(acceptExpired)
549
+ .build()
550
+ }
551
+
552
+ fun com.mapbox.maps.StylePack.toFLTStylePack (): StylePack {
553
+ return StylePack (
554
+ styleURI = this .styleURI,
555
+ glyphsRasterizationMode = this .glyphsRasterizationMode.toFLTGlyphsRasterizationMode(),
556
+ requiredResourceCount = this .requiredResourceCount,
557
+ completedResourceSize = this .completedResourceCount,
558
+ completedResourceCount = this .completedResourceCount,
559
+ expires = this .expires?.time
560
+ )
561
+ }
562
+
563
+ fun com.mapbox.maps.StylePackLoadProgress.toFLTStylePackLoadProgress (): StylePackLoadProgress {
564
+ return StylePackLoadProgress (
565
+ this .completedResourceCount,
566
+ this .completedResourceSize,
567
+ this .erroredResourceCount,
568
+ this .requiredResourceCount,
569
+ this .loadedResourceCount,
570
+ this .loadedResourceSize
571
+ )
572
+ }
573
+
574
+ fun TilesetDescriptorOptions.toTilesetDescriptorOptions (): com.mapbox.maps.TilesetDescriptorOptions {
575
+ val builder = com.mapbox.maps.TilesetDescriptorOptions .Builder ()
576
+ .styleURI(styleURI)
577
+ .minZoom(minZoom.toByte())
578
+ .maxZoom(maxZoom.toByte())
579
+ pixelRatio?.let { builder.pixelRatio(it.toFloat()) }
580
+ tilesets?.let { builder.tilesets(it) }
581
+ stylePackOptions?.let { builder.stylePackOptions(it.toStylePackLoadOptions()) }
582
+ extraOptions?.let { builder.extraOptions(it.toValue()) }
583
+ return builder.build()
584
+ }
585
+
586
+ fun NetworkRestriction.toNetworkRestriction (): com.mapbox.common.NetworkRestriction {
587
+ return when (this ) {
588
+ NetworkRestriction .DISALLOW_ALL -> com.mapbox.common.NetworkRestriction .DISALLOW_ALL
589
+ NetworkRestriction .DISALLOW_EXPENSIVE -> com.mapbox.common.NetworkRestriction .DISALLOW_EXPENSIVE
590
+ NetworkRestriction .NONE -> com.mapbox.common.NetworkRestriction .NONE
591
+ }
592
+ }
593
+
594
+ fun com.mapbox.common.TileRegion.toFLTTileRegion (): TileRegion {
595
+ return TileRegion (
596
+ this .id,
597
+ this .requiredResourceCount,
598
+ this .completedResourceCount,
599
+ this .completedResourceSize
600
+ )
601
+ }
602
+
603
+ fun TileRegionEstimateOptions.toTileRegionEstimateOptions (): com.mapbox.common.TileRegionEstimateOptions {
604
+ return com.mapbox.common.TileRegionEstimateOptions (
605
+ this .errorMargin.toFloat(),
606
+ this .preciseEstimationTimeout.toLong(),
607
+ this .preciseEstimationTimeout.toLong(),
608
+ this .extraOptions?.toValue()
609
+ )
610
+ }
611
+
612
+ fun com.mapbox.common.TileRegionEstimateResult.toFLTTileRegionEstimateResult (): TileRegionEstimateResult {
613
+ return TileRegionEstimateResult (
614
+ this .errorMargin, this .transferSize, this .storageSize,
615
+ )
616
+ }
617
+
618
+ fun com.mapbox.common.TileRegionLoadProgress.toFLTTileRegionLoadProgress (): TileRegionLoadProgress {
619
+ return TileRegionLoadProgress (
620
+ this .completedResourceCount,
621
+ this .completedResourceSize,
622
+ this .erroredResourceCount,
623
+ this .requiredResourceCount,
624
+ this .loadedResourceCount,
625
+ this .loadedResourceSize
626
+ )
627
+ }
628
+
629
+ fun com.mapbox.common.TileRegionEstimateProgress.toFLTTileRegionEstimateProgress (): TileRegionEstimateProgress {
630
+ return TileRegionEstimateProgress (
631
+ this .requiredResourceCount, this .completedResourceCount, this .erroredResourceCount
632
+ )
633
+ }
634
+
531
635
fun Expected <String , None >.handleResult (callback : (Result <Unit >) -> Unit ) {
532
636
if (this .isError) {
533
637
callback(Result .failure(Throwable (this .error)))
534
638
} else {
535
639
callback(Result .success(Unit ))
536
640
}
641
+ }
642
+
643
+ @JvmName(" toStylePackResult" )
644
+ fun <V : Any , NewValue > Expected <StylePackError , V >.toResult (valueTransform : (V ) -> NewValue ): Result <NewValue > {
645
+ return fold(
646
+ {
647
+ Result .failure(Throwable (it.message))
648
+ },
649
+ {
650
+ Result .success(valueTransform(it))
651
+ }
652
+ )
653
+ }
654
+
655
+ @JvmName(" toTileRegionResult" )
656
+ fun <V : Any , NewValue > Expected <TileRegionError , V >.toResult (valueTransform : (V ) -> NewValue ): Result <NewValue > {
657
+ return fold(
658
+ {
659
+ Result .failure(Throwable (it.message))
660
+ },
661
+ {
662
+ Result .success(valueTransform(it))
663
+ }
664
+ )
665
+ }
666
+
667
+ fun Value.toFLTValue (): Any? {
668
+ return when (contents) {
669
+ is List <* > -> {
670
+ (contents as List <* >).map { (it as ? Value )?.toFLTValue() ? : it }
671
+ }
672
+
673
+ is Map <* , * > -> {
674
+ (contents as Map <* , * >)
675
+ .mapKeys { (it.key as ? Value )?.toFLTValue() ? : it.key }
676
+ .mapValues { (it.value as ? Value )?.toFLTValue() ? : it.value }
677
+ }
678
+
679
+ else -> {
680
+ contents
681
+ }
682
+ }
683
+ }
684
+
685
+ fun Any.toValue (): Value {
686
+ return if (this is String ) {
687
+ if (this .startsWith(" {" ) || this .startsWith(" [" )) {
688
+ Value .fromJson(this ).value!!
689
+ } else {
690
+ val number = this .toDoubleOrNull()
691
+ if (number != null ) {
692
+ Value .valueOf(number)
693
+ } else {
694
+ Value .valueOf(this )
695
+ }
696
+ }
697
+ } else if (this is Double ) {
698
+ Value .valueOf(this )
699
+ } else if (this is Long ) {
700
+ Value .valueOf(this )
701
+ } else if (this is Int ) {
702
+ Value .valueOf(this .toLong())
703
+ } else if (this is Boolean ) {
704
+ Value .valueOf(this )
705
+ } else if (this is IntArray ) {
706
+ val valueArray = this .map { Value (it.toLong()) }
707
+ Value (valueArray)
708
+ } else if (this is BooleanArray ) {
709
+ val valueArray = this .map(::Value )
710
+ Value (valueArray)
711
+ } else if (this is DoubleArray ) {
712
+ val valueArray = this .map(::Value )
713
+ Value (valueArray)
714
+ } else if (this is FloatArray ) {
715
+ val valueArray = this .map { Value (it.toDouble()) }
716
+ Value (valueArray)
717
+ } else if (this is LongArray ) {
718
+ val valueArray = this .map(::Value )
719
+ Value (valueArray)
720
+ } else if (this is Array <* >) {
721
+ val valueArray = this .map { it?.toValue() }
722
+ Value (valueArray)
723
+ } else if (this is List <* >) {
724
+ val valueArray = this .map { it?.toValue() }
725
+ Value (valueArray)
726
+ } else if (this is HashMap <* , * >) {
727
+ val valueMap = this
728
+ .mapKeys { it.key as ? String }
729
+ .mapValues { it.value.toValue() }
730
+ Value .valueOf(kotlin.collections.HashMap (valueMap))
731
+ } else {
732
+ logE(
733
+ " StyleController" ,
734
+ " Can not map value, type is not supported: ${this ::class .java.canonicalName} "
735
+ )
736
+ Value .valueOf(" " )
737
+ }
537
738
}
0 commit comments