Skip to content

Commit 6b043db

Browse files
1 parent 6ab6a21 commit 6b043db

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

+33-4
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
import android.support.v4.app.FragmentTransaction;
1717
import android.support.v7.app.AlertDialog;
1818

19+
import android.text.TextUtils;
1920
import android.view.Menu;
2021
import android.view.MenuInflater;
2122
import android.view.MenuItem;
2223
import android.view.View;
2324
import android.widget.LinearLayout;
2425
import android.widget.ProgressBar;
2526

27+
import android.widget.Toast;
2628
import com.google.gson.Gson;
2729
import com.google.gson.GsonBuilder;
2830

31+
import io.reactivex.functions.Consumer;
32+
import java.io.IOException;
33+
import java.net.UnknownHostException;
2934
import java.util.List;
3035

3136
import javax.inject.Inject;
@@ -427,8 +432,17 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
427432
.loadAttractionsFromLocation(curLatLng))
428433
.subscribeOn(Schedulers.io())
429434
.observeOn(AndroidSchedulers.mainThread())
430-
.subscribe(this::populatePlaces);
431-
} else if (locationChangeType.equals(LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED)) {
435+
.subscribe(this::populatePlaces,
436+
throwable -> {
437+
if (throwable instanceof UnknownHostException) {
438+
showErrorMessage(getString(R.string.slow_internet));
439+
} else {
440+
showErrorMessage(throwable.getMessage());
441+
}
442+
progressBar.setVisibility(View.GONE);
443+
});
444+
} else if (locationChangeType
445+
.equals(LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED)) {
432446
Gson gson = new GsonBuilder()
433447
.registerTypeAdapter(Uri.class, new UriSerializer())
434448
.create();
@@ -451,7 +465,7 @@ private void populatePlaces(NearbyController.NearbyPlacesInfo nearbyPlacesInfo)
451465
if (placeList.size() == 0) {
452466
ViewUtil.showSnackbar(findViewById(R.id.container), R.string.no_nearby);
453467
}
454-
468+
455469
bundle.putString("PlaceList", gsonPlaceList);
456470
//bundle.putString("CurLatLng", gsonCurLatLng);
457471
bundle.putString("BoundaryCoord", gsonBoundaryCoordinates);
@@ -580,7 +594,15 @@ private void updateMapFragment(boolean isSlightUpdate) {
580594
.loadAttractionsFromLocation(curLatLng))
581595
.subscribeOn(Schedulers.io())
582596
.observeOn(AndroidSchedulers.mainThread())
583-
.subscribe(this::populatePlaces);
597+
.subscribe(this::populatePlaces,
598+
throwable -> {
599+
if (throwable instanceof UnknownHostException) {
600+
showErrorMessage(getString(R.string.slow_internet));
601+
} else {
602+
showErrorMessage(throwable.getMessage());
603+
}
604+
progressBar.setVisibility(View.GONE);
605+
});
584606
nearbyMapFragment.setBundleForUpdtes(bundle);
585607
nearbyMapFragment.updateMapSignificantly();
586608
updateListFragment();
@@ -646,4 +668,11 @@ public void onLocationChangedSlightly(LatLng latLng) {
646668
public void prepareViewsForSheetPosition(int bottomSheetState) {
647669
// TODO
648670
}
671+
672+
private void showErrorMessage(String message) {
673+
if (TextUtils.isEmpty(message)) {
674+
message = getString(R.string.something_went_wrong);
675+
}
676+
ViewUtil.showLongToast(NearbyActivity.this, message);
677+
}
649678
}

app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.mapbox.mapboxsdk.annotations.IconFactory;
99

10+
import java.io.IOException;
1011
import java.util.ArrayList;
1112
import java.util.Collections;
1213
import java.util.HashMap;
@@ -44,7 +45,7 @@ public NearbyController(NearbyPlaces nearbyPlaces,
4445
* @return NearbyPlacesInfo a variable holds Place list without distance information
4546
* and boundary coordinates of current Place List
4647
*/
47-
public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng) {
48+
public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng) throws IOException {
4849

4950
Timber.d("Loading attractions near %s", curLatLng);
5051
NearbyPlacesInfo nearbyPlacesInfo = new NearbyPlacesInfo();

app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ public NearbyPlaces() {
4040
}
4141
}
4242

43-
List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
43+
List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) throws IOException {
4444
List<Place> places = Collections.emptyList();
4545

46-
try {
4746
// increase the radius gradually to find a satisfactory number of nearby places
4847
while (radius <= MAX_RADIUS) {
4948
places = getFromWikidataQuery(curLatLng, lang, radius);
@@ -54,13 +53,6 @@ List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
5453
radius *= RADIUS_MULTIPLIER;
5554
}
5655
}
57-
} catch (IOException e) {
58-
Timber.d(e.toString());
59-
// errors tend to be caused by too many results (and time out)
60-
// try a small radius next time
61-
Timber.d("back to initial radius: %f", radius);
62-
radius = INITIAL_RADIUS;
63-
}
6456
// make sure we will be able to send at least one request next time
6557
if (radius > MAX_RADIUS) {
6658
radius = MAX_RADIUS;

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,7 @@
282282

283283
<string name="share_app_title">Share App</string>
284284
<string name="share_coordinates_not_present">Coordinates were not specified during image selection</string>
285+
<string name="something_went_wrong">Something went wrong</string>
286+
<string name="slow_internet">You seem to have slow internet connection</string>
285287

286288
</resources>

0 commit comments

Comments
 (0)