Simplify updating refresh menu

This commit is contained in:
ByteHamster 2022-08-19 20:03:40 +02:00
parent e96f929f0a
commit 9558e61bad
6 changed files with 30 additions and 105 deletions

View File

@ -4,7 +4,6 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -22,7 +21,6 @@ import de.danoeh.antennapod.adapter.actionbutton.DeleteActionButton;
import de.danoeh.antennapod.core.event.DownloadEvent; import de.danoeh.antennapod.core.event.DownloadEvent;
import de.danoeh.antennapod.core.event.DownloadLogEvent; import de.danoeh.antennapod.core.event.DownloadLogEvent;
import de.danoeh.antennapod.core.menuhandler.MenuItemUtils; import de.danoeh.antennapod.core.menuhandler.MenuItemUtils;
import de.danoeh.antennapod.core.service.download.DownloadService;
import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager; import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
@ -67,16 +65,14 @@ public class CompletedDownloadsFragment extends Fragment
private Disposable disposable; private Disposable disposable;
private EmptyViewHandler emptyView; private EmptyViewHandler emptyView;
private boolean displayUpArrow; private boolean displayUpArrow;
private boolean isUpdatingFeeds = false;
private SpeedDialView speedDialView; private SpeedDialView speedDialView;
private Toolbar toolbar;
private SwipeActions swipeActions; private SwipeActions swipeActions;
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.simple_list_fragment, container, false); View root = inflater.inflate(R.layout.simple_list_fragment, container, false);
toolbar = root.findViewById(R.id.toolbar); Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.downloads_label); toolbar.setTitle(R.string.downloads_label);
toolbar.inflateMenu(R.menu.downloads_completed); toolbar.inflateMenu(R.menu.downloads_completed);
toolbar.setOnMenuItemClickListener(this); toolbar.setOnMenuItemClickListener(this);
@ -85,7 +81,6 @@ public class CompletedDownloadsFragment extends Fragment
recyclerView.post(() -> recyclerView.smoothScrollToPosition(0)); recyclerView.post(() -> recyclerView.smoothScrollToPosition(0));
return false; return false;
}); });
refreshToolbarState();
displayUpArrow = getParentFragmentManager().getBackStackEntryCount() != 0; displayUpArrow = getParentFragmentManager().getBackStackEntryCount() != 0;
if (savedInstanceState != null) { if (savedInstanceState != null) {
displayUpArrow = savedInstanceState.getBoolean(KEY_UP_ARROW); displayUpArrow = savedInstanceState.getBoolean(KEY_UP_ARROW);
@ -166,12 +161,6 @@ public class CompletedDownloadsFragment extends Fragment
} }
} }
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
menu.findItem(R.id.clear_logs_item).setVisible(false);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.refresh_item) { if (item.getItemId() == R.id.refresh_item) {
@ -190,9 +179,6 @@ public class CompletedDownloadsFragment extends Fragment
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
refreshToolbarState();
}
if (!Arrays.equals(event.update.mediaIds, runningDownloads)) { if (!Arrays.equals(event.update.mediaIds, runningDownloads)) {
runningDownloads = event.update.mediaIds; runningDownloads = event.update.mediaIds;
loadItems(); loadItems();
@ -208,9 +194,6 @@ public class CompletedDownloadsFragment extends Fragment
} }
} }
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker =
() -> DownloadService.isRunning && DownloadService.isDownloadingFeeds();
@Override @Override
public boolean onContextItemSelected(@NonNull MenuItem item) { public boolean onContextItemSelected(@NonNull MenuItem item) {
FeedItem selectedItem = adapter.getLongPressedItem(); FeedItem selectedItem = adapter.getLongPressedItem();
@ -270,11 +253,6 @@ public class CompletedDownloadsFragment extends Fragment
} }
} }
private void refreshToolbarState() {
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(),
R.id.refresh_item, updateRefreshMenuItemChecker);
}
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onPlayerStatusChanged(PlayerStatusEvent event) { public void onPlayerStatusChanged(PlayerStatusEvent event) {
loadItems(); loadItems();

View File

@ -8,7 +8,6 @@ import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -84,11 +83,9 @@ public abstract class EpisodesListFragment extends Fragment
@NonNull @NonNull
List<FeedItem> episodes = new ArrayList<>(); List<FeedItem> episodes = new ArrayList<>();
private volatile boolean isUpdatingFeeds;
protected Disposable disposable; protected Disposable disposable;
protected TextView txtvInformation; protected TextView txtvInformation;
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
@ -118,14 +115,6 @@ public abstract class EpisodesListFragment extends Fragment
} }
} }
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker =
() -> DownloadService.isRunning && DownloadService.isDownloadingFeeds();
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (super.onOptionsItemSelected(item)) { if (super.onOptionsItemSelected(item)) {
@ -423,9 +412,7 @@ public abstract class EpisodesListFragment extends Fragment
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
DownloaderUpdate update = event.update; DownloaderUpdate update = event.update;
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
updateToolbar(); updateToolbar();
}
if (update.mediaIds.length > 0) { if (update.mediaIds.length > 0) {
for (long mediaId : update.mediaIds) { for (long mediaId : update.mediaIds) {
int pos = FeedItemUtil.indexOfItemWithMediaId(episodes, mediaId); int pos = FeedItemUtil.indexOfItemWithMediaId(episodes, mediaId);
@ -436,26 +423,19 @@ public abstract class EpisodesListFragment extends Fragment
} }
} }
private void updateUi() {
loadItems();
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
updateToolbar();
}
}
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onPlayerStatusChanged(PlayerStatusEvent event) { public void onPlayerStatusChanged(PlayerStatusEvent event) {
updateUi(); loadItems();
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) {
updateUi(); loadItems();
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onFeedListChanged(FeedListUpdateEvent event) { public void onFeedListChanged(FeedListUpdateEvent event) {
updateUi(); loadItems();
} }
void loadItems() { void loadItems() {
@ -498,8 +478,10 @@ public abstract class EpisodesListFragment extends Fragment
protected abstract String getPrefName(); protected abstract String getPrefName();
protected void updateToolbar() { protected void updateToolbar() {
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(), if (toolbar.getMenu().findItem(R.id.refresh_item) != null) {
R.id.refresh_item, updateRefreshMenuItemChecker); MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(), R.id.refresh_item,
DownloadService.isRunning && DownloadService.isDownloadingFeeds());
}
} }
@Override @Override

View File

@ -92,7 +92,6 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
private long feedID; private long feedID;
private Feed feed; private Feed feed;
private boolean headerCreated = false; private boolean headerCreated = false;
private boolean isUpdatingFeed;
private Disposable disposable; private Disposable disposable;
private FeedItemListFragmentBinding viewBinding; private FeedItemListFragmentBinding viewBinding;
private MultiSelectSpeedDialBinding speedDialBinding; private MultiSelectSpeedDialBinding speedDialBinding;
@ -140,7 +139,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
displayUpArrow = savedInstanceState.getBoolean(KEY_UP_ARROW); displayUpArrow = savedInstanceState.getBoolean(KEY_UP_ARROW);
} }
((MainActivity) getActivity()).setupToolbarToggle(viewBinding.toolbar, displayUpArrow); ((MainActivity) getActivity()).setupToolbarToggle(viewBinding.toolbar, displayUpArrow);
refreshToolbarState(); updateToolbar();
viewBinding.recyclerView.setRecycledViewPool(((MainActivity) getActivity()).getRecycledViewPool()); viewBinding.recyclerView.setRecycledViewPool(((MainActivity) getActivity()).getRecycledViewPool());
viewBinding.progLoading.setVisibility(View.VISIBLE); viewBinding.progLoading.setVisibility(View.VISIBLE);
@ -234,17 +233,14 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker = private void updateToolbar() {
() -> DownloadService.isRunning && DownloadService.isDownloadingFile(feed.getDownload_url());
private void refreshToolbarState() {
if (feed == null) { if (feed == null) {
return; return;
} }
viewBinding.toolbar.getMenu().findItem(R.id.visit_website_item).setVisible(feed.getLink() != null); viewBinding.toolbar.getMenu().findItem(R.id.visit_website_item).setVisible(feed.getLink() != null);
isUpdatingFeed = MenuItemUtils.updateRefreshMenuItem(viewBinding.toolbar.getMenu(), MenuItemUtils.updateRefreshMenuItem(viewBinding.toolbar.getMenu(), R.id.refresh_item,
R.id.refresh_item, updateRefreshMenuItemChecker); DownloadService.isRunning && DownloadService.isDownloadingFile(feed.getDownload_url()));
FeedMenuHandler.onPrepareOptionsMenu(viewBinding.toolbar.getMenu(), feed); FeedMenuHandler.onPrepareOptionsMenu(viewBinding.toolbar.getMenu(), feed);
} }
@ -338,9 +334,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
DownloaderUpdate update = event.update; DownloaderUpdate update = event.update;
if (event.hasChangedFeedUpdateStatus(isUpdatingFeed)) { updateToolbar();
updateSyncProgressBarVisibility();
}
if (adapter != null && update.mediaIds.length > 0 && feed != null) { if (adapter != null && update.mediaIds.length > 0 && feed != null) {
for (long mediaId : update.mediaIds) { for (long mediaId : update.mediaIds) {
int pos = FeedItemUtil.indexOfItemWithMediaId(feed.getItems(), mediaId); int pos = FeedItemUtil.indexOfItemWithMediaId(feed.getItems(), mediaId);
@ -383,7 +377,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
speedDialBinding.fabSD.removeActionItemById(R.id.delete_batch); speedDialBinding.fabSD.removeActionItemById(R.id.delete_batch);
} }
speedDialBinding.fabSD.setVisibility(View.VISIBLE); speedDialBinding.fabSD.setVisibility(View.VISIBLE);
refreshToolbarState(); updateToolbar();
} }
@Override @Override
@ -416,9 +410,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
} }
private void updateSyncProgressBarVisibility() { private void updateSyncProgressBarVisibility() {
if (isUpdatingFeed != updateRefreshMenuItemChecker.isRefreshing()) { updateToolbar();
refreshToolbarState();
}
if (!DownloadService.isDownloadingFeeds()) { if (!DownloadService.isDownloadingFeeds()) {
nextPageLoader.getRoot().setVisibility(View.GONE); nextPageLoader.getRoot().setVisibility(View.GONE);
} }
@ -443,7 +435,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
swipeActions.setFilter(feed.getItemFilter()); swipeActions.setFilter(feed.getItemFilter());
} }
refreshToolbarState(); updateToolbar();
updateSyncProgressBarVisibility(); updateSyncProgressBarVisibility();
} }

View File

@ -87,8 +87,6 @@ public class QueueFragment extends Fragment implements Toolbar.OnMenuItemClickLi
private List<FeedItem> queue; private List<FeedItem> queue;
private boolean isUpdatingFeeds = false;
private static final String PREFS = "QueueFragment"; private static final String PREFS = "QueueFragment";
private static final String PREF_SHOW_LOCK_WARNING = "show_lock_warning"; private static final String PREF_SHOW_LOCK_WARNING = "show_lock_warning";
@ -191,9 +189,7 @@ public class QueueFragment extends Fragment implements Toolbar.OnMenuItemClickLi
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with DownloadEvent"); Log.d(TAG, "onEventMainThread() called with DownloadEvent");
DownloaderUpdate update = event.update; DownloaderUpdate update = event.update;
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
refreshToolbarState(); refreshToolbarState();
}
if (recyclerAdapter != null && update.mediaIds.length > 0) { if (recyclerAdapter != null && update.mediaIds.length > 0) {
for (long mediaId : update.mediaIds) { for (long mediaId : update.mediaIds) {
int pos = FeedItemUtil.indexOfItemWithMediaId(queue, mediaId); int pos = FeedItemUtil.indexOfItemWithMediaId(queue, mediaId);
@ -221,19 +217,15 @@ public class QueueFragment extends Fragment implements Toolbar.OnMenuItemClickLi
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onPlayerStatusChanged(PlayerStatusEvent event) { public void onPlayerStatusChanged(PlayerStatusEvent event) {
loadItems(false); loadItems(false);
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
refreshToolbarState(); refreshToolbarState();
} }
}
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) {
// Sent when playback position is reset // Sent when playback position is reset
loadItems(false); loadItems(false);
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
refreshToolbarState(); refreshToolbarState();
} }
}
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onKeyUp(KeyEvent event) { public void onKeyUp(KeyEvent event) {
@ -261,17 +253,14 @@ public class QueueFragment extends Fragment implements Toolbar.OnMenuItemClickLi
recyclerAdapter = null; recyclerAdapter = null;
} }
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker =
() -> DownloadService.isRunning && DownloadService.isDownloadingFeeds();
private void refreshToolbarState() { private void refreshToolbarState() {
boolean keepSorted = UserPreferences.isQueueKeepSorted(); boolean keepSorted = UserPreferences.isQueueKeepSorted();
toolbar.getMenu().findItem(R.id.queue_lock).setChecked(UserPreferences.isQueueLocked()); toolbar.getMenu().findItem(R.id.queue_lock).setChecked(UserPreferences.isQueueLocked());
toolbar.getMenu().findItem(R.id.queue_lock).setVisible(!keepSorted); toolbar.getMenu().findItem(R.id.queue_lock).setVisible(!keepSorted);
toolbar.getMenu().findItem(R.id.queue_sort_random).setVisible(!keepSorted); toolbar.getMenu().findItem(R.id.queue_sort_random).setVisible(!keepSorted);
toolbar.getMenu().findItem(R.id.queue_keep_sorted).setChecked(keepSorted); toolbar.getMenu().findItem(R.id.queue_keep_sorted).setChecked(keepSorted);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(), MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(),
R.id.refresh_item, updateRefreshMenuItemChecker); R.id.refresh_item, DownloadService.isRunning && DownloadService.isDownloadingFeeds());
} }
@Override @Override

View File

@ -93,7 +93,6 @@ public class SubscriptionFragment extends Fragment
private TextView feedsFilteredMsg; private TextView feedsFilteredMsg;
private Toolbar toolbar; private Toolbar toolbar;
private String displayedFolder = null; private String displayedFolder = null;
private boolean isUpdatingFeeds = false;
private boolean displayUpArrow; private boolean displayUpArrow;
private Disposable disposable; private Disposable disposable;
@ -200,8 +199,8 @@ public class SubscriptionFragment extends Fragment
int columns = prefs.getInt(PREF_NUM_COLUMNS, getDefaultNumOfColumns()); int columns = prefs.getInt(PREF_NUM_COLUMNS, getDefaultNumOfColumns());
toolbar.getMenu().findItem(COLUMN_CHECKBOX_IDS[columns - MIN_NUM_COLUMNS]).setChecked(true); toolbar.getMenu().findItem(COLUMN_CHECKBOX_IDS[columns - MIN_NUM_COLUMNS]).setChecked(true);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(), MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(), R.id.refresh_item,
R.id.refresh_item, updateRefreshMenuItemChecker); DownloadService.isRunning && DownloadService.isDownloadingFeeds());
} }
@Override @Override
@ -407,13 +406,8 @@ public class SubscriptionFragment extends Fragment
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
refreshToolbarState(); refreshToolbarState();
} }
}
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker =
() -> DownloadService.isRunning && DownloadService.isDownloadingFeeds();
@Override @Override
public void onEndSelectMode() { public void onEndSelectMode() {

View File

@ -11,31 +11,21 @@ import de.danoeh.antennapod.core.R;
public class MenuItemUtils { public class MenuItemUtils {
/** /**
* Changes the appearance of a MenuItem depending on whether the given UpdateRefreshMenuItemChecker
* is refreshing or not. If it returns true, the menu item will be replaced by an indeterminate progress
* bar, otherwise the progress bar will be hidden.
*
* @param menu The menu that the MenuItem belongs to * @param menu The menu that the MenuItem belongs to
* @param resId The id of the MenuItem * @param resId The id of the MenuItem
* @param checker Is used for checking whether to show the progress indicator or not.
* @return The returned value of the UpdateRefreshMenuItemChecker's isRefreshing() method.
*/ */
public static boolean updateRefreshMenuItem(Menu menu, int resId, UpdateRefreshMenuItemChecker checker) { public static void updateRefreshMenuItem(Menu menu, int resId, boolean isRefreshing) {
// expand actionview if feeds are being downloaded, collapse otherwise // expand actionview if feeds are being downloaded, collapse otherwise
MenuItem refreshItem = menu.findItem(resId); MenuItem refreshItem = menu.findItem(resId);
if (checker.isRefreshing()) { if (isRefreshing) {
if (refreshItem.getActionView() == null) {
refreshItem.setActionView(R.layout.refresh_action_view); refreshItem.setActionView(R.layout.refresh_action_view);
return true; }
} else { } else {
refreshItem.setActionView(null); refreshItem.setActionView(null);
return false;
} }
} }
public interface UpdateRefreshMenuItemChecker {
boolean isRefreshing();
}
/** /**
* When pressing a context menu item, Android calls onContextItemSelected * When pressing a context menu item, Android calls onContextItemSelected
* for ALL fragments in arbitrary order, not just for the fragment that the * for ALL fragments in arbitrary order, not just for the fragment that the