Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] removing plain text getter for "text-field"
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Nov 14, 2018
1 parent db2e1b2 commit b5f2eba
Show file tree
Hide file tree
Showing 19 changed files with 571 additions and 581 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2117,8 +2117,8 @@ public static PropertyValue<String> textField(String value) {
/**
* Value to use for a text label. If a plain `string` is provided, it will be treated as a `formatted` with default/inherited formatting options.
*
* @param value a String value
* @return property wrapper around String
* @param value a Formatted value
* @return property wrapper around Formatted
*/
public static PropertyValue<Expression> textField(Expression value) {
return new LayoutPropertyValue<>("text-field", value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,41 +387,11 @@ public PropertyValue<String> getTextRotationAlignment() {
/**
* Get the TextField property
*
* @return property wrapper value around String
*/
@NonNull
@SuppressWarnings("unchecked")
public PropertyValue<String> getTextField() {
checkThread();

PropertyValue propertyValue = new PropertyValue<>("text-field", nativeGetTextField());
if (propertyValue.isExpression()) {
return (PropertyValue<String>) propertyValue;
} else {
String text = null;

Formatted formatted = (Formatted) nativeGetTextField();
if (formatted != null) {
StringBuilder builder = new StringBuilder();
for (FormattedSection section : formatted.getFormattedSections()) {
builder.append(section.getText());
}
text = builder.toString();
}

return (PropertyValue<String>) new PropertyValue("text-field", text);
}
}

/**
* Get the TextField property as {@link Formatted} object
*
* @return property wrapper value around String
* @see Expression#format(Expression.FormatEntry...)
* @return property wrapper value around Formatted
*/
@NonNull
@SuppressWarnings("unchecked")
public PropertyValue<Formatted> getFormattedTextField() {
public PropertyValue<Formatted> getTextField() {
checkThread();
return (PropertyValue<Formatted>) new PropertyValue("text-field", nativeGetTextField());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,44 +181,8 @@ public class <%- camelize(type) %>Layer extends Layer {
@SuppressWarnings("unchecked")
public PropertyValue<<%- propertyType(property) %>> get<%- camelize(property.name) %>() {
checkThread();
<% if (property.name === 'text-field' && property.type === 'formatted') { -%>
PropertyValue propertyValue = new PropertyValue<>("text-field", nativeGetTextField());
if (propertyValue.isExpression()) {
return (PropertyValue<String>) propertyValue;
} else {
String text = null;
Formatted formatted = (Formatted) nativeGetTextField();
if (formatted != null) {
StringBuilder builder = new StringBuilder();
for (FormattedSection section : formatted.getFormattedSections()) {
builder.append(section.getText());
}
text = builder.toString();
}
return (PropertyValue<String>) new PropertyValue("text-field", text);
}
<% } else { -%>
return (PropertyValue<<%- propertyType(property) %>>) new PropertyValue("<%- property.name %>", nativeGet<%- camelize(property.name) %>());
<% } -%>
}
<% if (property.name === 'text-field' && property.type === 'formatted') { -%>
/**
* Get the <%- camelize(property.name) %> property as {@link Formatted} object
*
* @return property wrapper value around <%- propertyType(property) %>
* @see Expression#format(Expression.FormatEntry...)
*/
@NonNull
@SuppressWarnings("unchecked")
public PropertyValue<Formatted> getFormatted<%- camelize(property.name) %>() {
checkThread();
return (PropertyValue<Formatted>) new PropertyValue("<%- property.name %>", nativeGet<%- camelize(property.name) %>());
}
<% } -%>
<% if (property.type == 'color') { -%>
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public class PropertyFactory {
<% } -%>
<% for (const property of layoutProperties) { -%>
<% if (property.type === 'formatted') { -%>
/**
* <%- propertyFactoryMethodDoc(property) %>
*
* @param value a String value
* @return property wrapper around String
*/
public static PropertyValue<String> <%- camelizeWithLeadingLowercase(property.name) %>(String value) {
return new LayoutPropertyValue<>("<%- property.name %>", value);
}
<% } else {-%>
/**
* <%- propertyFactoryMethodDoc(property) %>
*
Expand All @@ -72,6 +83,7 @@ public class PropertyFactory {
public static PropertyValue<<%- propertyType(property) %>> <%- camelizeWithLeadingLowercase(property.name) %>(<%- propertyTypeAnnotation(property) %><%- iff(() => propertyTypeAnnotation(property), " ") %><%- propertyType(property) %> value) {
return new LayoutPropertyValue<>("<%- property.name %>", value);
}
<% } -%>
/**
* <%- propertyFactoryMethodDoc(property) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.support.annotation.Keep;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import java.util.Arrays;

Expand All @@ -15,12 +14,20 @@
public class Formatted {
private final FormattedSection[] formattedSections;

/**
* Create a new formatted text.
*
* @param formattedSection section with formatting options
*/
public Formatted(FormattedSection formattedSection) {
this(new FormattedSection[] {formattedSection});
}

/**
* Create a new formatted text.
*
* @param formattedSections sections with formatting options
*/
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public Formatted(FormattedSection[] formattedSections) {
this.formattedSections = formattedSections;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.support.annotation.Keep;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import java.util.Arrays;

Expand All @@ -12,20 +11,19 @@
*/
@Keep
public class FormattedSection {
private String text;
private double fontScale;
@Nullable
private String[] fontStack;
private final String text;
private final Number fontScale;
private final String[] fontStack;

/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, 1.0 is default
* @param fontStack main and fallback fonts that are a part of the style
* @param fontScale scale of the font, setting to null will fall back to style's default settings
* @param fontStack main and fallback fonts that are a part of the style,
* setting null will fall back to style's default settings
*/
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public FormattedSection(@NonNull String text, double fontScale, @Nullable String[] fontStack) {
public FormattedSection(@NonNull String text, @Nullable Number fontScale, @Nullable String[] fontStack) {
this.text = text;
this.fontScale = fontScale;
this.fontStack = fontStack;
Expand All @@ -35,12 +33,30 @@ public FormattedSection(@NonNull String text, double fontScale, @Nullable String
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, 1.0 is default
* @param fontScale scale of the font, setting to null will fall back to style's default settings
*/
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public FormattedSection(@NonNull String text, double fontScale) {
this.text = text;
this.fontScale = fontScale;
public FormattedSection(@NonNull String text, @Nullable Number fontScale) {
this(text, fontScale, null);
}

/**
* Creates a formatted section.
*
* @param text displayed string
*/
public FormattedSection(@NonNull String text) {
this(text, null, null);
}

/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontStack main and fallback fonts that are a part of the style,
* setting null will fall back to style's default settings
*/
public FormattedSection(@NonNull String text, @Nullable String[] fontStack) {
this(text, null, fontStack);
}

/**
Expand All @@ -56,9 +72,10 @@ public String getText() {
/**
* Returns displayed text's font scale.
*
* @return font scale, defaults to 1.0
* @return font scale
*/
public double getFontScale() {
@Nullable
public Number getFontScale() {
return fontScale;
}

Expand All @@ -73,28 +90,30 @@ public String[] getFontStack() {
}

@Override
public boolean equals(@Nullable Object o) {
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

FormattedSection section = (FormattedSection) o;
FormattedSection that = (FormattedSection) o;

return Double.compare(section.fontScale, fontScale) == 0
&& (text != null ? text.equals(section.text) : section.text == null)
&& Arrays.equals(fontStack, section.fontStack);
if (text != null ? !text.equals(that.text) : that.text != null) {
return false;
}
if (fontScale != null ? !fontScale.equals(that.fontScale) : that.fontScale != null) {
return false;
}
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(fontStack, that.fontStack);
}

@Override
public int hashCode() {
int result;
long temp;
result = text != null ? text.hashCode() : 0;
temp = Double.doubleToLongBits(fontScale);
result = 31 * result + (int) (temp ^ (temp >>> 32));
int result = text != null ? text.hashCode() : 0;
result = 31 * result + (fontScale != null ? fontScale.hashCode() : 0);
result = 31 * result + Arrays.hashCode(fontStack);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ public void testBackgroundColorAsConstant() {
Timber.i("background-color");
invoke(mapboxMap, (uiController, mapboxMap) -> {
assertNotNull(layer);
assertNull((String) layer.getBackgroundColor().getValue());
assertNull(layer.getBackgroundColor().getValue());

// Set and Get
layer.setProperties(backgroundColor("rgba(0, 0, 0, 1)"));
assertEquals((String) layer.getBackgroundColor().getValue(), (String) "rgba(0, 0, 0, 1)");
String propertyValue = "rgba(0, 0, 0, 1)";
layer.setProperties(backgroundColor(propertyValue));
assertEquals(layer.getBackgroundColor().getValue(), propertyValue);
});
}

Expand Down Expand Up @@ -126,11 +127,12 @@ public void testBackgroundPatternAsConstant() {
Timber.i("background-pattern");
invoke(mapboxMap, (uiController, mapboxMap) -> {
assertNotNull(layer);
assertNull((String) layer.getBackgroundPattern().getValue());
assertNull(layer.getBackgroundPattern().getValue());

// Set and Get
layer.setProperties(backgroundPattern("pedestrian-polygon"));
assertEquals((String) layer.getBackgroundPattern().getValue(), (String) "pedestrian-polygon");
String propertyValue = "pedestrian-polygon";
layer.setProperties(backgroundPattern(propertyValue));
assertEquals(layer.getBackgroundPattern().getValue(), propertyValue);
});
}

Expand All @@ -156,11 +158,12 @@ public void testBackgroundOpacityAsConstant() {
Timber.i("background-opacity");
invoke(mapboxMap, (uiController, mapboxMap) -> {
assertNotNull(layer);
assertNull((Float) layer.getBackgroundOpacity().getValue());
assertNull(layer.getBackgroundOpacity().getValue());

// Set and Get
layer.setProperties(backgroundOpacity(0.3f));
assertEquals((Float) layer.getBackgroundOpacity().getValue(), (Float) 0.3f);
Float propertyValue = 0.3f;
layer.setProperties(backgroundOpacity(propertyValue));
assertEquals(layer.getBackgroundOpacity().getValue(), propertyValue);
});
}
}
Loading

0 comments on commit b5f2eba

Please sign in to comment.