Merge pull request #6024 from ByteHamster/simplify-refresh-menu

Simplify updating refresh menu
This commit is contained in:
ByteHamster 2022-08-21 18:00:24 +02:00 committed by GitHub
commit d84a3f3e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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.DownloadLogEvent;
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.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
@ -67,16 +65,14 @@ public class CompletedDownloadsFragment extends Fragment
private Disposable disposable;
private EmptyViewHandler emptyView;
private boolean displayUpArrow;
private boolean isUpdatingFeeds = false;
private SpeedDialView speedDialView;
private Toolbar toolbar;
private SwipeActions swipeActions;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
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.inflateMenu(R.menu.downloads_completed);
toolbar.setOnMenuItemClickListener(this);
@ -85,7 +81,6 @@ public class CompletedDownloadsFragment extends Fragment
recyclerView.post(() -> recyclerView.smoothScrollToPosition(0));
return false;
});
refreshToolbarState();
displayUpArrow = getParentFragmentManager().getBackStackEntryCount() != 0;
if (savedInstanceState != null) {
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
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.refresh_item) {
@ -190,9 +179,6 @@ public class CompletedDownloadsFragment extends Fragment
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
refreshToolbarState();
}
if (!Arrays.equals(event.update.mediaIds, runningDownloads)) {
runningDownloads = event.update.mediaIds;
loadItems();
@ -208,9 +194,6 @@ public class CompletedDownloadsFragment extends Fragment
}
}
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker =
() -> DownloadService.isRunning && DownloadService.isDownloadingFeeds();
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
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)
public void onPlayerStatusChanged(PlayerStatusEvent event) {
loadItems();

View File

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

View File

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

View File

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

View File

@ -93,7 +93,6 @@ public class SubscriptionFragment extends Fragment
private TextView feedsFilteredMsg;
private Toolbar toolbar;
private String displayedFolder = null;
private boolean isUpdatingFeeds = false;
private boolean displayUpArrow;
private Disposable disposable;
@ -200,8 +199,8 @@ public class SubscriptionFragment extends Fragment
int columns = prefs.getInt(PREF_NUM_COLUMNS, getDefaultNumOfColumns());
toolbar.getMenu().findItem(COLUMN_CHECKBOX_IDS[columns - MIN_NUM_COLUMNS]).setChecked(true);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(),
R.id.refresh_item, updateRefreshMenuItemChecker);
MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(), R.id.refresh_item,
DownloadService.isRunning && DownloadService.isDownloadingFeeds());
}
@Override
@ -407,14 +406,9 @@ public class SubscriptionFragment extends Fragment
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent 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
public void onEndSelectMode() {
speedDialView.close();

View File

@ -11,31 +11,21 @@ import de.danoeh.antennapod.core.R;
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 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
MenuItem refreshItem = menu.findItem(resId);
if (checker.isRefreshing()) {
refreshItem.setActionView(R.layout.refresh_action_view);
return true;
if (isRefreshing) {
if (refreshItem.getActionView() == null) {
refreshItem.setActionView(R.layout.refresh_action_view);
}
} else {
refreshItem.setActionView(null);
return false;
}
}
public interface UpdateRefreshMenuItemChecker {
boolean isRefreshing();
}
/**
* When pressing a context menu item, Android calls onContextItemSelected
* for ALL fragments in arbitrary order, not just for the fragment that the