Fix map fragment

This commit is contained in:
Neslihan 2017-05-13 14:11:20 +03:00
parent 0901e67853
commit fc2557d47f
3 changed files with 73 additions and 161 deletions

View file

@ -21,7 +21,6 @@ import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.UriDeserializer;
import fr.free.nrw.commons.utils.UriSerializer;
public class NearbyActivity extends BaseActivity {
@ -46,6 +45,7 @@ public class NearbyActivity extends BaseActivity {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
bundle = new Bundle();
locationManager = new LocationServiceManager(this);
locationManager.registerLocationManager();
curLatLang = locationManager.getLatestLocation();
@ -78,15 +78,7 @@ public class NearbyActivity extends BaseActivity {
private void showMapView() {
if (!isMapViewActive) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
NearbyMapFragment fragment = new NearbyMapFragment();
fragment.setArguments(bundle);
ft.add(R.id.container, fragment);
ft.commit();
//NearbyController.loadAttractionsFromLocationToBaseMarkerOptions(curLatLang, placeList);
/*getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NearbyMapFragment()).commit();*/
setMapFragment();
isMapViewActive = true;
}
}
@ -97,15 +89,8 @@ public class NearbyActivity extends BaseActivity {
}
protected void refreshView() {
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, this);
nearbyAsyncTask = new NearbyAsyncTask();
nearbyAsyncTask.execute();
if (isMapViewActive) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NearbyMapFragment()).commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NearbyListFragment()).commit();
}
}
public LocationServiceManager getLocationManager() {
@ -144,27 +129,44 @@ public class NearbyActivity extends BaseActivity {
if (isCancelled()) {
return;
}
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, getApplicationContext());
Gson gson = new GsonBuilder()
gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();
gsonPlaceList = gson.toJson(placeList);
gsonCurLatLng = gson.toJson(curLatLang);
bundle = new Bundle();
bundle.clear();
bundle.putString("PlaceList", gsonPlaceList);
bundle.putString("CurLatLng", gsonCurLatLng);
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
NearbyListFragment fragment = new NearbyListFragment();
fragment.setArguments(bundle);
ft.add(R.id.container, fragment);
ft.commit();
if (isMapViewActive) {
setMapFragment();
} else {
setListFragment();
}
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
}
public void setMapFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
NearbyMapFragment fragment = new NearbyMapFragment();
fragment.setArguments(bundle);
ft.add(R.id.container, fragment);
ft.commit();
}
public void setListFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
NearbyListFragment fragment = new NearbyListFragment();
fragment.setArguments(bundle);
ft.add(R.id.container, fragment);
ft.commit();
}
}

View file

@ -2,21 +2,18 @@ package fr.free.nrw.commons.nearby;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
@ -45,19 +42,6 @@ public class NearbyListFragment extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();
if (bundle != null){
String gsonPlaceList = bundle.getString("PlaceList");
String gsonLatLng = bundle.getString("CurLatLng");
Type listType = new TypeToken<List<Place>>() {}.getType();
placeList = gson.fromJson(gsonPlaceList, listType);
Type curLatLngType = new TypeToken<LatLng>() {}.getType();
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
NearbyController.loadAttractionsFromLocationToPlaces(curLatLng, placeList);
}
setRetainInstance(true);
}
@ -78,20 +62,22 @@ public class NearbyListFragment extends ListFragment {
// Check that this is the first time view is created,
// to avoid double list when screen orientation changed
Bundle bundle = this.getArguments();
gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();
if (bundle != null){
String gsonPlaceList = bundle.getString("PlaceList");
String gsonLatLng = bundle.getString("CurLatLng");
Type listType = new TypeToken<List<Place>>() {}.getType();
placeList = gson.fromJson(gsonPlaceList, listType);
Type curLatLngType = new TypeToken<LatLng>() {}.getType();
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
placeList = NearbyController.loadAttractionsFromLocationToPlaces(curLatLng, placeList);
}
if (savedInstanceState == null) {
adapter.clear();
//nearbyAsyncTask = new NearbyAsyncTask();
//nearbyAsyncTask.execute();
//progressBar.setVisibility(View.VISIBLE);
Timber.d("Saved instance state is null, populating ListView");
} else {
//progressBar.setVisibility(View.GONE);
}
// If we are returning here from a screen orientation and the AsyncTask is still working,
// re-create and display the progress dialog.
if (isTaskRunning()) {
//progressBar.setVisibility(View.VISIBLE);
}
adapter.clear();
@ -99,72 +85,6 @@ public class NearbyListFragment extends ListFragment {
adapter.notifyDataSetChanged();
}
private boolean isTaskRunning() {
//return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED;
return false;
}
@Override
public void onDetach() {
// All dialogs should be closed before leaving the activity in order to avoid
// the: Activity has leaked window com.android.internal.policy... exception
/* if (progressBar != null && progressBar.isShown()) {
progressBar.setVisibility(View.GONE);
}*/
super.onDetach();
}
@Override
public void onDestroy() {
super.onDestroy();
// See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app
if (isTaskRunning()) {
//nearbyAsyncTask.cancel(true);
}
}
/*
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}
@Override
protected List<Place> doInBackground(Void... params) {
return NearbyController.loadAttractionsFromLocationToPlaces(
((NearbyActivity)getActivity())
.getLocationManager()
.getLatestLocation(), getActivity()
);
}
@Override
protected void onPostExecute(List<Place> places) {
super.onPostExecute(places);
if (isCancelled()) {
return;
}
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
adapter.clear();
adapter.addAll(places);
adapter.notifyDataSetChanged();
}
}
*/
@OnItemClick(R.id.listView)
void onItemClicked(int position) {
Place place = (Place) listview.getItemAtPosition(position);

View file

@ -1,12 +1,15 @@
package fr.free.nrw.commons.nearby;
import android.os.AsyncTask;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
@ -18,14 +21,19 @@ import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.android.telemetry.MapboxTelemetry;
import java.lang.reflect.Type;
import java.util.List;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.utils.UriDeserializer;
public class NearbyMapFragment extends android.support.v4.app.Fragment {
//private NearbyAsyncTask nearbyAsyncTask;
private fr.free.nrw.commons.location.LatLng currentLocation;
private MapView mapView;
private Gson gson;
private List<Place> placeList;
private List<BaseMarkerOptions> baseMarkerOptionses;
private fr.free.nrw.commons.location.LatLng curLatLng;
public NearbyMapFragment() {
@ -34,7 +42,21 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentLocation = ((NearbyActivity)getActivity()).getLocationManager().getLatestLocation();
Bundle bundle = this.getArguments();
gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();
if (bundle != null){
String gsonPlaceList = bundle.getString("PlaceList");
String gsonLatLng = bundle.getString("CurLatLng");
Type listType = new TypeToken<List<Place>>() {}.getType();
placeList = gson.fromJson(gsonPlaceList, listType);
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {}.getType();
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
baseMarkerOptionses = NearbyController
.loadAttractionsFromLocationToBaseMarkerOptions(curLatLng, placeList);
}
Mapbox.getInstance(getActivity(),
getString(R.string.mapbox_commons_app_token));
MapboxTelemetry.getInstance().setTelemetryEnabled(false);
@ -46,13 +68,19 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
MapboxMapOptions options = new MapboxMapOptions()
.styleUrl(Style.OUTDOORS)
.camera(new CameraPosition.Builder()
.target(new LatLng(currentLocation.latitude, currentLocation.longitude))
.target(new LatLng(curLatLng.latitude, curLatLng.longitude))
.zoom(11)
.build());
// create map
mapView = new MapView(getActivity(), options);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
mapboxMap.addMarkers(baseMarkerOptionses);
}
});
setHasOptionsMenu(false);
return mapView;
}
@ -60,8 +88,6 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//nearbyAsyncTask = new NearbyAsyncTask();
//nearbyAsyncTask.execute();
}
@Override
@ -93,40 +119,4 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
mapView.onDestroy();
super.onDestroyView();
}
/*
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected List<BaseMarkerOptions> doInBackground(Void... params) {
return NearbyController
.loadAttractionsFromLocationToBaseMarkerOptions(currentLocation, getActivity()
);
}
@Override
protected void onPostExecute(final List<BaseMarkerOptions> baseMarkerOptionses) {
super.onPostExecute(baseMarkerOptionses);
if (isCancelled()) {
return;
}
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
mapboxMap.addMarkers(baseMarkerOptionses);
}
});
}
}
*/
}