[android] add plural style builder methods

This commit is contained in:
Łukasz Paczos
2019-02-28 11:13:22 +01:00
committed by Łukasz Paczos
parent 405e20e3c8
commit 638b91afc4
2 changed files with 200 additions and 0 deletions

View File

@@ -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;
@@ -607,6 +609,18 @@ public class Style {
return this;
}
/**
* 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.
*
@@ -619,6 +633,20 @@ public class Style {
return this;
}
/**
* 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.
*
@@ -683,6 +711,17 @@ public class Style {
return this.withImage(id, bitmap, false);
}
/**
* 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.
*
@@ -695,6 +734,20 @@ public class Style {
return this.withImage(id, image, false);
}
/**
* 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.
*
@@ -712,6 +765,25 @@ public class Style {
return this.withImage(id, bitmap, sdf);
}
/**
* 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.
*
@@ -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;
}

View File

@@ -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
@@ -40,6 +42,16 @@ class StyleBuilderTest {
assertEquals(layer, builder.layers[0].layer)
}
@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
@@ -78,6 +90,16 @@ class StyleBuilderTest {
assertEquals(source, builder.sources[0])
}
@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)
@@ -88,6 +110,20 @@ class StyleBuilderTest {
assertEquals(false, builder.images[0].sdf)
}
@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)
@@ -98,8 +134,37 @@ class StyleBuilderTest {
assertEquals(true, builder.images[0].sdf)
}
@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
@@ -113,6 +178,54 @@ class StyleBuilderTest {
assertEquals(true, builder.images[0].sdf)
}
@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)