diff options
author | ByteHamster <info@bytehamster.com> | 2021-02-26 11:18:19 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2021-02-26 11:18:19 +0100 |
commit | fb45ceffa628c05ffa58773048481c5ceeda2751 (patch) | |
tree | a607a21234acbff02590e943326a8434afb2d4b5 /app/src | |
parent | 39f9796656ae9b37e995af8da2977fd16746bfda (diff) | |
download | AntennaPod-fb45ceffa628c05ffa58773048481c5ceeda2751.zip |
Removed AsyncTask from TagListFragment
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/fragment/gpodnet/TagListFragment.java | 83 |
1 files changed, 31 insertions, 52 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/gpodnet/TagListFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/gpodnet/TagListFragment.java index a26ec9e84..9d0f99aa9 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/gpodnet/TagListFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/gpodnet/TagListFragment.java @@ -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)); + }); } } |