package net.simonvt.cathode.ui.fragment;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import net.simonvt.cathode.CathodeApp;
import net.simonvt.cathode.R;
import net.simonvt.cathode.provider.CathodeContract;
import net.simonvt.cathode.ui.LibraryType;
import net.simonvt.cathode.ui.ShowsNavigationListener;
import net.simonvt.cathode.ui.adapter.ShowDescriptionAdapter;

public class TrendingShowsFragment extends AbsAdapterFragment
    implements LoaderManager.LoaderCallbacks<Cursor> {

  private static final String TAG = "TrendingShowsFragment";

  private static final int LOADER_TRENDING = 601;

  private ShowDescriptionAdapter showsAdapter;

  private ShowsNavigationListener navigationListener;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
      navigationListener = (ShowsNavigationListener) activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString() + " must implement ShowsNavigationListener");
    }
  }

  @Override
  public void onCreate(Bundle state) {
    super.onCreate(state);
    CathodeApp.inject(getActivity(), this);

    setHasOptionsMenu(true);

    getLoaderManager().initLoader(LOADER_TRENDING, null, this);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_shows_watched, container, false);
  }

  @Override
  protected void onItemClick(AdapterView l, View v, int position, long id) {
    Cursor c = (Cursor) getAdapter().getItem(position);
    navigationListener.onDisplayShow(id, c.getString(c.getColumnIndex(CathodeContract.Shows.TITLE)),
        LibraryType.WATCHED);
  }

  private void setCursor(Cursor cursor) {
    if (showsAdapter == null) {
      showsAdapter = new ShowDescriptionAdapter(getActivity());
      setAdapter(showsAdapter);
    }

    showsAdapter.changeCursor(cursor);
  }

  @Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    final Uri contentUri = CathodeContract.Shows.SHOWS_TRENDING;
    CursorLoader cl =
        new CursorLoader(getActivity(), contentUri, ShowDescriptionAdapter.PROJECTION, null, null,
            null);
    cl.setUpdateThrottle(2 * DateUtils.SECOND_IN_MILLIS);
    return cl;
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    setCursor(data);
  }

  @Override
  public void onLoaderReset(Loader<Cursor> loader) {
  }
}

@@ -351,24 +351,114 @@
 utInflater;%0A
+import android.view.Menu;%0Aimport android.view.MenuInflater;%0Aimport android.view.MenuItem;%0A
 import andro
@@ -467,24 +467,24 @@
 .view.View;%0A
-
 import andro
@@ -533,24 +533,52 @@
 dapterView;%0A
+import javax.inject.Inject;%0A
 import net.s
@@ -683,24 +683,123 @@
 deContract;%0A
+import net.simonvt.cathode.remote.TraktTaskQueue;%0Aimport net.simonvt.cathode.remote.sync.SyncTask;%0A
 import net.s
@@ -1281,16 +1281,49 @@
 tener;%0A%0A
+  @Inject TraktTaskQueue queue;%0A%0A
   @Overr
@@ -1960,24 +1960,24 @@
 nceState) %7B%0A
-
     return i
@@ -2041,32 +2041,530 @@
 r, false);%0A  %7D%0A%0A
+  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) %7B%0A    inflater.inflate(R.menu.fragment_shows, menu);%0A  %7D%0A%0A  @Override%0A  public boolean onOptionsItemSelected(MenuItem item) %7B%0A    switch (item.getItemId()) %7B%0A      case R.id.menu_refresh:%0A        queue.add(new SyncTask());%0A        return true;%0A%0A      case R.id.menu_search:%0A        navigationListener.onStartShowSearch();%0A        return true;%0A%0A      default:%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A  %7D%0A%0A
   @Override%0A  pr

Add refresh and search to TrendingShowsFragment.