From 68da2e022ddd51d069abbf7612b88f6a23d5f034 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Wed, 23 Oct 2019 22:41:20 +0200 Subject: [PATCH] Migrated download log events to EventBus --- .../fragment/CompletedDownloadsFragment.java | 19 +++++++++++--- .../fragment/DownloadLogFragment.java | 25 +++++++++++-------- .../core/event/DownloadLogEvent.java | 16 ++++++++++++ .../core/feed/EventDistributor.java | 5 ---- .../antennapod/core/storage/DBWriter.java | 7 +++--- 5 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 core/src/main/java/de/danoeh/antennapod/core/event/DownloadLogEvent.java 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 1917e4c75..b85578060 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java @@ -16,6 +16,7 @@ import java.util.List; 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; @@ -27,6 +28,8 @@ 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 static de.danoeh.antennapod.dialog.EpisodesApplyActionFragment.ACTION_ADD_TO_QUEUE; import static de.danoeh.antennapod.dialog.EpisodesApplyActionFragment.ACTION_DELETE; @@ -38,9 +41,7 @@ public class CompletedDownloadsFragment extends ListFragment { private static final String TAG = CompletedDownloadsFragment.class.getSimpleName(); - private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED | - EventDistributor.DOWNLOADLOG_UPDATE | - EventDistributor.UNREAD_ITEMS_UPDATE; + private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED | EventDistributor.UNREAD_ITEMS_UPDATE; private List items = new ArrayList<>(); private DownloadedEpisodesListAdapter listAdapter; @@ -56,6 +57,13 @@ public class CompletedDownloadsFragment extends ListFragment { listAdapter = new DownloadedEpisodesListAdapter(getActivity(), itemAccess); setListAdapter(listAdapter); setListShown(false); + EventBus.getDefault().register(this); + } + + @Override + public void onDestroyView() { + EventBus.getDefault().unregister(this); + super.onDestroyView(); } @Override @@ -144,6 +152,11 @@ public class CompletedDownloadsFragment extends ListFragment { } }; + @Subscribe + public void onDownloadLogChanged(DownloadLogEvent event) { + loadItems(); + } + private void loadItems() { if (disposable != null) { disposable.dispose(); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/DownloadLogFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/DownloadLogFragment.java index 528c50747..2c5417b85 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/DownloadLogFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/DownloadLogFragment.java @@ -19,6 +19,7 @@ import java.util.List; import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.DownloadLogAdapter; +import de.danoeh.antennapod.core.event.DownloadLogEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedMedia; @@ -30,6 +31,8 @@ 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; /** * Shows the download log @@ -45,14 +48,12 @@ public class DownloadLogFragment 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(); } @@ -77,6 +78,13 @@ public class DownloadLogFragment extends ListFragment { adapter = new DownloadLogAdapter(getActivity(), itemAccess); setListAdapter(adapter); + EventBus.getDefault().register(this); + } + + @Override + public void onDestroyView() { + EventBus.getDefault().unregister(this); + super.onDestroyView(); } private void onFragmentLoaded() { @@ -133,15 +141,10 @@ public class DownloadLogFragment extends ListFragment { } }; - private final EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { - - @Override - public void update(EventDistributor eventDistributor, Integer arg) { - if ((arg & EventDistributor.DOWNLOADLOG_UPDATE) != 0) { - loadItems(); - } - } - }; + @Subscribe + public void onDownloadLogChanged(DownloadLogEvent event) { + loadItems(); + } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/event/DownloadLogEvent.java b/core/src/main/java/de/danoeh/antennapod/core/event/DownloadLogEvent.java new file mode 100644 index 000000000..7428c5b00 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/event/DownloadLogEvent.java @@ -0,0 +1,16 @@ +package de.danoeh.antennapod.core.event; + +public class DownloadLogEvent { + + private DownloadLogEvent() { + } + + public static DownloadLogEvent listUpdated() { + return new DownloadLogEvent(); + } + + @Override + public String toString() { + return "DownloadLogEvent"; + } +} 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 b769eaf55..a4eeeae72 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 @@ -22,7 +22,6 @@ public class EventDistributor extends Observable { public static final int FEED_LIST_UPDATE = 1; public static final int UNREAD_ITEMS_UPDATE = 2; - public static final int DOWNLOADLOG_UPDATE = 8; public static final int PLAYBACK_HISTORY_UPDATE = 16; public static final int DOWNLOAD_HANDLED = 64; public static final int PLAYER_STATUS_UPDATE = 128; @@ -90,10 +89,6 @@ public class EventDistributor extends Observable { addEvent(PLAYBACK_HISTORY_UPDATE); } - public void sendDownloadLogUpdateBroadcast() { - addEvent(DOWNLOADLOG_UPDATE); - } - public void sendPlayerStatusUpdateBroadcast() { addEvent(PLAYER_STATUS_UPDATE); } public abstract static class EventListener implements Observer { 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 e0e15c1ff..60333afc0 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 @@ -6,6 +6,7 @@ import android.util.Log; import androidx.annotation.NonNull; +import de.danoeh.antennapod.core.event.DownloadLogEvent; import org.greenrobot.eventbus.EventBus; import java.io.File; @@ -176,7 +177,7 @@ public class DBWriter { // 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 // to retry these - EventDistributor.getInstance().sendDownloadLogUpdateBroadcast(); + EventBus.getDefault().post(DownloadLogEvent.listUpdated()); BackupManager backupManager = new BackupManager(context); backupManager.dataChanged(); @@ -206,7 +207,7 @@ public class DBWriter { adapter.open(); adapter.clearDownloadLog(); adapter.close(); - EventDistributor.getInstance().sendDownloadLogUpdateBroadcast(); + EventBus.getDefault().post(DownloadLogEvent.listUpdated()); }); } @@ -243,7 +244,7 @@ public class DBWriter { adapter.open(); adapter.setDownloadStatus(status); adapter.close(); - EventDistributor.getInstance().sendDownloadLogUpdateBroadcast(); + EventBus.getDefault().post(DownloadLogEvent.listUpdated()); }); }