Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Map and Navigation component enums #16

Merged
merged 8 commits into from Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -376,6 +376,8 @@ private static void upgradeComponentProperties(Map<String, JSONValue> componentP
srcCompVersion = upgradeRectangleProperties(componentProperties, srcCompVersion);
} else if (componentType.equals("FeatureCollection")) {
srcCompVersion = upgradeFeatureCollection(componentProperties, srcCompVersion);
} else if (componentType.equals("Navigation")) {
srcCompVersion = upgradeNavigationProperties(componentProperties, srcCompVersion);
} else if (componentType.equals("YandexTranslate")) {
srcCompVersion = upgradeYandexTranslateProperties(componentProperties, srcCompVersion);
}
Expand Down Expand Up @@ -1725,6 +1727,21 @@ private static int upgradeMapProperties(Map<String, JSONValue> componentProperti
// The ScaleUnits and ShowScale properties were added
srcCompVersion = 5;
}
if (srcCompVersion < 6) {
// Adds Units and MapType dropdowns.
srcCompVersion = 6;
}
return srcCompVersion;
}

private static int upgradeNavigationProperties(
Map<String, JSONValue> componentProperties,
int srcCompVersion
) {
if (srcCompVersion < 2) {
// - Adds TransportMethod dropdown.
srcCompVersion = 2;
}
return srcCompVersion;
}

Expand Down
19 changes: 18 additions & 1 deletion appinventor/blocklyeditor/src/versioning.js
Expand Up @@ -2119,7 +2119,14 @@ Blockly.Versioning.AllUpgradeMaps =

// AI2:
// - The ScaleUnits and ShowScale properties were added to Map
5: "noUpgrade"
5: "noUpgrade",

// AI2:
// - Adds Units and MapType dropdowns.
6: [Blockly.Versioning.makeSetterUseDropdown(
'Map', 'ScaleUnits', 'Units'),
Blockly.Versioning.makeSetterUseDropdown(
'Map', 'MapType', 'MapType')]

}, // End Map upgraders

Expand Down Expand Up @@ -2164,6 +2171,16 @@ Blockly.Versioning.AllUpgradeMaps =
2: "noUpgrade"
}, // End Rectangle upgraders

"Navigation": {
// This is an initial version. Placehoder for future upgrades.
1: "noUpgrade",

// Adds TransportMethod dropdown.
2: [Blockly.Versioning.makeSetterUseDropdown(
'Navigation', 'TransportationMethod', 'TransportMethod')]

}, // End Navigation upgraders.

"NearField": {

//This is initial version. Placeholder for future upgrades
Expand Down
@@ -0,0 +1,40 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a MapType type used by the Map component.
*/
public enum MapType implements OptionList<Integer> {
Road(1),
Aerial(2),
Terrain(3);

private Integer value;

MapType(Integer value) {
this.value = value;
}

public Integer toUnderlyingValue() {
return value;
}

private static final Map<Integer, MapType> lookup = new HashMap<>();
BeksOmega marked this conversation as resolved.
Show resolved Hide resolved

static {
for(MapType type : MapType.values()) {
lookup.put(type.toUnderlyingValue(), type);
}
}

public static MapType fromUnderlyingValue(Integer type) {
return lookup.get(type);
}
}
@@ -0,0 +1,39 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a ScaleUnits type used by the Map component.
*/
public enum ScaleUnits implements OptionList<Integer> {
Metric(1),
Imperial(2);

private Integer value;

ScaleUnits(Integer value) {
this.value = value;
}

public Integer toUnderlyingValue() {
return value;
}

private static final Map<Integer, ScaleUnits> lookup = new HashMap<>();

static {
for(ScaleUnits unit : ScaleUnits.values()) {
lookup.put(unit.toUnderlyingValue(), unit);
}
}

public static ScaleUnits fromUnderlyingValue(Integer unit) {
return lookup.get(unit);
}
}
@@ -0,0 +1,41 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2020 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.common;

import java.util.HashMap;
import java.util.Map;

/**
* Defines a TransportMethod type used by the Navigation component.
*/
public enum TransportMethod implements OptionList<String> {
Foot("foot-walking"),
Car("driving-car"),
Bicycle("cycling-regular"),
Wheelchair("wheelchair");

private String value;

TransportMethod(String value) {
this.value = value;
}

public String toUnderlyingValue() {
return value;
}

private static final Map<String, TransportMethod> lookup = new HashMap<>();

static {
for(TransportMethod method : TransportMethod.values()) {
lookup.put(method.toUnderlyingValue(), method);
}
}

public static TransportMethod fromUnderlyingValue(String method) {
return lookup.get(method);
}
}
Expand Up @@ -1022,7 +1022,9 @@ private YaVersion() {
// For MAP_COMPONENT_VERSION 5:
// - Added ShowScale property
// - Added ScaleUnits property
public static final int MAP_COMPONENT_VERSION = 5;
// For MAP_COMPONENT_VERSION 6:
// - Adds Units and MapType dropdowns.
public static final int MAP_COMPONENT_VERSION = 6;

// For MARKER_COMPONENT_VERSION 1:
// - Initial Marker implementation using OpenStreetMap
Expand All @@ -1036,7 +1038,9 @@ private YaVersion() {

// For NAVIGATION_COMPONENT_VERSION 1:
// - Initial Navigation implementation
public static final int NAVIGATION_COMPONENT_VERSION = 1;
// For NAVIGATION_COMPONENT_VERSION 2:
// - Adds TransportMethod dropdown.
public static final int NAVIGATION_COMPONENT_VERSION = 2;

// For NEARFIELD_COMPONENT_VERSION 1:
public static final int NEARFIELD_COMPONENT_VERSION = 1;
Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.Options;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
Expand All @@ -27,7 +28,9 @@
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.MapType;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.ScaleUnits;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.util.MapFactory.MapCircle;
import com.google.appinventor.components.runtime.util.MapFactory.MapController;
Expand Down Expand Up @@ -110,7 +113,7 @@ public Map(final ComponentContainer container) {
ZoomLevel(13);
EnableZoom(true);
EnablePan(true);
MapType(1);
MapTypeAbstract(MapType.Road);
ShowCompass(false);
LocationSensor(new LocationSensor(container.$form(), false));
ShowUser(false);
Expand Down Expand Up @@ -295,9 +298,18 @@ public float Rotation() {
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_MAP_TYPE,
defaultValue = "1")
@SimpleProperty
public void MapType(int type) {
MapFactory.MapType newType = MapFactory.MapType.values()[type];
mapController.setMapType(newType);
public void MapType(@Options(MapType.class) int type) {
MapType mapType = MapType.fromUnderlyingValue(type);
if (mapType != null) {
MapTypeAbstract(mapType);
}
}

/**
* Returns the current tile layer used to draw the Map background.
*/
public void MapTypeAbstract(MapType type) {
mapController.setMapTypeAbstract(type);
}

/**
Expand All @@ -316,8 +328,15 @@ public void MapType(int type) {
@SimpleProperty(category = PropertyCategory.APPEARANCE,
description = "The type of tile layer to use as the base of the map. Valid values " +
"are: 1 (Roads), 2 (Aerial), 3 (Terrain)")
public int MapType() {
return mapController.getMapType().ordinal();
public @Options(MapType.class) int MapType() {
return MapTypeAbstract().toUnderlyingValue();
}

/**
* Sets the tile layer used to draw the Map background.
*/
public MapType MapTypeAbstract() {
return mapController.getMapTypeAbstract();
}

/**
Expand Down Expand Up @@ -511,25 +530,34 @@ public boolean ShowScale() {
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_MAP_UNIT_SYSTEM,
defaultValue = "1")
@SimpleProperty
public void ScaleUnits(int units) {
if (1 <= units && units < MapScaleUnits.values().length) {
mapController.setScaleUnits(MapScaleUnits.values()[units]);
} else {
public void ScaleUnits(@Options(ScaleUnits.class) int units) {
// Make sure units is a valid ScaleUnits.
ScaleUnits scaleUnits = ScaleUnits.fromUnderlyingValue(units);
if (scaleUnits == null) {
$form().dispatchErrorOccurredEvent(this, "ScaleUnits",
ErrorMessages.ERROR_INVALID_UNIT_SYSTEM, units);
return;
}
ScaleUnitsAbstract(scaleUnits);
}

/**
* Sets the system of measurement used by the map.
*/
public void ScaleUnitsAbstract(ScaleUnits units) {
mapController.setScaleUnitsAbstract(units);
}

@SimpleProperty
public int ScaleUnits() {
switch (mapController.getScaleUnits()) {
case METRIC:
return 1;
case IMPERIAL:
return 2;
default:
return 0;
}
public @Options(ScaleUnits.class) int ScaleUnits() {
return ScaleUnitsAbstract().toUnderlyingValue();
}

/**
* Returns the system of measurement used by the map.
*/
public ScaleUnits ScaleUnitsAbstract() {
return mapController.getScaleUnitsAbstract();
}

@SimpleProperty(category = PropertyCategory.BEHAVIOR,
Expand Down