Skip to content

Showing an overlay dialog when a nearby item is tapped #569

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

Merged
merged 1 commit into from
May 14, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/src/main/assets/queries/nearby_query.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SELECT
(SAMPLE(?location) as ?location)
?item
(SAMPLE(COALESCE(?item_label_preferred_language, ?item_label_any_language)) as ?label)
(SAMPLE(?classId) as ?class)
(SAMPLE(COALESCE(?class_label_preferred_language, ?class_label_any_language, "?")) as ?class_label)
(SAMPLE(COALESCE(?icon0, ?icon1)) as ?icon)
(SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)
(SAMPLE(?sitelink) as ?sitelink)
WHERE {
# Around given location...
SERVICE wikibase:around {
?item wdt:P625 ?location.
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
bd:serviceParam wikibase:radius "${RADIUS}" . # Radius in kilometers.
}

# ... and without an image.
MINUS {?item wdt:P18 []}

# Get the label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {?item rdfs:label ?item_label_preferred_language. FILTER (lang(?item_label_preferred_language) = "${LANG}")}
OPTIONAL {?item rdfs:label ?item_label_any_language}

# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {
?item p:P31/ps:P31 ?classId.
OPTIONAL {?classId rdfs:label ?class_label_preferred_language. FILTER (lang(?class_label_preferred_language) = "${LANG}")}
OPTIONAL {?classId rdfs:label ?class_label_any_language}

# Get icon
OPTIONAL { ?classId wdt:P2910 ?icon0. }
OPTIONAL { ?classId wdt:P279*/wdt:P2910 ?icon1. }
# Get emoji
OPTIONAL { ?classId wdt:P487 ?emoji0. }
OPTIONAL { ?classId wdt:P279*/wdt:P487 ?emoji1. }
OPTIONAL {
?sitelink schema:about ?item .
?sitelink schema:inLanguage "en"
}
}
}
GROUP BY ?item
88 changes: 88 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyBaseMarker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.free.nrw.commons.nearby;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;

import fr.free.nrw.commons.utils.UriDeserializer;
import fr.free.nrw.commons.utils.UriSerializer;

public class NearbyBaseMarker extends BaseMarkerOptions<NearbyMarker, NearbyBaseMarker> {
private Place place;
public NearbyBaseMarker() {

}

public NearbyBaseMarker place(Place place) {
this.place = place;
return getThis();
}

public NearbyBaseMarker(Parcel in) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();

position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
snippet(in.readString());
String iconId = in.readString();
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
Icon icon = IconFactory.recreate(iconId, iconBitmap);
icon(icon);
title(in.readString());
String gsonString = in.readString();
place(gson.fromJson(gsonString, Place.class));
}

@Override
public NearbyBaseMarker getThis() {
return this;
}

@Override
public NearbyMarker getMarker() {
return new NearbyMarker(this, place);
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();

dest.writeParcelable(position, flags);
dest.writeString(snippet);
dest.writeString(icon.getId());
dest.writeParcelable(icon.getBitmap(), flags);
dest.writeString(title);
dest.writeString(gson.toJson(place));
}

public Place getPlace() {
return place;
}

public static final Parcelable.Creator<NearbyBaseMarker> CREATOR
= new Parcelable.Creator<NearbyBaseMarker>() {
public NearbyBaseMarker createFromParcel(Parcel in) {
return new NearbyBaseMarker(in);
}

public NearbyBaseMarker[] newArray(int size) {
return new NearbyBaseMarker[size];
}
};
}
36 changes: 20 additions & 16 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package fr.free.nrw.commons.nearby;

import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Icon;
import android.preference.PreferenceManager;

import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;

import fr.free.nrw.commons.location.LatLng;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -20,8 +13,12 @@
import java.util.Locale;
import java.util.Map;

import fr.free.nrw.commons.location.LatLng;
import timber.log.Timber;

import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;


public class NearbyController {
private static final int MAX_RESULTS = 1000;
Expand All @@ -40,7 +37,9 @@ public static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<Place> places = prefs.getBoolean("useWikidata", true)
? NearbyPlaces.getInstance().getFromWikidataQuery(
curLatLng, Locale.getDefault().getLanguage())
context,
curLatLng,
Locale.getDefault().getLanguage())
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
if (curLatLng != null) {
Timber.d("Sorting places by distance...");
Expand Down Expand Up @@ -85,19 +84,24 @@ public static List<Place> loadAttractionsFromLocationToPlaces(
* @param placeList list of nearby places in Place data type
* @return BaseMarkerOprions list that holds nearby places
*/
public static List<BaseMarkerOptions> loadAttractionsFromLocationToBaseMarkerOptions(
public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOptions(
LatLng curLatLng,
List<Place> placeList) {
List<BaseMarkerOptions> baseMarkerOptionses = new ArrayList<>();
List<NearbyBaseMarker> baseMarkerOptionses = new ArrayList<>();
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
for (Place place: placeList) {
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);
baseMarkerOptionses.add(new MarkerOptions()
.position(new com.mapbox.mapboxsdk.geometry
.LatLng(place.location.latitude,place.location.longitude))
.title(place.name)
.snippet(place.description));

NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker();
nearbyBaseMarker.title(place.name);
nearbyBaseMarker.position(
new com.mapbox.mapboxsdk.geometry.LatLng(
place.location.latitude,
place.location.longitude));
nearbyBaseMarker.place(place);

baseMarkerOptionses.add(nearbyBaseMarker);
}
return baseMarkerOptionses;
}
Expand Down
110 changes: 110 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyInfoDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fr.free.nrw.commons.nearby;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.ui.widget.OverlayDialog;
import fr.free.nrw.commons.utils.DialogUtil;

public class NearbyInfoDialog extends OverlayDialog {

private final static String ARG_TITLE = "placeTitle";
private final static String ARG_DESC = "placeDesc";
private final static String ARG_LATITUDE = "latitude";
private final static String ARG_LONGITUDE = "longitude";
private final static String ARG_ARTICLE_LINK = "articleLink";
private final static String ARG_WIKI_DATA_LINK = "wikiDataLink";

@BindView(R.id.link_preview_title)
TextView placeTitle;
@BindView(R.id.link_preview_extract)
TextView placeDescription;

@BindView(R.id.link_preview_go_button)
TextView goToButton;

private Unbinder unbinder;

private LatLng location;
private Uri articleLink;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_nearby_info, container, false);
unbinder = ButterKnife.bind(this, view);
initUi();
return view;
}

private void initUi() {
Bundle bundle = getArguments();
placeTitle.setText(bundle.getString(ARG_TITLE));
placeDescription.setText(bundle.getString(ARG_DESC));
location = new LatLng(bundle.getDouble(ARG_LATITUDE), bundle.getDouble(ARG_LONGITUDE));
getArticleLink(bundle);
}

private void getArticleLink(Bundle bundle) {
String articleLink = bundle.getString(ARG_ARTICLE_LINK);
articleLink = articleLink.replace("<", "").replace(">", "");

if (Utils.isNullOrWhiteSpace(articleLink) || articleLink == "\n") {
articleLink = bundle.getString(ARG_WIKI_DATA_LINK).replace("<", "").replace(">", "");
}

if (!Utils.isNullOrWhiteSpace(articleLink) && articleLink != "\n") {
this.articleLink = Uri.parse(articleLink);
} else {
goToButton.setVisibility(View.GONE);
}
}

public static void showYourself(FragmentActivity fragmentActivity, Place place) {
NearbyInfoDialog mDialog = new NearbyInfoDialog();
Bundle bundle = new Bundle();
bundle.putString(ARG_TITLE, place.name);
bundle.putString(ARG_DESC, place.description);
bundle.putDouble(ARG_LATITUDE, place.location.latitude);
bundle.putDouble(ARG_LONGITUDE, place.location.longitude);
bundle.putString(ARG_ARTICLE_LINK, place.siteLink.toString());
bundle.putString(ARG_WIKI_DATA_LINK, place.wikiDataLink.toString());
mDialog.setArguments(bundle);
DialogUtil.showSafely(fragmentActivity, mDialog);
}

@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}

@OnClick(R.id.link_preview_directions_button)
void onDirectionsClick() {
//Open map app at given position
Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + location.latitude + "," + location.longitude);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);

if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(mapIntent);
}
}

@OnClick(R.id.link_preview_go_button)
void onReadArticleClick() {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, articleLink);
startActivity(browserIntent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import timber.log.Timber;

public class NearbyListFragment extends ListFragment {

//private NearbyAsyncTask nearbyAsyncTask;
private Gson gson;
private List<Place> placeList;
private LatLng curLatLng;
Expand Down Expand Up @@ -98,12 +96,6 @@ void onItemClicked(int position) {

Timber.d("Item at position %d has coords: Lat: %f Long: %f", position, latitude, longitude);

//Open map app at given position
Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + latitude + "," + longitude);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);

if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(mapIntent);
}
NearbyInfoDialog.showYourself(getActivity(), place);
}
}
Loading