Merge pull request #742 from veyndan/place

Use an enum for the place description
This commit is contained in:
Yusuke Matsubara 2017-06-30 16:46:26 +09:00 committed by GitHub
commit b83c32f81e
5 changed files with 84 additions and 101 deletions

View file

@ -121,7 +121,7 @@ public class NearbyInfoDialog extends OverlayDialog {
NearbyInfoDialog mDialog = new NearbyInfoDialog();
Bundle bundle = new Bundle();
bundle.putString(ARG_TITLE, place.name);
bundle.putString(ARG_DESC, place.description);
bundle.putString(ARG_DESC, place.getDescription().getText());
bundle.putDouble(ARG_LATITUDE, place.location.getLatitude());
bundle.putDouble(ARG_LONGITUDE, place.location.getLongitude());
bundle.putParcelable(ARG_SITE_LINK, place.siteLinks);

View file

@ -122,7 +122,7 @@ public class NearbyPlaces {
places.add(new Place(
name,
type, // list
Place.Description.fromText(type), // list
type, // details
Uri.parse(icon),
new LatLng(latitude, longitude, 0),
@ -184,7 +184,7 @@ public class NearbyPlaces {
places.add(new Place(
name,
type, // list
Place.Description.fromText(type), // list
type, // details
null,
new LatLng(latitude, longitude, 0),

View file

@ -9,7 +9,6 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.ViewHolder;
import fr.free.nrw.commons.utils.ResourceUtils;
public class NearbyViewHolder implements ViewHolder<Place> {
@BindView(R.id.tvName) TextView tvName;
@ -25,12 +24,12 @@ public class NearbyViewHolder implements ViewHolder<Place> {
public void bindModel(Context context, Place place) {
// Populate the data into the template view using the data object
tvName.setText(place.name);
String description = place.description;
if ( description == null || description.isEmpty() || description.equals("?")) {
description = context.getString(R.string.no_description_found);
String descriptionText = place.getDescription().getText();
if (descriptionText.equals("?")) {
descriptionText = context.getString(R.string.no_description_found);
}
tvDesc.setText(description);
tvDesc.setText(descriptionText);
distance.setText(place.distance);
icon.setImageResource(ResourceUtils.getDescriptionIcon(place.description));
icon.setImageResource(place.getDescription().getIcon());
}
}

View file

@ -2,13 +2,18 @@ package fr.free.nrw.commons.nearby;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import java.util.HashMap;
import java.util.Map;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
public class Place {
public final String name;
public final String description;
private final Description description;
public final String longDescription;
public final Uri secondaryImageUrl;
public final LatLng location;
@ -19,7 +24,7 @@ public class Place {
public Sitelinks siteLinks;
public Place(String name, String description, String longDescription,
public Place(String name, Description description, String longDescription,
Uri secondaryImageUrl, LatLng location, Sitelinks siteLinks) {
this.name = name;
this.description = description;
@ -29,6 +34,10 @@ public class Place {
this.siteLinks = siteLinks;
}
public Description getDescription() {
return description;
}
public void setDistance(String distance) {
this.distance = distance;
}
@ -36,7 +45,7 @@ public class Place {
@Override
public boolean equals(Object o) {
if (o instanceof Place) {
Place that = (Place)o;
Place that = (Place) o;
return this.name.equals(that.name) && this.location.equals(that.location);
} else {
return false;
@ -53,4 +62,68 @@ public class Place {
return String.format("Place(%s@%s)", name, location);
}
/**
* See https://github.com/commons-app/apps-android-commons/issues/250
* Most common types of desc: building, house, cottage, farmhouse,
* village, civil parish, church, railway station,
* gatehouse, milestone, inn, secondary school, hotel
*
* TODO Give a more accurate class name (see issue #742).
*/
public enum Description {
BUILDING("building", R.drawable.round_icon_generic_building),
HOUSE("house", R.drawable.round_icon_house),
COTTAGE("cottage", R.drawable.round_icon_house),
FARMHOUSE("farmhouse", R.drawable.round_icon_house),
CHURCH("church", R.drawable.round_icon_church),
RAILWAY_STATION("railway station", R.drawable.round_icon_railway_station),
GATEHOUSE("gatehouse", R.drawable.round_icon_gatehouse),
MILESTONE("milestone", R.drawable.round_icon_milestone),
INN("inn", R.drawable.round_icon_house),
CITY("city", R.drawable.round_icon_city),
SECONDARY_SCHOOL("secondary school", R.drawable.round_icon_school),
EDU("edu", R.drawable.round_icon_school),
ISLE("isle", R.drawable.round_icon_island),
MOUNTAIN("mountain", R.drawable.round_icon_mountain),
AIRPORT("airport", R.drawable.round_icon_airport),
BRIDGE("bridge", R.drawable.round_icon_bridge),
ROAD("road", R.drawable.round_icon_road),
FOREST("forest", R.drawable.round_icon_forest),
PARK("park", R.drawable.round_icon_park),
RIVER("river", R.drawable.round_icon_river),
WATERFALL("waterfall", R.drawable.round_icon_waterfall),
UNKNOWN("?", R.drawable.round_icon_unknown);
private static final Map<String, Description> TEXT_TO_DESCRIPTION
= new HashMap<>(Description.values().length);
static {
for (Description description : values()) {
TEXT_TO_DESCRIPTION.put(description.text, description);
}
}
private final String text;
@DrawableRes private final int icon;
Description(String text, @DrawableRes int icon) {
this.text = text;
this.icon = icon;
}
public String getText() {
return text;
}
@DrawableRes
public int getIcon() {
return icon;
}
public static Description fromText(String text) {
Description description = TEXT_TO_DESCRIPTION.get(text);
return description == null ? UNKNOWN : description;
}
}
}

View file

@ -1,89 +0,0 @@
package fr.free.nrw.commons.utils;
import android.support.annotation.DrawableRes;
import fr.free.nrw.commons.R;
public class ResourceUtils {
/**
* See https://github.com/commons-app/apps-android-commons/issues/250
* Most common types of desc: building, house, cottage, farmhouse,
* village, civil parish, church, railway station,
* gatehouse, milestone, inn, secondary school, hotel
* @param description Place description
* @return icon res id
*/
@DrawableRes
public static int getDescriptionIcon(String description) {
int resourceId;
switch (description) {
case "building":
resourceId = R.drawable.round_icon_generic_building;
break;
case "house":
resourceId = R.drawable.round_icon_house;
break;
case "cottage":
resourceId = R.drawable.round_icon_house;
break;
case "farmhouse":
resourceId = R.drawable.round_icon_house;
break;
case "church":
resourceId = R.drawable.round_icon_church;
break;
case "railway station":
resourceId = R.drawable.round_icon_railway_station;
break;
case "gatehouse":
resourceId = R.drawable.round_icon_gatehouse;
break;
case "milestone":
resourceId = R.drawable.round_icon_milestone;
break;
case "inn":
resourceId = R.drawable.round_icon_house;
break;
case "city":
resourceId = R.drawable.round_icon_city;
break;
case "secondary school":
resourceId = R.drawable.round_icon_school;
break;
case "edu":
resourceId = R.drawable.round_icon_school;
break;
case "isle":
resourceId = R.drawable.round_icon_island;
break;
case "mountain":
resourceId = R.drawable.round_icon_mountain;
break;
case "airport":
resourceId = R.drawable.round_icon_airport;
break;
case "bridge":
resourceId = R.drawable.round_icon_bridge;
break;
case "road":
resourceId = R.drawable.round_icon_road;
break;
case "forest":
resourceId = R.drawable.round_icon_forest;
break;
case "park":
resourceId = R.drawable.round_icon_park;
break;
case "river":
resourceId = R.drawable.round_icon_river;
break;
case "waterfall":
resourceId = R.drawable.round_icon_waterfall;
break;
default:
resourceId = R.drawable.round_icon_unknown;
}
return resourceId;
}
}