diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 6a38f8f0a..4cddb4ead 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -34,6 +34,7 @@ import android.widget.Toast; import com.bumptech.glide.Glide; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.Validate; import org.greenrobot.eventbus.EventBus; @@ -84,8 +85,7 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi private static final String TAG = "MainActivity"; - private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE - | EventDistributor.UNREAD_ITEMS_UPDATE; + 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"; @@ -817,6 +817,11 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi snackbar.show(); } + @Subscribe + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + loadData(); + } + private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { @Override diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java index 7196198cc..58d64da19 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java @@ -17,7 +17,6 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.adapter.DownloadedEpisodesListAdapter; import de.danoeh.antennapod.core.event.DownloadLogEvent; -import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; @@ -67,14 +66,12 @@ public class CompletedDownloadsFragment extends ListFragment { @Override public void onStart() { super.onStart(); - EventDistributor.getInstance().register(contentUpdate); loadItems(); } @Override public void onStop() { super.onStop(); - EventDistributor.getInstance().unregister(contentUpdate); if (disposable != null) { disposable.dispose(); } @@ -141,15 +138,6 @@ public class CompletedDownloadsFragment extends ListFragment { } }; - private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { - @Override - public void update(EventDistributor eventDistributor, Integer arg) { - if ((arg & EventDistributor.UNREAD_ITEMS_UPDATE) != 0) { - loadItems(); - } - } - }; - @Subscribe public void onDownloadLogChanged(DownloadLogEvent event) { loadItems(); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java index 44b80f4ec..74f652b11 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java @@ -24,8 +24,10 @@ import android.widget.Toast; import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration; +import de.danoeh.antennapod.core.event.DownloadLogEvent; import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import de.danoeh.antennapod.core.event.PlayerStatusEvent; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -66,8 +68,7 @@ public abstract class EpisodesListFragment extends Fragment { public static final String TAG = "EpisodesListFragment"; - private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE | - EventDistributor.UNREAD_ITEMS_UPDATE; + 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"; @@ -414,14 +415,23 @@ public abstract class EpisodesListFragment extends Fragment { } } - @Subscribe(threadMode = ThreadMode.MAIN) - public void onPlayerStatusChanged(PlayerStatusEvent event) { + private void updateUi() { loadItems(); if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) { requireActivity().invalidateOptionsMenu(); } } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onPlayerStatusChanged(PlayerStatusEvent event) { + updateUi(); + } + + @Subscribe + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + updateUi(); + } + private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { @Override public void update(EventDistributor eventDistributor, Integer arg) { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java index df641cd74..2061c0591 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java @@ -30,6 +30,7 @@ import com.joanzapata.iconify.widget.IconTextView; import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import de.danoeh.antennapod.core.event.PlayerStatusEvent; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import org.apache.commons.lang3.Validate; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -81,8 +82,7 @@ import io.reactivex.schedulers.Schedulers; public class FeedItemlistFragment extends ListFragment { private static final String TAG = "ItemlistFragment"; - private static final int EVENTS = EventDistributor.UNREAD_ITEMS_UPDATE - | EventDistributor.FEED_LIST_UPDATE; + private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE; private static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id"; @@ -391,13 +391,22 @@ public class FeedItemlistFragment extends ListFragment { } } - @Subscribe(threadMode = ThreadMode.MAIN) - public void onPlayerStatusChanged(PlayerStatusEvent event) { + private void updateUi() { refreshHeaderView(); loadItems(); updateProgressBarVisibility(); } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onPlayerStatusChanged(PlayerStatusEvent event) { + updateUi(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + updateUi(); + } + private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { @Override diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java index 2f64ac106..f76427d1f 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java @@ -35,6 +35,7 @@ import com.bumptech.glide.request.RequestOptions; import com.joanzapata.iconify.Iconify; import com.joanzapata.iconify.widget.IconButton; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import org.apache.commons.lang3.ArrayUtils; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -81,9 +82,6 @@ import io.reactivex.schedulers.Schedulers; public class ItemFragment extends Fragment implements OnSwipeGesture { private static final String TAG = "ItemFragment"; - - private static final int EVENTS = EventDistributor.UNREAD_ITEMS_UPDATE; - private static final String ARG_FEEDITEMS = "feeditems"; private static final String ARG_FEEDITEM_POS = "feeditem_pos"; @@ -268,7 +266,6 @@ public class ItemFragment extends Fragment implements OnSwipeGesture { @Override public void onStart() { super.onStart(); - EventDistributor.getInstance().register(contentUpdate); EventBus.getDefault().register(this); load(); } @@ -285,7 +282,6 @@ public class ItemFragment extends Fragment implements OnSwipeGesture { @Override public void onStop() { super.onStop(); - EventDistributor.getInstance().unregister(contentUpdate); EventBus.getDefault().unregister(this); } @@ -558,15 +554,10 @@ public class ItemFragment extends Fragment implements OnSwipeGesture { } } - - private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { - @Override - public void update(EventDistributor eventDistributor, Integer arg) { - if ((arg & EVENTS) != 0) { - load(); - } - } - }; + @Subscribe + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + load(); + } private void load() { if(disposable != null) { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index 8d9dcad86..b7693eb42 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -28,6 +28,7 @@ import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration; import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import de.danoeh.antennapod.core.event.PlayerStatusEvent; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -74,11 +75,8 @@ import static de.danoeh.antennapod.dialog.EpisodesApplyActionFragment.ACTION_REM * Shows all items in the queue */ public class QueueFragment extends Fragment { - public static final String TAG = "QueueFragment"; - private static final int EVENTS = EventDistributor.UNREAD_ITEMS_UPDATE; // sent when playback position is reset - private TextView infoBar; private RecyclerView recyclerView; private QueueRecyclerAdapter recyclerAdapter; @@ -116,7 +114,6 @@ public class QueueFragment extends Fragment { onFragmentLoaded(true); } loadItems(true); - EventDistributor.getInstance().register(contentUpdate); EventBus.getDefault().register(this); } @@ -129,9 +126,8 @@ public class QueueFragment extends Fragment { @Override public void onStop() { super.onStop(); - EventDistributor.getInstance().unregister(contentUpdate); EventBus.getDefault().unregister(this); - if(disposable != null) { + if (disposable != null) { disposable.dispose(); } } @@ -229,6 +225,15 @@ public class QueueFragment extends Fragment { } } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + // Sent when playback position is reset + loadItems(false); + if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) { + getActivity().supportInvalidateOptionsMenu(); + } + } + private void saveScrollPosition() { int firstItem = layoutManager.findFirstVisibleItemPosition(); View firstItemView = layoutManager.findViewByPosition(firstItem); @@ -718,19 +723,6 @@ public class QueueFragment extends Fragment { } }; - private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { - @Override - public void update(EventDistributor eventDistributor, Integer arg) { - if ((arg & EVENTS) != 0) { - Log.d(TAG, "arg: " + arg); - loadItems(false); - if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) { - getActivity().supportInvalidateOptionsMenu(); - } - } - } - }; - private void loadItems(final boolean restoreScrollPosition) { Log.d(TAG, "loadItems()"); if(disposable != null) { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java index b05b7025c..415b0f757 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java @@ -20,6 +20,7 @@ import java.util.List; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.adapter.SearchlistAdapter; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedComponent; @@ -30,6 +31,9 @@ import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; /** * Performs a search operation on all feeds or one specific feed and displays the search result. @@ -76,7 +80,6 @@ public class SearchFragment extends ListFragment { @Override public void onStart() { super.onStart(); - EventDistributor.getInstance().register(contentUpdate); search(); } @@ -86,7 +89,6 @@ public class SearchFragment extends ListFragment { if(disposable != null) { disposable.dispose(); } - EventDistributor.getInstance().unregister(contentUpdate); } @Override @@ -103,6 +105,13 @@ public class SearchFragment extends ListFragment { searchAdapter = new SearchlistAdapter(getActivity(), itemAccess); setListAdapter(searchAdapter); + EventBus.getDefault().register(this); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + EventBus.getDefault().unregister(this); } @Override @@ -145,14 +154,10 @@ public class SearchFragment extends ListFragment { MenuItemCompat.setActionView(item, sv); } - private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { - @Override - public void update(EventDistributor eventDistributor, Integer arg) { - if ((arg & EventDistributor.UNREAD_ITEMS_UPDATE) != 0) { - search(); - } - } - }; + @Subscribe + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + search(); + } private void onSearchResults(List results) { searchResults = results; diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java index 253c99c4e..38d1598ce 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java @@ -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.UnreadItemsUpdateEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; @@ -56,8 +57,7 @@ public class SubscriptionFragment extends Fragment { public static final String TAG = "SubscriptionFragment"; - private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE - | EventDistributor.UNREAD_ITEMS_UPDATE; + private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE; private static final String PREFS = "SubscriptionFragment"; private static final String PREF_NUM_COLUMNS = "columns"; @@ -304,6 +304,11 @@ public class SubscriptionFragment extends Fragment { } }; + @Subscribe + public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { + loadSubscriptions(); + } + @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) public void onEventMainThread(DownloadEvent event) { Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); diff --git a/core/src/main/java/de/danoeh/antennapod/core/event/UnreadItemsUpdateEvent.java b/core/src/main/java/de/danoeh/antennapod/core/event/UnreadItemsUpdateEvent.java new file mode 100644 index 000000000..c3efbfe8b --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/event/UnreadItemsUpdateEvent.java @@ -0,0 +1,6 @@ +package de.danoeh.antennapod.core.event; + +public class UnreadItemsUpdateEvent { + public UnreadItemsUpdateEvent() { + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java b/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java index c95568e60..d7357ff09 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/EventDistributor.java @@ -21,7 +21,6 @@ public class EventDistributor extends Observable { private static final String TAG = "EventDistributor"; public static final int FEED_LIST_UPDATE = 1; - public static final int UNREAD_ITEMS_UPDATE = 2; private final Handler handler; private final AbstractQueue events; @@ -74,10 +73,6 @@ public class EventDistributor extends Observable { super.addObserver(observer); } - public void sendUnreadItemsUpdateBroadcast() { - addEvent(UNREAD_ITEMS_UPDATE); - } - public void sendFeedUpdateBroadcast() { addEvent(FEED_LIST_UPDATE); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 9225155cf..50172a5ce 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -8,6 +8,7 @@ import androidx.annotation.NonNull; import de.danoeh.antennapod.core.event.DownloadLogEvent; import de.danoeh.antennapod.core.event.PlaybackHistoryEvent; +import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent; import org.greenrobot.eventbus.EventBus; import java.io.File; @@ -125,7 +126,7 @@ public class DBWriter { } } EventBus.getDefault().post(FeedItemEvent.deletedMedia(Collections.singletonList(media.getItem()))); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); return true; } @@ -611,7 +612,7 @@ public class DBWriter { adapter.setFeedItemRead(played, itemIds); adapter.close(); if (broadcastUpdate) { - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); } }); } @@ -643,7 +644,7 @@ public class DBWriter { resetMediaPosition); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -659,7 +660,7 @@ public class DBWriter { adapter.setFeedItems(FeedItem.NEW, FeedItem.UNPLAYED, feedId); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -675,7 +676,7 @@ public class DBWriter { adapter.setFeedItems(FeedItem.PLAYED, feedId); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -689,7 +690,7 @@ public class DBWriter { adapter.setFeedItems(FeedItem.PLAYED); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -703,7 +704,7 @@ public class DBWriter { adapter.setFeedItems(FeedItem.NEW, FeedItem.UNPLAYED); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -886,7 +887,7 @@ public class DBWriter { adapter.open(); adapter.setFeedItemAutoDownload(feedItem, autoDownload ? 1 : 0); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -905,7 +906,7 @@ public class DBWriter { adapter.open(); adapter.setFeedItemAutoDownload(feedItem, autoDownload); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); } @@ -923,7 +924,7 @@ public class DBWriter { adapter.open(); adapter.setFeedsItemsAutoDownload(feed, autoDownload); adapter.close(); - EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + EventBus.getDefault().post(new UnreadItemsUpdateEvent()); }); }