Improved channel loading behavior of MiroChannellistFragment

This commit is contained in:
daniel oeh 2012-08-02 22:49:22 +02:00
parent c9abaa970c
commit c084d172de
2 changed files with 112 additions and 57 deletions

View File

@ -14,6 +14,7 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.fragment.MiroChannellistFragment;
/** Displays results when a search for miroguide channels has been performed */
public class MiroSearchActivity extends SherlockFragmentActivity {
private static final String TAG = "MiroSearchActivity";

View File

@ -9,6 +9,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
@ -16,12 +17,19 @@ import android.widget.AbsListView.OnScrollListener;
import com.actionbarsherlock.app.SherlockListFragment;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.MiroChannellistAdapter;
import de.danoeh.antennapod.miroguide.con.MiroException;
import de.danoeh.antennapod.miroguide.con.MiroService;
import de.danoeh.antennapod.miroguide.model.MiroChannel;
/**
* Displays a list of MiroChannel objects that were results of a certain
* MiroService query. If the user reaches the bottom of the list, more entries
* will be loaded until all entries have been loaded or the maximum number of
* channels has been reached.
* */
public class MiroChannellistFragment extends SherlockListFragment {
private static final String TAG = "MiroChannellistFragment";
@ -29,11 +37,19 @@ public class MiroChannellistFragment extends SherlockListFragment {
private static final String ARG_FILTER_VALUE = "filter_value";
private static final String ARG_SORT = "sort";
private static final int MAX_CHANNELS = 200;
private static final int CHANNELS_PER_QUERY = MiroService.DEFAULT_CHANNEL_LIMIT;
private ArrayList<MiroChannel> channels;
private MiroChannellistAdapter listAdapter;
private int offset;
private boolean isLoadingChannels;
/**
* True if there are no more entries to load or if the maximum number of
* channels in the channellist has been reached
*/
private boolean stopLoading;
private View footer;
@ -41,6 +57,8 @@ public class MiroChannellistFragment extends SherlockListFragment {
private String filterValue;
private String sort;
private AsyncTask<Void, Void, List<MiroChannel>> channelLoader;
/**
* Creates a new instance of Channellist fragment.
*
@ -89,8 +107,6 @@ public class MiroChannellistFragment extends SherlockListFragment {
listAdapter = new MiroChannellistAdapter(getActivity(), 0, channels);
}
@Override
public void onResume() {
super.onResume();
@ -100,11 +116,19 @@ public class MiroChannellistFragment extends SherlockListFragment {
}
}
@Override
public void onPause() {
super.onPause();
if (channelLoader != null) {
channelLoader.cancel(true);
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().addFooterView(footer);
getListView().addFooterView(footer); // footer has to be added before the adapter has been set
getListView().setAdapter(listAdapter);
getListView().removeFooterView(footer);
@ -115,6 +139,7 @@ public class MiroChannellistFragment extends SherlockListFragment {
int visibleItemCount, int totalItemCount) {
int lastVisibleItem = firstVisibleItem + visibleItemCount;
if (lastVisibleItem == totalItemCount) {
if (AppConfig.DEBUG)
loadChannels();
}
}
@ -128,19 +153,43 @@ public class MiroChannellistFragment extends SherlockListFragment {
@SuppressLint("NewApi")
private void loadChannels() {
if (!isLoadingChannels) {
if (!stopLoading) {
isLoadingChannels = true;
AsyncTask<Void, Void, List<MiroChannel>> channelLoader = new AsyncTask<Void, Void, List<MiroChannel>>() {
channelLoader = new AsyncTask<Void, Void, List<MiroChannel>>() {
private MiroException exception;
@Override
protected void onCancelled() {
if (AppConfig.DEBUG)
Log.d(TAG, "Channel loader was cancelled");
}
@Override
protected void onPostExecute(List<MiroChannel> result) {
if (AppConfig.DEBUG)
Log.d(TAG, "Channel loading finished");
if (exception == null) {
getListView().removeFooterView(footer);
for (MiroChannel channel : result) {
channels.add(channel);
}
listAdapter.notifyDataSetChanged();
offset += MiroService.DEFAULT_CHANNEL_LIMIT;
offset += CHANNELS_PER_QUERY;
// check if fragment should not send any more
// queries
if (result.size() < CHANNELS_PER_QUERY) {
if (AppConfig.DEBUG)
Log.d(TAG,
"Query result was less than requested number of channels. Stopping to send any more queries");
stopLoading = true;
}
if (offset >= MAX_CHANNELS) {
if (AppConfig.DEBUG)
Log.d(TAG,
"Maximum number of feeds has been reached. Stopping to send any more queries");
stopLoading = true;
}
setListShown(true);
} else {
AlertDialog.Builder dialog = new AlertDialog.Builder(
@ -151,14 +200,15 @@ public class MiroChannellistFragment extends SherlockListFragment {
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
public void onClick(
DialogInterface dialog,
int which) {
dialog.dismiss();
getActivity().finish();
}
});
dialog.create().show();
}
isLoadingChannels = false;
}
@Override
@ -168,12 +218,12 @@ public class MiroChannellistFragment extends SherlockListFragment {
@Override
protected List<MiroChannel> doInBackground(Void... params) {
if (AppConfig.DEBUG)
Log.d(TAG, "Background channel loader started");
MiroService service = new MiroService();
try {
return service
.getChannelList(filter, filterValue, sort,
MiroService.DEFAULT_CHANNEL_LIMIT,
offset);
return service.getChannelList(filter, filterValue,
sort, CHANNELS_PER_QUERY, offset);
} catch (MiroException e) {
exception = e;
e.printStackTrace();
@ -183,11 +233,15 @@ public class MiroChannellistFragment extends SherlockListFragment {
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
channelLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
channelLoader
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
channelLoader.execute();
}
}
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "Channels are already being loaded");
}
}
}