Removed AsyncTask from TagListFragment

This commit is contained in:
ByteHamster 2021-02-26 11:18:19 +01:00
parent 39f9796656
commit fb45ceffa6
1 changed files with 31 additions and 52 deletions

View File

@ -1,32 +1,34 @@
package de.danoeh.antennapod.fragment.gpodnet; package de.danoeh.antennapod.fragment.gpodnet;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.ListFragment; import androidx.fragment.app.ListFragment;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.adapter.gpodnet.TagListAdapter; import de.danoeh.antennapod.adapter.gpodnet.TagListAdapter;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences; import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; 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.model.GpodnetTag; import de.danoeh.antennapod.core.sync.gpoddernet.model.GpodnetTag;
import io.reactivex.Observable;
import java.util.List; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class TagListFragment extends ListFragment { public class TagListFragment extends ListFragment {
private static final int COUNT = 50; private static final int COUNT = 50;
private static final String TAG = "TagListFragment";
private Disposable disposable;
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
getListView().setOnItemClickListener((parent, view1, position, id) -> { getListView().setOnItemClickListener((parent, view1, position, id) -> {
GpodnetTag tag = (GpodnetTag) getListAdapter().getItem(position); GpodnetTag tag = (GpodnetTag) getListAdapter().getItem(position);
MainActivity activity = (MainActivity) getActivity(); ((MainActivity) getActivity()).loadChildFragment(TagFragment.newInstance(tag));
activity.loadChildFragment(TagFragment.newInstance(tag));
}); });
startLoadTask(); startLoadTask();
@ -35,59 +37,36 @@ public class TagListFragment extends ListFragment {
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
cancelLoadTask();
}
private AsyncTask<Void, Void, List<GpodnetTag>> loadTask; if (disposable != null) {
disposable.dispose();
private void cancelLoadTask() {
if (loadTask != null && !loadTask.isCancelled()) {
loadTask.cancel(true);
} }
} }
private void startLoadTask() { private void startLoadTask() {
cancelLoadTask(); if (disposable != null) {
loadTask = new AsyncTask<Void, Void, List<GpodnetTag>>() { disposable.dispose();
private Exception exception; }
setListShown(false);
@Override disposable = Observable.fromCallable(
protected List<GpodnetTag> doInBackground(Void... params) { () -> {
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(),
GpodnetPreferences.getHosturl()); GpodnetPreferences.getHosturl());
try { return service.getTopTags(COUNT);
return service.getTopTags(COUNT); })
} catch (GpodnetServiceException e) { .subscribeOn(Schedulers.io())
e.printStackTrace(); .observeOn(AndroidSchedulers.mainThread())
exception = e; .subscribe(
return null; tags -> {
} setListAdapter(new TagListAdapter(getContext(), android.R.layout.simple_list_item_1, tags));
} setListShown(true);
}, error -> {
@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) {
TextView txtvError = new TextView(getActivity()); TextView txtvError = new TextView(getActivity());
txtvError.setText(exception.getMessage()); txtvError.setText(error.getMessage());
getListView().setEmptyView(txtvError); getListView().setEmptyView(txtvError);
} setListShown(true);
setListShown(true); Log.e(TAG, Log.getStackTraceString(error));
});
}
}
};
loadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }