Merge pull request #4625 from ByteHamster/toolbar-rework

Toolbar rework
This commit is contained in:
ByteHamster 2020-11-01 17:20:11 +01:00 committed by GitHub
commit b3c69f1a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 484 additions and 593 deletions

View File

@ -24,7 +24,10 @@ import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.ActivityResultMatchers.hasResultCode;
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@ -32,7 +35,7 @@ import static de.test.antennapod.EspressoTestUtils.clickPreference;
import static de.test.antennapod.EspressoTestUtils.openNavDrawer;
import static de.test.antennapod.EspressoTestUtils.waitForView;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertThat;
/**
@ -80,10 +83,6 @@ public class MainActivityTest {
onView(isRoot()).perform(waitForView(withId(R.id.butShowSettings), 5000));
}
private String getActionbarTitle() {
return ((MainActivity) solo.getCurrentActivity()).getSupportActionBar().getTitle().toString();
}
@Test
public void testBackButtonBehaviorGoToPage() {
openNavDrawer();
@ -98,7 +97,8 @@ public class MainActivityTest {
solo.goBackToActivity(MainActivity.class.getSimpleName());
solo.goBack();
solo.goBack();
assertEquals(solo.getString(R.string.subscriptions_label), getActionbarTitle());
onView(allOf(withId(R.id.toolbar), isDisplayed())).check(
matches(hasDescendant(withText(R.string.subscriptions_label))));
solo.goBack();
assertThat(mActivityRule.getActivityResult(), hasResultCode(Activity.RESULT_CANCELED));
}

View File

@ -5,7 +5,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
@ -13,7 +12,6 @@ import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
@ -114,15 +112,6 @@ public class MainActivity extends CastEnabledActivity {
setNavDrawerSize();
final FragmentManager fm = getSupportFragmentManager();
fm.addOnBackStackChangedListener(() -> {
boolean showArrow = fm.getBackStackEntryCount() != 0;
if (drawerToggle != null) { // Tablet layout does not have a drawer
drawerToggle.setDrawerIndicatorEnabled(!showArrow);
} else if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(showArrow);
}
});
if (fm.findFragmentByTag(MAIN_FRAGMENT_TAG) == null) {
String lastFragment = NavDrawerFragment.getLastNavFragment(this);
if (ArrayUtils.contains(NavDrawerFragment.NAV_DRAWER_TAGS, lastFragment)) {
@ -194,8 +183,7 @@ public class MainActivity extends CastEnabledActivity {
}
};
@Override
public void setSupportActionBar(@Nullable Toolbar toolbar) {
public void setupToolbarToggle(@Nullable Toolbar toolbar) {
if (drawerLayout != null) { // Tablet layout does not have a drawer
drawerLayout.removeDrawerListener(drawerToggle);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
@ -203,12 +191,13 @@ public class MainActivity extends CastEnabledActivity {
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
drawerToggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
drawerToggle.setToolbarNavigationClickListener(v -> getSupportFragmentManager().popBackStack());
} else if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
toolbar.setNavigationIcon(null);
} else {
toolbar.setNavigationIcon(ThemeUtils.getDrawableFromAttr(this, R.attr.homeAsUpIndicator));
toolbar.setNavigationOnClickListener(v -> getSupportFragmentManager().popBackStack());
}
super.setSupportActionBar(toolbar);
}
private void checkFirstLaunch() {

View File

@ -1,12 +1,8 @@
package de.danoeh.antennapod.dialog;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -17,10 +13,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.PluralsRes;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.collection.ArrayMap;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import com.google.android.material.snackbar.Snackbar;
import com.leinardi.android.speeddial.SpeedDialView;
@ -34,6 +28,7 @@ import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.FeedItemPermutors;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.SortOrder;
import de.danoeh.antennapod.core.util.ThemeUtils;
import java.util.ArrayList;
import java.util.Arrays;
@ -41,7 +36,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
public class EpisodesApplyActionFragment extends Fragment {
public class EpisodesApplyActionFragment extends Fragment implements Toolbar.OnMenuItemClickListener {
public static final String TAG = "EpisodeActionFragment";
@ -53,7 +48,6 @@ public class EpisodesApplyActionFragment extends Fragment {
public static final int ACTION_DELETE = 32;
public static final int ACTION_ALL = ACTION_ADD_TO_QUEUE | ACTION_REMOVE_FROM_QUEUE
| ACTION_MARK_PLAYED | ACTION_MARK_UNPLAYED | ACTION_DOWNLOAD | ACTION_DELETE;
private Toolbar toolbar;
/**
* Specify an action (defined by #flag) 's UI bindings.
@ -84,7 +78,7 @@ public class EpisodesApplyActionFragment extends Fragment {
private ListView mListView;
private ArrayAdapter<String> mAdapter;
private SpeedDialView mSpeedDialView;
private MenuItem mSelectToggle;
private Toolbar toolbar;
public EpisodesApplyActionFragment() {
actionBindings = Arrays.asList(
@ -117,7 +111,6 @@ public class EpisodesApplyActionFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
@Override
@ -125,6 +118,12 @@ public class EpisodesApplyActionFragment extends Fragment {
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.episodes_apply_action_fragment, container, false);
toolbar = view.findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.episodes_apply_action_options);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
toolbar.setOnMenuItemClickListener(this);
refreshToolbarState();
mListView = view.findViewById(android.R.id.list);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setOnItemClickListener((listView, view1, position, rowId) -> {
@ -167,8 +166,6 @@ public class EpisodesApplyActionFragment extends Fragment {
mAdapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item_multiple_choice_on_start, titles);
mListView.setAdapter(mAdapter);
toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
// Init action UI (via a FAB Speed Dial)
mSpeedDialView = view.findViewById(R.id.fabSD);
@ -215,42 +212,15 @@ public class EpisodesApplyActionFragment extends Fragment {
return view;
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.episodes_apply_action_options, menu);
mSelectToggle = menu.findItem(R.id.select_toggle);
mSelectToggle.setOnMenuItemClickListener(item -> {
if (checkedIds.size() == episodes.size()) {
checkNone();
} else {
checkAll();
}
return true;
});
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
// Prepare icon for select toggle button
int[] icon = new int[1];
@StringRes int titleResId;
public void refreshToolbarState() {
MenuItem selectAllItem = toolbar.getMenu().findItem(R.id.select_toggle);
if (checkedIds.size() == episodes.size()) {
icon[0] = R.attr.ic_select_none;
titleResId = R.string.deselect_all_label;
selectAllItem.setIcon(ThemeUtils.getDrawableFromAttr(getContext(), R.attr.ic_select_none));
selectAllItem.setTitle(R.string.deselect_all_label);
} else {
icon[0] = R.attr.ic_select_all;
titleResId = R.string.select_all_label;
selectAllItem.setIcon(ThemeUtils.getDrawableFromAttr(getContext(), R.attr.ic_select_all));
selectAllItem.setTitle(R.string.select_all_label);
}
TypedArray a = getActivity().obtainStyledAttributes(icon);
Drawable iconDrawable = a.getDrawable(0);
a.recycle();
mSelectToggle.setIcon(iconDrawable);
mSelectToggle.setTitle(titleResId);
}
private static final Map<Integer, SortOrder> menuItemIdToSortOrder;
@ -266,11 +236,18 @@ public class EpisodesApplyActionFragment extends Fragment {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onMenuItemClick(MenuItem item) {
@StringRes int resId = 0;
switch(item.getItemId()) {
switch (item.getItemId()) {
case R.id.select_options:
return true;
case R.id.select_toggle:
if (checkedIds.size() == episodes.size()) {
checkNone();
} else {
checkAll();
}
return true;
case R.id.check_all:
checkAll();
resId = R.string.selected_all_label;
@ -409,7 +386,7 @@ public class EpisodesApplyActionFragment extends Fragment {
boolean checked = checkedIds.contains(episode.getId());
mListView.setItemChecked(i, checked);
}
getActivity().invalidateOptionsMenu();
refreshToolbarState();
toolbar.setTitle(getResources().getQuantityString(R.plurals.num_selected_label,
checkedIds.size(), checkedIds.size()));
}

View File

@ -17,7 +17,7 @@ import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment;
import com.google.android.material.snackbar.Snackbar;
@ -61,7 +61,8 @@ public class AddFeedFragment extends Fragment {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.addfeed, container, false);
activity = (MainActivity) getActivity();
((AppCompatActivity) getActivity()).setSupportActionBar(root.findViewById(R.id.toolbar));
Toolbar toolbar = root.findViewById(R.id.toolbar);
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
root.findViewById(R.id.btn_search_itunes).setOnClickListener(v
-> activity.loadChildFragment(OnlineSearchFragment.newInstance(ItunesPodcastSearcher.class)));
@ -146,11 +147,6 @@ public class AddFeedFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// So, we certainly *don't* have an options menu,
// but unless we say we do, old options menus sometimes
// persist. mfietz thinks this causes the ActionBar to be invalidated
setHasOptionsMenu(true);
}
@Override

View File

@ -56,10 +56,11 @@ public class AllEpisodesFragment extends EpisodesListFragment {
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.filter_items).setVisible(true);
menu.findItem(R.id.mark_all_read_item).setVisible(true);
menu.findItem(R.id.remove_all_new_flags_item).setVisible(false);
}
@Override

View File

@ -96,12 +96,6 @@ public class CompletedDownloadsFragment extends Fragment {
loadItems();
}
@Override
public void onResume() {
super.onResume();
setHasOptionsMenu(true);
}
@Override
public void onStop() {
super.onStop();
@ -111,9 +105,8 @@ public class CompletedDownloadsFragment extends Fragment {
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.downloads_completed, menu);
public void onPrepareOptionsMenu(@NonNull Menu menu) {
menu.findItem(R.id.clear_logs_item).setVisible(false);
menu.findItem(R.id.episode_actions).setVisible(items.size() > 0);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@ -135,7 +128,7 @@ public class CompletedDownloadsFragment extends Fragment {
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
getActivity().invalidateOptionsMenu();
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
}
@ -225,7 +218,7 @@ public class CompletedDownloadsFragment extends Fragment {
.subscribe(result -> {
items = result;
adapter.updateItems(result);
requireActivity().invalidateOptionsMenu();
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
progressBar.setVisibility(View.GONE);
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
}

View File

@ -15,7 +15,7 @@ import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import org.greenrobot.eventbus.EventBus;
@ -90,7 +90,6 @@ public class DiscoveryFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, MODE_PRIVATE);
countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, Locale.getDefault().getCountry());
}
@ -100,11 +99,13 @@ public class DiscoveryFragment extends Fragment {
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_itunes_search, container, false);
((AppCompatActivity) getActivity()).setSupportActionBar(root.findViewById(R.id.toolbar));
gridView = root.findViewById(R.id.gridView);
adapter = new ItunesAdapter(getActivity(), new ArrayList<>());
gridView.setAdapter(adapter);
Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
//Show information about the podcast when the list item is clicked
gridView.setOnItemClickListener((parent, view1, position, id) -> {
PodcastSearchResult podcast = searchResults.get(position);

View File

@ -65,12 +65,6 @@ public class DownloadLogFragment extends ListFragment {
loadItems();
}
@Override
public void onResume() {
super.onResume();
setHasOptionsMenu(true);
}
@Override
public void onStop() {
super.onStop();
@ -108,7 +102,7 @@ public class DownloadLogFragment extends ListFragment {
private void onFragmentLoaded() {
setListShown(true);
adapter.notifyDataSetChanged();
getActivity().invalidateOptionsMenu();
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
@Override
@ -171,20 +165,11 @@ public class DownloadLogFragment extends ListFragment {
loadItems();
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.downloads_log, menu);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.findItem(R.id.clear_history_item);
if (menuItem != null) {
menuItem.setVisible(!downloadLog.isEmpty());
}
menu.findItem(R.id.episode_actions).setVisible(false);
menu.findItem(R.id.clear_logs_item).setVisible(!downloadLog.isEmpty());
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@Override
@ -209,7 +194,7 @@ public class DownloadLogFragment extends ListFragment {
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
getActivity().invalidateOptionsMenu();
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
}

View File

@ -9,7 +9,6 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
@ -19,11 +18,12 @@ import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
/**
* Shows the CompletedDownloadsFragment and the RunningDownloadsFragment
* Shows the CompletedDownloadsFragment and the RunningDownloadsFragment.
*/
public class DownloadsFragment extends Fragment {
public class DownloadsFragment extends PagedToolbarFragment {
public static final String TAG = "DownloadsFragment";
@ -47,11 +47,13 @@ public class DownloadsFragment extends Fragment {
View root = inflater.inflate(R.layout.pager_fragment, container, false);
Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.downloads_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.downloads);
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
viewPager = root.findViewById(R.id.viewpager);
viewPager.setAdapter(new DownloadsPagerAdapter(this));
viewPager.setOffscreenPageLimit(2);
super.setupPagedToolbar(toolbar, viewPager);
// Give the TabLayout the ViewPager
tabLayout = root.findViewById(R.id.sliding_tabs);

View File

@ -8,7 +8,6 @@ import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
@ -18,8 +17,10 @@ import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.menuhandler.MenuItemUtils;
public class EpisodesFragment extends Fragment {
public class EpisodesFragment extends PagedToolbarFragment {
public static final String TAG = "EpisodesFragment";
private static final String PREF_LAST_TAB_POSITION = "tab_position";
@ -29,18 +30,11 @@ public class EpisodesFragment extends Fragment {
private static final int POS_FAV_EPISODES = 2;
private static final int TOTAL_COUNT = 3;
private TabLayout tabLayout;
private ViewPager2 viewPager;
//Mandatory Constructor
public EpisodesFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -48,10 +42,14 @@ public class EpisodesFragment extends Fragment {
View rootView = inflater.inflate(R.layout.pager_fragment, container, false);
Toolbar toolbar = rootView.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.episodes_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
viewPager = rootView.findViewById(R.id.viewpager);
toolbar.inflateMenu(R.menu.episodes);
MenuItemUtils.setupSearchItem(toolbar.getMenu(), (MainActivity) getActivity(), 0, "");
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
ViewPager2 viewPager = rootView.findViewById(R.id.viewpager);
viewPager.setAdapter(new EpisodesPagerAdapter(this));
viewPager.setOffscreenPageLimit(2);
super.setupPagedToolbar(toolbar, viewPager);
// Give the TabLayout the ViewPager
tabLayout = rootView.findViewById(R.id.sliding_tabs);

View File

@ -13,7 +13,6 @@ import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -76,7 +75,6 @@ public abstract class EpisodesListFragment extends Fragment {
List<FeedItem> episodes = new ArrayList<>();
private volatile boolean isUpdatingFeeds;
private boolean isMenuVisible = true;
protected Disposable disposable;
protected TextView txtvInformation;
@ -94,7 +92,6 @@ public abstract class EpisodesListFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
setHasOptionsMenu(true);
registerForContextMenu(recyclerView);
}
@ -118,19 +115,7 @@ public abstract class EpisodesListFragment extends Fragment {
() -> DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds();
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
isMenuVisible = visible;
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
if (!isAdded()) {
return;
}
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.episodes, menu);
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), 0, "");
public void onPrepareOptionsMenu(@NonNull Menu menu) {
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@ -289,8 +274,8 @@ public abstract class EpisodesListFragment extends Fragment {
if (restoreScrollPosition) {
recyclerView.restoreScrollPosition(getPrefName());
}
if (isMenuVisible && isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
requireActivity().invalidateOptionsMenu();
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
}
@ -344,8 +329,8 @@ public abstract class EpisodesListFragment extends Fragment {
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
DownloaderUpdate update = event.update;
if (isMenuVisible && event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
requireActivity().invalidateOptionsMenu();
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
if (update.mediaIds.length > 0) {
for (long mediaId : update.mediaIds) {
@ -359,8 +344,8 @@ public abstract class EpisodesListFragment extends Fragment {
private void updateUi() {
loadItems();
if (isMenuVisible && isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
requireActivity().invalidateOptionsMenu();
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
}
@ -391,6 +376,7 @@ public abstract class EpisodesListFragment extends Fragment {
hasMoreItems = true;
episodes = data;
onFragmentLoaded(episodes);
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
}

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.view.Menu;
import androidx.annotation.NonNull;
import com.google.android.material.snackbar.Snackbar;
import androidx.recyclerview.widget.RecyclerView;
@ -42,6 +43,14 @@ public class FavoriteEpisodesFragment extends EpisodesListFragment {
loadItems();
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.filter_items).setVisible(false);
menu.findItem(R.id.mark_all_read_item).setVisible(false);
menu.findItem(R.id.remove_all_new_flags_item).setVisible(false);
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View File

@ -13,7 +13,6 @@ import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment;
@ -21,8 +20,6 @@ import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -67,7 +64,7 @@ import java.util.Locale;
/**
* Displays information about a feed.
*/
public class FeedInfoFragment extends Fragment {
public class FeedInfoFragment extends Fragment implements Toolbar.OnMenuItemClickListener {
private static final String EXTRA_FEED_ID = "de.danoeh.antennapod.extra.feedId";
private static final String TAG = "FeedInfoActivity";
@ -89,7 +86,7 @@ public class FeedInfoFragment extends Fragment {
private ImageView imgvBackground;
private View infoContainer;
private View header;
private Menu optionsMenu;
private Toolbar toolbar;
private ToolbarIconTintManager iconTintManager;
public static FeedInfoFragment newInstance(Feed feed) {
@ -119,25 +116,25 @@ public class FeedInfoFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.feedinfo, null);
Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar = root.findViewById(R.id.toolbar);
toolbar.setTitle("");
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.feedinfo);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
toolbar.setOnMenuItemClickListener(this);
refreshToolbarState();
AppBarLayout appBar = root.findViewById(R.id.appBar);
CollapsingToolbarLayout collapsingToolbar = root.findViewById(R.id.collapsing_toolbar);
iconTintManager = new ToolbarIconTintManager(getContext(), toolbar, collapsingToolbar) {
@Override
protected void doTint(Context themedContext) {
if (optionsMenu == null) {
return;
}
optionsMenu.findItem(R.id.visit_website_item)
toolbar.getMenu().findItem(R.id.visit_website_item)
.setIcon(ThemeUtils.getDrawableFromAttr(themedContext, R.attr.location_web_site));
}
};
iconTintManager.updateTint();
appBar.addOnOffsetChangedListener(iconTintManager);
setHasOptionsMenu(true);
imgvCover = root.findViewById(R.id.imgvCover);
txtvTitle = root.findViewById(R.id.txtvTitle);
txtvAuthorHeader = root.findViewById(R.id.txtvAuthor);
@ -234,7 +231,7 @@ public class FeedInfoFragment extends Fragment {
txtvUrl.setText(feed.getDownload_url() + " {fa-paperclip}");
Iconify.addIcons(txtvUrl);
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
private void loadStatistics() {
@ -286,29 +283,19 @@ public class FeedInfoFragment extends Fragment {
}
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.feedinfo, menu);
optionsMenu = menu;
iconTintManager.updateTint();
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.reconnect_local_folder).setVisible(feed != null && feed.isLocalFeed());
menu.findItem(R.id.share_link_item).setVisible(feed != null && feed.getLink() != null);
menu.findItem(R.id.visit_website_item).setVisible(feed != null && feed.getLink() != null
private void refreshToolbarState() {
toolbar.getMenu().findItem(R.id.reconnect_local_folder).setVisible(feed != null && feed.isLocalFeed());
toolbar.getMenu().findItem(R.id.share_link_item).setVisible(feed != null && feed.getLink() != null);
toolbar.getMenu().findItem(R.id.visit_website_item).setVisible(feed != null && feed.getLink() != null
&& IntentUtils.isCallable(getContext(), new Intent(Intent.ACTION_VIEW, Uri.parse(feed.getLink()))));
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemClick(MenuItem item) {
if (feed == null) {
((MainActivity) getActivity()).showSnackbarAbovePlayer(
R.string.please_wait_for_data, Toast.LENGTH_LONG);
return super.onOptionsItemSelected(item);
return false;
}
boolean handled = false;
try {
@ -335,7 +322,7 @@ public class FeedInfoFragment extends Fragment {
return true;
}
return handled || super.onOptionsItemSelected(item);
return handled;
}
@Override

View File

@ -10,8 +10,6 @@ import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -24,7 +22,6 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
@ -90,7 +87,8 @@ import java.util.Set;
/**
* Displays a list of FeedItems.
*/
public class FeedItemlistFragment extends Fragment implements AdapterView.OnItemClickListener {
public class FeedItemlistFragment extends Fragment implements AdapterView.OnItemClickListener,
Toolbar.OnMenuItemClickListener {
private static final String TAG = "ItemlistFragment";
private static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id";
@ -108,7 +106,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
private ImageButton butShowInfo;
private ImageButton butShowSettings;
private View header;
private Menu optionsMenu;
private Toolbar toolbar;
private ToolbarIconTintManager iconTintManager;
private long feedID;
@ -136,7 +134,6 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
Bundle args = getArguments();
Validate.notNull(args);
@ -148,9 +145,11 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.feed_item_list_fragment, container, false);
Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar.setTitle("");
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar = root.findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.feedlist);
toolbar.setOnMenuItemClickListener(this);
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
refreshToolbarState();
recyclerView = root.findViewById(R.id.recyclerView);
recyclerView.setRecycledViewPool(((MainActivity) getActivity()).getRecycledViewPool());
@ -172,19 +171,17 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
iconTintManager = new ToolbarIconTintManager(getContext(), toolbar, collapsingToolbar) {
@Override
protected void doTint(Context themedContext) {
if (optionsMenu == null) {
return;
}
optionsMenu.findItem(R.id.sort_items)
toolbar.getMenu().findItem(R.id.sort_items)
.setIcon(ThemeUtils.getDrawableFromAttr(themedContext, R.attr.ic_sort));
optionsMenu.findItem(R.id.filter_items)
toolbar.getMenu().findItem(R.id.filter_items)
.setIcon(ThemeUtils.getDrawableFromAttr(themedContext, R.attr.ic_filter));
optionsMenu.findItem(R.id.refresh_item)
toolbar.getMenu().findItem(R.id.refresh_item)
.setIcon(ThemeUtils.getDrawableFromAttr(themedContext, R.attr.navigation_refresh));
optionsMenu.findItem(R.id.action_search)
toolbar.getMenu().findItem(R.id.action_search)
.setIcon(ThemeUtils.getDrawableFromAttr(themedContext, R.attr.action_search));
}
};
iconTintManager.updateTint();
appBar.addOnOffsetChangedListener(iconTintManager);
nextPageLoader = new MoreContentListFooterUtil(root.findViewById(R.id.more_content_list_footer));
@ -243,33 +240,18 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
}
};
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
if (!isAdded()) {
private void refreshToolbarState() {
if (feed == null) {
return;
}
super.onCreateOptionsMenu(menu, inflater);
optionsMenu = menu;
FeedMenuHandler.onCreateOptionsMenu(inflater, menu);
iconTintManager.updateTint();
if (feed != null) {
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), feedID, feed.getTitle());
} else {
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), feedID, "");
}
if (feed == null || feed.getLink() == null) {
menu.findItem(R.id.share_link_item).setVisible(false);
menu.findItem(R.id.visit_website_item).setVisible(false);
}
MenuItemUtils.setupSearchItem(toolbar.getMenu(), (MainActivity) getActivity(), feedID, feed.getTitle());
isUpdatingFeed = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
toolbar.getMenu().findItem(R.id.share_link_item).setVisible(feed.getLink() != null);
toolbar.getMenu().findItem(R.id.visit_website_item).setVisible(feed.getLink() != null);
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
if (feed != null) {
FeedMenuHandler.onPrepareOptionsMenu(menu, feed);
}
isUpdatingFeed = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(),
R.id.refresh_item, updateRefreshMenuItemChecker);
FeedMenuHandler.onPrepareOptionsMenu(toolbar.getMenu(), feed);
}
@Override
@ -280,71 +262,67 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
item.getActionView().post(() -> iconTintManager.updateTint());
}
if (super.onOptionsItemSelected(item)) {
if (feed == null) {
((MainActivity) getActivity()).showSnackbarAbovePlayer(
R.string.please_wait_for_data, Toast.LENGTH_LONG);
return true;
} else {
if (feed == null) {
((MainActivity) getActivity()).showSnackbarAbovePlayer(
R.string.please_wait_for_data, Toast.LENGTH_LONG);
return true;
}
try {
if (!FeedMenuHandler.onOptionsItemClicked(getActivity(), item, feed)) {
switch (item.getItemId()) {
case R.id.episode_actions:
int actions = EpisodesApplyActionFragment.ACTION_ALL;
if (feed.isLocalFeed()) {
// turn off download and delete actions for local feed
actions ^= EpisodesApplyActionFragment.ACTION_DOWNLOAD;
actions ^= EpisodesApplyActionFragment.ACTION_DELETE;
}
EpisodesApplyActionFragment fragment = EpisodesApplyActionFragment
.newInstance(feed.getItems(), actions);
((MainActivity)getActivity()).loadChildFragment(fragment);
return true;
case R.id.rename_item:
new RenameFeedDialog(getActivity(), feed).show();
return true;
case R.id.remove_item:
final FeedRemover remover = new FeedRemover(
getActivity(), feed) {
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
((MainActivity) getActivity()).loadFragment(EpisodesFragment.TAG, null);
}
};
int messageId = feed.isLocalFeed() ? R.string.feed_delete_confirmation_local_msg
: R.string.feed_delete_confirmation_msg;
ConfirmationDialog conDialog = new ConfirmationDialog(getActivity(),
R.string.remove_feed_label,
getString(messageId, feed.getTitle())) {
@Override
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
remover.executeAsync();
}
};
conDialog.createNewDialog().show();
return true;
default:
return false;
}
} else {
return true;
}
boolean feedMenuHandled;
try {
feedMenuHandled = FeedMenuHandler.onOptionsItemClicked(getActivity(), item, feed);
} catch (DownloadRequestException e) {
e.printStackTrace();
DownloadRequestErrorDialogCreator.newRequestErrorDialog(getActivity(), e.getMessage());
return true;
}
if (feedMenuHandled) {
return true;
}
switch (item.getItemId()) {
case R.id.episode_actions:
int actions = EpisodesApplyActionFragment.ACTION_ALL;
if (feed.isLocalFeed()) {
// turn off download and delete actions for local feed
actions ^= EpisodesApplyActionFragment.ACTION_DOWNLOAD;
actions ^= EpisodesApplyActionFragment.ACTION_DELETE;
}
} catch (DownloadRequestException e) {
e.printStackTrace();
DownloadRequestErrorDialogCreator.newRequestErrorDialog(getActivity(), e.getMessage());
EpisodesApplyActionFragment fragment = EpisodesApplyActionFragment
.newInstance(feed.getItems(), actions);
((MainActivity) getActivity()).loadChildFragment(fragment);
return true;
}
case R.id.rename_item:
new RenameFeedDialog(getActivity(), feed).show();
return true;
case R.id.remove_item:
final FeedRemover remover = new FeedRemover(
getActivity(), feed) {
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
((MainActivity) getActivity()).loadFragment(EpisodesFragment.TAG, null);
}
};
int messageId = feed.isLocalFeed() ? R.string.feed_delete_confirmation_local_msg
: R.string.feed_delete_confirmation_msg;
ConfirmationDialog conDialog = new ConfirmationDialog(getActivity(),
R.string.remove_feed_label,
getString(messageId, feed.getTitle())) {
@Override
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
remover.executeAsync();
}
};
conDialog.createNewDialog().show();
return true;
default:
return false;
}
}
@ -450,7 +428,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
private void updateSyncProgressBarVisibility() {
if (isUpdatingFeed != updateRefreshMenuItemChecker.isRefreshing()) {
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
if (!DownloadRequester.getInstance().isDownloadingFeeds()) {
nextPageLoader.getRoot().setVisibility(View.GONE);
@ -474,7 +452,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
adapter.updateItems(feed.getItems());
}
getActivity().invalidateOptionsMenu();
refreshToolbarState();
updateSyncProgressBarVisibility();
}

View File

@ -9,7 +9,6 @@ import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.preference.ListPreference;
@ -65,7 +64,7 @@ public class FeedSettingsFragment extends Fragment {
long feedId = getArguments().getLong(EXTRA_FEED_ID);
Toolbar toolbar = root.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
getParentFragmentManager().beginTransaction()
.replace(R.id.settings_fragment_container,

View File

@ -2,15 +2,12 @@ package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
@ -35,7 +32,7 @@ import io.reactivex.schedulers.Schedulers;
/**
* Displays information about a list of FeedItems.
*/
public class ItemPagerFragment extends Fragment {
public class ItemPagerFragment extends Fragment implements Toolbar.OnMenuItemClickListener {
private static final String ARG_FEEDITEMS = "feeditems";
private static final String ARG_FEEDITEM_POS = "feeditem_pos";
private static final String KEY_PAGER_ID = "pager_id";
@ -60,21 +57,18 @@ public class ItemPagerFragment extends Fragment {
private long[] feedItems;
private FeedItem item;
private Disposable disposable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
private Toolbar toolbar;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View layout = inflater.inflate(R.layout.feeditem_pager_fragment, container, false);
Toolbar toolbar = layout.findViewById(R.id.toolbar);
toolbar = layout.findViewById(R.id.toolbar);
toolbar.setTitle("");
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.feeditem_options);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
toolbar.setOnMenuItemClickListener(this);
feedItems = getArguments().getLongArray(ARG_FEEDITEMS);
int feedItemPos = getArguments().getInt(ARG_FEEDITEM_POS);
@ -130,28 +124,25 @@ public class ItemPagerFragment extends Fragment {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
item = result;
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}, Throwable::printStackTrace);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (!isAdded() || item == null) {
public void refreshToolbarState() {
if (item == null) {
return;
}
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.feeditem_options, menu);
if (item.hasMedia()) {
FeedItemMenuHandler.onPrepareMenu(menu, item);
FeedItemMenuHandler.onPrepareMenu(toolbar.getMenu(), item);
} else {
// these are already available via button1 and button2
FeedItemMenuHandler.onPrepareMenu(menu, item,
FeedItemMenuHandler.onPrepareMenu(toolbar.getMenu(), item,
R.id.mark_read_item, R.id.visit_website_item);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.open_podcast) {
openPodcast();
return true;
@ -164,7 +155,7 @@ public class ItemPagerFragment extends Fragment {
for (FeedItem item : event.items) {
if (this.item != null && this.item.getId() == item.getId()) {
this.item = item;
getActivity().invalidateOptionsMenu();
refreshToolbarState();
return;
}
}

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.view.MenuInflater;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;
@ -37,8 +38,10 @@ public class NewEpisodesFragment extends EpisodesListFragment {
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.filter_items).setVisible(false);
menu.findItem(R.id.mark_all_read_item).setVisible(false);
menu.findItem(R.id.remove_all_new_flags_item).setVisible(true);
}

View File

@ -2,14 +2,11 @@ package de.danoeh.antennapod.fragment;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.core.view.MenuItemCompat;
import androidx.appcompat.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -77,7 +74,6 @@ public class OnlineSearchFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
for (PodcastSearcherRegistry.SearcherInfo info : PodcastSearcherRegistry.getSearchProviders()) {
if (info.searcher.getClass().getName().equals(getArguments().getString(ARG_SEARCHER))) {
@ -94,7 +90,7 @@ public class OnlineSearchFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_itunes_search, container, false);
((AppCompatActivity) getActivity()).setSupportActionBar(root.findViewById(R.id.toolbar));
setupToolbar(root.findViewById(R.id.toolbar));
root.findViewById(R.id.spinner_country).setVisibility(INVISIBLE);
gridView = root.findViewById(R.id.gridView);
adapter = new ItunesAdapter(getActivity(), new ArrayList<>());
@ -126,11 +122,11 @@ public class OnlineSearchFragment extends Fragment {
adapter = null;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.online_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
private void setupToolbar(Toolbar toolbar) {
toolbar.inflateMenu(R.menu.online_search);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
MenuItem searchItem = toolbar.getMenu().findItem(R.id.action_search);
final SearchView sv = (SearchView) searchItem.getActionView();
sv.setQueryHint(getString(R.string.search_podcast_hint));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@ -163,7 +159,6 @@ public class OnlineSearchFragment extends Fragment {
if (getArguments().getString(ARG_QUERY, null) != null) {
sv.setQuery(getArguments().getString(ARG_QUERY, null), true);
}
}
private void search(String query) {

View File

@ -0,0 +1,47 @@
package de.danoeh.antennapod.fragment;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
/**
* Fragment with a ViewPager where the displayed items influence the top toolbar's menu.
* All items share the same general menu items and are just allowed to show/hide them.
*/
public abstract class PagedToolbarFragment extends Fragment {
private Toolbar toolbar;
private ViewPager2 viewPager;
/**
* Invalidate the toolbar menu if the current child fragment is visible.
* @param child The fragment, or null to force-refresh whatever the active fragment is.
*/
void invalidateOptionsMenuIfActive(Fragment child) {
Fragment visibleChild = getChildFragmentManager().findFragmentByTag("f" + viewPager.getCurrentItem());
if (visibleChild == child || child == null) {
child.onPrepareOptionsMenu(toolbar.getMenu());
}
}
protected void setupPagedToolbar(Toolbar toolbar, ViewPager2 viewPager) {
this.toolbar = toolbar;
this.viewPager = viewPager;
toolbar.setOnMenuItemClickListener(item -> {
Fragment child = getChildFragmentManager().findFragmentByTag("f" + viewPager.getCurrentItem());
if (child != null) {
return child.onOptionsItemSelected(item);
}
return false;
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
Fragment child = getChildFragmentManager().findFragmentByTag("f" + position);
if (child != null) {
child.onPrepareOptionsMenu(toolbar.getMenu());
}
}
});
}
}

View File

@ -1,20 +1,15 @@
package de.danoeh.antennapod.fragment;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
@ -43,7 +38,7 @@ import org.greenrobot.eventbus.ThreadMode;
import java.util.List;
public class PlaybackHistoryFragment extends Fragment {
public class PlaybackHistoryFragment extends Fragment implements Toolbar.OnMenuItemClickListener {
public static final String TAG = "PlaybackHistoryFragment";
private List<FeedItem> playbackHistory;
@ -52,21 +47,24 @@ public class PlaybackHistoryFragment extends Fragment {
private EpisodeItemListRecyclerView recyclerView;
private EmptyViewHandler emptyView;
private ProgressBar progressBar;
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
@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 toolbar = root.findViewById(R.id.toolbar);
toolbar = root.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.playback_history_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setOnMenuItemClickListener(this);
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
toolbar.inflateMenu(R.menu.playback_history);
refreshToolbarState();
recyclerView = root.findViewById(R.id.recyclerView);
recyclerView.setRecycledViewPool(((MainActivity) getActivity()).getRecycledViewPool());
@ -146,39 +144,18 @@ public class PlaybackHistoryFragment extends Fragment {
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (!isAdded()) {
return;
}
super.onCreateOptionsMenu(menu, inflater);
MenuItem clearHistory = menu.add(Menu.NONE, R.id.clear_history_item, Menu.CATEGORY_CONTAINER, R.string.clear_history_label);
clearHistory.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
TypedArray drawables = getActivity().obtainStyledAttributes(new int[]{R.attr.ic_delete});
clearHistory.setIcon(drawables.getDrawable(0));
drawables.recycle();
public void refreshToolbarState() {
boolean hasHistory = playbackHistory != null && !playbackHistory.isEmpty();
toolbar.getMenu().findItem(R.id.clear_history_item).setVisible(hasHistory);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.findItem(R.id.clear_history_item);
if (menuItem != null) {
menuItem.setVisible(playbackHistory != null && !playbackHistory.isEmpty());
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!super.onOptionsItemSelected(item)) {
if (item.getItemId() == R.id.clear_history_item) {
DBWriter.clearPlaybackHistory();
return true;
}
return false;
} else {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.clear_history_item) {
DBWriter.clearPlaybackHistory();
return true;
}
return false;
}
@Override
@ -194,18 +171,18 @@ public class PlaybackHistoryFragment extends Fragment {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onHistoryUpdated(PlaybackHistoryEvent event) {
loadItems();
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPlayerStatusChanged(PlayerStatusEvent event) {
loadItems();
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
private void onFragmentLoaded() {
adapter.notifyDataSetChanged();
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
private void loadItems() {

View File

@ -6,8 +6,6 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -15,7 +13,7 @@ import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
@ -67,7 +65,7 @@ import static de.danoeh.antennapod.dialog.EpisodesApplyActionFragment.ACTION_REM
/**
* Shows all items in the queue.
*/
public class QueueFragment extends Fragment {
public class QueueFragment extends Fragment implements Toolbar.OnMenuItemClickListener {
public static final String TAG = "QueueFragment";
private TextView infoBar;
@ -75,6 +73,7 @@ public class QueueFragment extends Fragment {
private QueueRecyclerAdapter recyclerAdapter;
private EmptyViewHandler emptyView;
private ProgressBar progLoading;
private Toolbar toolbar;
private List<FeedItem> queue;
@ -91,7 +90,6 @@ public class QueueFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
prefs = getActivity().getSharedPreferences(PREFS, Context.MODE_PRIVATE);
}
@ -182,7 +180,7 @@ public class QueueFragment extends Fragment {
Log.d(TAG, "onEventMainThread() called with DownloadEvent");
DownloaderUpdate update = event.update;
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
if (recyclerAdapter != null && update.mediaIds.length > 0) {
for (long mediaId : update.mediaIds) {
@ -212,7 +210,7 @@ public class QueueFragment extends Fragment {
public void onPlayerStatusChanged(PlayerStatusEvent event) {
loadItems(false);
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
}
@ -221,7 +219,7 @@ public class QueueFragment extends Fragment {
// Sent when playback position is reset
loadItems(false);
if (isUpdatingFeeds != updateRefreshMenuItemChecker.isRefreshing()) {
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
}
@ -238,114 +236,94 @@ public class QueueFragment extends Fragment {
private final MenuItemUtils.UpdateRefreshMenuItemChecker updateRefreshMenuItemChecker =
() -> DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds();
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(!isAdded()) {
return;
}
super.onCreateOptionsMenu(menu, inflater);
if (queue != null) {
inflater.inflate(R.menu.queue, menu);
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), 0, "");
MenuItemUtils.refreshLockItem(getActivity(), menu);
private void refreshToolbarState() {
MenuItemUtils.refreshLockItem(getActivity(), toolbar.getMenu());
boolean keepSorted = UserPreferences.isQueueKeepSorted();
toolbar.getMenu().findItem(R.id.queue_lock).setVisible(!keepSorted);
// Show Lock Item only if queue is sorted manually
boolean keepSorted = UserPreferences.isQueueKeepSorted();
MenuItem lockItem = menu.findItem(R.id.queue_lock);
lockItem.setVisible(!keepSorted);
// Random sort is not supported in keep sorted mode
MenuItem sortRandomItem = menu.findItem(R.id.queue_sort_random);
sortRandomItem.setVisible(!keepSorted);
// Set keep sorted checkbox
MenuItem keepSortedItem = menu.findItem(R.id.queue_keep_sorted);
keepSortedItem.setChecked(keepSorted);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
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);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!super.onOptionsItemSelected(item)) {
switch (item.getItemId()) {
case R.id.queue_lock:
toggleQueueLock();
return true;
case R.id.refresh_item:
AutoUpdateManager.runImmediate(requireContext());
return true;
case R.id.clear_queue:
// make sure the user really wants to clear the queue
ConfirmationDialog conDialog = new ConfirmationDialog(getActivity(),
R.string.clear_queue_label,
R.string.clear_queue_confirmation_msg) {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.queue_lock:
toggleQueueLock();
return true;
case R.id.refresh_item:
AutoUpdateManager.runImmediate(requireContext());
return true;
case R.id.clear_queue:
// make sure the user really wants to clear the queue
ConfirmationDialog conDialog = new ConfirmationDialog(getActivity(),
R.string.clear_queue_label,
R.string.clear_queue_confirmation_msg) {
@Override
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
DBWriter.clearQueue();
}
};
conDialog.createNewDialog().show();
return true;
case R.id.episode_actions:
((MainActivity) requireActivity()).loadChildFragment(
EpisodesApplyActionFragment.newInstance(queue, ACTION_DELETE | ACTION_REMOVE_FROM_QUEUE | ACTION_DOWNLOAD));
return true;
case R.id.queue_sort_episode_title_asc:
setSortOrder(SortOrder.EPISODE_TITLE_A_Z);
return true;
case R.id.queue_sort_episode_title_desc:
setSortOrder(SortOrder.EPISODE_TITLE_Z_A);
return true;
case R.id.queue_sort_date_asc:
setSortOrder(SortOrder.DATE_OLD_NEW);
return true;
case R.id.queue_sort_date_desc:
setSortOrder(SortOrder.DATE_NEW_OLD);
return true;
case R.id.queue_sort_duration_asc:
setSortOrder(SortOrder.DURATION_SHORT_LONG);
return true;
case R.id.queue_sort_duration_desc:
setSortOrder(SortOrder.DURATION_LONG_SHORT);
return true;
case R.id.queue_sort_feed_title_asc:
setSortOrder(SortOrder.FEED_TITLE_A_Z);
return true;
case R.id.queue_sort_feed_title_desc:
setSortOrder(SortOrder.FEED_TITLE_Z_A);
return true;
case R.id.queue_sort_random:
setSortOrder(SortOrder.RANDOM);
return true;
case R.id.queue_sort_smart_shuffle_asc:
setSortOrder(SortOrder.SMART_SHUFFLE_OLD_NEW);
return true;
case R.id.queue_sort_smart_shuffle_desc:
setSortOrder(SortOrder.SMART_SHUFFLE_NEW_OLD);
return true;
case R.id.queue_keep_sorted:
boolean keepSortedOld = UserPreferences.isQueueKeepSorted();
boolean keepSortedNew = !keepSortedOld;
UserPreferences.setQueueKeepSorted(keepSortedNew);
if (keepSortedNew) {
SortOrder sortOrder = UserPreferences.getQueueKeepSortedOrder();
DBWriter.reorderQueue(sortOrder, true);
@Override
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
DBWriter.clearQueue();
}
if (recyclerAdapter != null) {
recyclerAdapter.updateDragDropEnabled();
}
getActivity().invalidateOptionsMenu();
return true;
default:
return false;
}
} else {
return true;
};
conDialog.createNewDialog().show();
return true;
case R.id.episode_actions:
((MainActivity) requireActivity()).loadChildFragment(
EpisodesApplyActionFragment.newInstance(queue,
ACTION_DELETE | ACTION_REMOVE_FROM_QUEUE | ACTION_DOWNLOAD));
return true;
case R.id.queue_sort_episode_title_asc:
setSortOrder(SortOrder.EPISODE_TITLE_A_Z);
return true;
case R.id.queue_sort_episode_title_desc:
setSortOrder(SortOrder.EPISODE_TITLE_Z_A);
return true;
case R.id.queue_sort_date_asc:
setSortOrder(SortOrder.DATE_OLD_NEW);
return true;
case R.id.queue_sort_date_desc:
setSortOrder(SortOrder.DATE_NEW_OLD);
return true;
case R.id.queue_sort_duration_asc:
setSortOrder(SortOrder.DURATION_SHORT_LONG);
return true;
case R.id.queue_sort_duration_desc:
setSortOrder(SortOrder.DURATION_LONG_SHORT);
return true;
case R.id.queue_sort_feed_title_asc:
setSortOrder(SortOrder.FEED_TITLE_A_Z);
return true;
case R.id.queue_sort_feed_title_desc:
setSortOrder(SortOrder.FEED_TITLE_Z_A);
return true;
case R.id.queue_sort_random:
setSortOrder(SortOrder.RANDOM);
return true;
case R.id.queue_sort_smart_shuffle_asc:
setSortOrder(SortOrder.SMART_SHUFFLE_OLD_NEW);
return true;
case R.id.queue_sort_smart_shuffle_desc:
setSortOrder(SortOrder.SMART_SHUFFLE_NEW_OLD);
return true;
case R.id.queue_keep_sorted:
boolean keepSortedOld = UserPreferences.isQueueKeepSorted();
boolean keepSortedNew = !keepSortedOld;
UserPreferences.setQueueKeepSorted(keepSortedNew);
if (keepSortedNew) {
SortOrder sortOrder = UserPreferences.getQueueKeepSortedOrder();
DBWriter.reorderQueue(sortOrder, true);
}
if (recyclerAdapter != null) {
recyclerAdapter.updateDragDropEnabled();
}
refreshToolbarState();
return true;
default:
return false;
}
}
@ -378,7 +356,7 @@ public class QueueFragment extends Fragment {
private void setQueueLocked(boolean locked) {
UserPreferences.setQueueLocked(locked);
getActivity().invalidateOptionsMenu();
refreshToolbarState();
if (recyclerAdapter != null) {
recyclerAdapter.updateDragDropEnabled();
}
@ -440,7 +418,13 @@ public class QueueFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.queue_fragment, container, false);
((AppCompatActivity) getActivity()).setSupportActionBar(root.findViewById(R.id.toolbar));
toolbar = root.findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(this);
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
toolbar.inflateMenu(R.menu.queue);
MenuItemUtils.setupSearchItem(toolbar.getMenu(), (MainActivity) getActivity(), 0, "");
refreshToolbarState();
infoBar = root.findViewById(R.id.info_bar);
recyclerView = root.findViewById(R.id.recyclerView);
RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
@ -568,7 +552,7 @@ public class QueueFragment extends Fragment {
// we need to refresh the options menu because it sometimes
// needs data that may have just been loaded.
getActivity().invalidateOptionsMenu();
refreshToolbarState();
refreshInfoBar();
}

View File

@ -75,12 +75,6 @@ public class RunningDownloadsFragment extends ListFragment {
EventBus.getDefault().register(this);
}
@Override
public void onResume() {
super.onResume();
setHasOptionsMenu(true);
}
@Override
public void onStop() {
super.onStop();
@ -94,9 +88,9 @@ public class RunningDownloadsFragment extends ListFragment {
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.downloads_running, menu);
public void onPrepareOptionsMenu(@NonNull Menu menu) {
menu.findItem(R.id.clear_logs_item).setVisible(false);
menu.findItem(R.id.episode_actions).setVisible(false);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@ -113,7 +107,7 @@ public class RunningDownloadsFragment extends ListFragment {
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
getActivity().invalidateOptionsMenu();
((PagedToolbarFragment) getParentFragment()).invalidateOptionsMenuIfActive(this);
}
}

View File

@ -4,16 +4,14 @@ import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -96,7 +94,6 @@ public class SearchFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
@Override
@ -118,7 +115,7 @@ public class SearchFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.search_fragment, container, false);
((AppCompatActivity) getActivity()).setSupportActionBar(layout.findViewById(R.id.toolbar));
setupToolbar(layout.findViewById(R.id.toolbar));
progressBar = layout.findViewById(R.id.progressBar);
recyclerView = layout.findViewById(R.id.recyclerView);
@ -154,11 +151,12 @@ public class SearchFragment extends Fragment {
EventBus.getDefault().unregister(this);
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
private void setupToolbar(Toolbar toolbar) {
toolbar.setTitle(R.string.search_label);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
toolbar.inflateMenu(R.menu.search);
MenuItem item = toolbar.getMenu().findItem(R.id.action_search);
item.expandActionView();
final SearchView sv = (SearchView) item.getActionView();
sv.setQueryHint(getString(R.string.search_label));

View File

@ -9,14 +9,13 @@ import android.os.Handler;
import android.os.Looper;
import android.widget.ProgressBar;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
@ -65,7 +64,7 @@ import org.greenrobot.eventbus.ThreadMode;
/**
* Fragment for displaying feed subscriptions
*/
public class SubscriptionFragment extends Fragment {
public class SubscriptionFragment extends Fragment implements Toolbar.OnMenuItemClickListener {
public static final String TAG = "SubscriptionFragment";
private static final String PREFS = "SubscriptionFragment";
@ -78,6 +77,7 @@ public class SubscriptionFragment extends Fragment {
private ProgressBar progressBar;
private EmptyViewHandler emptyView;
private TextView feedsFilteredMsg;
private Toolbar toolbar;
private int mPosition = -1;
private boolean isUpdatingFeeds = false;
@ -89,7 +89,6 @@ public class SubscriptionFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
prefs = requireActivity().getSharedPreferences(PREFS, Context.MODE_PRIVATE);
}
@ -98,7 +97,12 @@ public class SubscriptionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_subscriptions, container, false);
((AppCompatActivity) getActivity()).setSupportActionBar(root.findViewById(R.id.toolbar));
toolbar = root.findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(this);
((MainActivity) getActivity()).setupToolbarToggle(toolbar);
toolbar.inflateMenu(R.menu.subscriptions);
refreshToolbarState();
subscriptionGridLayout = root.findViewById(R.id.subscriptions_grid);
subscriptionGridLayout.setNumColumns(prefs.getInt(PREF_NUM_COLUMNS, getDefaultNumOfColumns()));
registerForContextMenu(subscriptionGridLayout);
@ -117,25 +121,19 @@ public class SubscriptionFragment extends Fragment {
return root;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.subscriptions, menu);
private void refreshToolbarState() {
int columns = prefs.getInt(PREF_NUM_COLUMNS, getDefaultNumOfColumns());
menu.findItem(R.id.subscription_num_columns_2).setChecked(columns == 2);
menu.findItem(R.id.subscription_num_columns_3).setChecked(columns == 3);
menu.findItem(R.id.subscription_num_columns_4).setChecked(columns == 4);
menu.findItem(R.id.subscription_num_columns_5).setChecked(columns == 5);
toolbar.getMenu().findItem(R.id.subscription_num_columns_2).setChecked(columns == 2);
toolbar.getMenu().findItem(R.id.subscription_num_columns_3).setChecked(columns == 3);
toolbar.getMenu().findItem(R.id.subscription_num_columns_4).setChecked(columns == 4);
toolbar.getMenu().findItem(R.id.subscription_num_columns_5).setChecked(columns == 5);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(toolbar.getMenu(),
R.id.refresh_item, updateRefreshMenuItemChecker);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (super.onOptionsItemSelected(item)) {
return true;
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh_item:
AutoUpdateManager.runImmediate(requireContext());
@ -166,7 +164,7 @@ public class SubscriptionFragment extends Fragment {
private void setColumnNumber(int columns) {
subscriptionGridLayout.setNumColumns(columns);
prefs.edit().putInt(PREF_NUM_COLUMNS, columns).apply();
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
private void setupEmptyView() {
@ -361,7 +359,7 @@ public class SubscriptionFragment extends Fragment {
public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
if (event.hasChangedFeedUpdateStatus(isUpdatingFeeds)) {
getActivity().invalidateOptionsMenu();
refreshToolbarState();
}
}

View File

@ -6,7 +6,6 @@ import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
@ -33,7 +32,7 @@ public class GpodnetMainFragment extends Fragment {
View root = inflater.inflate(R.layout.pager_fragment, container, false);
Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.gpodnet_main_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
ViewPager2 viewPager = root.findViewById(R.id.viewpager);
GpodnetPagerAdapter pagerAdapter = new GpodnetPagerAdapter(this);

View File

@ -4,15 +4,11 @@ import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.core.view.MenuItemCompat;
import androidx.appcompat.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -46,16 +42,34 @@ public abstract class PodcastListFragment extends Fragment {
private Button butRetry;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.gpodnet_podcast_list, container, false);
setupToolbar(root.findViewById(R.id.toolbar));
gridView = root.findViewById(R.id.gridView);
progressBar = root.findViewById(R.id.progressBar);
txtvError = root.findViewById(R.id.txtvError);
butRetry = root.findViewById(R.id.butRetry);
gridView.setOnItemClickListener((parent, view, position, id) ->
onPodcastSelected((GpodnetPodcast) gridView.getAdapter().getItem(position)));
butRetry.setOnClickListener(v -> loadData());
loadData();
return root;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.gpodder_podcasts, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
private void setupToolbar(Toolbar toolbar) {
if (getArguments() != null && getArguments().getBoolean(ARGUMENT_HIDE_TOOLBAR, false)) {
toolbar.setVisibility(View.GONE);
return;
}
toolbar.setTitle(R.string.gpodnet_main_label);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack());
toolbar.inflateMenu(R.menu.gpodder_podcasts);
MenuItem searchItem = toolbar.getMenu().findItem(R.id.action_search);
final SearchView sv = (SearchView) searchItem.getActionView();
sv.setQueryHint(getString(R.string.gpodnet_search_hint));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@ -76,30 +90,6 @@ public abstract class PodcastListFragment extends Fragment {
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.gpodnet_podcast_list, container, false);
Toolbar toolbar = root.findViewById(R.id.toolbar);
if (getArguments() == null || !getArguments().getBoolean(ARGUMENT_HIDE_TOOLBAR, false)) {
toolbar.setTitle(R.string.gpodnet_main_label);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
} else {
toolbar.setVisibility(View.GONE);
}
gridView = root.findViewById(R.id.gridView);
progressBar = root.findViewById(R.id.progressBar);
txtvError = root.findViewById(R.id.txtvError);
butRetry = root.findViewById(R.id.butRetry);
gridView.setOnItemClickListener((parent, view, position, id) ->
onPodcastSelected((GpodnetPodcast) gridView.getAdapter().getItem(position)));
butRetry.setOnClickListener(v -> loadData());
loadData();
return root;
}
private void onPodcastSelected(GpodnetPodcast selection) {
Log.d(TAG, "Selected podcast: " + selection.toString());
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);

View File

@ -34,11 +34,6 @@ public class FeedMenuHandler {
private static final String TAG = "FeedMenuHandler";
public static boolean onCreateOptionsMenu(MenuInflater inflater, Menu menu) {
inflater.inflate(R.menu.feedlist, menu);
return true;
}
public static boolean onPrepareOptionsMenu(Menu menu, Feed selectedFeed) {
if (selectedFeed == null) {
return true;

View File

@ -1,9 +1,10 @@
package de.danoeh.antennapod.menuhandler;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.view.menu.MenuItemImpl;
import androidx.appcompat.widget.SearchView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
@ -11,24 +12,23 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.ThemeUtils;
import de.danoeh.antennapod.fragment.SearchFragment;
import java.util.HashMap;
import java.util.Map;
/**
* Utilities for menu items
* Utilities for menu items.
*/
public class MenuItemUtils extends de.danoeh.antennapod.core.menuhandler.MenuItemUtils {
@SuppressWarnings("ResourceType")
public static void refreshLockItem(Context context, Menu menu) {
final MenuItem queueLock = menu.findItem(de.danoeh.antennapod.R.id.queue_lock);
int[] lockIcons = new int[] { de.danoeh.antennapod.R.attr.ic_lock_open, de.danoeh.antennapod.R.attr.ic_lock_closed };
TypedArray ta = context.obtainStyledAttributes(lockIcons);
final MenuItem queueLock = menu.findItem(R.id.queue_lock);
if (UserPreferences.isQueueLocked()) {
queueLock.setTitle(de.danoeh.antennapod.R.string.unlock_queue);
queueLock.setIcon(ta.getDrawable(0));
queueLock.setIcon(ThemeUtils.getDrawableFromAttr(context, R.attr.ic_lock_open));
} else {
queueLock.setTitle(de.danoeh.antennapod.R.string.lock_queue);
queueLock.setIcon(ta.getDrawable(1));
queueLock.setIcon(ThemeUtils.getDrawableFromAttr(context, R.attr.ic_lock_closed));
}
ta.recycle();
}
public static void setupSearchItem(Menu menu, MainActivity activity, long feedId, String feedTitle) {
@ -51,21 +51,49 @@ public class MenuItemUtils extends de.danoeh.antennapod.core.menuhandler.MenuIte
}
});
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
private final Map<Integer, Integer> oldShowAsActionState = new HashMap<>();
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
public boolean onMenuItemActionExpand(MenuItem clickedItem) {
oldShowAsActionState.clear();
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() != searchItem.getItemId()) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
MenuItem item = menu.getItem(i);
if (item.getItemId() != searchItem.getItemId()) {
oldShowAsActionState.put(item.getItemId(), getShowAsActionFlag(item));
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
}
}
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
activity.invalidateOptionsMenu();
public boolean onMenuItemActionCollapse(MenuItem collapsedItem) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() != searchItem.getItemId()
&& oldShowAsActionState.containsKey(item.getItemId())) {
item.setShowAsAction(oldShowAsActionState.get(item.getItemId()));
}
}
return true;
}
});
}
@SuppressLint("RestrictedApi")
private static int getShowAsActionFlag(MenuItem item) {
if (!(item instanceof MenuItemImpl)) {
return MenuItemImpl.SHOW_AS_ACTION_NEVER;
}
MenuItemImpl itemImpl = ((MenuItemImpl) item);
if (itemImpl.requiresActionButton()) {
return MenuItemImpl.SHOW_AS_ACTION_ALWAYS;
} else if (itemImpl.requestsActionButton()) {
return MenuItemImpl.SHOW_AS_ACTION_IF_ROOM;
} else if (itemImpl.showsTextAsAction()) {
return MenuItemImpl.SHOW_AS_ACTION_WITH_TEXT;
} else {
return MenuItemImpl.SHOW_AS_ACTION_NEVER;
}
}
}

View File

@ -6,6 +6,14 @@
android:menuCategory="container"
android:title="@string/multi_select"
android:icon="?attr/checkbox_multiple"
android:visible="false"
app:showAsAction="ifRoom" />
<item
android:id="@+id/clear_logs_item"
android:menuCategory="container"
android:title="@string/clear_history_label"
android:icon="?attr/ic_delete"
android:visible="false"
app:showAsAction="ifRoom" />
<item
android:id="@+id/refresh_item"

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/clear_history_item"
android:menuCategory="container"
android:title="@string/clear_history_label"
android:icon="?attr/ic_delete"
app:showAsAction="ifRoom" />
<item
android:id="@+id/refresh_item"
android:title="@string/refresh_label"
android:menuCategory="container"
app:showAsAction="ifRoom"
android:icon="?attr/navigation_refresh"/>
</menu>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/refresh_item"
android:title="@string/refresh_label"
android:menuCategory="container"
app:showAsAction="ifRoom"
android:icon="?attr/navigation_refresh"/>
</menu>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/clear_history_item"
android:icon="?attr/ic_delete"
android:title="@string/clear_history_label"
app:showAsAction="ifRoom"/>
</menu>