extract drawer menu filtering to separate util class

This commit is contained in:
AndyScherzinger 2018-06-27 09:28:14 +02:00
parent d5bcf14709
commit 139357778d
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
2 changed files with 113 additions and 66 deletions

View file

@ -89,6 +89,7 @@ import com.owncloud.android.ui.events.MenuItemClickEvent;
import com.owncloud.android.ui.events.SearchEvent;
import com.owncloud.android.ui.trashbin.TrashbinActivity;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.DrawerMenuUtil;
import com.owncloud.android.utils.FilesSyncHelper;
import com.owncloud.android.utils.ThemeUtils;
import com.owncloud.android.utils.svg.MenuSimpleTarget;
@ -342,75 +343,19 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
}
private void filterDrawerMenu(Menu menu, Account account) {
boolean searchSupported = AccountUtils.hasSearchSupport(account);
DrawerMenuUtil.filterForBottomToolbarMenuItems(menu, getResources());
DrawerMenuUtil.filterSearchMenuItems(menu, account, getResources());
DrawerMenuUtil.filterTrashbinMenuItems(menu, account, getContentResolver());
if (getResources().getBoolean(R.bool.bottom_toolbar_enabled) && account != null) {
filterMenuItems(menu, R.id.nav_all_files, R.id.nav_settings, R.id.nav_favorites, R.id.nav_photos);
}
DrawerMenuUtil.setupHomeMenuItem(menu, getResources());
if (!searchSupported && account != null) {
filterMenuItems(menu, R.id.nav_photos, R.id.nav_favorites, R.id.nav_videos);
}
DrawerMenuUtil.removeMenuItem(menu, R.id.nav_participate, !getResources().getBoolean(R.bool.participate_enabled));
DrawerMenuUtil.removeMenuItem(menu, R.id.nav_shared, !getResources().getBoolean(R.bool.shared_enabled));
DrawerMenuUtil.removeMenuItem(menu, R.id.nav_contacts, !getResources().getBoolean(R.bool.contacts_backup)
|| !getResources().getBoolean(R.bool.show_drawer_contacts_backup));
if (account != null) {
FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());
OCCapability capability = storageManager.getCapability(account.name);
if (AccountUtils.getServerVersion(getAccount()).compareTo(OwnCloudVersion.nextcloud_14) < 0 ||
capability.getFilesUndelete().isFalse() || capability.getFilesUndelete().isUnknown()) {
filterMenuItems(menu, R.id.nav_trashbin);
}
}
if (getResources().getBoolean(R.bool.use_home) && menu.findItem(R.id.nav_all_files) != null) {
menu.findItem(R.id.nav_all_files).setTitle(getResources().getString(R.string.drawer_item_home));
menu.findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home);
}
if (!getResources().getBoolean(R.bool.participate_enabled)) {
menu.removeItem(R.id.nav_participate);
}
if (!getResources().getBoolean(R.bool.shared_enabled)) {
menu.removeItem(R.id.nav_shared);
}
if (!getResources().getBoolean(R.bool.contacts_backup)
|| !getResources().getBoolean(R.bool.show_drawer_contacts_backup)) {
menu.removeItem(R.id.nav_contacts);
}
if (getResources().getBoolean(R.bool.syncedFolder_light)) {
menu.removeItem(R.id.nav_synced_folders);
}
if (!getResources().getBoolean(R.bool.show_drawer_logout)) {
menu.removeItem(R.id.nav_logout);
}
if (AccountUtils.hasSearchSupport(account)) {
if (!getResources().getBoolean(R.bool.recently_added_enabled)) {
menu.removeItem(R.id.nav_recently_added);
}
if (!getResources().getBoolean(R.bool.recently_modified_enabled)) {
menu.removeItem(R.id.nav_recently_modified);
}
if (!getResources().getBoolean(R.bool.videos_enabled)) {
menu.removeItem(R.id.nav_videos);
}
} else if (account != null) {
filterMenuItems(menu, R.id.nav_recently_added, R.id.nav_recently_modified, R.id.nav_videos);
}
}
private static void filterMenuItems(Menu menu, int... menuIds) {
if (menuIds != null) {
for (int menuId : menuIds) {
menu.removeItem(menuId);
}
}
DrawerMenuUtil.removeMenuItem(menu, R.id.nav_synced_folders, getResources().getBoolean(R.bool.syncedFolder_light));
DrawerMenuUtil.removeMenuItem(menu, R.id.nav_logout, !getResources().getBoolean(R.bool.show_drawer_logout));
}
@Subscribe(threadMode = ThreadMode.MAIN)

View file

@ -0,0 +1,102 @@
/*
* Nextcloud Android client application
*
* @author Andy Scherzinger
* Copyright (C) 2018 Andy Scherzinger
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.utils;
import android.accounts.Account;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.view.Menu;
import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.lib.resources.status.OCCapability;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
/**
* A helper class for drawer menu related operations.
*/
public class DrawerMenuUtil {
private DrawerMenuUtil() {
}
public static void filterForBottomToolbarMenuItems(Menu menu, Resources resources) {
if (resources.getBoolean(R.bool.bottom_toolbar_enabled)) {
filterMenuItems(menu, R.id.nav_all_files, R.id.nav_settings, R.id.nav_favorites, R.id.nav_photos);
}
}
public static void filterSearchMenuItems(Menu menu, Account account, Resources resources) {
boolean hasSearchSupport = AccountUtils.hasSearchSupport(account);
if (account != null && !hasSearchSupport) {
filterMenuItems(menu, R.id.nav_photos, R.id.nav_favorites, R.id.nav_videos);
}
if (hasSearchSupport) {
if (!resources.getBoolean(R.bool.recently_added_enabled)) {
menu.removeItem(R.id.nav_recently_added);
}
if (!resources.getBoolean(R.bool.recently_modified_enabled)) {
menu.removeItem(R.id.nav_recently_modified);
}
if (!resources.getBoolean(R.bool.videos_enabled)) {
menu.removeItem(R.id.nav_videos);
}
} else if (account != null) {
filterMenuItems(menu, R.id.nav_recently_added, R.id.nav_recently_modified, R.id.nav_videos);
}
}
public static void filterTrashbinMenuItems(Menu menu, Account account, ContentResolver contentResolver) {
if (account != null) {
FileDataStorageManager storageManager = new FileDataStorageManager(account, contentResolver);
OCCapability capability = storageManager.getCapability(account.name);
if (AccountUtils.getServerVersion(account).compareTo(OwnCloudVersion.nextcloud_14) < 0 ||
capability.getFilesUndelete().isFalse() || capability.getFilesUndelete().isUnknown()) {
filterMenuItems(menu, R.id.nav_trashbin);
}
}
}
public static void removeMenuItem(Menu menu, int id, boolean remove) {
if (remove) {
menu.removeItem(id);
}
}
public static void setupHomeMenuItem(Menu menu, Resources resources) {
if (resources.getBoolean(R.bool.use_home) && menu.findItem(R.id.nav_all_files) != null) {
menu.findItem(R.id.nav_all_files).setTitle(resources.getString(R.string.drawer_item_home));
menu.findItem(R.id.nav_all_files).setIcon(R.drawable.ic_home);
}
}
private static void filterMenuItems(Menu menu, int... menuIds) {
if (menuIds != null) {
for (int menuId : menuIds) {
menu.removeItem(menuId);
}
}
}
}