Optimise unnecesarry method call

This commit is contained in:
Neslihan 2017-05-13 13:41:53 +03:00
parent a99ae9e8bb
commit 0901e67853
9 changed files with 192 additions and 46 deletions

View file

@ -1,35 +1,57 @@
package fr.free.nrw.commons.nearby; package fr.free.nrw.commons.nearby;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.R; 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.location.LocationServiceManager;
import fr.free.nrw.commons.theme.BaseActivity; 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 { public class NearbyActivity extends BaseActivity {
@BindView(R.id.progressBar)
ProgressBar progressBar;
private boolean isMapViewActive = false; private boolean isMapViewActive = false;
//public List<Place> placeList;
private LocationServiceManager locationManager; private LocationServiceManager locationManager;
private LatLng curLatLang;
private Gson gson;
private String gsonPlaceList;
private String gsonCurLatLng;
private Bundle bundle;
private NearbyAsyncTask nearbyAsyncTask;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby); setContentView(R.layout.activity_nearby);
ButterKnife.bind(this);
if (getSupportActionBar() != null) { if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} }
locationManager = new LocationServiceManager(this); locationManager = new LocationServiceManager(this);
locationManager.registerLocationManager(); locationManager.registerLocationManager();
curLatLang = locationManager.getLatestLocation();
nearbyAsyncTask = new NearbyAsyncTask();
nearbyAsyncTask.execute();
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
NearbyListFragment fragment = new NearbyListFragment();
ft.add(R.id.container, fragment);
ft.commit();
} }
@Override @Override
@ -56,8 +78,15 @@ public class NearbyActivity extends BaseActivity {
private void showMapView() { private void showMapView() {
if (!isMapViewActive) { if (!isMapViewActive) {
getSupportFragmentManager().beginTransaction() FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
.replace(R.id.container, new NearbyMapFragment()).commit(); 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();*/
isMapViewActive = true; isMapViewActive = true;
} }
} }
@ -68,6 +97,8 @@ public class NearbyActivity extends BaseActivity {
} }
protected void refreshView() { protected void refreshView() {
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, this);
nearbyAsyncTask.execute();
if (isMapViewActive) { if (isMapViewActive) {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NearbyMapFragment()).commit(); .replace(R.id.container, new NearbyMapFragment()).commit();
@ -86,4 +117,54 @@ public class NearbyActivity extends BaseActivity {
super.onDestroy(); super.onDestroy();
locationManager.unregisterLocationManager(); locationManager.unregisterLocationManager();
} }
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected List<Place> doInBackground(Void... params) {
return NearbyController
.loadAttractionsFromLocation(curLatLang, getApplicationContext()
);
}
@Override
protected void onPostExecute(List<Place> placeList) {
super.onPostExecute(placeList);
if (isCancelled()) {
return;
}
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, getApplicationContext());
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();
gsonPlaceList = gson.toJson(placeList);
gsonCurLatLng = gson.toJson(curLatLang);
bundle = new Bundle();
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 (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
}
} }

View file

@ -26,7 +26,7 @@ import timber.log.Timber;
public class NearbyController { public class NearbyController {
private static final int MAX_RESULTS = 1000; private static final int MAX_RESULTS = 1000;
private static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context context) { public static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context context) {
Timber.d("Loading attractions near %s", curLatLng); Timber.d("Loading attractions near %s", curLatLng);
if (curLatLng == null) { if (curLatLng == null) {
return Collections.emptyList(); return Collections.emptyList();
@ -59,35 +59,33 @@ public class NearbyController {
/** /**
* Loads attractions from location for list view, we need to return Place data type. * Loads attractions from location for list view, we need to return Place data type.
* @param curLatLng users current location * @param curLatLng users current location
* @param context current activity * @param placeList list of nearby places in Place data type
* @return Place list that holds nearby places * @return Place list that holds nearby places
*/ */
public static List<Place> loadAttractionsFromLocationToPlaces(LatLng curLatLng, public static List<Place> loadAttractionsFromLocationToPlaces(
Context context) { LatLng curLatLng,
List<Place> placeList) {
List<Place> places = loadAttractionsFromLocation(curLatLng,context); placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
places = places.subList(0, Math.min(places.size(), MAX_RESULTS)); for (Place place: placeList) {
for (Place place: places) {
String distance = formatDistanceBetween(curLatLng, place.location); String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance); place.setDistance(distance);
} }
return places; return placeList;
} }
/** /**
*Loads attractions from location for map view, we need to return BaseMarkerOption data type. *Loads attractions from location for map view, we need to return BaseMarkerOption data type.
* @param curLatLng users current location * @param curLatLng users current location
* @param context the activity * @param placeList list of nearby places in Place data type
* @return BaseMarkerOprions list that holds nearby places * @return BaseMarkerOprions list that holds nearby places
*/ */
public static List<BaseMarkerOptions> loadAttractionsFromLocationToBaseMarkerOptions( public static List<BaseMarkerOptions> loadAttractionsFromLocationToBaseMarkerOptions (
LatLng curLatLng, LatLng curLatLng,
Context context) { List<Place> placeList) {
List<BaseMarkerOptions> baseMarkerOptionses = new ArrayList<>(); List<BaseMarkerOptions> baseMarkerOptionses = new ArrayList<>();
List<Place> places = loadAttractionsFromLocation(curLatLng,context); placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
places = places.subList(0, Math.min(places.size(), MAX_RESULTS)); for (Place place: placeList) {
for (Place place: places) {
String distance = formatDistanceBetween(curLatLng, place.location); String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance); place.setDistance(distance);
baseMarkerOptionses.add(new MarkerOptions() baseMarkerOptionses.add(new MarkerOptions()

View file

@ -11,6 +11,12 @@ import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ProgressBar; 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 java.util.List;
import butterknife.BindView; import butterknife.BindView;
@ -18,14 +24,18 @@ import butterknife.ButterKnife;
import butterknife.OnItemClick; import butterknife.OnItemClick;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.utils.UriDeserializer;
import timber.log.Timber; import timber.log.Timber;
public class NearbyListFragment extends ListFragment { public class NearbyListFragment extends ListFragment {
private NearbyAsyncTask nearbyAsyncTask; //private NearbyAsyncTask nearbyAsyncTask;
private Gson gson;
private List<Place> placeList;
private LatLng curLatLng;
@BindView(R.id.listView) ListView listview; @BindView(R.id.listView) ListView listview;
@BindView(R.id.progressBar) ProgressBar progressBar;
private NearbyAdapter adapter; private NearbyAdapter adapter;
@ -35,6 +45,19 @@ public class NearbyListFragment extends ListFragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(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); setRetainInstance(true);
} }
@ -57,32 +80,37 @@ public class NearbyListFragment extends ListFragment {
// to avoid double list when screen orientation changed // to avoid double list when screen orientation changed
if (savedInstanceState == null) { if (savedInstanceState == null) {
adapter.clear(); adapter.clear();
nearbyAsyncTask = new NearbyAsyncTask(); //nearbyAsyncTask = new NearbyAsyncTask();
nearbyAsyncTask.execute(); //nearbyAsyncTask.execute();
progressBar.setVisibility(View.VISIBLE); //progressBar.setVisibility(View.VISIBLE);
Timber.d("Saved instance state is null, populating ListView"); Timber.d("Saved instance state is null, populating ListView");
} else { } else {
progressBar.setVisibility(View.GONE); //progressBar.setVisibility(View.GONE);
} }
// If we are returning here from a screen orientation and the AsyncTask is still working, // If we are returning here from a screen orientation and the AsyncTask is still working,
// re-create and display the progress dialog. // re-create and display the progress dialog.
if (isTaskRunning()) { if (isTaskRunning()) {
progressBar.setVisibility(View.VISIBLE); //progressBar.setVisibility(View.VISIBLE);
} }
adapter.clear();
adapter.addAll(placeList);
adapter.notifyDataSetChanged();
} }
private boolean isTaskRunning() { private boolean isTaskRunning() {
return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED; //return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED;
return false;
} }
@Override @Override
public void onDetach() { public void onDetach() {
// All dialogs should be closed before leaving the activity in order to avoid // All dialogs should be closed before leaving the activity in order to avoid
// the: Activity has leaked window com.android.internal.policy... exception // the: Activity has leaked window com.android.internal.policy... exception
if (progressBar != null && progressBar.isShown()) { /* if (progressBar != null && progressBar.isShown()) {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
} }*/
super.onDetach(); super.onDetach();
} }
@ -92,10 +120,10 @@ public class NearbyListFragment extends ListFragment {
// See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app // See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app
if (isTaskRunning()) { if (isTaskRunning()) {
nearbyAsyncTask.cancel(true); //nearbyAsyncTask.cancel(true);
} }
} }
/*
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> { private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
@Override @Override
@ -135,6 +163,7 @@ public class NearbyListFragment extends ListFragment {
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} }
} }
*/
@OnItemClick(R.id.listView) @OnItemClick(R.id.listView)
void onItemClicked(int position) { void onItemClicked(int position) {

View file

@ -23,7 +23,7 @@ import java.util.List;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
public class NearbyMapFragment extends android.support.v4.app.Fragment { public class NearbyMapFragment extends android.support.v4.app.Fragment {
private NearbyAsyncTask nearbyAsyncTask; //private NearbyAsyncTask nearbyAsyncTask;
private fr.free.nrw.commons.location.LatLng currentLocation; private fr.free.nrw.commons.location.LatLng currentLocation;
private MapView mapView; private MapView mapView;
@ -60,8 +60,8 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
@Override @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
nearbyAsyncTask = new NearbyAsyncTask(); //nearbyAsyncTask = new NearbyAsyncTask();
nearbyAsyncTask.execute(); //nearbyAsyncTask.execute();
} }
@Override @Override
@ -93,7 +93,7 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
mapView.onDestroy(); mapView.onDestroy();
super.onDestroyView(); super.onDestroyView();
} }
/*
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> { private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<BaseMarkerOptions>> {
@Override @Override
@ -128,4 +128,5 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment {
}); });
} }
} }
*/
} }

View file

@ -5,7 +5,7 @@ import android.net.Uri;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
public class Place { public class Place{
public final String name; public final String name;
public final String description; public final String description;
@ -17,6 +17,7 @@ public class Place {
public Bitmap secondaryImage; public Bitmap secondaryImage;
public String distance; public String distance;
public Place(String name, String description, String longDescription, public Place(String name, String description, String longDescription,
Uri secondaryImageUrl, LatLng location) { Uri secondaryImageUrl, LatLng location) {
this.name = name; this.name = name;

View file

@ -0,0 +1,19 @@
package fr.free.nrw.commons.utils;
import android.net.Uri;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
public class UriDeserializer implements JsonDeserializer<Uri> {
@Override
public Uri deserialize(final JsonElement src, final Type srcType,
final JsonDeserializationContext context) throws JsonParseException {
return Uri.parse(src.getAsString());
}
}

View file

@ -0,0 +1,17 @@
package fr.free.nrw.commons.utils;
import android.net.Uri;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
public class UriSerializer implements JsonSerializer<Uri> {
public JsonElement serialize(Uri src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
}

View file

@ -5,6 +5,13 @@
android:layout_height="match_parent" android:layout_height="match_parent"
> >
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<FrameLayout <FrameLayout
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -4,13 +4,6 @@
android:orientation="vertical" android:orientation="vertical"
> >
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<ListView <ListView
android:id="@+id/listView" android:id="@+id/listView"
android:layout_width="match_parent" android:layout_width="match_parent"