Merge pull request #3179 from ByteHamster/io-main-thread

Do not query database on main thread
This commit is contained in:
H. Lehmann 2019-05-12 10:17:52 +02:00 committed by GitHub
commit 627c943a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
@ -12,12 +13,17 @@ import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.greenrobot.event.EventBus;
import io.reactivex.Maybe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class ChaptersFragment extends ListFragment {
private static final String TAG = "ChaptersFragment";
private ChaptersListAdapter adapter;
private PlaybackController controller;
private Disposable disposable;
@Override
@ -42,10 +48,7 @@ public class ChaptersFragment extends ListFragment {
controller = new PlaybackController(getActivity(), false) {
@Override
public boolean loadMediaInfo() {
if (getMedia() == null) {
return false;
}
onMediaChanged(getMedia());
ChaptersFragment.this.loadMediaInfo();
return true;
}
@ -55,7 +58,7 @@ public class ChaptersFragment extends ListFragment {
}
};
controller.init();
onMediaChanged(controller.getMedia());
loadMediaInfo();
EventBus.getDefault().register(this);
}
@ -73,6 +76,24 @@ public class ChaptersFragment extends ListFragment {
}
}
private void loadMediaInfo() {
if (disposable != null) {
disposable.dispose();
}
disposable = Maybe.create(emitter -> {
Playable media = controller.getMedia();
if (media != null) {
emitter.onSuccess(media);
} else {
emitter.onComplete();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(media -> onMediaChanged((Playable) media),
error -> Log.e(TAG, Log.getStackTraceString(error)));
}
private void onMediaChanged(Playable media) {
if (adapter != null) {
adapter.setMedia(media);

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
@ -18,6 +19,10 @@ import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.greenrobot.event.EventBus;
import io.reactivex.Maybe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* Displays the cover and the title of a FeedItem.
@ -31,6 +36,7 @@ public class CoverFragment extends Fragment {
private TextView txtvEpisodeTitle;
private ImageView imgvCover;
private PlaybackController controller;
private Disposable disposable;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -44,11 +50,24 @@ public class CoverFragment extends Fragment {
}
private void loadMediaInfo() {
Playable media = controller.getMedia();
if (media == null) {
Log.w(TAG, "loadMediaInfo was called while media was null");
return;
if (disposable != null) {
disposable.dispose();
}
disposable = Maybe.create(emitter -> {
Playable media = controller.getMedia();
if (media != null) {
emitter.onSuccess(media);
} else {
emitter.onComplete();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(media -> displayMediaInfo((Playable) media),
error -> Log.e(TAG, Log.getStackTraceString(error)));
}
private void displayMediaInfo(@NonNull Playable media) {
txtvPodcastTitle.setText(media.getFeedTitle());
txtvEpisodeTitle.setText(media.getEpisodeTitle());
Glide.with(this)
@ -73,9 +92,6 @@ public class CoverFragment extends Fragment {
controller = new PlaybackController(getActivity(), false) {
@Override
public boolean loadMediaInfo() {
if (getMedia() == null) {
return false;
}
CoverFragment.this.loadMediaInfo();
return true;
}