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;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuInflater;
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.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 {
@BindView(R.id.progressBar)
ProgressBar progressBar;
private boolean isMapViewActive = false;
//public List<Place> placeList;
private LocationServiceManager locationManager;
private LatLng curLatLang;
private Gson gson;
private String gsonPlaceList;
private String gsonCurLatLng;
private Bundle bundle;
private NearbyAsyncTask nearbyAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby);
ButterKnife.bind(this);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
locationManager = new LocationServiceManager(this);
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
@ -56,8 +78,15 @@ public class NearbyActivity extends BaseActivity {
private void showMapView() {
if (!isMapViewActive) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NearbyMapFragment()).commit();
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();*/
isMapViewActive = true;
}
}
@ -68,6 +97,8 @@ public class NearbyActivity extends BaseActivity {
}
protected void refreshView() {
//placeList = NearbyController.loadAttractionsFromLocation(curLatLang, this);
nearbyAsyncTask.execute();
if (isMapViewActive) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NearbyMapFragment()).commit();
@ -86,4 +117,54 @@ public class NearbyActivity extends BaseActivity {
super.onDestroy();
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 {
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);
if (curLatLng == null) {
return Collections.emptyList();
@ -59,35 +59,33 @@ public class NearbyController {
/**
* Loads attractions from location for list view, we need to return Place data type.
* @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
*/
public static List<Place> loadAttractionsFromLocationToPlaces(LatLng curLatLng,
Context context) {
List<Place> places = loadAttractionsFromLocation(curLatLng,context);
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
for (Place place: places) {
public static List<Place> loadAttractionsFromLocationToPlaces(
LatLng curLatLng,
List<Place> placeList) {
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
for (Place place: placeList) {
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);
}
return places;
return placeList;
}
/**
*Loads attractions from location for map view, we need to return BaseMarkerOption data type.
* @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
*/
public static List<BaseMarkerOptions> loadAttractionsFromLocationToBaseMarkerOptions (
LatLng curLatLng,
Context context) {
List<Place> placeList) {
List<BaseMarkerOptions> baseMarkerOptionses = new ArrayList<>();
List<Place> places = loadAttractionsFromLocation(curLatLng,context);
places = places.subList(0, Math.min(places.size(), MAX_RESULTS));
for (Place place: places) {
placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS));
for (Place place: placeList) {
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);
baseMarkerOptionses.add(new MarkerOptions()

View file

@ -11,6 +11,12 @@ 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;
@ -18,14 +24,18 @@ import butterknife.ButterKnife;
import butterknife.OnItemClick;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.utils.UriDeserializer;
import timber.log.Timber;
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.progressBar) ProgressBar progressBar;
private NearbyAdapter adapter;
@ -35,6 +45,19 @@ 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);
}
@ -57,32 +80,37 @@ public class NearbyListFragment extends ListFragment {
// to avoid double list when screen orientation changed
if (savedInstanceState == null) {
adapter.clear();
nearbyAsyncTask = new NearbyAsyncTask();
nearbyAsyncTask.execute();
progressBar.setVisibility(View.VISIBLE);
//nearbyAsyncTask = new NearbyAsyncTask();
//nearbyAsyncTask.execute();
//progressBar.setVisibility(View.VISIBLE);
Timber.d("Saved instance state is null, populating ListView");
} else {
progressBar.setVisibility(View.GONE);
//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);
//progressBar.setVisibility(View.VISIBLE);
}
adapter.clear();
adapter.addAll(placeList);
adapter.notifyDataSetChanged();
}
private boolean isTaskRunning() {
return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED;
//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()) {
/* if (progressBar != null && progressBar.isShown()) {
progressBar.setVisibility(View.GONE);
}
}*/
super.onDetach();
}
@ -92,10 +120,10 @@ public class NearbyListFragment extends ListFragment {
// See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app
if (isTaskRunning()) {
nearbyAsyncTask.cancel(true);
//nearbyAsyncTask.cancel(true);
}
}
/*
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
@Override
@ -135,6 +163,7 @@ public class NearbyListFragment extends ListFragment {
adapter.notifyDataSetChanged();
}
}
*/
@OnItemClick(R.id.listView)
void onItemClicked(int position) {

View file

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

View file

@ -17,6 +17,7 @@ public class Place {
public Bitmap secondaryImage;
public String distance;
public Place(String name, String description, String longDescription,
Uri secondaryImageUrl, LatLng location) {
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"
>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"

View file

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