From 072e27ed2742574cd083016ac4721564711a3374 Mon Sep 17 00:00:00 2001 From: Somethingweirdhere Date: Sat, 16 Jun 2018 13:11:45 +0200 Subject: [PATCH] Code cleanup --- .../java/org/schabi/newpipe/MainActivity.java | 56 +++++++++++-------- .../newpipe/fragments/MainFragment.java | 52 +++++++++-------- .../subscription/SubscriptionFragment.java | 41 ++++---------- 3 files changed, 74 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index c4385a065..e2874709b 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -87,6 +87,16 @@ public class MainActivity extends AppCompatActivity { private boolean servicesShown = false; private ImageView serviceArrow; + private static final int ITEM_ID_SUBSCRIPTIONS = - 1; + private static final int ITEM_ID_FEED = - 2; + private static final int ITEM_ID_BOOKMARKS = - 3; + private static final int ITEM_ID_DOWNLOADS = - 4; + private static final int ITEM_ID_HISTORY = - 5; + private static final int ITEM_ID_SETTINGS = 0; + private static final int ITEM_ID_ABOUT = 1; + + private static final int ORDER = 0; + /*////////////////////////////////////////////////////////////////////////// // Activity's LifeCycle //////////////////////////////////////////////////////////////////////////*/ @@ -136,27 +146,27 @@ public class MainActivity extends AppCompatActivity { } drawerItems.getMenu() - .add(R.id.menu_tabs_group, -1, 0, R.string.tab_subscriptions) + .add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER, 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) + .add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, 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) + .add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, 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) + .add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads) .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download)); drawerItems.getMenu() - .add(R.id.menu_tabs_group, -5, 0, R.string.action_history) + .add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, 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) + .add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, 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) + .add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about) .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info)); toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close); @@ -216,19 +226,19 @@ public class MainActivity extends AppCompatActivity { private void tabSelected(MenuItem item) throws ExtractionException { switch(item.getItemId()) { - case -1: + case ITEM_ID_SUBSCRIPTIONS: NavigationHelper.openSubscriptionFragment(getSupportFragmentManager()); break; - case -2: + case ITEM_ID_FEED: NavigationHelper.openWhatsNewFragment(getSupportFragmentManager()); break; - case -3: + case ITEM_ID_BOOKMARKS: NavigationHelper.openBookmarksFragment(getSupportFragmentManager()); break; - case -4: + case ITEM_ID_DOWNLOADS: NavigationHelper.openDownloads(this); break; - case -5: + case ITEM_ID_HISTORY: NavigationHelper.openStatisticFragment(getSupportFragmentManager()); break; default: @@ -251,10 +261,10 @@ public class MainActivity extends AppCompatActivity { private void optionsAboutSelected(MenuItem item) { switch(item.getItemId()) { - case 0: + case ITEM_ID_SETTINGS: NavigationHelper.openSettings(this); break; - case 1: + case ITEM_ID_ABOUT: NavigationHelper.openAbout(this); break; } @@ -298,7 +308,7 @@ public class MainActivity extends AppCompatActivity { (ServiceHelper.isBeta(s) ? " (beta)" : ""); drawerItems.getMenu() - .add(R.id.menu_services_group, s.getServiceId(), 0, title) + .add(R.id.menu_services_group, s.getServiceId(), ORDER, title) .setIcon(ServiceHelper.getIcon(s.getServiceId())); } drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); @@ -315,33 +325,33 @@ public class MainActivity extends AppCompatActivity { for (final String ks : service.getKioskList().getAvailableKiosks()) { drawerItems.getMenu() - .add(R.id.menu_tabs_group, kioskId, 0, KioskTranslator.getTranslatedKioskName(ks, this)) + .add(R.id.menu_tabs_group, kioskId, ORDER, KioskTranslator.getTranslatedKioskName(ks, this)) .setIcon(KioskTranslator.getKioskIcons(ks, this)); kioskId ++; } drawerItems.getMenu() - .add(R.id.menu_tabs_group, -1, 0, R.string.tab_subscriptions) + .add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER, 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) + .add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, 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) + .add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, 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) + .add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads) .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download)); drawerItems.getMenu() - .add(R.id.menu_tabs_group, -5, 0, R.string.action_history) + .add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, 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) + .add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, 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) + .add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about) .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info)); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java index 4e8b7ac9e..3d76f09a0 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -60,6 +60,14 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte private SharedPreferences prefs; private Bundle savedInstanceStateBundle; + private static final String TAB_NUMBER_BLANK = "0"; + private static final String TAB_NUMBER_KIOSK = "1"; + private static final String TAB_NUMBER_SUBSCIRPTIONS = "2"; + private static final String TAB_NUMBER_FEED = "3"; + private static final String TAB_NUMBER_BOOKMARKS = "4"; + private static final String TAB_NUMBER_HISTORY = "5"; + private static final String TAB_NUMBER_CHANNEL = "6"; + SharedPreferences.OnSharedPreferenceChangeListener listener = (prefs, key) -> { if(key.equals("service")||key.equals("saveUsedTabs")) { mainPageChanged(); @@ -127,7 +135,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte if((tabs.size()>0)&&activity != null) { String tabNumber = tabs.get(0); - if (tabNumber.startsWith("1\t")) { + if (tabNumber.startsWith(TAB_NUMBER_KIOSK + "\t")) { String kiosk[] = tabNumber.split("\t"); if (kiosk.length == 2) { try { @@ -136,7 +144,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte //ignore this. It WILL be thrown while the service is changed. } } - } else if (tabNumber.startsWith("6\t")) { + } else if (tabNumber.startsWith(TAB_NUMBER_CHANNEL + "\t")) { String channelInfo[] = tabNumber.split("\t"); if(channelInfo.length==4) { @@ -144,19 +152,19 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } } else { switch (tabNumber) { - case "0": + case TAB_NUMBER_BLANK: setTitle(getString(R.string.app_name)); break; - case "2": + case TAB_NUMBER_SUBSCIRPTIONS: setTitle(getString(R.string.tab_subscriptions)); break; - case "3": + case TAB_NUMBER_FEED: setTitle(getString(R.string.fragment_whats_new)); break; - case "4": + case TAB_NUMBER_BOOKMARKS: setTitle(getString(R.string.tab_bookmarks)); break; - case "5": + case TAB_NUMBER_HISTORY: setTitle(getString(R.string.title_activity_history)); break; } @@ -175,7 +183,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte if (tabToSet != null && c != null) { - if (tabNumber.startsWith("1\t")) { + if (tabNumber.startsWith(TAB_NUMBER_KIOSK + "\t")) { String kiosk[] = tabNumber.split("\t"); if (kiosk.length == 2) { try { @@ -184,23 +192,23 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte //ignore this. It WILL be thrown while the service is changed. } } - } else if (tabNumber.startsWith("6\t")) { + } else if (tabNumber.startsWith(TAB_NUMBER_CHANNEL + "\t")) { tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_channel)); } else { switch (tabNumber) { - case "0": + case TAB_NUMBER_BLANK: tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_hot)); break; - case "2": + case TAB_NUMBER_SUBSCIRPTIONS: tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_channel)); break; - case "3": + case TAB_NUMBER_FEED: tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.rss)); break; - case "4": + case TAB_NUMBER_BOOKMARKS: tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_bookmark)); break; - case "5": + case TAB_NUMBER_HISTORY: tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.history)); break; } @@ -231,7 +239,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } for(String tabNumber:tabsArray) { - if(tabNumber.equals("1")) { + if(tabNumber.equals(TAB_NUMBER_KIOSK)) { if (kl != null) { for(String ks : kl.getAvailableKiosks()) { tabs.add(tabNumber+"\t"+ks); @@ -302,7 +310,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte public Fragment getItem(int position) { String tabNumber = tabs.get(position); - if(tabNumber.startsWith("1\t")) { + if(tabNumber.startsWith(TAB_NUMBER_KIOSK + "\t")) { String kiosk[] = tabNumber.split("\t"); if(kiosk.length==2) { KioskFragment fragment = null; @@ -318,7 +326,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte "none", "", R.string.app_ui_crash)); } } - } else if(tabNumber.startsWith("6\t")) { + } else if(tabNumber.startsWith(TAB_NUMBER_CHANNEL + "\t")) { String channelInfo[] = tabNumber.split("\t"); if(channelInfo.length==4) { ChannelFragment fragment = ChannelFragment.getInstance(Integer.parseInt(channelInfo[3]), channelInfo[1], channelInfo[2]); @@ -329,21 +337,21 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } } else { switch (tabNumber) { - case "0": + case TAB_NUMBER_BLANK: return new BlankFragment(); - case "2": + case TAB_NUMBER_SUBSCIRPTIONS: SubscriptionFragment sFragment = new SubscriptionFragment(); sFragment.useAsFrontPage(true); return sFragment; - case "3": + case TAB_NUMBER_FEED: FeedFragment fFragment = new FeedFragment(); fFragment.useAsFrontPage(true); return fFragment; - case "4": + case TAB_NUMBER_BOOKMARKS: BookmarkFragment bFragment = new BookmarkFragment(); bFragment.useAsFrontPage(true); return bFragment; - case "5": + case TAB_NUMBER_HISTORY: StatisticsPlaylistFragment cFragment = new StatisticsPlaylistFragment(); cFragment.useAsFrontPage(true); return cFragment; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java index bbd1150c4..9e658c8ce 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java @@ -242,14 +242,7 @@ public class SubscriptionFragment extends BaseStateFragment() { @Override public void selected(ChannelInfoItem selectedItem) { - try { - final FragmentManager fragmentManager = getParentFragment() == null - ? getFragmentManager() - : getParentFragment().getFragmentManager(); - - // Requires the parent fragment to find holder for fragment replacement - NavigationHelper.openChannelFragment(fragmentManager, - selectedItem.getServiceId(), - selectedItem.getUrl(), - selectedItem.getName()); - } catch (Exception e) { - ErrorActivity.reportUiError((AppCompatActivity) getActivity(), e); - } - + FragmentManager fragmentManager = getFM(); + NavigationHelper.openChannelFragment(fragmentManager, + selectedItem.getServiceId(), selectedItem.getUrl(), selectedItem.getName()); } }); //noinspection ConstantConditions whatsNewItemListHeader.setOnClickListener(v -> { - FragmentManager fragmentManager; - - if (getParentFragment() == null) - { - fragmentManager = getFragmentManager(); - } else { - fragmentManager = getParentFragment().getFragmentManager(); - } + FragmentManager fragmentManager = getFM(); NavigationHelper.openWhatsNewFragment(fragmentManager); }); importExportListHeader.setOnClickListener(v -> importExportOptions.switchState()); } + private FragmentManager getFM() { + return getParentFragment() == null + ? getFragmentManager() + : getParentFragment().getFragmentManager(); + } + private void resetFragment() { if (disposables != null) disposables.clear(); if (infoListAdapter != null) infoListAdapter.clearStreamItemList();