From 45519fd6c4523f80c636f2f18214ee36442d38be 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 | 36 ++++-------- 3 files changed, 75 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 599e66391..ac6eb0806 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -85,6 +85,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 //////////////////////////////////////////////////////////////////////////*/ @@ -134,27 +144,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, @@ -227,19 +237,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: @@ -262,10 +272,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; } @@ -309,7 +319,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); @@ -326,33 +336,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 b7783edce..55fbe833f 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -59,6 +59,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(); @@ -126,7 +134,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 { @@ -135,7 +143,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) { @@ -143,19 +151,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; } @@ -174,7 +182,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 { @@ -183,23 +191,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; } @@ -230,7 +238,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); @@ -294,7 +302,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; @@ -310,7 +318,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]); @@ -321,21 +329,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 83bf849c5..185122af3 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 @@ -240,14 +240,7 @@ public class SubscriptionFragment extends BaseStateFragment() { @Override public void selected(ChannelInfoItem selectedItem) { - FragmentManager fragmentManager; - - if (getParentFragment() == null) - { - fragmentManager = getFragmentManager(); - } else { - fragmentManager = getParentFragment().getFragmentManager(); - } + FragmentManager fragmentManager = getFM(); NavigationHelper.openChannelFragment(fragmentManager, selectedItem.getServiceId(), selectedItem.getUrl(), selectedItem.getName()); } @@ -342,19 +328,21 @@ public class SubscriptionFragment extends BaseStateFragment { - 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() { + if (getParentFragment() == null) + { + return getFragmentManager(); + } else { + return getParentFragment().getFragmentManager(); + } + } + private void resetFragment() { if (disposables != null) disposables.clear(); if (infoListAdapter != null) infoListAdapter.clearStreamItemList();