コード例 #1
0
ファイル: Search.java プロジェクト: rigardin/Slide
  public void openPopup2() {

    final DialogInterface.OnClickListener l2 =
        new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            switch (i) {
              case 0:
                Reddit.search = SubmissionSearchPaginator.SearchSort.RELEVANCE;
                break;
              case 1:
                Reddit.search = SubmissionSearchPaginator.SearchSort.TOP;
                break;
              case 2:
                Reddit.search = SubmissionSearchPaginator.SearchSort.NEW;
                break;
              case 3:
                Reddit.search = SubmissionSearchPaginator.SearchSort.COMMENTS;
                break;
            }
            reloadSubs();
          }
        };
    AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(Search.this);
    builder.setTitle(R.string.sorting_choose);
    builder.setSingleChoiceItems(Reddit.getSearch(getBaseContext()), Reddit.getTypeSearch(), l2);
    builder.show();
  }
コード例 #2
0
 private static void openImage(Activity contextActivity, String submission) {
   if (SettingValues.image) {
     Intent myIntent = new Intent(contextActivity, FullscreenImage.class);
     myIntent.putExtra(FullscreenImage.EXTRA_URL, submission);
     contextActivity.startActivity(myIntent);
   } else {
     Reddit.defaultShare(submission, contextActivity);
   }
 }
コード例 #3
0
ファイル: MultiredditPosts.java プロジェクト: KonradIT/Slide
    @Override
    protected List<Submission> doInBackground(MultiReddit... subredditPaginators) {

      if (!NetworkUtil.isConnected(context)) {
        offline = true;
        return null;
      } else {
        offline = false;
      }

      stillShow = true;

      if (reset || paginator == null) {
        offline = false;
        paginator = new MultiRedditPaginator(Authentication.reddit, subredditPaginators[0]);
        paginator.setSorting(
            Reddit.getSorting("multi" + subredditPaginators[0].getDisplayName().toLowerCase()));
        paginator.setTimePeriod(
            Reddit.getTime("multi" + subredditPaginators[0].getDisplayName().toLowerCase()));

        LogUtil.v("Sorting is " + paginator.getSorting().name());
        paginator.setLimit(50);
      }

      List<Submission> things = new ArrayList<>();

      try {
        if (paginator != null && paginator.hasNext()) {
          things.addAll(paginator.next());
        } else {
          nomore = true;
        }

      } catch (Exception e) {
        e.printStackTrace();
        if (e.getMessage().contains("Forbidden")) {
          Reddit.authentication.updateToken(context);
        }
      }

      return things;
    }
コード例 #4
0
ファイル: Search.java プロジェクト: rigardin/Slide
  public void openPopup() {

    final DialogInterface.OnClickListener l2 =
        new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            switch (i) {
              case 0:
                time = TimePeriod.HOUR;
                break;
              case 1:
                time = TimePeriod.DAY;
                break;
              case 2:
                time = TimePeriod.WEEK;
                break;
              case 3:
                time = TimePeriod.MONTH;
                break;
              case 4:
                time = TimePeriod.YEAR;
                break;
              case 5:
                time = TimePeriod.ALL;
                break;
            }
            reloadSubs();
            getSupportActionBar()
                .setSubtitle(
                    Reddit.getSortingStringsSearch(getBaseContext())[
                        Reddit.getSortingIdSearch(Search.this)]);
          }
        };
    AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(Search.this);
    builder.setTitle(R.string.sorting_choose);
    builder.setSingleChoiceItems(
        Reddit.getSortingStringsSearch(getBaseContext()), Reddit.getSortingIdSearch(this), l2);
    builder.show();
  }
コード例 #5
0
 private static void openGif(final boolean gfy, Activity contextActivity, String submission) {
   if (SettingValues.gif) {
     Intent myIntent = new Intent(contextActivity, GifView.class);
     if (gfy) {
       myIntent.putExtra(GifView.EXTRA_URL, "gfy" + submission);
     } else {
       myIntent.putExtra(GifView.EXTRA_URL, "" + submission);
     }
     contextActivity.startActivity(myIntent);
     contextActivity.overridePendingTransition(R.anim.slideright, R.anim.fade_out);
   } else {
     Reddit.defaultShare(submission, contextActivity);
   }
 }
コード例 #6
0
  /**
   * Set the text for the corresponding views.
   *
   * @param blocks list of all blocks to be set
   * @param subreddit
   */
  public void setViews(List<String> blocks, String subreddit) {
    removeAllViews();

    Context context = getContext();

    if (!blocks.isEmpty()) {
      setVisibility(View.VISIBLE);
    }

    for (String block : blocks) {
      if (block.startsWith("<table>")) {
        HorizontalScrollView scrollView = new HorizontalScrollView(context);
        scrollView.setScrollbarFadingEnabled(false);
        TableLayout table = formatTable(block, subreddit);
        scrollView.setLayoutParams(MARGIN_PARAMS);
        table.setPaddingRelative(0, 0, 0, Reddit.pxToDp(10, context));
        scrollView.addView(table);
        addView(scrollView);
      } else if (block.startsWith("<pre>")) {
        HorizontalScrollView scrollView = new HorizontalScrollView(context);
        scrollView.setScrollbarFadingEnabled(false);
        SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context);
        newTextView.setTextHtml(block, subreddit);
        setStyle(newTextView, subreddit);
        scrollView.setLayoutParams(MARGIN_PARAMS);
        newTextView.setPaddingRelative(0, 0, 0, Reddit.pxToDp(10, context));
        scrollView.addView(newTextView);
        addView(scrollView);
      } else {
        SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context);
        newTextView.setTextHtml(block, subreddit);
        setStyle(newTextView, subreddit);
        newTextView.setLayoutParams(MARGIN_PARAMS);
        addView(newTextView);
      }
    }
  }
コード例 #7
0
ファイル: SubmissionsView.java プロジェクト: KonradIT/Slide
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final Context contextThemeWrapper =
        new ContextThemeWrapper(
            getActivity(), new ColorPreferences(inflater.getContext()).getThemeSubreddit(id));
    View v =
        ((LayoutInflater) contextThemeWrapper.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.fragment_verticalcontent, container, false);

    rv = ((RecyclerView) v.findViewById(R.id.vertical_content));
    final RecyclerView.LayoutManager mLayoutManager;
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
        && SettingValues.tabletUI) {
      mLayoutManager =
          new CatchStaggeredGridLayoutManager(
              Reddit.dpWidth, CatchStaggeredGridLayoutManager.VERTICAL);
    } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT
        && SettingValues.dualPortrait) {
      mLayoutManager =
          new CatchStaggeredGridLayoutManager(2, CatchStaggeredGridLayoutManager.VERTICAL);

    } else {
      mLayoutManager = new PreCachingLayoutManager(getActivity());
    }

    if (!(getActivity() instanceof SubredditView)) {
      v.findViewById(R.id.back).setBackground(null);
    }
    rv.setLayoutManager(mLayoutManager);
    rv.setItemAnimator(new SlideInUpAnimator(new AccelerateDecelerateInterpolator()));

    mSwipeRefreshLayout =
        (SwipeRefreshLayout) v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getContext()));

    // If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    // So, we just do 13% of the device screen height as a general estimate for the Tabs view type
    int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
    int headerOffset = Math.round((float) (screenHeight * 0.13));

    // if the view type is "single" (and therefore "commentPager"), we need a different offset
    if (SettingValues.single) {
      headerOffset = Math.round((float) (screenHeight * 0.07));
    }

    mSwipeRefreshLayout.setProgressViewOffset(
        false,
        headerOffset - Reddit.pxToDp(42, getContext()),
        headerOffset + Reddit.pxToDp(42, getContext()));

    if (SettingValues.fab) {
      fab = (FloatingActionButton) v.findViewById(R.id.post_floating_action_button);

      if (SettingValues.fabType == R.integer.FAB_POST) {
        fab.setImageResource(R.drawable.add);
        fab.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                Intent inte = new Intent(getActivity(), Submit.class);
                inte.putExtra(Submit.EXTRA_SUBREDDIT, id);
                getActivity().startActivity(inte);
              }
            });
      } else {
        fab.setImageResource(R.drawable.hide);
        fab.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                if (!Reddit.fabClear) {
                  new AlertDialogWrapper.Builder(getActivity())
                      .setTitle(R.string.settings_fabclear)
                      .setMessage(R.string.settings_fabclear_msg)
                      .setPositiveButton(
                          R.string.btn_ok,
                          new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                              Reddit.seen
                                  .edit()
                                  .putBoolean(SettingValues.PREF_FAB_CLEAR, true)
                                  .apply();
                              Reddit.fabClear = true;
                              clearSeenPosts(false);
                            }
                          })
                      .show();
                } else {
                  clearSeenPosts(false);
                }
              }
            });
        fab.setOnLongClickListener(
            new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
                if (!Reddit.fabClear) {
                  new AlertDialogWrapper.Builder(getActivity())
                      .setTitle(R.string.settings_fabclear)
                      .setMessage(R.string.settings_fabclear_msg)
                      .setPositiveButton(
                          R.string.btn_ok,
                          new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                              Reddit.seen
                                  .edit()
                                  .putBoolean(SettingValues.PREF_FAB_CLEAR, true)
                                  .apply();
                              Reddit.fabClear = true;
                              clearSeenPosts(true);
                            }
                          })
                      .show();
                } else {
                  clearSeenPosts(true);
                }
                /*
                ToDo Make a sncakbar with an undo option of the clear all
                View.OnClickListener undoAction = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        adapter.dataSet.posts = original;
                        for(Submission post : adapter.dataSet.posts){
                            if(HasSeen.getSeen(post.getFullName()))
                                Hidden.undoHidden(post);
                        }
                    }
                };*/
                Snackbar s =
                    Snackbar.make(
                        rv,
                        getResources().getString(R.string.posts_hidden_forever),
                        Snackbar.LENGTH_LONG);
                View view = s.getView();
                TextView tv =
                    (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(Color.WHITE);
                s.show();

                return false;
              }
            });
      }
    } else {
      v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    }

    rv.addOnScrollListener(
        new ToolbarScrollHideHandler(
            ((BaseActivity) getActivity()).mToolbar, getActivity().findViewById(R.id.header)) {
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (!posts.loading && !posts.nomore && !posts.offline) {

              visibleItemCount = rv.getLayoutManager().getChildCount();
              totalItemCount = rv.getLayoutManager().getItemCount();
              if (rv.getLayoutManager() instanceof PreCachingLayoutManager) {
                pastVisiblesItems =
                    ((PreCachingLayoutManager) rv.getLayoutManager())
                        .findFirstVisibleItemPosition();
                if (SettingValues.scrollSeen) {
                  if (pastVisiblesItems > 0) {
                    HasSeen.addSeen(posts.posts.get(pastVisiblesItems - 1).getFullName());
                  }
                }
              } else {
                int[] firstVisibleItems = null;
                firstVisibleItems =
                    ((CatchStaggeredGridLayoutManager) rv.getLayoutManager())
                        .findFirstVisibleItemPositions(firstVisibleItems);
                if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                  for (int i = 0; i < firstVisibleItems.length; i++) {
                    pastVisiblesItems = firstVisibleItems[i];
                    if (SettingValues.scrollSeen) {
                      if (pastVisiblesItems > 0) {
                        HasSeen.addSeen(posts.posts.get(pastVisiblesItems - 1).getFullName());
                      }
                    }
                  }
                }
              }

              if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                posts.loading = true;
                posts.loadMore(
                    mSwipeRefreshLayout.getContext(), SubmissionsView.this, false, posts.subreddit);
              }
            }

            /*
            if(dy <= 0 && !down){
                (getActivity()).findViewById(R.id.header).animate().translationY(((BaseActivity)getActivity()).mToolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();
                down = true;
            } else if(down){
                (getActivity()).findViewById(R.id.header).animate().translationY(((BaseActivity)getActivity()).mToolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();
                down = false;
            }*/
            // todo For future implementation instead of scrollFlags

            if (fab != null) {
              if (dy <= 0 && fab.getId() != 0 && SettingValues.fab) {

                fab.show();

              } else {
                fab.hide();
              }
            }
          }
        });

    Reddit.isLoading = false;
    if (MainActivity.shouldLoad == null
        || id == null
        || (MainActivity.shouldLoad != null && id != null && MainActivity.shouldLoad.equals(id))
        || !(getActivity() instanceof MainActivity)) {
      doAdapter();
    }
    return v;
  }
コード例 #8
0
    /**
     * @param url the url link (e.g. #s for some spoilers)
     * @param xOffset the last index of the url text (not the link)
     */
    public void onLinkClick(String url, int xOffset) {
      if (url == null) {
        return;
      }

      ContentType.ImageType type = ContentType.getImageType(url);
      switch (type) {
        case IMGUR:
          Intent intent2 = new Intent(c, Imgur.class);
          intent2.putExtra(Imgur.EXTRA_URL, url);
          c.startActivity(intent2);
          break;
        case NSFW_IMAGE:
          openImage(c, url);
          break;
        case NSFW_GIF:
          openGif(false, c, url);
          break;
        case NSFW_GFY:
          openGif(true, c, url);
          break;
        case REDDIT:
          new OpenRedditLink(c, url);
          break;
        case LINK:
        case IMAGE_LINK:
        case NSFW_LINK:
          CustomTabUtil.openUrl(url, Palette.getColor(subreddit), c);
          break;
        case SELF:
          break;

        case GFY:
          openGif(true, c, url);
          break;
        case ALBUM:
          if (SettingValues.album) {
            Intent i = new Intent(c, Album.class);
            i.putExtra(Album.EXTRA_URL, url);
            c.startActivity(i);
          } else {
            Reddit.defaultShare(url, c);
          }
          break;
        case IMAGE:
          openImage(c, url);
          break;
        case GIF:
          openGif(false, c, url);
          break;
        case NONE_GFY:
          openGif(true, c, url);
          break;
        case NONE_GIF:
          openGif(false, c, url);
          break;
        case NONE:
          break;
        case NONE_IMAGE:
          openImage(c, url);
          break;
        case NONE_URL:
          CustomTabUtil.openUrl(url, Palette.getColor(subreddit), c);
          break;
        case VIDEO:
          if (SettingValues.video) {
            Intent intent = new Intent(c, FullscreenVideo.class);
            intent.putExtra(FullscreenVideo.EXTRA_HTML, url);
            c.startActivity(intent);
          } else {
            Reddit.defaultShare(url, c);
          }
        case SPOILER:
          comm.spoilerClicked = true;

          setOrRemoveSpoilerSpans(comm, (Spannable) sequence, xOffset);
          break;
      }
    }
コード例 #9
0
ファイル: ImageFull.java プロジェクト: prashantkhurana/Slide
  private static void addClickFunctions(
      final View base,
      final View clickingArea,
      ContentType.ImageType type,
      final Activity contextActivity,
      final Submission submission) {
    if (!PostMatch.openExternal(submission.getUrl())) {

      switch (type) {
        case NSFW_IMAGE:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openImage(contextActivity, submission);
                }
              });
          break;
        case VID_ME:
        case STREAMABLE:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  if (SettingValues.video) {
                    Intent myIntent = new Intent(contextActivity, GifView.class);

                    myIntent.putExtra(GifView.EXTRA_STREAMABLE, submission.getUrl());
                    contextActivity.startActivity(myIntent);

                  } else {
                    Reddit.defaultShare(submission.getUrl(), contextActivity);
                  }
                }
              });
        case EMBEDDED:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  if (SettingValues.video) {
                    Reddit.defaultShare(submission.getUrl(), contextActivity);
                    String data =
                        submission.getDataNode().get("media_embed").get("content").asText();
                    {
                      Intent i = new Intent(contextActivity, FullscreenVideo.class);
                      i.putExtra(FullscreenVideo.EXTRA_HTML, data);
                      contextActivity.startActivity(i);
                    }
                  } else {
                    Reddit.defaultShare(submission.getUrl(), contextActivity);
                  }
                }
              });
          break;
        case NSFW_GIF:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openGif(false, contextActivity, submission);
                }
              });
          break;
        case NSFW_GFY:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openGif(true, contextActivity, submission);
                }
              });
          break;
        case REDDIT:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openRedditContent(
                      submission.getUrl(), contextActivity);
                }
              });
          break;
        case LINK:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  CustomTabUtil.openUrl(
                      submission.getUrl(),
                      Palette.getColor(submission.getSubredditName()),
                      contextActivity);
                }
              });
          break;
        case IMAGE_LINK:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  CustomTabUtil.openUrl(
                      submission.getUrl(),
                      Palette.getColor(submission.getSubredditName()),
                      contextActivity);
                }
              });
          break;
        case NSFW_LINK:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  CustomTabUtil.openUrl(
                      submission.getUrl(),
                      Palette.getColor(submission.getSubredditName()),
                      contextActivity);
                }
              });
          break;
        case SELF:
          break;
        case GFY:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openGif(true, contextActivity, submission);
                }
              });
          break;
        case ALBUM:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {

                  if (SettingValues.album) {
                    if (SettingValues.albumSwipe) {
                      Intent i = new Intent(contextActivity, AlbumPager.class);
                      i.putExtra(Album.EXTRA_URL, submission.getUrl());
                      contextActivity.startActivity(i);
                      contextActivity.overridePendingTransition(R.anim.slideright, R.anim.fade_out);
                    } else {
                      Intent i = new Intent(contextActivity, Album.class);
                      i.putExtra(Album.EXTRA_URL, submission.getUrl());
                      contextActivity.startActivity(i);
                      contextActivity.overridePendingTransition(R.anim.slideright, R.anim.fade_out);
                    }

                  } else {
                    Reddit.defaultShare(submission.getUrl(), contextActivity);
                  }
                }
              });
          break;
        case DEVIANTART:
        case IMAGE:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openImage(contextActivity, submission);
                }
              });
          break;
        case GIF:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openGif(false, contextActivity, submission);
                }
              });
          break;
        case NONE_GFY:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openGif(true, contextActivity, submission);
                }
              });
          break;
        case NONE_GIF:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openGif(false, contextActivity, submission);
                }
              });
          break;

        case NONE:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  CustomTabUtil.openUrl(
                      submission.getUrl(),
                      Palette.getColor(submission.getSubredditName()),
                      contextActivity);
                }
              });
          break;
        case NONE_IMAGE:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View v2) {
                  PopulateSubmissionViewHolder.openImage(contextActivity, submission);
                }
              });
          break;
        case NONE_URL:
          base.setOnClickListener(
              new View.OnClickListener() {

                @Override
                public void onClick(View v2) {
                  CustomTabUtil.openUrl(
                      submission.getUrl(),
                      Palette.getColor(submission.getSubredditName()),
                      contextActivity);
                }
              });
          break;
        case VIDEO:
          base.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  Reddit.defaultShare(submission.getUrl(), contextActivity);
                }
              });
      }
    } else {
      Reddit.defaultShare(submission.getUrl(), contextActivity);
    }
  }
コード例 #10
0
ファイル: Search.java プロジェクト: rigardin/Slide
  @Override
  public void onCreate(Bundle savedInstanceState) {
    overrideRedditSwipeAnywhere();

    super.onCreate(savedInstanceState);
    applyColorTheme("");
    setContentView(R.layout.activity_saved);
    where = getIntent().getExtras().getString(EXTRA_TERM, "");
    subreddit = getIntent().getExtras().getString(EXTRA_SUBREDDIT, "");
    setupSubredditAppBar(R.id.toolbar, "Search", true, subreddit.toLowerCase());

    Log.v(LogUtil.getTag(), "Searching for " + where + " in " + subreddit);

    time = TimePeriod.ALL;

    getSupportActionBar().setTitle(where);
    getSupportActionBar()
        .setSubtitle(
            Reddit.getSortingStringsSearch(getBaseContext())[Reddit.getSortingIdSearch(this)]);
    final RecyclerView rv = ((RecyclerView) findViewById(R.id.vertical_content));
    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE
        || !SettingValues.tabletUI) {
      final PreCachingLayoutManager mLayoutManager;
      mLayoutManager = new PreCachingLayoutManager(this);
      rv.setLayoutManager(mLayoutManager);
    } else {
      final StaggeredGridLayoutManager mLayoutManager;
      mLayoutManager =
          new StaggeredGridLayoutManager(Reddit.dpWidth, StaggeredGridLayoutManager.VERTICAL);
      rv.setLayoutManager(mLayoutManager);
    }
    rv.addOnScrollListener(
        new RecyclerView.OnScrollListener() {
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            visibleItemCount = rv.getLayoutManager().getChildCount();
            totalItemCount = rv.getLayoutManager().getItemCount();
            if (rv.getLayoutManager() instanceof PreCachingLayoutManager) {
              pastVisiblesItems =
                  ((PreCachingLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
            } else {
              int[] firstVisibleItems = null;
              firstVisibleItems =
                  ((StaggeredGridLayoutManager) rv.getLayoutManager())
                      .findFirstVisibleItemPositions(firstVisibleItems);
              if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                pastVisiblesItems = firstVisibleItems[0];
              }
            }

            if (!posts.loading) {
              if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                posts.loading = true;
                posts.loadMore(adapter, subreddit, where, false);
              }
            }
          }
        });
    SwipeRefreshLayout mSwipeRefreshLayout =
        (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
    TypedValue typed_value = new TypedValue();
    getTheme()
        .resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, typed_value, true);
    mSwipeRefreshLayout.setProgressViewOffset(
        false, 0, getResources().getDimensionPixelSize(typed_value.resourceId));

    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(subreddit, this));

    mSwipeRefreshLayout.setRefreshing(true);
    posts = new SubredditSearchPosts(subreddit, where.toLowerCase(), this);
    adapter = new ContributionAdapter(this, posts, rv);
    rv.setAdapter(adapter);

    try {
      posts.bindAdapter(adapter, mSwipeRefreshLayout);
    } catch (ExecutionException | InterruptedException e) {
      e.printStackTrace();
    }
    // TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(
        new SwipeRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
            posts.loadMore(adapter, subreddit, where, true);

            // TODO catch errors
          }
        });
  }