From c9b267b072ea4cd6a8a256a96902775ddac9c555 Mon Sep 17 00:00:00 2001 From: Somethingweirdhere Date: Fri, 8 Jun 2018 21:07:25 +0200 Subject: [PATCH] Added drawer menu --- .../java/org/schabi/newpipe/MainActivity.java | 147 +++++++++++++++--- .../local/bookmark/BookmarkFragment.java | 12 +- .../schabi/newpipe/util/KioskTranslator.java | 15 +- .../schabi/newpipe/util/NavigationHelper.java | 16 ++ app/src/main/res/layout/drawer_layout.xml | 23 --- app/src/main/res/menu/drawer_items.xml | 14 +- 6 files changed, 168 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index a923c0c5d..b26d0a5d7 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -49,12 +49,15 @@ import android.widget.TextView; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.fragments.BackPressable; import org.schabi.newpipe.fragments.MainFragment; import org.schabi.newpipe.fragments.detail.VideoDetailFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment; import org.schabi.newpipe.report.ErrorActivity; +import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.Constants; +import org.schabi.newpipe.util.KioskTranslator; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PermissionHelper; import org.schabi.newpipe.util.ServiceHelper; @@ -88,24 +91,70 @@ public class MainActivity extends AppCompatActivity { } setSupportActionBar(findViewById(R.id.toolbar)); - setupDrawer(); + try { + setupDrawer(); + } catch (Exception e) { + ErrorActivity.reportUiError(this, e); + } } - private void setupDrawer() { + private void setupDrawer() throws Exception { final Toolbar toolbar = findViewById(R.id.toolbar); drawer = findViewById(R.id.drawer_layout); drawerItems = findViewById(R.id.navigation); + //Services + for(StreamingService s : NewPipe.getServices()) { final String title = s.getServiceInfo().getName() + (ServiceHelper.isBeta(s) ? " (beta)" : ""); - final MenuItem item = drawerItems.getMenu() - .add(R.id.menu_services_group, s.getServiceId(), 0, title); - item.setIcon(ServiceHelper.getIcon(s.getServiceId())); + + drawerItems.getMenu() + .add(R.id.menu_services_group, s.getServiceId(), 0, title) + .setIcon(ServiceHelper.getIcon(s.getServiceId())); } + //Tabs - drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); + int currentServiceId = ServiceHelper.getSelectedServiceId(this); + StreamingService service = NewPipe.getService(currentServiceId); + + int kioskId = 0; + + for (final String ks : service.getKioskList().getAvailableKiosks()) { + drawerItems.getMenu() + .add(R.id.menu_tabs_group, kioskId, 0, KioskTranslator.getTranslatedKioskName(ks, this)) + .setIcon(KioskTranslator.getKioskIcons(ks, this)); + kioskId ++; + } + + drawerItems.getMenu() + .add(R.id.menu_tabs_group, -1, 0, R.string.tab_subscriptions) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel)); + drawerItems.getMenu() + .add(R.id.menu_tabs_group, -2, 0, R.string.fragment_whats_new) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.rss)); + drawerItems.getMenu() + .add(R.id.menu_tabs_group, -3, 0, R.string.tab_bookmarks) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark)); + drawerItems.getMenu() + .add(R.id.menu_tabs_group, -4, 0, R.string.downloads) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download)); + drawerItems.getMenu() + .add(R.id.menu_tabs_group, -5, 0, R.string.action_history) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.history)); + + //Settings and About + + drawerItems.getMenu() + .add(R.id.menu_options_about_group, 0, 0, R.string.settings) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.settings)); + drawerItems.getMenu() + .add(R.id.menu_options_about_group, 1, 0, R.string.tab_about) + .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info)); + + + drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close); toggle.syncState(); @@ -126,34 +175,84 @@ public class MainActivity extends AppCompatActivity { } }); - drawerItems.setNavigationItemSelectedListener(this::changeService); - - setupDrawerFooter(); + drawerItems.setNavigationItemSelectedListener(this::drawerItemSelected); setupDrawerHeader(); } - private boolean changeService(MenuItem item) { - if (item.getGroupId() == R.id.menu_services_group) { - drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(false); - ServiceHelper.setSelectedServiceId(this, item.getItemId()); - drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); - } else { - return false; + private boolean drawerItemSelected(MenuItem item) { + switch (item.getGroupId()) { + case R.id.menu_services_group: + changeService(item); + break; + case R.id.menu_tabs_group: + try { + tabSelected(item); + } catch (Exception e) { + ErrorActivity.reportUiError(this, e); + } + break; + case R.id.menu_options_about_group: + optionsAboutSelected(item); + break; + default: + return false; } + drawer.closeDrawers(); return true; } - private void setupDrawerFooter() { - ImageButton settings = findViewById(R.id.drawer_settings); - ImageButton downloads = findViewById(R.id.drawer_downloads); - ImageButton history = findViewById(R.id.drawer_history); + private void changeService(MenuItem item) { + drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(false); + ServiceHelper.setSelectedServiceId(this, item.getItemId()); + drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); + } - settings.setOnClickListener(view -> NavigationHelper.openSettings(this)); - downloads.setOnClickListener(view ->NavigationHelper.openDownloads(this)); - history.setOnClickListener(view -> - NavigationHelper.openStatisticFragment(getSupportFragmentManager())); + private void tabSelected(MenuItem item) throws ExtractionException { + switch(item.getItemId()) { + case -1: + NavigationHelper.openSubscriptionFragment(getSupportFragmentManager()); + break; + case -2: + NavigationHelper.openWhatsNewFragment(getSupportFragmentManager()); + break; + case -3: + NavigationHelper.openBookmarksFragment(getSupportFragmentManager()); + break; + case -4: + NavigationHelper.openDownloads(this); + break; + case -5: + NavigationHelper.openStatisticFragment(getSupportFragmentManager()); + break; + default: + int currentServiceId = ServiceHelper.getSelectedServiceId(this); + StreamingService service = NewPipe.getService(currentServiceId); + String serviceName = ""; + + int kioskId = 0; + for (final String ks : service.getKioskList().getAvailableKiosks()) { + if(kioskId == item.getItemId()) { + serviceName = ks; + } + kioskId ++; + } + + NavigationHelper.openKioskFragment(getSupportFragmentManager(), currentServiceId, serviceName); + break; + } + } + + private void optionsAboutSelected(MenuItem item) { + switch(item.getItemId()) { + case 0: + NavigationHelper.openSettings(this); + break; + case 1: + NavigationHelper.openAbout(this); + break; + } } private void setupDrawerHeader() { diff --git a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java index f3f390c4d..5718e1620 100644 --- a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java @@ -99,9 +99,15 @@ public final class BookmarkFragment itemListAdapter.setSelectedListener(new OnClickGesture() { @Override public void selected(LocalItem selectedItem) { - // Requires the parent fragment to find holder for fragment replacement - if (getParentFragment() == null) return; - final FragmentManager fragmentManager = getParentFragment().getFragmentManager(); + + FragmentManager fragmentManager; + + if (getParentFragment() == null) + { + fragmentManager = getFragmentManager(); + } else { + fragmentManager = getParentFragment().getFragmentManager(); + } if (selectedItem instanceof PlaylistMetadataEntry) { final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem); diff --git a/app/src/main/java/org/schabi/newpipe/util/KioskTranslator.java b/app/src/main/java/org/schabi/newpipe/util/KioskTranslator.java index 4740b82e0..392892cef 100644 --- a/app/src/main/java/org/schabi/newpipe/util/KioskTranslator.java +++ b/app/src/main/java/org/schabi/newpipe/util/KioskTranslator.java @@ -24,7 +24,7 @@ import org.schabi.newpipe.R; public class KioskTranslator { public static String getTranslatedKioskName(String kioskId, Context c) { - switch(kioskId) { + switch (kioskId) { case "Trending": return c.getString(R.string.trending); case "Top 50": @@ -35,4 +35,17 @@ public class KioskTranslator { return kioskId; } } + + public static int getKioskIcons(String kioskId, Context c) { + switch(kioskId) { + case "Trending": + return ThemeHelper.resolveResourceIdFromAttr(c, R.attr.ic_hot); + case "Top 50": + return ThemeHelper.resolveResourceIdFromAttr(c, R.attr.ic_hot); + case "New & hot": + return ThemeHelper.resolveResourceIdFromAttr(c, R.attr.ic_hot); + default: + return 0; + } + } } diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index ebbeb06f8..7ed596926 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -33,12 +33,14 @@ import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.fragments.MainFragment; import org.schabi.newpipe.fragments.detail.VideoDetailFragment; import org.schabi.newpipe.fragments.list.channel.ChannelFragment; +import org.schabi.newpipe.local.bookmark.BookmarkFragment; import org.schabi.newpipe.local.feed.FeedFragment; import org.schabi.newpipe.fragments.list.kiosk.KioskFragment; import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment; import org.schabi.newpipe.local.history.StatisticsPlaylistFragment; import org.schabi.newpipe.local.playlist.LocalPlaylistFragment; +import org.schabi.newpipe.local.subscription.SubscriptionFragment; import org.schabi.newpipe.local.subscription.SubscriptionsImportFragment; import org.schabi.newpipe.player.BackgroundPlayer; import org.schabi.newpipe.player.BackgroundPlayerActivity; @@ -335,6 +337,20 @@ public class NavigationHelper { .commit(); } + public static void openBookmarksFragment(FragmentManager fragmentManager) { + defaultTransaction(fragmentManager) + .replace(R.id.fragment_holder, new BookmarkFragment()) + .addToBackStack(null) + .commit(); + } + + public static void openSubscriptionFragment(FragmentManager fragmentManager) { + defaultTransaction(fragmentManager) + .replace(R.id.fragment_holder, new SubscriptionFragment()) + .addToBackStack(null) + .commit(); + } + public static void openKioskFragment(FragmentManager fragmentManager, int serviceId, String kioskId) throws ExtractionException { defaultTransaction(fragmentManager) .replace(R.id.fragment_holder, KioskFragment.getInstance(serviceId, kioskId)) diff --git a/app/src/main/res/layout/drawer_layout.xml b/app/src/main/res/layout/drawer_layout.xml index c0186a02c..2eb3abe26 100644 --- a/app/src/main/res/layout/drawer_layout.xml +++ b/app/src/main/res/layout/drawer_layout.xml @@ -89,29 +89,6 @@ android:layout_alignEnd="@id/navigation" android:layout_alignParentBottom="true"> - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/drawer_items.xml b/app/src/main/res/menu/drawer_items.xml index ae4598edd..9d5e67bb4 100644 --- a/app/src/main/res/menu/drawer_items.xml +++ b/app/src/main/res/menu/drawer_items.xml @@ -2,13 +2,11 @@ - - + + + + \ No newline at end of file