Move nearby activity to new nearby frament

This commit is contained in:
neslihanturan 2018-09-25 16:42:09 +03:00
parent 5395967c86
commit 77840ae9d0
4 changed files with 317 additions and 16 deletions

View file

@ -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 {
}

View file

@ -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()));

View file

@ -1,13 +1,163 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/listView"
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:gravity="center_vertical"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:id="@+id/transparentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@id/toolbar"
android:background="#aa969696"
android:elevation="6dp">
</View>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_recenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/toolbar"
android:layout_marginRight="12dp"
android:layout_marginTop="6dp"
android:clickable="true"
android:visibility="invisible"
app:backgroundTint="@color/main_background_light"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchorGravity="top|right|end"
app:srcCompat="@drawable/ic_my_location_black_24dp" />
</RelativeLayout>
<include layout="@layout/bottom_sheet_nearby" />
<include
android:id="@+id/bottom_sheet_details"
layout="@layout/bottom_sheet_details" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:clickable="true"
android:visibility="invisible"
app:backgroundTint="@color/button_blue"
app:elevation="6dp"
app:fabSize="normal"
app:layout_anchor="@id/bottom_sheet_details"
app:layout_anchorGravity="top|right|end"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/ic_add_white_24dp" />
<View
android:id="@+id/empty_view2"
android:layout_width="56dp"
android:layout_height="306dip"
android:visibility="invisible"
app:layout_anchor="@id/fab_plus"
app:layout_anchorGravity="center_horizontal" />
<View
android:id="@+id/empty_view1"
android:layout_width="56dp"
android:layout_height="186dip"
android:visibility="invisible"
app:layout_anchor="@id/fab_plus"
app:layout_anchorGravity="center_horizontal" />
<View
android:id="@+id/empty_view"
android:layout_width="56dp"
android:layout_height="66dip"
android:visibility="invisible"
app:layout_anchor="@id/fab_plus"
app:layout_anchorGravity="center_horizontal" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:tint="@color/button_blue"
android:visibility="invisible"
app:backgroundTint="@color/main_background_light"
app:elevation="6dp"
app:fabSize="mini"
app:layout_anchor="@id/empty_view1"
app:layout_anchorGravity="center_horizontal"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/ic_photo_camera_white_24dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_galery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:tint="@color/button_blue"
android:visibility="invisible"
app:backgroundTint="@color/main_background_light"
app:elevation="6dp"
app:fabSize="mini"
app:layout_anchor="@id/empty_view"
app:layout_anchorGravity="center_horizontal"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/ic_photo_white_24dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_commons_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:visibility="invisible"
app:backgroundTint="@color/main_background_light"
app:elevation="6dp"
app:fabSize="mini"
app:layout_anchor="@id/empty_view2"
app:layout_anchorGravity="center_horizontal"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/ic_commons_icon_vector" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

View file

@ -0,0 +1,13 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>