From 77840ae9d06b857cce9868abc5261ec562e0a607 Mon Sep 17 00:00:00 2001 From: neslihanturan Date: Tue, 25 Sep 2018 16:42:09 +0300 Subject: [PATCH] Move nearby activity to new nearby frament --- .../nrw/commons/nearby/NearbyFragment.java | 144 ++++++++++++++- .../commons/nearby/NearbyListFragment.java | 4 +- app/src/main/res/layout/fragment_nearby.xml | 172 ++++++++++++++++-- .../main/res/layout/fragment_nearby_list.xml | 13 ++ 4 files changed, 317 insertions(+), 16 deletions(-) create mode 100644 app/src/main/res/layout/fragment_nearby_list.xml diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java index ef626b761..e11bf8aa9 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java @@ -1,6 +1,146 @@ package fr.free.nrw.commons.nearby; -import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; +import android.content.SharedPreferences; +import android.net.Uri; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.ProgressBar; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import fr.free.nrw.commons.R; +import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; +import fr.free.nrw.commons.location.LatLng; +import fr.free.nrw.commons.location.LocationServiceManager; +import fr.free.nrw.commons.location.LocationUpdateListener; +import fr.free.nrw.commons.utils.NetworkUtils; +import fr.free.nrw.commons.utils.UriSerializer; +import fr.free.nrw.commons.wikidata.WikidataEditListener; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; +import timber.log.Timber; + +import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED; +import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED; +import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.MAP_UPDATED; +import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED; + +public class NearbyFragment extends CommonsDaggerSupportFragment + implements LocationUpdateListener, + WikidataEditListener.WikidataP18EditListener { + + private static final int LOCATION_REQUEST = 1; + + @BindView(R.id.progressBar) + ProgressBar progressBar; + + @BindView(R.id.bottom_sheet) + LinearLayout bottomSheet; + @BindView(R.id.bottom_sheet_details) + LinearLayout bottomSheetDetails; + @BindView(R.id.transparentView) + View transparentView; + @BindView(R.id.fab_recenter) + View fabRecenter; + + @Inject + LocationServiceManager locationManager; + @Inject + NearbyController nearbyController; + @Inject + WikidataEditListener wikidataEditListener; + + @Inject + @Named("application_preferences") + SharedPreferences applicationPrefs; + + @Override + public void onLocationChangedSignificantly(LatLng latLng) { + refreshView(LOCATION_SIGNIFICANTLY_CHANGED); + } + + @Override + public void onLocationChangedSlightly(LatLng latLng) { + refreshView(LOCATION_SLIGHTLY_CHANGED); + } + + @Override + public void onWikidataEditSuccessful() { + refreshView(MAP_UPDATED); + } + + /** + * This method should be the single point to load/refresh nearby places + * + * @param locationChangeType defines if location shanged significantly or slightly + */ + private void refreshView(LocationServiceManager.LocationChangeType locationChangeType) { + if (lockNearbyView) { + return; + } + + if (!NetworkUtils.isInternetConnectionEstablished(this)) { + hideProgressBar(); + return; + } + + registerLocationUpdates(); + LatLng lastLocation = locationManager.getLastLocation(); + + if (curLatLng != null && curLatLng.equals(lastLocation) + && !locationChangeType.equals(MAP_UPDATED)) { //refresh view only if location has changed + return; + } + curLatLng = lastLocation; + + if (locationChangeType.equals(PERMISSION_JUST_GRANTED)) { + curLatLng = lastKnownLocation; + } + + if (curLatLng == null) { + Timber.d("Skipping update of nearby places as location is unavailable"); + return; + } + + if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED) + || locationChangeType.equals(PERMISSION_JUST_GRANTED) + || locationChangeType.equals(MAP_UPDATED)) { + progressBar.setVisibility(View.VISIBLE); + + //TODO: This hack inserts curLatLng before populatePlaces is called (see #1440). Ideally a proper fix should be found + Gson gson = new GsonBuilder() + .registerTypeAdapter(Uri.class, new UriSerializer()) + .create(); + String gsonCurLatLng = gson.toJson(curLatLng); + bundle.clear(); + bundle.putString("CurLatLng", gsonCurLatLng); + + placesDisposable = Observable.fromCallable(() -> nearbyController + .loadAttractionsFromLocation(curLatLng, false)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::populatePlaces, + throwable -> { + Timber.d(throwable); + showErrorMessage(getString(R.string.error_fetching_nearby_places)); + progressBar.setVisibility(View.GONE); + }); + } else if (locationChangeType + .equals(LOCATION_SLIGHTLY_CHANGED)) { + Gson gson = new GsonBuilder() + .registerTypeAdapter(Uri.class, new UriSerializer()) + .create(); + String gsonCurLatLng = gson.toJson(curLatLng); + bundle.putString("CurLatLng", gsonCurLatLng); + updateMapFragment(true); + } + } + -public class NearbyFragment extends CommonsDaggerSupportFragment { } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java index e9acd9979..8dd6cf3b9 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java @@ -30,13 +30,11 @@ import dagger.android.support.DaggerFragment; import fr.free.nrw.commons.R; import fr.free.nrw.commons.contributions.ContributionController; import fr.free.nrw.commons.location.LatLng; -import fr.free.nrw.commons.utils.ContributionUtils; import fr.free.nrw.commons.utils.UriDeserializer; import timber.log.Timber; import static android.app.Activity.RESULT_OK; import static android.content.pm.PackageManager.PERMISSION_GRANTED; -import static fr.free.nrw.commons.wikidata.WikidataConstants.WIKIDATA_ENTITY_ID_PREF; public class NearbyListFragment extends DaggerFragment { private Bundle bundleForUpdates; // Carry information from activity about changed nearby places and current location @@ -75,7 +73,7 @@ public class NearbyListFragment extends DaggerFragment { ViewGroup container, Bundle savedInstanceState) { Timber.d("NearbyListFragment created"); - View view = inflater.inflate(R.layout.fragment_nearby, container, false); + View view = inflater.inflate(R.layout.fragment_nearby_list, container, false); recyclerView = view.findViewById(R.id.listView); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); diff --git a/app/src/main/res/layout/fragment_nearby.xml b/app/src/main/res/layout/fragment_nearby.xml index ef580fe99..ccfb02f0e 100644 --- a/app/src/main/res/layout/fragment_nearby.xml +++ b/app/src/main/res/layout/fragment_nearby.xml @@ -1,13 +1,163 @@ - - - + + android:layout_height="match_parent"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - diff --git a/app/src/main/res/layout/fragment_nearby_list.xml b/app/src/main/res/layout/fragment_nearby_list.xml new file mode 100644 index 000000000..ef580fe99 --- /dev/null +++ b/app/src/main/res/layout/fragment_nearby_list.xml @@ -0,0 +1,13 @@ + + + + +