Search in sources :

Example 1 with MainActivity

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

the class SubmissionAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder2, final int pos) {
    int i = pos != 0 ? pos - 1 : pos;
    if (holder2 instanceof SubmissionViewHolder) {
        final SubmissionViewHolder holder = (SubmissionViewHolder) holder2;
        final Submission submission = dataSet.posts.get(i);
        CreateCardView.colorCard(submission.getSubredditName().toLowerCase(Locale.ENGLISH), holder.itemView, subreddit, (subreddit.equals("frontpage") || subreddit.equals("mod") || subreddit.equals("friends") || (subreddit.equals("all")) || subreddit.contains(".") || subreddit.contains("+")));
        holder.itemView.setOnClickListener(new OnSingleClickListener() {

            @Override
            public void onSingleClick(View v) {
                if (Authentication.didOnline || submission.getComments() != null) {
                    holder.title.setAlpha(0.54f);
                    holder.body.setAlpha(0.54f);
                    if (context instanceof MainActivity) {
                        final MainActivity a = (MainActivity) context;
                        if (a.singleMode && a.commentPager && a.adapter instanceof MainActivity.OverviewPagerAdapterComment) {
                            if (a.openingComments != submission) {
                                clicked = holder2.getAdapterPosition();
                                a.openingComments = submission;
                                a.toOpenComments = a.pager.getCurrentItem() + 1;
                                a.currentComment = holder.getAdapterPosition() - 1;
                                ((MainActivity.OverviewPagerAdapterComment) (a).adapter).storedFragment = (a).adapter.getCurrentFragment();
                                ((MainActivity.OverviewPagerAdapterComment) (a).adapter).size = a.toOpenComments + 1;
                                try {
                                    a.adapter.notifyDataSetChanged();
                                } catch (Exception ignored) {
                                }
                            }
                            a.pager.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    a.pager.setCurrentItem(a.pager.getCurrentItem() + 1, true);
                                }
                            }, 400);
                        } else {
                            Intent i2 = new Intent(context, CommentsScreen.class);
                            i2.putExtra(CommentsScreen.EXTRA_PAGE, holder2.getAdapterPosition() - 1);
                            i2.putExtra(CommentsScreen.EXTRA_SUBREDDIT, subreddit);
                            i2.putExtra("fullname", submission.getFullName());
                            context.startActivityForResult(i2, 940);
                            clicked = holder2.getAdapterPosition();
                        }
                    } else if (context instanceof SubredditView) {
                        final SubredditView a = (SubredditView) context;
                        if (a.singleMode && a.commentPager) {
                            if (a.openingComments != submission) {
                                clicked = holder2.getAdapterPosition();
                                a.openingComments = submission;
                                a.currentComment = holder.getAdapterPosition() - 1;
                                ((SubredditView.OverviewPagerAdapterComment) (a).adapter).storedFragment = (a).adapter.getCurrentFragment();
                                ((SubredditView.OverviewPagerAdapterComment) a.adapter).size = 3;
                                a.adapter.notifyDataSetChanged();
                            }
                            a.pager.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    a.pager.setCurrentItem(a.pager.getCurrentItem() + 1, true);
                                }
                            }, 400);
                        } else {
                            Intent i2 = new Intent(context, CommentsScreen.class);
                            i2.putExtra(CommentsScreen.EXTRA_PAGE, holder2.getAdapterPosition() - 1);
                            i2.putExtra(CommentsScreen.EXTRA_SUBREDDIT, subreddit);
                            i2.putExtra("fullname", submission.getFullName());
                            context.startActivityForResult(i2, 940);
                            clicked = holder2.getAdapterPosition();
                        }
                    }
                } else {
                    if (!Reddit.appRestart.contains("offlinepopup")) {
                        new AlertDialogWrapper.Builder(context).setTitle(R.string.cache_no_comments_found).setMessage(R.string.cache_no_comments_found_message).setCancelable(false).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Reddit.appRestart.edit().putString("offlinepopup", "").apply();
                            }
                        }).show();
                    } else {
                        Snackbar s = Snackbar.make(holder.itemView, R.string.cache_no_comments_found_snackbar, Snackbar.LENGTH_SHORT);
                        s.setAction(R.string.misc_more_info, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                new AlertDialogWrapper.Builder(context).setTitle(R.string.cache_no_comments_found).setMessage(R.string.cache_no_comments_found_message).setCancelable(false).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Reddit.appRestart.edit().putString("offlinepopup", "").apply();
                                    }
                                }).show();
                            }
                        });
                        View view = s.getView();
                        TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                        tv.setTextColor(Color.WHITE);
                        s.show();
                    }
                }
            }
        });
        new PopulateSubmissionViewHolder().populateSubmissionViewHolder(holder, submission, context, false, false, dataSet.posts, listView, custom, dataSet.offline, dataSet.subreddit.toLowerCase(Locale.ENGLISH), null);
    }
    if (holder2 instanceof SubmissionFooterViewHolder) {
        Handler handler = new Handler();
        final Runnable r = new Runnable() {

            public void run() {
                notifyItemChanged(dataSet.posts.size() + // the loading spinner to replaced by nomoreposts.xml
                1);
            }
        };
        handler.post(r);
        if (holder2.itemView.findViewById(R.id.reload) != null) {
            holder2.itemView.findViewById(R.id.reload).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((SubmissionsView) displayer).forceRefresh();
                }
            });
        }
    }
    if (holder2 instanceof SpacerViewHolder) {
        View header = (context).findViewById(R.id.header);
        int height = header.getHeight();
        if (height == 0) {
            header.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            height = header.getMeasuredHeight();
            holder2.itemView.findViewById(R.id.height).setLayoutParams(new LinearLayout.LayoutParams(holder2.itemView.getWidth(), height));
            if (listView.getLayoutManager() instanceof CatchStaggeredGridLayoutManager) {
                CatchStaggeredGridLayoutManager.LayoutParams layoutParams = new CatchStaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                layoutParams.setFullSpan(true);
                holder2.itemView.setLayoutParams(layoutParams);
            }
        } else {
            holder2.itemView.findViewById(R.id.height).setLayoutParams(new LinearLayout.LayoutParams(holder2.itemView.getWidth(), height));
            if (listView.getLayoutManager() instanceof CatchStaggeredGridLayoutManager) {
                CatchStaggeredGridLayoutManager.LayoutParams layoutParams = new CatchStaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                layoutParams.setFullSpan(true);
                holder2.itemView.setLayoutParams(layoutParams);
            }
        }
    }
}
Also used : CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) DialogInterface(android.content.DialogInterface) OnSingleClickListener(me.ccrama.redditslide.util.OnSingleClickListener) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TextView(android.widget.TextView) PopulateSubmissionViewHolder(me.ccrama.redditslide.SubmissionViews.PopulateSubmissionViewHolder) CommentsScreen(me.ccrama.redditslide.Activities.CommentsScreen) Submission(net.dean.jraw.models.Submission) PopulateSubmissionViewHolder(me.ccrama.redditslide.SubmissionViews.PopulateSubmissionViewHolder) Handler(android.os.Handler) Intent(android.content.Intent) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) LinearLayout(android.widget.LinearLayout) Snackbar(android.support.design.widget.Snackbar)

Example 2 with MainActivity

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

the class NewsView method updateOfflineError.

@Override
public void updateOfflineError() {
    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 3 with MainActivity

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

the class NewsView method resetScroll.

public void resetScroll() {
    if (toolbarScroll == null) {
        toolbarScroll = new ToolbarScrollHideHandler(((BaseActivity) getActivity()).mToolbar, header) {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!posts.loading && !posts.nomore && !posts.offline && !adapter.isError) {
                    visibleItemCount = rv.getLayoutManager().getChildCount();
                    totalItemCount = rv.getLayoutManager().getItemCount();
                    int[] firstVisibleItems;
                    firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(null);
                    if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                        for (int firstVisibleItem : firstVisibleItems) {
                            pastVisiblesItems = firstVisibleItem;
                            if (SettingValues.scrollSeen && pastVisiblesItems > 0 && SettingValues.storeHistory) {
                                HasSeen.addSeenScrolling(posts.posts.get(pastVisiblesItems - 1).getFullName());
                            }
                        }
                    }
                    if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                        posts.loading = true;
                        posts.loadMore(mSwipeRefreshLayout.getContext(), NewsView.this, false, posts.subreddit);
                    }
                }
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) {
                    diff += dy;
                } else {
                    diff = 0;
                }
                if (fab != null) {
                    if (dy <= 0 && fab.getId() != 0 && SettingValues.fab) {
                        if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING || diff < -fab.getHeight() * 2) {
                            fab.show();
                        }
                    } else {
                        fab.hide();
                    }
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                // switch (newState) {
                // case RecyclerView.SCROLL_STATE_IDLE:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_DRAGGING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_SETTLING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().pause();
                // break;
                // }
                super.onScrollStateChanged(recyclerView, newState);
                // If the toolbar search is open, and the user scrolls in the Main view--close the search UI
                if (getActivity() instanceof MainActivity && (SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_TOOLBAR || SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_BOTH) && ((MainActivity) getContext()).findViewById(R.id.toolbar_search).getVisibility() == View.VISIBLE) {
                    ((MainActivity) getContext()).findViewById(R.id.close_search_toolbar).performClick();
                }
            }
        };
        rv.addOnScrollListener(toolbarScroll);
    } else {
        toolbarScroll.reset = true;
    }
}
Also used : CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) BaseActivity(me.ccrama.redditslide.Activities.BaseActivity) RecyclerView(android.support.v7.widget.RecyclerView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

Example 4 with MainActivity

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

the class Reddit method setDefaultErrorHandler.

public static void setDefaultErrorHandler(Context base) {
    // START code adapted from https://github.com/QuantumBadger/RedReader/
    final Thread.UncaughtExceptionHandler androidHandler = Thread.getDefaultUncaughtExceptionHandler();
    final WeakReference<Context> cont = new WeakReference<>(base);
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(Thread thread, Throwable t) {
            if (cont.get() != null) {
                final Context c = cont.get();
                Writer writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                t.printStackTrace(printWriter);
                String stacktrace = writer.toString().replace(";", ",");
                if (stacktrace.contains("UnknownHostException") || stacktrace.contains("SocketTimeoutException") || stacktrace.contains("ConnectException")) {
                    // is offline
                    final Handler mHandler = new Handler(Looper.getMainLooper());
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                new AlertDialogWrapper.Builder(c).setTitle(R.string.err_title).setMessage(R.string.err_connection_failed_msg).setNegativeButton(R.string.btn_close, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (!(c instanceof MainActivity)) {
                                            ((Activity) c).finish();
                                        }
                                    }
                                }).setPositiveButton(R.string.btn_offline, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Reddit.appRestart.edit().putBoolean("forceoffline", true).apply();
                                        Reddit.forceRestart(c);
                                    }
                                }).show();
                            } catch (Exception ignored) {
                            }
                        }
                    });
                } else if (stacktrace.contains("403 Forbidden") || stacktrace.contains("401 Unauthorized")) {
                    // Un-authenticated
                    final Handler mHandler = new Handler(Looper.getMainLooper());
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                new AlertDialogWrapper.Builder(c).setTitle(R.string.err_title).setMessage(R.string.err_refused_request_msg).setNegativeButton("No", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (!(c instanceof MainActivity)) {
                                            ((Activity) c).finish();
                                        }
                                    }
                                }).setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        authentication.updateToken((c));
                                    }
                                }).show();
                            } catch (Exception ignored) {
                            }
                        }
                    });
                } else if (stacktrace.contains("404 Not Found") || stacktrace.contains("400 Bad Request")) {
                    final Handler mHandler = new Handler(Looper.getMainLooper());
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                new AlertDialogWrapper.Builder(c).setTitle(R.string.err_title).setMessage(R.string.err_could_not_find_content_msg).setNegativeButton("Close", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (!(c instanceof MainActivity)) {
                                            ((Activity) c).finish();
                                        }
                                    }
                                }).show();
                            } catch (Exception ignored) {
                            }
                        }
                    });
                } else if (t instanceof NetworkException) {
                    Toast.makeText(c, "Error " + ((NetworkException) t).getResponse().getStatusMessage() + ": " + (t).getMessage(), Toast.LENGTH_LONG).show();
                } else if (t instanceof NullPointerException && t.getMessage().contains("Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference")) {
                    t.printStackTrace();
                } else if (t instanceof MaterialDialog.DialogException) {
                    t.printStackTrace();
                } else if (t instanceof IllegalArgumentException && t.getMessage().contains("pointerIndex out of range")) {
                    t.printStackTrace();
                } else {
                    appRestart.edit().putString("startScreen", "a").apply();
                    try {
                        SharedPreferences prefs = c.getSharedPreferences("STACKTRACE", Context.MODE_PRIVATE);
                        prefs.edit().putString("stacktrace", stacktrace).apply();
                    } catch (Throwable ignored) {
                    }
                    androidHandler.uncaughtException(thread, t);
                }
            } else {
                androidHandler.uncaughtException(thread, t);
            }
        }
    });
// END adaptation
}
Also used : DialogInterface(android.content.DialogInterface) MainActivity(me.ccrama.redditslide.Activities.MainActivity) Activity(android.app.Activity) SpannableString(android.text.SpannableString) MainActivity(me.ccrama.redditslide.Activities.MainActivity) StringWriter(java.io.StringWriter) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) WeakReference(java.lang.ref.WeakReference) NetworkException(net.dean.jraw.http.NetworkException) PrintWriter(java.io.PrintWriter) Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) Handler(android.os.Handler) UnknownHostException(java.net.UnknownHostException) NetworkException(net.dean.jraw.http.NetworkException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter)

Example 5 with MainActivity

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

the class SubmissionsView method resetScroll.

public void resetScroll() {
    if (toolbarScroll == null) {
        toolbarScroll = new ToolbarScrollHideHandler(((BaseActivity) getActivity()).mToolbar, header) {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!posts.loading && !posts.nomore && !posts.offline && !adapter.isError) {
                    visibleItemCount = rv.getLayoutManager().getChildCount();
                    totalItemCount = rv.getLayoutManager().getItemCount();
                    int[] firstVisibleItems;
                    firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(null);
                    if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                        for (int firstVisibleItem : firstVisibleItems) {
                            pastVisiblesItems = firstVisibleItem;
                            if (SettingValues.scrollSeen && pastVisiblesItems > 0 && SettingValues.storeHistory) {
                                HasSeen.addSeenScrolling(posts.posts.get(pastVisiblesItems - 1).getFullName());
                            }
                        }
                    }
                    if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount) {
                        posts.loading = true;
                        posts.loadMore(mSwipeRefreshLayout.getContext(), SubmissionsView.this, false, posts.subreddit);
                    }
                }
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) {
                    diff += dy;
                } else {
                    diff = 0;
                }
                if (fab != null) {
                    if (dy <= 0 && fab.getId() != 0 && SettingValues.fab) {
                        if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING || diff < -fab.getHeight() * 2) {
                            fab.show();
                        }
                    } else {
                        fab.hide();
                    }
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                // switch (newState) {
                // case RecyclerView.SCROLL_STATE_IDLE:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_DRAGGING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().resume();
                // break;
                // case RecyclerView.SCROLL_STATE_SETTLING:
                // ((Reddit)getActivity().getApplicationContext()).getImageLoader().pause();
                // break;
                // }
                super.onScrollStateChanged(recyclerView, newState);
                // If the toolbar search is open, and the user scrolls in the Main view--close the search UI
                if (getActivity() instanceof MainActivity && (SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_TOOLBAR || SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_BOTH) && ((MainActivity) getContext()).findViewById(R.id.toolbar_search).getVisibility() == View.VISIBLE) {
                    ((MainActivity) getContext()).findViewById(R.id.close_search_toolbar).performClick();
                }
            }
        };
        rv.addOnScrollListener(toolbarScroll);
    } else {
        toolbarScroll.reset = true;
    }
}
Also used : CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) BaseActivity(me.ccrama.redditslide.Activities.BaseActivity) RecyclerView(android.support.v7.widget.RecyclerView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler)

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