diff options
author | Łukasz Paczos <lukas.paczos@gmail.com> | 2019-02-28 11:13:22 +0100 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2019-03-04 12:33:14 +0100 |
commit | 638b91afc4f9b1aa2b9a222eb541f728094031aa (patch) | |
tree | d595fd5204d34459f21f64c1f32a30ac4582a1c1 | |
parent | 405e20e3c8e1ba489fb48ad76fcbe5738ce643b3 (diff) | |
download | qtlocation-mapboxgl-638b91afc4f9b1aa2b9a222eb541f728094031aa.tar.gz |
[android] add plural style builder methods
2 files changed, 200 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java index 2e3a5335fe..d6bb0a9f01 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java @@ -8,6 +8,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringDef; import android.util.DisplayMetrics; +import android.util.Pair; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.style.layers.Layer; @@ -21,6 +22,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.ref.WeakReference; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -608,6 +610,18 @@ public class Style { } /** + * Will add the sources when map style has loaded. + * + * @param sources the sources to add + * @return this + */ + @NonNull + public Builder withSources(@NonNull Source... sources) { + this.sources.addAll(Arrays.asList(sources)); + return this; + } + + /** * Will add the layer when the style has loaded. * * @param layer the layer to be added @@ -620,6 +634,20 @@ public class Style { } /** + * Will add the layers when the style has loaded. + * + * @param layers the layers to be added + * @return this + */ + @NonNull + public Builder withLayers(@NonNull Layer... layers) { + for (Layer layer : layers) { + this.layers.add(new LayerWrapper(layer)); + } + return this; + } + + /** * Will add the layer when the style has loaded at a specified index. * * @param layer the layer to be added @@ -684,6 +712,17 @@ public class Style { } /** + * Will add the drawables as images when the map style has loaded. + * + * @param values pairs, where first is the id for te image and second is the drawable + * @return this + */ + @NonNull + public Builder withDrawableImages(@NonNull Pair<String, Drawable>... values) { + return this.withDrawableImages(false, values); + } + + /** * Will add the image when the map style has loaded. * * @param id the id for the image @@ -696,6 +735,20 @@ public class Style { } /** + * Will add the images when the map style has loaded. + * + * @param values pairs, where first is the id for te image and second is the bitmap + * @return this + */ + @NonNull + public Builder withBitmapImages(@NonNull Pair<String, Bitmap>... values) { + for (Pair<String, Bitmap> value : values) { + this.withImage(value.first, value.second, false); + } + return this; + } + + /** * Will add the drawable as image when the map style has loaded. * * @param id the id for the image @@ -713,6 +766,25 @@ public class Style { } /** + * Will add the drawables as images when the map style has loaded. + * + * @param sdf the flag indicating image is an SDF or template image + * @param values pairs, where first is the id for te image and second is the drawable + * @return this + */ + @NonNull + public Builder withDrawableImages(boolean sdf, @NonNull Pair<String, Drawable>... values) { + for (Pair<String, Drawable> value : values) { + Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(value.second); + if (bitmap == null) { + throw new IllegalArgumentException("Provided drawable couldn't be converted to a Bitmap."); + } + this.withImage(value.first, bitmap, sdf); + } + return this; + } + + /** * Will add the image when the map style has loaded. * * @param id the id for the image @@ -726,6 +798,21 @@ public class Style { return this; } + /** + * Will add the images when the map style has loaded. + * + * @param sdf the flag indicating image is an SDF or template image + * @param values pairs, where first is the id for te image and second is the bitmap + * @return this + */ + @NonNull + public Builder withBitmapImages(boolean sdf, @NonNull Pair<String, Bitmap>... values) { + for (Pair<String, Bitmap> value : values) { + this.withImage(value.first, value.second, sdf); + } + return this; + } + String getUrl() { return styleUrl; } diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/StyleBuilderTest.kt b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/StyleBuilderTest.kt index 2dd6b55e28..ebab33347f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/StyleBuilderTest.kt +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/StyleBuilderTest.kt @@ -1,7 +1,9 @@ package com.mapbox.mapboxsdk.maps import android.graphics.Bitmap +import android.graphics.drawable.Drawable import android.graphics.drawable.ShapeDrawable +import android.util.Pair import com.mapbox.mapboxsdk.style.layers.SymbolLayer import com.mapbox.mapboxsdk.style.layers.TransitionOptions import com.mapbox.mapboxsdk.style.sources.GeoJsonSource @@ -41,6 +43,16 @@ class StyleBuilderTest { } @Test + fun testWithLayers() { + val layer1 = mockk<SymbolLayer>() + val layer2 = mockk<SymbolLayer>() + val builder = Style.Builder() + builder.withLayers(layer1, layer2) + assertEquals(layer1, builder.layers[0].layer) + assertEquals(layer2, builder.layers[1].layer) + } + + @Test fun testWithLayerAt() { val expectedIndex = 5 val layer = mockk<SymbolLayer>() @@ -79,6 +91,16 @@ class StyleBuilderTest { } @Test + fun testWithSources() { + val source1 = mockk<GeoJsonSource>() + val source2 = mockk<GeoJsonSource>() + val builder = Style.Builder() + builder.withSources(source1, source2) + assertEquals(source1, builder.sources[0]) + assertEquals(source2, builder.sources[1]) + } + + @Test fun testWithImage() { val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) val builder = Style.Builder() @@ -89,6 +111,20 @@ class StyleBuilderTest { } @Test + fun testWithImages() { + val bitmap1 = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) + val bitmap2 = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) + val builder = Style.Builder() + builder.withBitmapImages(Pair("id1", bitmap1), Pair("id2", bitmap2)) + assertEquals(bitmap1, builder.images[0].bitmap) + assertEquals("id1", builder.images[0].id) + assertEquals(false, builder.images[0].sdf) + assertEquals(bitmap2, builder.images[1].bitmap) + assertEquals("id2", builder.images[1].id) + assertEquals(false, builder.images[1].sdf) + } + + @Test fun testWithImageSdf() { val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) val builder = Style.Builder() @@ -99,11 +135,40 @@ class StyleBuilderTest { } @Test + fun testWithImageSdfs() { + val bitmap1 = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) + val bitmap2 = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) + val builder = Style.Builder() + builder.withBitmapImages(true, Pair("id1", bitmap1), Pair("id2", bitmap2)) + assertEquals(bitmap1, builder.images[0].bitmap) + assertEquals("id1", builder.images[0].id) + assertEquals(true, builder.images[0].sdf) + assertEquals(bitmap2, builder.images[1].bitmap) + assertEquals("id2", builder.images[1].id) + assertEquals(true, builder.images[1].sdf) + } + + @Test fun testWithImageDrawable() { val drawable = ShapeDrawable() drawable.intrinsicWidth = 1 drawable.intrinsicHeight = 1 val builder = Style.Builder() + builder.withImage("id", drawable) + assertTrue(BitmapUtils.equals( + BitmapUtils.getBitmapFromDrawable(drawable)!!, + builder.images[0].bitmap) + ) + assertEquals("id", builder.images[0].id) + assertEquals(false, builder.images[0].sdf) + } + + @Test + fun testWithImageDrawableSdf() { + val drawable = ShapeDrawable() + drawable.intrinsicWidth = 1 + drawable.intrinsicHeight = 1 + val builder = Style.Builder() builder.withImage("id", drawable, true) assertTrue(BitmapUtils.equals( BitmapUtils.getBitmapFromDrawable(drawable)!!, @@ -114,6 +179,54 @@ class StyleBuilderTest { } @Test + fun testWithImageDrawables() { + val drawable1 = ShapeDrawable() + drawable1.intrinsicWidth = 1 + drawable1.intrinsicHeight = 1 + val drawable2 = ShapeDrawable() + drawable2.intrinsicWidth = 1 + drawable2.intrinsicHeight = 1 + val builder = Style.Builder() + builder.withDrawableImages(Pair<String, Drawable>("id1", drawable1), Pair<String, Drawable>("id2", drawable2)) + assertTrue(BitmapUtils.equals( + BitmapUtils.getBitmapFromDrawable(drawable1)!!, + builder.images[0].bitmap) + ) + assertEquals("id1", builder.images[0].id) + assertEquals(false, builder.images[0].sdf) + assertTrue(BitmapUtils.equals( + BitmapUtils.getBitmapFromDrawable(drawable2)!!, + builder.images[1].bitmap) + ) + assertEquals("id2", builder.images[1].id) + assertEquals(false, builder.images[1].sdf) + } + + @Test + fun testWithImageSdfDrawables() { + val drawable1 = ShapeDrawable() + drawable1.intrinsicWidth = 1 + drawable1.intrinsicHeight = 1 + val drawable2 = ShapeDrawable() + drawable2.intrinsicWidth = 1 + drawable2.intrinsicHeight = 1 + val builder = Style.Builder() + builder.withDrawableImages(true, Pair<String, Drawable>("id1", drawable1), Pair<String, Drawable>("id2", drawable2)) + assertTrue(BitmapUtils.equals( + BitmapUtils.getBitmapFromDrawable(drawable1)!!, + builder.images[0].bitmap) + ) + assertEquals("id1", builder.images[0].id) + assertEquals(true, builder.images[0].sdf) + assertTrue(BitmapUtils.equals( + BitmapUtils.getBitmapFromDrawable(drawable2)!!, + builder.images[1].bitmap) + ) + assertEquals("id2", builder.images[1].id) + assertEquals(true, builder.images[1].sdf) + } + + @Test fun testWithTransitionOptions() { val transitionOptions = TransitionOptions(100, 200) val builder = Style.Builder() |