14
14
import android .support .design .widget .Snackbar ;
15
15
import android .support .v4 .app .FragmentTransaction ;
16
16
import android .support .v7 .app .AlertDialog ;
17
+ import android .util .Log ;
17
18
import android .view .LayoutInflater ;
18
19
19
20
import android .view .View ;
@@ -64,8 +65,6 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
64
65
LinearLayout bottomSheetDetails ;
65
66
@ BindView (R .id .transparentView )
66
67
View transparentView ;
67
- @ BindView (R .id .fab_recenter )
68
- View fabRecenter ;
69
68
70
69
@ Inject
71
70
LocationServiceManager locationManager ;
@@ -87,16 +86,19 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
87
86
88
87
private LatLng curLatLng ;
89
88
private Disposable placesDisposable ;
89
+ private Disposable placesDisposableCustom ;
90
90
private boolean lockNearbyView ; //Determines if the nearby places needs to be refreshed
91
91
public View view ;
92
92
private Snackbar snackbar ;
93
93
94
94
private LatLng lastKnownLocation ;
95
+ private LatLng customLatLng ;
95
96
96
97
private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE" ;
97
98
private BroadcastReceiver broadcastReceiver ;
98
99
99
100
private boolean onOrientationChanged = false ;
101
+ private boolean populateForCurrentLocation = false ;
100
102
101
103
@ Override
102
104
public void onCreate (@ Nullable Bundle savedInstanceState ) {
@@ -216,32 +218,35 @@ public void prepareViewsForSheetPosition(int bottomSheetState) {
216
218
217
219
@ Override
218
220
public void onLocationChangedSignificantly (LatLng latLng ) {
219
- refreshView (LOCATION_SIGNIFICANTLY_CHANGED );
221
+ refreshView (LOCATION_SIGNIFICANTLY_CHANGED );
220
222
}
221
223
222
224
@ Override
223
225
public void onLocationChangedSlightly (LatLng latLng ) {
224
- refreshView (LOCATION_SLIGHTLY_CHANGED );
226
+ refreshView (LOCATION_SLIGHTLY_CHANGED );
225
227
}
226
228
227
229
228
230
@ Override
229
231
public void onLocationChangedMedium (LatLng latLng ) {
230
232
// For nearby map actions, there are no differences between 500 meter location change (aka medium change) and slight change
231
- refreshView (LOCATION_SLIGHTLY_CHANGED );
233
+ refreshView (LOCATION_SLIGHTLY_CHANGED );
232
234
}
233
235
234
236
@ Override
235
237
public void onWikidataEditSuccessful () {
236
- refreshView (MAP_UPDATED );
238
+ // Do not refresh nearby map if we are checking other areas with search this area button
239
+ if (!nearbyMapFragment .searchThisAreaModeOn ) {
240
+ refreshView (MAP_UPDATED );
241
+ }
237
242
}
238
243
239
244
/**
240
245
* This method should be the single point to load/refresh nearby places
241
246
*
242
247
* @param locationChangeType defines if location shanged significantly or slightly
243
248
*/
244
- private void refreshView (LocationServiceManager .LocationChangeType locationChangeType ) {
249
+ public void refreshView (LocationServiceManager .LocationChangeType locationChangeType ) {
245
250
Timber .d ("Refreshing nearby places" );
246
251
if (lockNearbyView ) {
247
252
return ;
@@ -257,9 +262,11 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
257
262
258
263
if (curLatLng != null && curLatLng .equals (lastLocation )
259
264
&& !locationChangeType .equals (MAP_UPDATED )) { //refresh view only if location has changed
265
+ // Two exceptional cases to refresh nearby map manually.
260
266
if (!onOrientationChanged ) {
261
267
return ;
262
268
}
269
+
263
270
}
264
271
curLatLng = lastLocation ;
265
272
@@ -292,7 +299,7 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
292
299
bundle .putString ("CurLatLng" , gsonCurLatLng );
293
300
294
301
placesDisposable = Observable .fromCallable (() -> nearbyController
295
- .loadAttractionsFromLocation (curLatLng , false ))
302
+ .loadAttractionsFromLocation (curLatLng , curLatLng , false , true ))
296
303
.subscribeOn (Schedulers .io ())
297
304
.observeOn (AndroidSchedulers .mainThread ())
298
305
.subscribe (this ::populatePlaces ,
@@ -301,14 +308,70 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
301
308
showErrorMessage (getString (R .string .error_fetching_nearby_places ));
302
309
progressBar .setVisibility (View .GONE );
303
310
});
311
+
304
312
} else if (locationChangeType
305
313
.equals (LOCATION_SLIGHTLY_CHANGED )) {
306
314
Gson gson = new GsonBuilder ()
307
315
.registerTypeAdapter (Uri .class , new UriSerializer ())
308
316
.create ();
309
317
String gsonCurLatLng = gson .toJson (curLatLng );
310
318
bundle .putString ("CurLatLng" , gsonCurLatLng );
311
- updateMapFragment (true );
319
+ updateMapFragment (false ,true , null , null );
320
+ }
321
+
322
+ if (nearbyMapFragment != null ) {
323
+ nearbyMapFragment .searchThisAreaButton .setVisibility (View .GONE );
324
+ }
325
+ }
326
+
327
+ /**
328
+ * This method should be used with "Search this are button". This method will search nearby
329
+ * points around any custom location (target location when user clicked on search this area)
330
+ * button. It populates places for custom location.
331
+ * @param customLatLng Custom area which we will search around
332
+ */
333
+ public void refreshViewForCustomLocation (LatLng customLatLng , boolean refreshForCurrentLocation ) {
334
+
335
+ if (customLatLng == null ) {
336
+ // If null, return
337
+ return ;
338
+ }
339
+
340
+ populateForCurrentLocation = refreshForCurrentLocation ;
341
+ this .customLatLng = customLatLng ;
342
+ placesDisposableCustom = Observable .fromCallable (() -> nearbyController
343
+ .loadAttractionsFromLocation (curLatLng , customLatLng , false , populateForCurrentLocation ))
344
+ .subscribeOn (Schedulers .io ())
345
+ .observeOn (AndroidSchedulers .mainThread ())
346
+ .subscribe (this ::populatePlacesFromCustomLocation ,
347
+ throwable -> {
348
+ Timber .d (throwable );
349
+ showErrorMessage (getString (R .string .error_fetching_nearby_places ));
350
+ });
351
+
352
+ if (nearbyMapFragment != null ) {
353
+ nearbyMapFragment .searchThisAreaButton .setVisibility (View .GONE );
354
+ }
355
+ }
356
+
357
+ /**
358
+ * Populates places for custom location, should be used for finding nearby places around a
359
+ * location where you are not at.
360
+ * @param nearbyPlacesInfo This variable has place list information and distances.
361
+ */
362
+ private void populatePlacesFromCustomLocation (NearbyController .NearbyPlacesInfo nearbyPlacesInfo ) {
363
+ //NearbyMapFragment nearbyMapFragment = getMapFragment();
364
+ if (nearbyMapFragment != null ) {
365
+ nearbyMapFragment .searchThisAreaButtonProgressBar .setVisibility (View .GONE );
366
+ }
367
+
368
+ if (nearbyMapFragment != null && curLatLng != null ) {
369
+ if (!populateForCurrentLocation ) {
370
+ nearbyMapFragment .updateMapSignificantlyForCustomLocation (customLatLng , nearbyPlacesInfo .placeList );
371
+ } else {
372
+ updateMapFragment (true ,true , customLatLng , nearbyPlacesInfo );
373
+ }
374
+ updateListFragmentForCustomLocation (nearbyPlacesInfo .placeList );
312
375
}
313
376
}
314
377
@@ -342,7 +405,7 @@ private void populatePlaces(NearbyController.NearbyPlacesInfo nearbyPlacesInfo)
342
405
} else {
343
406
// There are fragments, just update the map and list
344
407
Timber .d ("Map fragment already exists, just update the map and list" );
345
- updateMapFragment (false );
408
+ updateMapFragment (false , false , null , null );
346
409
updateListFragment ();
347
410
}
348
411
}
@@ -364,7 +427,11 @@ private void lockNearbyView(boolean lock) {
364
427
}
365
428
}
366
429
367
- private void updateMapFragment (boolean isSlightUpdate ) {
430
+ private void updateMapFragment (boolean updateViaButton , boolean isSlightUpdate , @ Nullable LatLng customLatLng , @ Nullable NearbyController .NearbyPlacesInfo nearbyPlacesInfo ) {
431
+
432
+ if (nearbyMapFragment .searchThisAreaModeOn ) {
433
+ return ;
434
+ }
368
435
/*
369
436
Significant update means updating nearby place markers. Slightly update means only
370
437
updating current location marker and camera target.
@@ -380,14 +447,14 @@ private void updateMapFragment(boolean isSlightUpdate) {
380
447
* If we are close to nearby places boundaries, we need a significant update to
381
448
* get new nearby places. Check order is south, north, west, east
382
449
* */
383
- if (nearbyMapFragment .boundaryCoordinates != null
450
+ if (nearbyMapFragment .boundaryCoordinates != null && ! nearbyMapFragment . searchThisAreaModeOn
384
451
&& (curLatLng .getLatitude () <= nearbyMapFragment .boundaryCoordinates [0 ].getLatitude ()
385
452
|| curLatLng .getLatitude () >= nearbyMapFragment .boundaryCoordinates [1 ].getLatitude ()
386
453
|| curLatLng .getLongitude () <= nearbyMapFragment .boundaryCoordinates [2 ].getLongitude ()
387
454
|| curLatLng .getLongitude () >= nearbyMapFragment .boundaryCoordinates [3 ].getLongitude ())) {
388
455
// populate places
389
456
placesDisposable = Observable .fromCallable (() -> nearbyController
390
- .loadAttractionsFromLocation (curLatLng , false ))
457
+ .loadAttractionsFromLocation (curLatLng , curLatLng , false , updateViaButton ))
391
458
.subscribeOn (Schedulers .io ())
392
459
.observeOn (AndroidSchedulers .mainThread ())
393
460
.subscribe (this ::populatePlaces ,
@@ -397,11 +464,16 @@ private void updateMapFragment(boolean isSlightUpdate) {
397
464
progressBar .setVisibility (View .GONE );
398
465
});
399
466
nearbyMapFragment .setBundleForUpdtes (bundle );
400
- nearbyMapFragment .updateMapSignificantly ();
467
+ nearbyMapFragment .updateMapSignificantlyForCurrentLocation ();
401
468
updateListFragment ();
402
469
return ;
403
470
}
404
471
472
+ if (updateViaButton ) {
473
+ nearbyMapFragment .updateMapSignificantlyForCustomLocation (customLatLng , nearbyPlacesInfo .placeList );
474
+ return ;
475
+ }
476
+
405
477
/*
406
478
If this is the map update just after orientation change, then it is not a slight update
407
479
anymore. We want to significantly update map after each orientation change
@@ -416,7 +488,7 @@ private void updateMapFragment(boolean isSlightUpdate) {
416
488
nearbyMapFragment .updateMapSlightly ();
417
489
} else {
418
490
nearbyMapFragment .setBundleForUpdtes (bundle );
419
- nearbyMapFragment .updateMapSignificantly ();
491
+ nearbyMapFragment .updateMapSignificantlyForCurrentLocation ();
420
492
updateListFragment ();
421
493
}
422
494
} else {
@@ -433,6 +505,15 @@ private void updateListFragment() {
433
505
nearbyListFragment .updateNearbyListSignificantly ();
434
506
}
435
507
508
+ /**
509
+ * Updates nearby list for custom location, will be used with search this area method. When you
510
+ * want to search for a place where you are not at.
511
+ * @param placeList List of places around your manually chosen target location from map.
512
+ */
513
+ private void updateListFragmentForCustomLocation (List <Place > placeList ) {
514
+ nearbyListFragment .updateNearbyListSignificantlyForCustomLocation (placeList );
515
+ }
516
+
436
517
/**
437
518
* Calls fragment for map view.
438
519
*/
@@ -650,6 +731,9 @@ public void onDestroy() {
650
731
if (placesDisposable != null ) {
651
732
placesDisposable .dispose ();
652
733
}
734
+ if (placesDisposableCustom != null ) {
735
+ placesDisposableCustom .dispose ();
736
+ }
653
737
}
654
738
655
739
@ Override
0 commit comments