From 64d743285258e48033b3d806c68546ad8e368040 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Sat, 9 Oct 2021 16:37:34 +0200 Subject: [PATCH] Deduplicated drawer code in MainActivity --- .../java/org/schabi/newpipe/MainActivity.java | 117 +++++++----------- 1 file changed, 43 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 5b1cf48e5..3b6fdcc86 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -20,6 +20,8 @@ package org.schabi.newpipe; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -91,8 +93,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; - public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @SuppressWarnings("ConstantConditions") @@ -165,7 +165,42 @@ public class MainActivity extends AppCompatActivity { openMiniPlayerUponPlayerStarted(); } - private void setupDrawer() throws Exception { + private void setupDrawer() throws ExtractionException { + addDrawerMenuForCurrentService(); + + toggle = new ActionBarDrawerToggle(this, mainBinding.getRoot(), + toolbarLayoutBinding.toolbar, R.string.drawer_open, R.string.drawer_close); + toggle.syncState(); + mainBinding.getRoot().addDrawerListener(toggle); + mainBinding.getRoot().addDrawerListener(new DrawerLayout.SimpleDrawerListener() { + private int lastService; + + @Override + public void onDrawerOpened(final View drawerView) { + lastService = ServiceHelper.getSelectedServiceId(MainActivity.this); + } + + @Override + public void onDrawerClosed(final View drawerView) { + if (servicesShown) { + toggleServices(); + } + if (lastService != ServiceHelper.getSelectedServiceId(MainActivity.this)) { + ActivityCompat.recreate(MainActivity.this); + } + } + }); + + drawerLayoutBinding.navigation.setNavigationItemSelectedListener(this::drawerItemSelected); + setupDrawerHeader(); + } + + /** + * Builds the drawer menu for the current service. + * + * @throws ExtractionException + */ + private void addDrawerMenuForCurrentService() throws ExtractionException { //Tabs final int currentServiceId = ServiceHelper.getSelectedServiceId(this); final StreamingService service = NewPipe.getService(currentServiceId); @@ -204,32 +239,6 @@ public class MainActivity extends AppCompatActivity { drawerLayoutBinding.navigation.getMenu() .add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about) .setIcon(R.drawable.ic_info_outline); - - toggle = new ActionBarDrawerToggle(this, mainBinding.getRoot(), - toolbarLayoutBinding.toolbar, R.string.drawer_open, R.string.drawer_close); - toggle.syncState(); - mainBinding.getRoot().addDrawerListener(toggle); - mainBinding.getRoot().addDrawerListener(new DrawerLayout.SimpleDrawerListener() { - private int lastService; - - @Override - public void onDrawerOpened(final View drawerView) { - lastService = ServiceHelper.getSelectedServiceId(MainActivity.this); - } - - @Override - public void onDrawerClosed(final View drawerView) { - if (servicesShown) { - toggleServices(); - } - if (lastService != ServiceHelper.getSelectedServiceId(MainActivity.this)) { - ActivityCompat.recreate(MainActivity.this); - } - } - }); - - drawerLayoutBinding.navigation.setNavigationItemSelectedListener(this::drawerItemSelected); - setupDrawerHeader(); } private boolean drawerItemSelected(final MenuItem item) { @@ -337,11 +346,15 @@ public class MainActivity extends AppCompatActivity { drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_tabs_group); drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_options_about_group); + // Show up or down arrow + drawerHeaderBinding.drawerArrow.setImageResource( + servicesShown ? R.drawable.ic_arrow_drop_up : R.drawable.ic_arrow_drop_down); + if (servicesShown) { showServices(); } else { try { - showTabs(); + addDrawerMenuForCurrentService(); } catch (final Exception e) { ErrorActivity.reportUiErrorInSnackbar(this, "Showing main page tabs", e); } @@ -349,8 +362,6 @@ public class MainActivity extends AppCompatActivity { } private void showServices() { - drawerHeaderBinding.drawerArrow.setImageResource(R.drawable.ic_arrow_drop_up); - for (final StreamingService s : NewPipe.getServices()) { final String title = s.getServiceInfo().getName() + (ServiceHelper.isBeta(s) ? " (beta)" : ""); @@ -414,48 +425,6 @@ public class MainActivity extends AppCompatActivity { menuItem.setActionView(spinner); } - private void showTabs() throws ExtractionException { - drawerHeaderBinding.drawerArrow.setImageResource(R.drawable.ic_arrow_drop_down); - - //Tabs - final int currentServiceId = ServiceHelper.getSelectedServiceId(this); - final StreamingService service = NewPipe.getService(currentServiceId); - - int kioskId = 0; - - for (final String ks : service.getKioskList().getAvailableKiosks()) { - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_tabs_group, kioskId, ORDER, - KioskTranslator.getTranslatedKioskName(ks, this)) - .setIcon(KioskTranslator.getKioskIcon(ks, this)); - kioskId++; - } - - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER, R.string.tab_subscriptions) - .setIcon(R.drawable.ic_tv); - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, R.string.fragment_feed_title) - .setIcon(R.drawable.ic_rss_feed); - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, R.string.tab_bookmarks) - .setIcon(R.drawable.ic_bookmark); - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads) - .setIcon(R.drawable.ic_file_download); - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, R.string.action_history) - .setIcon(R.drawable.ic_history); - - //Settings and About - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, R.string.settings) - .setIcon(R.drawable.ic_settings); - drawerLayoutBinding.navigation.getMenu() - .add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about) - .setIcon(R.drawable.ic_info_outline); - } - @Override protected void onDestroy() { super.onDestroy();