content: package com.alium.orin.ui.fragments.mainactivity.library.pager;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v4.app.LoaderManager;
  import android.support.v4.content.Loader;
  import android.support.v7.widget.LinearLayoutManager;
  
  import com.alium.orin.interfaces.LoaderIds;
  import com.alium.orin.misc.WrappedAsyncTaskLoader;
  import com.alium.orin.model.Playlist;
  import com.alium.orin.model.smartplaylist.HistoryPlaylist;
  import com.alium.orin.model.smartplaylist.LastAddedPlaylist;
  import com.alium.orin.model.smartplaylist.MyTopTracksPlaylist;
  import com.alium.orin.R;
  import com.alium.orin.adapter.PlaylistAdapter;
  import com.alium.orin.loader.PlaylistLoader;
  
  import java.util.ArrayList;
  
  /**
   * @author Karim Abou Zeid (kabouzeid)
   */
  public class PlaylistsFragment extends AbsLibraryPagerRecyclerViewFragment<PlaylistAdapter, LinearLayoutManager> implements LoaderManager.LoaderCallbacks<ArrayList<Playlist>> {
  
      public static final String TAG = PlaylistsFragment.class.getSimpleName();
  
      private static final int LOADER_ID = LoaderIds.PLAYLISTS_FRAGMENT;
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          getLoaderManager().initLoader(LOADER_ID, null, this);
      }
  
      @NonNull
      @Override
      protected LinearLayoutManager createLayoutManager() {
          return new LinearLayoutManager(getActivity());
      }
  
      @NonNull
      @Override
      protected PlaylistAdapter createAdapter() {
          ArrayList<Playlist> dataSet = getAdapter() == null ? new ArrayList<Playlist>() : getAdapter().getDataSet();
          return new PlaylistAdapter(getLibraryFragment().getMainActivity(), dataSet, R.layout.item_list_single_row, getLibraryFragment());
      }
  
      @Override
      protected int getEmptyMessage() {
          return R.string.no_playlists;
      }
  
      @Override
      public void onMediaStoreChanged() {
          getLoaderManager().restartLoader(LOADER_ID, null, this);
      }
  
      @Override
      public Loader<ArrayList<Playlist>> onCreateLoader(int id, Bundle args) {
          return new AsyncPlaylistLoader(getActivity());
      }
  
      @Override
      public void onLoadFinished(Loader<ArrayList<Playlist>> loader, ArrayList<Playlist> data) {
          getAdapter().swapDataSet(data);
      }
  
      @Override
      public void onLoaderReset(Loader<ArrayList<Playlist>> loader) {
          getAdapter().swapDataSet(new ArrayList<Playlist>());
      }
  
      private static class AsyncPlaylistLoader extends WrappedAsyncTaskLoader<ArrayList<Playlist>> {
          public AsyncPlaylistLoader(Context context) {
              super(context);
          }
  
          private static ArrayList<Playlist> getAllPlaylists(Context context) {
              ArrayList<Playlist> playlists = new ArrayList<>();
  
              playlists.add(new LastAddedPlaylist(context));
              playlists.add(new HistoryPlaylist(context));
              playlists.add(new MyTopTracksPlaylist(context));
  
              playlists.addAll(PlaylistLoader.getAllPlaylists(context));
  
              return playlists;
          }
  
          @Override
          public ArrayList<Playlist> loadInBackground() {
              return getAllPlaylists(getContext());
          }
      }
  }