From 5cb777148443178719df82993f3e8cdad9edadd4 Mon Sep 17 00:00:00 2001 From: Somethingweirdhere Date: Mon, 11 Jun 2018 01:55:14 +0200 Subject: [PATCH] Fixed bugs&crashes --- .../newpipe/fragments/MainFragment.java | 79 +++++++++++++------ .../list/channel/ChannelFragment.java | 13 ++- .../local/bookmark/BookmarkFragment.java | 7 +- .../newpipe/local/feed/FeedFragment.java | 15 +++- 4 files changed, 82 insertions(+), 32 deletions(-) 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 56a2862bb..80c69b44d 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -59,7 +59,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte private Bundle savedInstanceStateBundle; SharedPreferences.OnSharedPreferenceChangeListener listener = (prefs, key) -> { - if(key.equals("saveUsedTabs")||key.equals("service")) { + if(key.equals("service")||key.equals("saveUsedTabs")) { mainPageChanged(); } }; @@ -99,19 +99,66 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte protected void initViews(View rootView, Bundle savedInstanceState) { super.initViews(rootView, savedInstanceState); - getTabOrder(); - tabLayout = rootView.findViewById(R.id.main_tab_layout); viewPager = rootView.findViewById(R.id.pager); /* Nested fragment, use child fragment here to maintain backstack in view pager. */ adapter = new PagerAdapter(getChildFragmentManager()); viewPager.setAdapter(adapter); - viewPager.setOffscreenPageLimit(adapter.getCount()); tabLayout.setupWithViewPager(viewPager); + + mainPageChanged(); } + + public void mainPageChanged() { + getTabOrder(); + adapter.notifyDataSetChanged(); + viewPager.setOffscreenPageLimit(adapter.getCount()); + setIcons(); + } + + private void setIcons() { + for (int i = 0; i < tabs.size(); i++) { + String tabNumber = tabs.get(i); + + TabLayout.Tab tabToSet = tabLayout.getTabAt(i); + + if (tabToSet != null) { + + if (tabNumber.startsWith("1\t")) { + String kiosk[] = tabNumber.split("\t"); + if (kiosk.length == 2) { + try { + tabToSet.setIcon(KioskTranslator.getKioskIcons(kiosk[1], getContext())); + } catch (Exception e) { + //ignore this. It WILL be thrown while the service is changed. + } + } + } else if (tabNumber.startsWith("6\t")) { + tabToSet.setIcon(R.drawable.ic_channel_white_24dp); + + } else { + switch (tabNumber) { + case "0": + tabToSet.setIcon(R.drawable.ic_whatshot_white_24dp); + case "2": + tabToSet.setIcon(R.drawable.ic_channel_white_24dp); + case "3": + tabToSet.setIcon(R.drawable.ic_rss_feed_white_24dp); + case "4": + tabToSet.setIcon(R.drawable.ic_bookmark_white_24dp); + case "5": + tabToSet.setIcon(R.drawable.ic_history_white_24dp); + } + } + + } + } + } + + private void getTabOrder() { tabs.clear(); @@ -194,11 +241,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte public void onTabReselected(TabLayout.Tab tab) { } - public void mainPageChanged() { - getTabOrder(); - adapter.notifyDataSetChanged(); - } - private class PagerAdapter extends FragmentPagerAdapter { PagerAdapter(FragmentManager fm) { super(fm); @@ -213,8 +255,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte if(kiosk.length==2) { KioskFragment fragment = null; try { - tabLayout.getTabAt(position).setIcon(KioskTranslator.getKioskIcons(kiosk[1], getContext())); - fragment = KioskFragment.getInstance(currentServiceId, kiosk[1]); fragment.useAsFrontPage(true); return fragment; @@ -229,8 +269,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } else if(tabNumber.startsWith("6\t")) { String channelInfo[] = tabNumber.split("\t"); if(channelInfo.length==4) { - tabLayout.getTabAt(position).setIcon(R.drawable.ic_channel_white_24dp); - ChannelFragment fragment = ChannelFragment.getInstance(Integer.parseInt(channelInfo[3]), channelInfo[1], channelInfo[2]); fragment.useAsFrontPage(true); return fragment; @@ -240,30 +278,20 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } else { switch (tabNumber) { case "0": - tabLayout.getTabAt(position).setIcon(R.drawable.ic_whatshot_white_24dp); - return new BlankFragment(); case "2": - tabLayout.getTabAt(position).setIcon(R.drawable.ic_channel_white_24dp); - SubscriptionFragment sfragment = new SubscriptionFragment(); sfragment.useAsFrontPage(true); return sfragment; case "3": - tabLayout.getTabAt(position).setIcon(R.drawable.ic_rss_feed_white_24dp); - FeedFragment ffragment = new FeedFragment(); ffragment.useAsFrontPage(true); return ffragment; case "4": - tabLayout.getTabAt(position).setIcon(R.drawable.ic_bookmark_white_24dp); - BookmarkFragment bFragment = new BookmarkFragment(); bFragment.useAsFrontPage(true); return bFragment; case "5": - tabLayout.getTabAt(position).setIcon(R.drawable.ic_history_white_24dp); - StatisticsPlaylistFragment cFragment = new StatisticsPlaylistFragment(); cFragment.useAsFrontPage(true); return cFragment; @@ -284,5 +312,10 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte public int getCount() { return tabs.size(); } + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + getFragmentManager().beginTransaction().remove((Fragment)object).commitNowAllowingStateLoss(); + } } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java index 42ba8e0ac..4043cb719 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java @@ -91,6 +91,8 @@ public class ChannelFragment extends BaseListInfoFragment { private MenuItem menuRssButton; + private boolean mIsVisibleToUser = false; + public static ChannelFragment getInstance(int serviceId, String url, String name) { ChannelFragment instance = new ChannelFragment(); instance.setInitialData(serviceId, url, name); @@ -104,6 +106,7 @@ public class ChannelFragment extends BaseListInfoFragment { @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); + mIsVisibleToUser = isVisibleToUser; if(activity != null && useAsFrontPage && isVisibleToUser) { @@ -516,7 +519,13 @@ public class ChannelFragment extends BaseListInfoFragment { @Override public void setTitle(String title) { - super.setTitle(title); - headerTitleView.setText(title); + if(!useAsFrontPage) { + super.setTitle(title); + headerTitleView.setText(title); + } else { + if(mIsVisibleToUser) { + super.setTitle(title); + } + } } } 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 975028d0d..c1477f696 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 @@ -69,11 +69,10 @@ public final class BookmarkFragment public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { - if (activity != null && activity.getSupportActionBar() != null) { - activity.getSupportActionBar().setDisplayShowTitleEnabled(true); - activity.setTitle(R.string.tab_subscriptions); - } + if(!useAsFrontPage) { + setTitle(activity.getString(R.string.tab_bookmarks)); + } return inflater.inflate(R.layout.fragment_bookmarks, container, false); } diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java index 4937bb094..6da66f418 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java @@ -71,6 +71,10 @@ public class FeedFragment extends BaseListFragment, Voi @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + + if(!useAsFrontPage) { + setTitle(activity.getString(R.string.fragment_whats_new)); + } return inflater.inflate(R.layout.fragment_feed, container, false); } @@ -105,6 +109,14 @@ public class FeedFragment extends BaseListFragment, Voi super.onDestroyView(); } + @Override + public void setUserVisibleHint(boolean isVisibleToUser) { + super.setUserVisibleHint(isVisibleToUser); + if (activity != null && isVisibleToUser) { + setTitle(activity.getString(R.string.fragment_whats_new)); + } + } + /*@Override protected RecyclerView.LayoutManager getListLayoutManager() { boolean isPortrait = getResources().getDisplayMetrics().heightPixels > getResources().getDisplayMetrics().widthPixels; @@ -116,9 +128,6 @@ public class FeedFragment extends BaseListFragment, Voi super.onCreateOptionsMenu(menu, inflater); ActionBar supportActionBar = activity.getSupportActionBar(); - if (supportActionBar != null) { - supportActionBar.setTitle(R.string.fragment_whats_new); - } if(useAsFrontPage) { supportActionBar.setDisplayShowTitleEnabled(true);