Search in sources :

Example 16 with MainActivity

use of me.ccrama.redditslide.Activities.MainActivity in project Slide by ccrama.

the class CommentPage method doAdapter.

public void doAdapter(boolean load) {
    commentSorting = SettingValues.getCommentSorting(subreddit);
    if (load)
        doRefresh(true);
    if (load)
        loaded = true;
    if (!single && getActivity() instanceof CommentsScreen && ((CommentsScreen) getActivity()).subredditPosts != null && Authentication.didOnline && ((CommentsScreen) getActivity()).currentPosts != null && ((CommentsScreen) getActivity()).currentPosts.size() > page) {
        try {
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
        } catch (IndexOutOfBoundsException e) {
            return;
        }
        Submission s = ((CommentsScreen) getActivity()).currentPosts.get(page);
        if (s != null && s.getDataNode().has("suggested_sort") && !s.getDataNode().get("suggested_sort").asText().equalsIgnoreCase("null")) {
            String sorting = s.getDataNode().get("suggested_sort").asText().toUpperCase();
            sorting = sorting.replace("İ", "I");
            commentSorting = CommentSort.valueOf(sorting);
        } else if (s != null) {
            commentSorting = SettingValues.getCommentSorting(s.getSubredditName());
        }
        if (load)
            comments.setSorting(commentSorting);
        if (adapter == null) {
            adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
            rv.setAdapter(adapter);
        }
    } else if (getActivity() instanceof MainActivity) {
        if (Authentication.didOnline) {
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
            Submission s = ((MainActivity) getActivity()).openingComments;
            if (s != null && s.getDataNode().has("suggested_sort") && !s.getDataNode().get("suggested_sort").asText().equalsIgnoreCase("null")) {
                String sorting = s.getDataNode().get("suggested_sort").asText().toUpperCase();
                sorting = sorting.replace("İ", "I");
                commentSorting = CommentSort.valueOf(sorting);
            } else if (s != null) {
                commentSorting = SettingValues.getCommentSorting(s.getSubredditName());
            }
            if (load)
                comments.setSorting(commentSorting);
            if (adapter == null) {
                adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                rv.setAdapter(adapter);
            }
        } else {
            Submission s = ((MainActivity) getActivity()).openingComments;
            doRefresh(false);
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout, s);
            if (adapter == null) {
                adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                rv.setAdapter(adapter);
            }
        }
    } else {
        Submission s = null;
        try {
            s = OfflineSubreddit.getSubmissionFromStorage(fullname.contains("_") ? fullname : "t3_" + fullname, getContext(), !NetworkUtil.isConnected(getActivity()), new ObjectMapper().reader());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (s != null && s.getComments() != null) {
            doRefresh(false);
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout, s);
            if (adapter == null) {
                adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                rv.setAdapter(adapter);
            }
        } else if (context.isEmpty()) {
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
            comments.setSorting(commentSorting);
            if (adapter == null) {
                if (s != null) {
                    adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                }
                rv.setAdapter(adapter);
            }
        } else {
            if (context.equals(Reddit.EMPTY_STRING)) {
                comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
                if (load)
                    comments.setSorting(commentSorting);
            } else {
                comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout, context, contextNumber);
                if (load)
                    comments.setSorting(commentSorting);
            }
        }
    }
}
Also used : CommentsScreen(me.ccrama.redditslide.Activities.CommentsScreen) SubmissionComments(me.ccrama.redditslide.Adapters.SubmissionComments) Submission(net.dean.jraw.models.Submission) MainActivity(me.ccrama.redditslide.Activities.MainActivity) IOException(java.io.IOException) CommentAdapter(me.ccrama.redditslide.Adapters.CommentAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with MainActivity

use of me.ccrama.redditslide.Activities.MainActivity in project Slide by ccrama.

the class CommentPage method doData.

public void doData(Boolean b) {
    if (adapter == null || single) {
        adapter = new CommentAdapter(this, comments, rv, comments.submission, getFragmentManager());
        rv.setAdapter(adapter);
        adapter.currentSelectedItem = context;
        if (context.isEmpty()) {
            if (SettingValues.collapseCommentsDefault) {
                adapter.collapseAll();
            }
        }
        adapter.reset(getContext(), comments, rv, comments.submission, b);
    } else if (!b) {
        try {
            adapter.reset(getContext(), comments, rv, (getActivity() instanceof MainActivity) ? ((MainActivity) getActivity()).openingComments : comments.submission, b);
            if (SettingValues.collapseCommentsDefault) {
                adapter.collapseAll();
            }
        } catch (Exception ignored) {
        }
    } else {
        adapter.reset(getContext(), comments, rv, comments.submission, b);
        if (SettingValues.collapseCommentsDefault) {
            adapter.collapseAll();
        }
        adapter.notifyItemChanged(1);
    }
}
Also used : MainActivity(me.ccrama.redditslide.Activities.MainActivity) CommentAdapter(me.ccrama.redditslide.Adapters.CommentAdapter) IOException(java.io.IOException) ApiException(net.dean.jraw.ApiException) ActivityNotFoundException(android.content.ActivityNotFoundException) NetworkException(net.dean.jraw.http.NetworkException)

Example 18 with MainActivity

use of me.ccrama.redditslide.Activities.MainActivity in project Slide by ccrama.

the class NewsView method updateError.

@Override
public void updateError() {
    if (getActivity() instanceof MainActivity) {
        if (((MainActivity) getActivity()).runAfterLoad != null) {
            new Handler().post(((MainActivity) getActivity()).runAfterLoad);
        }
    }
    mSwipeRefreshLayout.setRefreshing(false);
    adapter.setError(true);
}
Also used : Handler(android.os.Handler) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) MainActivity(me.ccrama.redditslide.Activities.MainActivity)

Example 19 with MainActivity

use of me.ccrama.redditslide.Activities.MainActivity in project Slide by ccrama.

the class NewsView method updateSuccess.

@Override
public void updateSuccess(final List<Submission> submissions, final int startIndex) {
    if (getActivity() != null) {
        if (getActivity() instanceof MainActivity) {
            if (((MainActivity) getActivity()).runAfterLoad != null) {
                new Handler().post(((MainActivity) getActivity()).runAfterLoad);
            }
        }
        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mSwipeRefreshLayout != null) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
                if (startIndex != -1 && !forced) {
                    adapter.notifyItemRangeInserted(startIndex + 1, posts.posts.size());
                    adapter.notifyDataSetChanged();
                } else {
                    forced = false;
                    rv.scrollToPosition(0);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        if (MainActivity.isRestart) {
            MainActivity.isRestart = false;
            posts.offline = false;
            rv.getLayoutManager().scrollToPosition(MainActivity.restartPage + 1);
        }
        if (startIndex < 10)
            resetScroll();
    }
}
Also used : Handler(android.os.Handler) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) MainActivity(me.ccrama.redditslide.Activities.MainActivity)

Example 20 with MainActivity

use of me.ccrama.redditslide.Activities.MainActivity in project Slide by ccrama.

the class NewsView method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), new ColorPreferences(inflater.getContext()).getThemeSubreddit(id));
    final View v = ((LayoutInflater) contextThemeWrapper.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.fragment_verticalcontent, container, false);
    if (getActivity() instanceof MainActivity) {
        v.findViewById(R.id.back).setBackgroundResource(0);
    }
    rv = v.findViewById(R.id.vertical_content);
    rv.setHasFixedSize(true);
    final RecyclerView.LayoutManager mLayoutManager;
    mLayoutManager = createLayoutManager(getNumColumns(getResources().getConfiguration().orientation, getActivity()));
    if (!(getActivity() instanceof SubredditView)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            v.findViewById(R.id.back).setBackground(null);
        } else {
            v.findViewById(R.id.back).setBackgroundDrawable(null);
        }
    }
    rv.setLayoutManager(mLayoutManager);
    rv.setItemAnimator(new SlideUpAlphaAnimator());
    rv.getLayoutManager().scrollToPosition(0);
    mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getContext()));
    /**
     * If using List view mode, we need to remove the start margin from the SwipeRefreshLayout.
     * The scrollbar style of "outsideInset" creates a 4dp padding around it. To counter this,
     * change the scrollbar style to "insideOverlay" when list view is enabled.
     * To recap: this removes the margins from the start/end so list view is full-width.
     */
    if (SettingValues.defaultCardView == CreateCardView.CardEnum.LIST) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            params.setMarginStart(0);
        } else {
            MarginLayoutParamsCompat.setMarginStart(params, 0);
        }
        rv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mSwipeRefreshLayout.setLayoutParams(params);
    }
    /**
     * If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
     * So, we estimate the height of the header in dp.
     * If the view type is "single" (and therefore "commentPager"), we need a different offset
     */
    final int HEADER_OFFSET = (SettingValues.single || getActivity() instanceof SubredditView) ? Constants.SINGLE_HEADER_VIEW_OFFSET : Constants.TAB_HEADER_VIEW_OFFSET;
    mSwipeRefreshLayout.setProgressViewOffset(false, HEADER_OFFSET - Constants.PTR_OFFSET_TOP, HEADER_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    if (SettingValues.fab) {
        fab = v.findViewById(R.id.post_floating_action_button);
        if (SettingValues.fabType == Constants.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.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                                Reddit.fabClear = true;
                                clearSeenPosts(false);
                            }
                        }).show();
                    } else {
                        clearSeenPosts(false);
                    }
                }
            });
            final Handler handler = new Handler();
            fab.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    detector.onTouchEvent(event);
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        origY = event.getY();
                        handler.postDelayed(mLongPressRunnable, android.view.ViewConfiguration.getLongPressTimeout());
                    }
                    if (((event.getAction() == MotionEvent.ACTION_MOVE) && Math.abs(event.getY() - origY) > fab.getHeight() / 2) || (event.getAction() == MotionEvent.ACTION_UP)) {
                        handler.removeCallbacks(mLongPressRunnable);
                    }
                    return false;
                }
            });
            mLongPressRunnable = new Runnable() {

                public void run() {
                    fab.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                    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.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                                Reddit.fabClear = true;
                                clearSeenPosts(true);
                            }
                        }).show();
                    } else {
                        clearSeenPosts(true);
                    }
                    Snackbar s = Snackbar.make(rv, getResources().getString(R.string.posts_hidden_forever), Snackbar.LENGTH_LONG);
                    /*Todo a way to unhide
                        s.setAction(R.string.btn_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                            }
                        });*/
                    View view = s.getView();
                    TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                    tv.setTextColor(Color.WHITE);
                    s.show();
                }
            };
        }
    } else {
        v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    }
    if (fab != null)
        fab.show();
    header = getActivity().findViewById(R.id.header);
    // TODO, have it so that if the user clicks anywhere in the rv to hide and cancel GoToSubreddit?
    // final TextInputEditText GO_TO_SUB_FIELD = (TextInputEditText) getActivity().findViewById(R.id.toolbar_search);
    // final Toolbar TOOLBAR = ((Toolbar) getActivity().findViewById(R.id.toolbar));
    // final String PREV_TITLE = TOOLBAR.getTitle().toString();
    // final ImageView CLOSE_BUTTON = (ImageView) getActivity().findViewById(R.id.close);
    // 
    // rv.setOnTouchListener(new View.OnTouchListener() {
    // @Override
    // public boolean onTouch(View v, MotionEvent event) {
    // System.out.println("touched");
    // InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    // imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    // 
    // GO_TO_SUB_FIELD.setText("");
    // GO_TO_SUB_FIELD.setVisibility(View.GONE);
    // CLOSE_BUTTON.setVisibility(View.GONE);
    // TOOLBAR.setTitle(PREV_TITLE);
    // 
    // return false;
    // }
    // });
    resetScroll();
    Reddit.isLoading = false;
    if (MainActivity.shouldLoad == null || id == null || (MainActivity.shouldLoad != null && MainActivity.shouldLoad.equals(id)) || !(getActivity() instanceof MainActivity)) {
        doAdapter();
    }
    return v;
}
Also used : DialogInterface(android.content.DialogInterface) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TextView(android.widget.TextView) Context(android.content.Context) ColorPreferences(me.ccrama.redditslide.ColorPreferences) Handler(android.os.Handler) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) RecyclerView(android.support.v7.widget.RecyclerView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) MotionEvent(android.view.MotionEvent) ContextThemeWrapper(android.support.v7.view.ContextThemeWrapper) LayoutInflater(android.view.LayoutInflater) RelativeLayout(android.widget.RelativeLayout) Submit(me.ccrama.redditslide.Activities.Submit) RecyclerView(android.support.v7.widget.RecyclerView) Snackbar(android.support.design.widget.Snackbar)

Aggregations

MainActivity (me.ccrama.redditslide.Activities.MainActivity)23 RecyclerView (android.support.v7.widget.RecyclerView)12 TextView (android.widget.TextView)12 SubredditView (me.ccrama.redditslide.Activities.SubredditView)12 Handler (android.os.Handler)11 View (android.view.View)11 DialogInterface (android.content.DialogInterface)10 Intent (android.content.Intent)10 Snackbar (android.support.design.widget.Snackbar)10 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)10 ToolbarScrollHideHandler (me.ccrama.redditslide.handler.ToolbarScrollHideHandler)10 CreateCardView (me.ccrama.redditslide.Views.CreateCardView)9 ImageView (android.widget.ImageView)7 SubmissionsView (me.ccrama.redditslide.Fragments.SubmissionsView)7 ApiException (net.dean.jraw.ApiException)7 MediaView (me.ccrama.redditslide.Activities.MediaView)6 OnSingleClickListener (me.ccrama.redditslide.util.OnSingleClickListener)5 Submission (net.dean.jraw.models.Submission)5 Context (android.content.Context)4 LayoutInflater (android.view.LayoutInflater)4