Migrated feed list update event to EventBus

This commit is contained in:
ByteHamster 2019-10-25 11:19:52 +02:00
parent d4b9d0fd59
commit 728ed23077
11 changed files with 62 additions and 112 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
@ -201,7 +202,7 @@ public class UITestUtils {
adapter.setCompleteFeed(hostedFeeds.toArray(new Feed[hostedFeeds.size()]));
adapter.setQueue(queue);
adapter.close();
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new FeedListUpdateEvent());
EventBus.getDefault().post(QueueEvent.setQueue(queue));
}

View File

@ -34,6 +34,7 @@ import android.widget.Toast;
import com.bumptech.glide.Glide;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.Validate;
@ -85,8 +86,6 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
private static final String TAG = "MainActivity";
private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE;
public static final String PREF_NAME = "MainActivityPrefs";
public static final String PREF_IS_FIRST_LAUNCH = "prefMainActivityIsFirstLaunch";
public static final String PREF_LAST_FRAGMENT_TAG = "prefMainActivityLastFragmentTag";
@ -490,7 +489,6 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
@Override
public void onStart() {
super.onStart();
EventDistributor.getInstance().register(contentUpdate);
EventBus.getDefault().register(this);
RatingDialog.init(this);
}
@ -518,7 +516,6 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
@Override
protected void onStop() {
super.onStop();
EventDistributor.getInstance().unregister(contentUpdate);
EventBus.getDefault().unregister(this);
if (disposable != null) {
disposable.dispose();
@ -822,16 +819,11 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
loadData();
}
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((EVENTS & arg) != 0) {
Log.d(TAG, "Received contentUpdate Intent.");
loadData();
}
}
};
@Subscribe
public void onFeedListChanged(FeedListUpdateEvent event) {
loadData();
}
private void handleNavIntent() {
Log.d(TAG, "handleNavIntent()");

View File

@ -36,6 +36,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.NavListAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
@ -119,7 +120,6 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
if (disposable != null) {
disposable.dispose();
}
EventDistributor.getInstance().unregister(contentUpdate);
saveCurrentFragment();
}
@ -171,7 +171,6 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
@Override
protected void onStart() {
super.onStart();
EventDistributor.getInstance().register(contentUpdate);
loadData();
}
@ -445,16 +444,10 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
snackbar.show();
}
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((EventDistributor.FEED_LIST_UPDATE & arg) != 0) {
Log.d(TAG, "Received contentUpdate Intent.");
loadData();
}
}
};
@Subscribe
public void onFeedListChanged(FeedListUpdateEvent event) {
loadData();
}
private final NavListAdapter.ItemAccess itemAccess = new NavListAdapter.ItemAccess() {
@Override

View File

@ -32,6 +32,7 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
@ -94,7 +95,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
public static final String ARG_TITLE = "title";
private static final int RESULT_ERROR = 2;
private static final String TAG = "OnlineFeedViewActivity";
private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE;
private volatile List<Feed> feeds;
private Feed feed;
private String selectedDownloadUrl;
@ -109,30 +109,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
private Disposable download;
private Disposable parser;
private Disposable updater;
private final EventDistributor.EventListener listener = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((arg & EventDistributor.FEED_LIST_UPDATE) != 0) {
updater = Observable.fromCallable(DBReader::getFeedList)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
feeds -> {
OnlineFeedViewActivity.this.feeds = feeds;
setSubscribeButtonState(feed);
}, error -> Log.e(TAG, Log.getStackTraceString(error))
);
} else if ((arg & EVENTS) != 0) {
setSubscribeButtonState(feed);
}
}
};
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
setSubscribeButtonState(feed);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -202,7 +178,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
protected void onStart() {
super.onStart();
isPaused = false;
EventDistributor.getInstance().register(listener);
EventBus.getDefault().register(this);
}
@ -210,7 +185,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
protected void onStop() {
super.onStop();
isPaused = true;
EventDistributor.getInstance().unregister(listener);
EventBus.getDefault().unregister(this);
if (downloader != null && !downloader.isFinished()) {
downloader.cancel();
@ -312,6 +286,25 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
}
}
@Subscribe
public void onFeedListChanged(FeedListUpdateEvent event) {
updater = Observable.fromCallable(DBReader::getFeedList)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
feeds -> {
OnlineFeedViewActivity.this.feeds = feeds;
setSubscribeButtonState(feed);
}, error -> Log.e(TAG, Log.getStackTraceString(error))
);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
setSubscribeButtonState(feed);
}
private void parseFeed() {
if (feed == null || (feed.getFile_url() == null && feed.isDownloaded())) {
throw new IllegalStateException("feed must be non-null and downloaded when parseFeed is called");
@ -388,7 +381,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
this.feed = feed;
this.selectedDownloadUrl = feed.getDownload_url();
EventDistributor.getInstance().register(listener);
ListView listView = findViewById(R.id.listview);
listView.setSelector(android.R.color.transparent);
LayoutInflater inflater = LayoutInflater.from(this);

View File

@ -25,6 +25,7 @@ import android.widget.Toast;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import de.danoeh.antennapod.core.event.DownloadLogEvent;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.PlayerStatusEvent;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
@ -67,9 +68,6 @@ import io.reactivex.schedulers.Schedulers;
public abstract class EpisodesListFragment extends Fragment {
public static final String TAG = "EpisodesListFragment";
private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE;
private static final String DEFAULT_PREF_NAME = "PrefAllEpisodesFragment";
private static final String PREF_SCROLL_POSITION = "scroll_position";
private static final String PREF_SCROLL_OFFSET = "scroll_offset";
@ -103,7 +101,6 @@ public abstract class EpisodesListFragment extends Fragment {
public void onStart() {
super.onStart();
setHasOptionsMenu(true);
EventDistributor.getInstance().register(contentUpdate);
EventBus.getDefault().register(this);
loadItems();
}
@ -125,7 +122,6 @@ public abstract class EpisodesListFragment extends Fragment {
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
EventDistributor.getInstance().unregister(contentUpdate);
if (disposable != null) {
disposable.dispose();
}
@ -427,22 +423,15 @@ public abstract class EpisodesListFragment extends Fragment {
updateUi();
}
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) {
updateUi();
}
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((arg & EVENTS) != 0) {
loadItems();
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
requireActivity().invalidateOptionsMenu();
}
}
}
};
@Subscribe(threadMode = ThreadMode.MAIN)
public void onFeedListChanged(FeedListUpdateEvent event) {
updateUi();
}
void loadItems() {
if (disposable != null) {

View File

@ -28,6 +28,7 @@ import com.bumptech.glide.request.RequestOptions;
import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.widget.IconTextView;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.PlayerStatusEvent;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
@ -81,9 +82,6 @@ import io.reactivex.schedulers.Schedulers;
@SuppressLint("ValidFragment")
public class FeedItemlistFragment extends ListFragment {
private static final String TAG = "ItemlistFragment";
private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE;
private static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id";
private FeedItemlistAdapter adapter;
@ -150,7 +148,6 @@ public class FeedItemlistFragment extends ListFragment {
registerForContextMenu(getListView());
EventDistributor.getInstance().register(contentUpdate);
EventBus.getDefault().register(this);
loadItems();
}
@ -159,7 +156,6 @@ public class FeedItemlistFragment extends ListFragment {
public void onDestroyView() {
super.onDestroyView();
EventDistributor.getInstance().unregister(contentUpdate);
EventBus.getDefault().unregister(this);
if (disposable != null) {
disposable.dispose();
@ -407,18 +403,10 @@ public class FeedItemlistFragment extends ListFragment {
updateUi();
}
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((EVENTS & arg) != 0) {
Log.d(TAG, "Received contentUpdate Intent. arg " + arg);
refreshHeaderView();
loadItems();
updateProgressBarVisibility();
}
}
};
@Subscribe(threadMode = ThreadMode.MAIN)
public void onFeedListChanged(FeedListUpdateEvent event) {
updateUi();
}
private void updateProgressBarVisibility() {
if (isUpdatingFeed != updateRefreshMenuItemChecker.isRefreshing()) {

View File

@ -28,6 +28,7 @@ import de.danoeh.antennapod.adapter.SubscriptionsAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.event.DownloadEvent;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
@ -56,8 +57,6 @@ import org.greenrobot.eventbus.ThreadMode;
public class SubscriptionFragment extends Fragment {
public static final String TAG = "SubscriptionFragment";
private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE;
private static final String PREFS = "SubscriptionFragment";
private static final String PREF_NUM_COLUMNS = "columns";
@ -159,7 +158,6 @@ public class SubscriptionFragment extends Fragment {
@Override
public void onStart() {
super.onStart();
EventDistributor.getInstance().register(contentUpdate);
EventBus.getDefault().register(this);
loadSubscriptions();
}
@ -167,7 +165,6 @@ public class SubscriptionFragment extends Fragment {
@Override
public void onStop() {
super.onStop();
EventDistributor.getInstance().unregister(contentUpdate);
EventBus.getDefault().unregister(this);
if(disposable != null) {
disposable.dispose();
@ -294,15 +291,10 @@ public class SubscriptionFragment extends Fragment {
dialog.createNewDialog().show();
}
private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((EVENTS & arg) != 0) {
Log.d(TAG, "Received contentUpdate Intent.");
loadSubscriptions();
}
}
};
@Subscribe
public void onFeedListChanged(FeedListUpdateEvent event) {
loadSubscriptions();
}
@Subscribe
public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) {

View File

@ -0,0 +1,6 @@
package de.danoeh.antennapod.core.event;
public class FeedListUpdateEvent {
public FeedListUpdateEvent() {
}
}

View File

@ -20,8 +20,6 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public class EventDistributor extends Observable {
private static final String TAG = "EventDistributor";
public static final int FEED_LIST_UPDATE = 1;
private final Handler handler;
private final AbstractQueue<Integer> events;
@ -73,10 +71,6 @@ public class EventDistributor extends Observable {
super.addObserver(observer);
}
public void sendFeedUpdateBroadcast() {
addEvent(FEED_LIST_UPDATE);
}
public abstract static class EventListener implements Observer {
@Override

View File

@ -21,6 +21,7 @@ import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicBoolean;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
@ -35,6 +36,7 @@ import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator;
import de.danoeh.antennapod.core.util.exception.MediaFileNotFoundException;
import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import org.greenrobot.eventbus.EventBus;
import static android.content.Context.MODE_PRIVATE;
@ -298,7 +300,7 @@ public final class DBTasks {
media.setDownloaded(false);
media.setFile_url(null);
DBWriter.setFeedMedia(media);
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new FeedListUpdateEvent());
}
/**
@ -563,7 +565,7 @@ public final class DBTasks {
e.printStackTrace();
}
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new FeedListUpdateEvent());
return resultFeeds;
}

View File

@ -7,6 +7,7 @@ import android.util.Log;
import androidx.annotation.NonNull;
import de.danoeh.antennapod.core.event.DownloadLogEvent;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
import de.danoeh.antennapod.core.event.PlaybackHistoryEvent;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
import org.greenrobot.eventbus.EventBus;
@ -174,7 +175,7 @@ public class DBWriter {
if (ClientConfig.gpodnetCallbacks.gpodnetEnabled()) {
GpodnetPreferences.addRemovedFeed(feed.getDownload_url());
}
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new FeedListUpdateEvent());
// we assume we also removed download log entries for the feed or its media files.
// especially important if download or refresh failed, as the user should not be able
@ -804,7 +805,7 @@ public class DBWriter {
adapter.open();
adapter.setFeedPreferences(preferences);
adapter.close();
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new FeedListUpdateEvent());
});
}
@ -843,7 +844,7 @@ public class DBWriter {
adapter.open();
adapter.setFeedCustomTitle(feed.getId(), feed.getCustomTitle());
adapter.close();
EventDistributor.getInstance().sendFeedUpdateBroadcast();
EventBus.getDefault().post(new FeedListUpdateEvent());
});
}