mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Merge pull request #531 from whym/nearby
Fix initial emptiness of Nearby list + refactoring
This commit is contained in:
commit
94c123af8e
4 changed files with 27 additions and 65 deletions
|
|
@ -8,26 +8,15 @@ import android.widget.ArrayAdapter;
|
||||||
|
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class NearbyAdapter extends ArrayAdapter<Place> {
|
public class NearbyAdapter extends ArrayAdapter<Place> {
|
||||||
private List<Place> placesList;
|
|
||||||
private Context context;
|
|
||||||
|
|
||||||
public List<Place> getPlacesList() {
|
|
||||||
return placesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Accepts activity context and list of places.
|
/** Accepts activity context and list of places.
|
||||||
* @param context activity context
|
* @param context activity context
|
||||||
* @param places list of places
|
|
||||||
*/
|
*/
|
||||||
public NearbyAdapter(Context context, List<Place> places) {
|
public NearbyAdapter(Context context) {
|
||||||
super(context, R.layout.item_place, places);
|
super(context, R.layout.item_place);
|
||||||
this.context = context;
|
|
||||||
placesList = places;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -43,13 +32,14 @@ public class NearbyAdapter extends ArrayAdapter<Place> {
|
||||||
}
|
}
|
||||||
|
|
||||||
NearbyViewHolder viewHolder = new NearbyViewHolder(convertView);
|
NearbyViewHolder viewHolder = new NearbyViewHolder(convertView);
|
||||||
viewHolder.bindModel(context, place);
|
viewHolder.bindModel(getContext(), place);
|
||||||
// Return the completed view to render on screen
|
// Return the completed view to render on screen
|
||||||
return convertView;
|
return convertView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(int position) {
|
public long getItemId(int position) {
|
||||||
|
// TODO: use Wikidata Q-ID instead?
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ 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 timber.log.Timber;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
@ -30,15 +29,17 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class NearbyListFragment extends ListFragment implements TaskListener {
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class NearbyListFragment extends ListFragment {
|
||||||
|
|
||||||
private static final int MAX_RESULTS = 1000;
|
private static final int MAX_RESULTS = 1000;
|
||||||
private NearbyAsyncTask nearbyAsyncTask;
|
private NearbyAsyncTask nearbyAsyncTask;
|
||||||
|
|
||||||
@BindView(R.id.listview) ListView listview;
|
@BindView(R.id.listView) ListView listview;
|
||||||
@BindView(R.id.progressBar) ProgressBar progressBar;
|
@BindView(R.id.progressBar) ProgressBar progressBar;
|
||||||
|
|
||||||
private boolean isTaskRunning = false;
|
private NearbyAdapter adapter;
|
||||||
|
|
||||||
public NearbyListFragment() {
|
public NearbyListFragment() {
|
||||||
}
|
}
|
||||||
|
|
@ -56,15 +57,19 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
|
||||||
Timber.d("NearbyListFragment created");
|
Timber.d("NearbyListFragment created");
|
||||||
View view = inflater.inflate(R.layout.fragment_nearby, container, false);
|
View view = inflater.inflate(R.layout.fragment_nearby, container, false);
|
||||||
ButterKnife.bind(this, view);
|
ButterKnife.bind(this, view);
|
||||||
|
adapter = new NearbyAdapter(getActivity());
|
||||||
|
listview.setAdapter(adapter);
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
|
|
||||||
//Check that this is the first time view is created, to avoid double list when screen orientation changed
|
// Check that this is the first time view is created,
|
||||||
|
// to avoid double list when screen orientation changed
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
nearbyAsyncTask = new NearbyAsyncTask(this);
|
adapter.clear();
|
||||||
|
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");
|
||||||
|
|
@ -74,29 +79,13 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private boolean isTaskRunning() {
|
||||||
public void onSaveInstanceState(Bundle outInstanceState) {
|
return nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED;
|
||||||
// See http://stackoverflow.com/questions/8942135/listview-added-dublicate-item-in-list-when-screen-orientation-changes
|
|
||||||
outInstanceState.putInt("value", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTaskStarted() {
|
|
||||||
isTaskRunning = true;
|
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTaskFinished(List<Place> result) {
|
|
||||||
if (progressBar != null) {
|
|
||||||
progressBar.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
isTaskRunning = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -114,23 +103,17 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
// See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app
|
// See http://stackoverflow.com/questions/18264408/incomplete-asynctask-crashes-my-app
|
||||||
if (nearbyAsyncTask != null && nearbyAsyncTask.getStatus() != AsyncTask.Status.FINISHED) {
|
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>> {
|
||||||
|
|
||||||
private final TaskListener listener;
|
|
||||||
|
|
||||||
public NearbyAsyncTask(TaskListener listener) {
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreExecute() {
|
protected void onPreExecute() {
|
||||||
super.onPreExecute();
|
super.onPreExecute();
|
||||||
listener.onTaskStarted();
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -154,17 +137,16 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
progressBar.setVisibility(View.GONE);
|
if (progressBar != null) {
|
||||||
NearbyAdapter adapter = new NearbyAdapter(getActivity(), places);
|
progressBar.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
listview.setAdapter(adapter);
|
adapter.clear();
|
||||||
|
adapter.addAll(places);
|
||||||
listener.onTaskFinished(places);
|
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnItemClick(R.id.listview)
|
@OnItemClick(R.id.listView)
|
||||||
void onItemClicked(int position) {
|
void onItemClicked(int position) {
|
||||||
Place place = (Place) listview.getItemAtPosition(position);
|
Place place = (Place) listview.getItemAtPosition(position);
|
||||||
LatLng placeLatLng = place.location;
|
LatLng placeLatLng = place.location;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
// As per https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/
|
|
||||||
public interface TaskListener {
|
|
||||||
void onTaskStarted();
|
|
||||||
|
|
||||||
void onTaskFinished(List<Place> result);
|
|
||||||
}
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ListView
|
<ListView
|
||||||
android:id="@+id/listview"
|
android:id="@+id/listView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue