content: package com.alium.orin.ui.fragments.mainactivity.library;
  
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.annotation.Nullable;
  import android.support.design.widget.AppBarLayout;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.view.ViewPager;
  import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.SubMenu;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.afollestad.materialcab.MaterialCab;
  import com.alium.orin.dialogs.CreatePlaylistDialog;
  import com.alium.orin.helper.MusicPlayerRemote;
  import com.alium.orin.interfaces.CabHolder;
  import com.alium.orin.ui.activities.MainActivity;
  import com.alium.orin.ui.activities.SearchActivity;
  import com.alium.orin.util.NavigationUtil;
  import com.alium.orin.util.PhonographColorUtil;
  import com.alium.orin.util.PreferenceUtil;
  import com.kabouzeid.appthemehelper.ThemeStore;
  import com.kabouzeid.appthemehelper.common.ATHToolbarActivity;
  import com.kabouzeid.appthemehelper.util.TabLayoutUtil;
  import com.kabouzeid.appthemehelper.util.ToolbarContentTintHelper;
  import com.alium.orin.R;
  import com.alium.orin.adapter.MusicLibraryPagerAdapter;
  import com.alium.orin.dialogs.SleepTimerDialog;
  import com.alium.orin.loader.SongLoader;
  import com.alium.orin.ui.fragments.mainactivity.AbsMainActivityFragment;
  import com.alium.orin.ui.fragments.mainactivity.library.pager.AbsLibraryPagerRecyclerViewCustomGridSizeFragment;
  import com.alium.orin.util.Util;
  
  import butterknife.BindView;
  import butterknife.ButterKnife;
  import butterknife.Unbinder;
  
  public class LibraryFragment extends AbsMainActivityFragment implements CabHolder, MainActivity.MainActivityFragmentCallbacks, ViewPager.OnPageChangeListener {
      public static final String TAG = LibraryFragment.class.getSimpleName();
  
      private Unbinder unbinder;
  
      @BindView(R.id.toolbar)
      Toolbar toolbar;
      @BindView(R.id.tabs)
      TabLayout tabs;
      @BindView(R.id.appbar)
      AppBarLayout appbar;
      @BindView(R.id.pager)
      ViewPager pager;
  
      private MusicLibraryPagerAdapter pagerAdapter;
      private MaterialCab cab;
  
      public static LibraryFragment newInstance() {
          return new LibraryFragment();
      }
  
      public LibraryFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_library, container, false);
          unbinder = ButterKnife.bind(this, view);
          return view;
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          pager.removeOnPageChangeListener(this);
          unbinder.unbind();
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          setStatusbarColorAuto(view);
          getMainActivity().setNavigationbarColorAuto();
          getMainActivity().setTaskDescriptionColorAuto();
  
          setUpToolbar();
          setUpViewPager();
      }
  
      private void setUpToolbar() {
          int primaryColor = ThemeStore.primaryColor(getActivity());
          appbar.setBackgroundColor(primaryColor);
          toolbar.setBackgroundColor(primaryColor);
          toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
          getActivity().setTitle(R.string.app_name);
          getMainActivity().setSupportActionBar(toolbar);
      }
  
      private void setUpViewPager() {
          pagerAdapter = new MusicLibraryPagerAdapter(getActivity(), getChildFragmentManager());
          pager.setAdapter(pagerAdapter);
          pager.setOffscreenPageLimit(pagerAdapter.getCount() - 1);
  
          tabs.setupWithViewPager(pager);
  
          int primaryColor = ThemeStore.primaryColor(getActivity());
          int normalColor = ToolbarContentTintHelper.toolbarSubtitleColor(getActivity(), primaryColor);
          int selectedColor = ToolbarContentTintHelper.toolbarTitleColor(getActivity(), primaryColor);
          TabLayoutUtil.setTabIconColors(tabs, normalColor, selectedColor);
          tabs.setTabTextColors(normalColor, selectedColor);
          tabs.setSelectedTabIndicatorColor(selectedColor); //ThemeStore.accentColor(getActivity()) ... former value, me no like
  
  
  
          int startPosition = PreferenceUtil.getInstance(getActivity()).getDefaultStartPage();
          startPosition = startPosition == -1 ? PreferenceUtil.getInstance(getActivity()).getLastPage() : startPosition;
          pager.setCurrentItem(startPosition);
          PreferenceUtil.getInstance(getActivity()).setLastPage(startPosition); // just in case
          pager.addOnPageChangeListener(this);
      }
  
      public Fragment getCurrentFragment() {
          return pagerAdapter.getFragment(pager.getCurrentItem());
      }
  
      private boolean isPlaylistPage() {
          return pager.getCurrentItem() == MusicLibraryPagerAdapter.MusicFragments.PLAYLIST.ordinal();
      }
  
      @NonNull
      @Override
      public MaterialCab openCab(final int menuRes, final MaterialCab.Callback callback) {
          if (cab != null && cab.isActive()) cab.finish();
          cab = new MaterialCab(getMainActivity(), R.id.cab_stub)
                  .setMenu(menuRes)
                  .setCloseDrawableRes(R.drawable.ic_close_white_24dp)
                  .setBackgroundColor(PhonographColorUtil.shiftBackgroundColorForLightText(ThemeStore.primaryColor(getActivity())))
                  .start(callback);
          return cab;
      }
  
      public void addOnAppBarOffsetChangedListener(AppBarLayout.OnOffsetChangedListener onOffsetChangedListener) {
          appbar.addOnOffsetChangedListener(onOffsetChangedListener);
      }
  
      public void removeOnAppBarOffsetChangedListener(AppBarLayout.OnOffsetChangedListener onOffsetChangedListener) {
          appbar.removeOnOffsetChangedListener(onOffsetChangedListener);
      }
  
      public int getTotalAppBarScrollingRange() {
          return appbar.getTotalScrollRange();
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          super.onCreateOptionsMenu(menu, inflater);
          if (pager == null) return;
          inflater.inflate(R.menu.menu_main, menu);
          if (isPlaylistPage()) {
              menu.add(0, R.id.action_new_playlist, 0, R.string.new_playlist_title);
          }
          Fragment currentFragment = getCurrentFragment();
          if (currentFragment instanceof AbsLibraryPagerRecyclerViewCustomGridSizeFragment && currentFragment.isAdded()) {
              AbsLibraryPagerRecyclerViewCustomGridSizeFragment absLibraryRecyclerViewCustomGridSizeFragment = (AbsLibraryPagerRecyclerViewCustomGridSizeFragment) currentFragment;
  
              menu.findItem(R.id.action_colored_footers).setChecked(absLibraryRecyclerViewCustomGridSizeFragment.usePalette());
              menu.findItem(R.id.action_colored_footers).setEnabled(absLibraryRecyclerViewCustomGridSizeFragment.canUsePalette());
          } else {
              menu.removeItem(R.id.action_colored_footers);
          }
          Activity activity = getActivity();
          if (activity == null) return;
          ToolbarContentTintHelper.handleOnCreateOptionsMenu(getActivity(), toolbar, menu, ATHToolbarActivity.getToolbarBackgroundColor(toolbar));
      }
  
      @Override
      public void onPrepareOptionsMenu(Menu menu) {
          super.onPrepareOptionsMenu(menu);
          Activity activity = getActivity();
          if (activity == null) return;
          ToolbarContentTintHelper.handleOnPrepareOptionsMenu(activity, toolbar);
      }
  
      @Override
      public boolean onOptionsItemSelected(@NonNull MenuItem item) {
          if (pager == null) return false;
          Fragment currentFragment = getCurrentFragment();
          if (currentFragment instanceof AbsLibraryPagerRecyclerViewCustomGridSizeFragment) {
              AbsLibraryPagerRecyclerViewCustomGridSizeFragment absLibraryRecyclerViewCustomGridSizeFragment = (AbsLibraryPagerRecyclerViewCustomGridSizeFragment) currentFragment;
              if (item.getItemId() == R.id.action_colored_footers) {
                  item.setChecked(!item.isChecked());
                  absLibraryRecyclerViewCustomGridSizeFragment.setAndSaveUsePalette(item.isChecked());
                  return true;
              }
          }
  
          int id = item.getItemId();
          switch (id) {
              case R.id.action_sleep_timer:
                  new SleepTimerDialog().show(getChildFragmentManager(), "SET_SLEEP_TIMER");
                  return true;
              case R.id.action_equalizer:
                  NavigationUtil.openEqualizer(getActivity());
                  return true;
              case R.id.action_shuffle_all:
                  MusicPlayerRemote.openAndShuffleQueue(SongLoader.getAllSongs(getActivity()), true);
                  return true;
              case R.id.action_new_playlist:
                  CreatePlaylistDialog.create().show(getChildFragmentManager(), "CREATE_PLAYLIST");
                  return true;
              case R.id.action_search:
                  startActivity(new Intent(getActivity(), SearchActivity.class));
                  return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public boolean handleBackPress() {
          if (cab != null && cab.isActive()) {
              cab.finish();
              return true;
          }
          return false;
      }
  
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      }
  
      @Override
      public void onPageSelected(int position) {
          PreferenceUtil.getInstance(getActivity()).setLastPage(position);
      }
  
      @Override
      public void onPageScrollStateChanged(int state) {
      }
  }
  