예제 #1
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);
      }
    }
  }
예제 #2
0
  @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;
  }