Merge pull request #4967 from ByteHamster/remove-asynctask
Remove deprecated asynctask from gpodder fragments
This commit is contained in:
commit
5bb92af38d
|
@ -1,10 +1,7 @@
|
|||
package de.danoeh.antennapod.fragment.gpodnet;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
@ -13,9 +10,7 @@ import android.widget.Button;
|
|||
import android.widget.GridView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.activity.MainActivity;
|
||||
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.GpodnetServiceException;
|
||||
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
|
||||
|
@ -36,6 +37,7 @@ public abstract class PodcastListFragment extends Fragment {
|
|||
private ProgressBar progressBar;
|
||||
private TextView txtvError;
|
||||
private Button butRetry;
|
||||
private Disposable disposable;
|
||||
|
||||
@Override
|
||||
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;
|
||||
|
||||
final void loadData() {
|
||||
AsyncTask<Void, Void, List<GpodnetPodcast>> loaderTask = new AsyncTask<Void, Void, List<GpodnetPodcast>>() {
|
||||
volatile Exception exception = null;
|
||||
|
||||
@Override
|
||||
protected List<GpodnetPodcast> doInBackground(Void... params) {
|
||||
try {
|
||||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
gridView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
txtvError.setVisibility(View.GONE);
|
||||
butRetry.setVisibility(View.GONE);
|
||||
disposable = Observable.fromCallable(
|
||||
() -> {
|
||||
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
|
||||
GpodnetPreferences.getHosturl());
|
||||
return loadPodcastData(service);
|
||||
} catch (GpodnetServiceException e) {
|
||||
exception = e;
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
podcasts -> {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
butRetry.setVisibility(View.GONE);
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<GpodnetPodcast> gpodnetPodcasts) {
|
||||
super.onPostExecute(gpodnetPodcasts);
|
||||
final Context context = getActivity();
|
||||
if (context != null && gpodnetPodcasts != null && gpodnetPodcasts.size() > 0) {
|
||||
PodcastListAdapter listAdapter = new PodcastListAdapter(context, 0, gpodnetPodcasts);
|
||||
gridView.setAdapter(listAdapter);
|
||||
listAdapter.notifyDataSetChanged();
|
||||
|
||||
progressBar.setVisibility(View.GONE);
|
||||
gridView.setVisibility(View.VISIBLE);
|
||||
txtvError.setVisibility(View.GONE);
|
||||
butRetry.setVisibility(View.GONE);
|
||||
} else if (context != null && gpodnetPodcasts != null) {
|
||||
gridView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
txtvError.setText(getString(R.string.search_status_no_results));
|
||||
txtvError.setVisibility(View.VISIBLE);
|
||||
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);
|
||||
if (podcasts.size() > 0) {
|
||||
PodcastListAdapter listAdapter = new PodcastListAdapter(getContext(), 0, podcasts);
|
||||
gridView.setAdapter(listAdapter);
|
||||
listAdapter.notifyDataSetChanged();
|
||||
gridView.setVisibility(View.VISIBLE);
|
||||
txtvError.setVisibility(View.GONE);
|
||||
} else {
|
||||
gridView.setVisibility(View.GONE);
|
||||
txtvError.setText(getString(R.string.search_status_no_results));
|
||||
txtvError.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}, error -> {
|
||||
gridView.setVisibility(View.GONE);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
txtvError.setText(getString(R.string.error_msg_prefix) + error.getMessage());
|
||||
txtvError.setVisibility(View.VISIBLE);
|
||||
butRetry.setVisibility(View.VISIBLE);
|
||||
Log.e(TAG, Log.getStackTraceString(error));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,32 +1,34 @@
|
|||
package de.danoeh.antennapod.fragment.gpodnet;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.ListFragment;
|
||||
import de.danoeh.antennapod.activity.MainActivity;
|
||||
import de.danoeh.antennapod.adapter.gpodnet.TagListAdapter;
|
||||
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
|
||||
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
|
||||
import de.danoeh.antennapod.core.sync.gpoddernet.GpodnetService;
|
||||
import de.danoeh.antennapod.core.sync.gpoddernet.GpodnetServiceException;
|
||||
import de.danoeh.antennapod.core.sync.gpoddernet.model.GpodnetTag;
|
||||
|
||||
import java.util.List;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class TagListFragment extends ListFragment {
|
||||
private static final int COUNT = 50;
|
||||
private static final String TAG = "TagListFragment";
|
||||
private Disposable disposable;
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
getListView().setOnItemClickListener((parent, view1, position, id) -> {
|
||||
GpodnetTag tag = (GpodnetTag) getListAdapter().getItem(position);
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
activity.loadChildFragment(TagFragment.newInstance(tag));
|
||||
((MainActivity) getActivity()).loadChildFragment(TagFragment.newInstance(tag));
|
||||
});
|
||||
|
||||
startLoadTask();
|
||||
|
@ -35,59 +37,36 @@ public class TagListFragment extends ListFragment {
|
|||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
cancelLoadTask();
|
||||
}
|
||||
|
||||
private AsyncTask<Void, Void, List<GpodnetTag>> loadTask;
|
||||
|
||||
private void cancelLoadTask() {
|
||||
if (loadTask != null && !loadTask.isCancelled()) {
|
||||
loadTask.cancel(true);
|
||||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void startLoadTask() {
|
||||
cancelLoadTask();
|
||||
loadTask = new AsyncTask<Void, Void, List<GpodnetTag>>() {
|
||||
private Exception exception;
|
||||
|
||||
@Override
|
||||
protected List<GpodnetTag> doInBackground(Void... params) {
|
||||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
setListShown(false);
|
||||
disposable = Observable.fromCallable(
|
||||
() -> {
|
||||
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
|
||||
GpodnetPreferences.getHosturl());
|
||||
try {
|
||||
return service.getTopTags(COUNT);
|
||||
} catch (GpodnetServiceException e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
setListShown(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<GpodnetTag> gpodnetTags) {
|
||||
super.onPostExecute(gpodnetTags);
|
||||
final Context context = getActivity();
|
||||
if (context != null) {
|
||||
if (gpodnetTags != null) {
|
||||
setListAdapter(new TagListAdapter(context, android.R.layout.simple_list_item_1, gpodnetTags));
|
||||
} else if (exception != null) {
|
||||
return service.getTopTags(COUNT);
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
tags -> {
|
||||
setListAdapter(new TagListAdapter(getContext(), android.R.layout.simple_list_item_1, tags));
|
||||
setListShown(true);
|
||||
}, error -> {
|
||||
TextView txtvError = new TextView(getActivity());
|
||||
txtvError.setText(exception.getMessage());
|
||||
txtvError.setText(error.getMessage());
|
||||
getListView().setEmptyView(txtvError);
|
||||
}
|
||||
setListShown(true);
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
loadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
setListShown(true);
|
||||
Log.e(TAG, Log.getStackTraceString(error));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue