Removed AsyncTask from PodcastListFragment

This commit is contained in:
ByteHamster 2021-02-26 11:22:14 +01:00
parent fb45ceffa6
commit 6eb0ac5596
1 changed files with 43 additions and 57 deletions

View File

@ -1,10 +1,7 @@
package de.danoeh.antennapod.fragment.gpodnet; package de.danoeh.antennapod.fragment.gpodnet;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -13,9 +10,7 @@ import android.widget.Button;
import android.widget.GridView; import android.widget.GridView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import androidx.fragment.app.Fragment;
import java.util.List;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.activity.OnlineFeedViewActivity; import de.danoeh.antennapod.activity.OnlineFeedViewActivity;
@ -25,6 +20,12 @@ import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.sync.gpoddernet.GpodnetService; import de.danoeh.antennapod.core.sync.gpoddernet.GpodnetService;
import de.danoeh.antennapod.core.sync.gpoddernet.GpodnetServiceException; import de.danoeh.antennapod.core.sync.gpoddernet.GpodnetServiceException;
import de.danoeh.antennapod.core.sync.gpoddernet.model.GpodnetPodcast; import de.danoeh.antennapod.core.sync.gpoddernet.model.GpodnetPodcast;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
/** /**
* Displays a list of GPodnetPodcast-Objects in a GridView * Displays a list of GPodnetPodcast-Objects in a GridView
@ -36,6 +37,7 @@ public abstract class PodcastListFragment extends Fragment {
private ProgressBar progressBar; private ProgressBar progressBar;
private TextView txtvError; private TextView txtvError;
private Button butRetry; private Button butRetry;
private Disposable disposable;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -64,60 +66,44 @@ public abstract class PodcastListFragment extends Fragment {
protected abstract List<GpodnetPodcast> loadPodcastData(GpodnetService service) throws GpodnetServiceException; protected abstract List<GpodnetPodcast> loadPodcastData(GpodnetService service) throws GpodnetServiceException;
final void loadData() { final void loadData() {
AsyncTask<Void, Void, List<GpodnetPodcast>> loaderTask = new AsyncTask<Void, Void, List<GpodnetPodcast>>() { if (disposable != null) {
volatile Exception exception = null; disposable.dispose();
}
@Override gridView.setVisibility(View.GONE);
protected List<GpodnetPodcast> doInBackground(Void... params) { progressBar.setVisibility(View.VISIBLE);
try { txtvError.setVisibility(View.GONE);
butRetry.setVisibility(View.GONE);
disposable = Observable.fromCallable(
() -> {
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
GpodnetPreferences.getHosturl()); GpodnetPreferences.getHosturl());
return loadPodcastData(service); return loadPodcastData(service);
} catch (GpodnetServiceException e) { })
exception = e; .subscribeOn(Schedulers.io())
e.printStackTrace(); .observeOn(AndroidSchedulers.mainThread())
return null; .subscribe(
} podcasts -> {
} progressBar.setVisibility(View.GONE);
butRetry.setVisibility(View.GONE);
@Override if (podcasts.size() > 0) {
protected void onPostExecute(List<GpodnetPodcast> gpodnetPodcasts) { PodcastListAdapter listAdapter = new PodcastListAdapter(getContext(), 0, podcasts);
super.onPostExecute(gpodnetPodcasts); gridView.setAdapter(listAdapter);
final Context context = getActivity(); listAdapter.notifyDataSetChanged();
if (context != null && gpodnetPodcasts != null && gpodnetPodcasts.size() > 0) { gridView.setVisibility(View.VISIBLE);
PodcastListAdapter listAdapter = new PodcastListAdapter(context, 0, gpodnetPodcasts); txtvError.setVisibility(View.GONE);
gridView.setAdapter(listAdapter); } else {
listAdapter.notifyDataSetChanged(); gridView.setVisibility(View.GONE);
txtvError.setText(getString(R.string.search_status_no_results));
progressBar.setVisibility(View.GONE); txtvError.setVisibility(View.VISIBLE);
gridView.setVisibility(View.VISIBLE); }
txtvError.setVisibility(View.GONE); }, error -> {
butRetry.setVisibility(View.GONE); gridView.setVisibility(View.GONE);
} else if (context != null && gpodnetPodcasts != null) { progressBar.setVisibility(View.GONE);
gridView.setVisibility(View.GONE); txtvError.setText(getString(R.string.error_msg_prefix) + error.getMessage());
progressBar.setVisibility(View.GONE); txtvError.setVisibility(View.VISIBLE);
txtvError.setText(getString(R.string.search_status_no_results)); butRetry.setVisibility(View.VISIBLE);
txtvError.setVisibility(View.VISIBLE); Log.e(TAG, Log.getStackTraceString(error));
butRetry.setVisibility(View.GONE); });
} else if (context != null) {
gridView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
txtvError.setText(getString(R.string.error_msg_prefix) + exception.getMessage());
txtvError.setVisibility(View.VISIBLE);
butRetry.setVisibility(View.VISIBLE);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
gridView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
txtvError.setVisibility(View.GONE);
butRetry.setVisibility(View.GONE);
}
};
loaderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }