Search in sources :

Example 6 with AccountManager

use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.

the class PopulateShadowboxInfo method showBottomSheet.

public static void showBottomSheet(final Activity mContext, final Submission submission, final View rootView) {
    int[] attrs = new int[] { R.attr.tintColor };
    TypedArray ta = mContext.obtainStyledAttributes(attrs);
    int color = ta.getColor(0, Color.WHITE);
    Drawable profile = mContext.getResources().getDrawable(R.drawable.profile);
    final Drawable sub = mContext.getResources().getDrawable(R.drawable.sub);
    final Drawable report = mContext.getResources().getDrawable(R.drawable.report);
    Drawable copy = mContext.getResources().getDrawable(R.drawable.ic_content_copy);
    Drawable open = mContext.getResources().getDrawable(R.drawable.openexternal);
    Drawable link = mContext.getResources().getDrawable(R.drawable.link);
    Drawable reddit = mContext.getResources().getDrawable(R.drawable.commentchange);
    profile.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    sub.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    report.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    open.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    link.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    reddit.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    ta.recycle();
    BottomSheet.Builder b = new BottomSheet.Builder(mContext).title(Html.fromHtml(submission.getTitle()));
    if (Authentication.didOnline) {
        b.sheet(1, profile, "/u/" + submission.getAuthor()).sheet(2, sub, "/r/" + submission.getSubredditName());
        if (Authentication.isLoggedIn) {
            b.sheet(12, report, mContext.getString(R.string.btn_report));
        }
    }
    b.sheet(7, open, mContext.getString(R.string.submission_link_extern)).sheet(4, link, mContext.getString(R.string.submission_share_permalink)).sheet(8, reddit, mContext.getString(R.string.submission_share_reddit_url)).listener(new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch(which) {
                case 1:
                    {
                        Intent i = new Intent(mContext, Profile.class);
                        i.putExtra(Profile.EXTRA_PROFILE, submission.getAuthor());
                        mContext.startActivity(i);
                    }
                    break;
                case 2:
                    {
                        Intent i = new Intent(mContext, SubredditView.class);
                        i.putExtra(SubredditView.EXTRA_SUBREDDIT, submission.getSubredditName());
                        mContext.startActivityForResult(i, 14);
                    }
                    break;
                case 7:
                    LinkUtil.openExternally(submission.getUrl(), mContext);
                    break;
                case 4:
                    Reddit.defaultShareText(submission.getTitle(), submission.getUrl(), mContext);
                    break;
                case 12:
                    reportReason = "";
                    new MaterialDialog.Builder(mContext).input(mContext.getString(R.string.input_reason_for_report), null, true, new MaterialDialog.InputCallback() {

                        @Override
                        public void onInput(MaterialDialog dialog, CharSequence input) {
                            reportReason = input.toString();
                        }
                    }).alwaysCallInputCallback().title(R.string.report_post).inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES).positiveText(R.string.btn_report).negativeText(R.string.btn_cancel).onNegative(null).onPositive(new MaterialDialog.SingleButtonCallback() {

                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            new AsyncTask<Void, Void, Void>() {

                                @Override
                                protected Void doInBackground(Void... params) {
                                    try {
                                        new AccountManager(Authentication.reddit).report(submission, reportReason);
                                    } catch (ApiException e) {
                                        e.printStackTrace();
                                    }
                                    return null;
                                }

                                @Override
                                protected void onPostExecute(Void aVoid) {
                                    Snackbar s = Snackbar.make(rootView, R.string.msg_report_sent, Snackbar.LENGTH_SHORT);
                                    View view = s.getView();
                                    TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                                    tv.setTextColor(Color.WHITE);
                                    s.show();
                                }
                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    }).show();
                    break;
                case 8:
                    if (SettingValues.shareLongLink) {
                        Reddit.defaultShareText(submission.getTitle(), "htts://reddit.com" + submission.getPermalink(), mContext);
                    } else {
                        Reddit.defaultShareText(submission.getTitle(), "https://redd.it/" + submission.getId(), mContext);
                    }
                    break;
                case 6:
                    {
                        ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("Link", submission.getUrl());
                        clipboard.setPrimaryClip(clip);
                        Toast.makeText(mContext, R.string.submission_link_copied, Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    });
    b.show();
}
Also used : DialogInterface(android.content.DialogInterface) SpannableStringBuilder(android.text.SpannableStringBuilder) TypedArray(android.content.res.TypedArray) TitleTextView(me.ccrama.redditslide.Views.TitleTextView) TextView(android.widget.TextView) ClipboardManager(android.content.ClipboardManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Drawable(android.graphics.drawable.Drawable) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) ImageView(android.widget.ImageView) TitleTextView(me.ccrama.redditslide.Views.TitleTextView) View(android.view.View) TextView(android.widget.TextView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) DialogAction(com.afollestad.materialdialogs.DialogAction) AccountManager(net.dean.jraw.managers.AccountManager) BottomSheet(com.cocosw.bottomsheet.BottomSheet) ClipData(android.content.ClipData) ApiException(net.dean.jraw.ApiException) Snackbar(android.support.design.widget.Snackbar)

Example 7 with AccountManager

use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.

the class PopulateShadowboxInfo method doActionbar.

public static void doActionbar(final CommentNode node, final View rootView, final Activity c, boolean extras) {
    final Comment s = node.getComment();
    TitleTextView title = (TitleTextView) rootView.findViewById(R.id.title);
    TextView desc = (TextView) rootView.findViewById(R.id.desc);
    String distingush = "";
    if (s != null) {
        if (s.getDistinguishedStatus() == DistinguishedStatus.MODERATOR)
            distingush = "[M]";
        else if (s.getDistinguishedStatus() == DistinguishedStatus.ADMIN)
            distingush = "[A]";
        SpannableStringBuilder commentTitle = new SpannableStringBuilder();
        SpannableStringBuilder level = new SpannableStringBuilder();
        if (!node.isTopLevel()) {
            level.append("[" + node.getDepth() + "] ");
            level.setSpan(new RelativeSizeSpan(0.7f), 0, level.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            commentTitle.append(level);
        }
        commentTitle.append(Html.fromHtml(s.getDataNode().get("body_html").asText().trim()));
        title.setTextHtml(commentTitle);
        title.setMaxLines(3);
        String spacer = c.getString(R.string.submission_properties_seperator);
        SpannableStringBuilder titleString = new SpannableStringBuilder();
        SpannableStringBuilder author = new SpannableStringBuilder(" /u/" + s.getAuthor() + " ");
        int authorcolor = Palette.getFontColorUser(s.getAuthor());
        if (Authentication.name != null && s.getAuthor().toLowerCase(Locale.ENGLISH).equals(Authentication.name.toLowerCase(Locale.ENGLISH))) {
            author.setSpan(new RoundedBackgroundSpan(c, R.color.white, R.color.md_deep_orange_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (s.getDistinguishedStatus() == DistinguishedStatus.MODERATOR || s.getDistinguishedStatus() == DistinguishedStatus.ADMIN) {
            author.setSpan(new RoundedBackgroundSpan(c, R.color.white, R.color.md_green_300, false), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (authorcolor != 0) {
            author.setSpan(new ForegroundColorSpan(authorcolor), 0, author.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        titleString.append(author);
        titleString.append(distingush);
        titleString.append(spacer);
        titleString.append(TimeUtils.getTimeAgo(s.getCreated().getTime(), c));
        desc.setText(titleString);
        ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", s.getScore()));
        if (extras) {
            final ImageView downvotebutton = (ImageView) rootView.findViewById(R.id.downvote);
            final ImageView upvotebutton = (ImageView) rootView.findViewById(R.id.upvote);
            if (s.isArchived()) {
                downvotebutton.setVisibility(View.GONE);
                upvotebutton.setVisibility(View.GONE);
            } else if (Authentication.isLoggedIn && Authentication.didOnline) {
                if (SettingValues.actionbarVisible && downvotebutton.getVisibility() != View.VISIBLE) {
                    downvotebutton.setVisibility(View.VISIBLE);
                    upvotebutton.setVisibility(View.VISIBLE);
                }
                switch(ActionStates.getVoteDirection(s)) {
                    case UPVOTE:
                        {
                            ((TextView) rootView.findViewById(R.id.score)).setTextColor(ContextCompat.getColor(c, R.color.md_orange_500));
                            upvotebutton.setColorFilter(ContextCompat.getColor(c, R.color.md_orange_500), PorterDuff.Mode.SRC_ATOP);
                            ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.BOLD);
                            ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", (s.getScore() + ((s.getAuthor().equals(Authentication.name)) ? 0 : 1))));
                            downvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                            break;
                        }
                    case DOWNVOTE:
                        {
                            ((TextView) rootView.findViewById(R.id.score)).setTextColor(ContextCompat.getColor(c, R.color.md_blue_500));
                            downvotebutton.setColorFilter(ContextCompat.getColor(c, R.color.md_blue_500), PorterDuff.Mode.SRC_ATOP);
                            ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.BOLD);
                            ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", (s.getScore() + ((s.getAuthor().equals(Authentication.name)) ? 0 : -1))));
                            upvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                            break;
                        }
                    case NO_VOTE:
                        {
                            ((TextView) rootView.findViewById(R.id.score)).setTextColor(((TextView) rootView.findViewById(R.id.comments)).getCurrentTextColor());
                            ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", s.getScore()));
                            ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.NORMAL);
                            downvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                            upvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                            break;
                        }
                }
            }
            if (Authentication.isLoggedIn && Authentication.didOnline) {
                if (ActionStates.isSaved(s)) {
                    ((ImageView) rootView.findViewById(R.id.save)).setColorFilter(ContextCompat.getColor(c, R.color.md_amber_500), PorterDuff.Mode.SRC_ATOP);
                } else {
                    ((ImageView) rootView.findViewById(R.id.save)).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                }
                rootView.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        new AsyncTask<Void, Void, Void>() {

                            @Override
                            protected Void doInBackground(Void... params) {
                                try {
                                    if (ActionStates.isSaved(s)) {
                                        new AccountManager(Authentication.reddit).unsave(s);
                                        ActionStates.setSaved(s, false);
                                    } else {
                                        new AccountManager(Authentication.reddit).save(s);
                                        ActionStates.setSaved(s, true);
                                    }
                                } catch (ApiException e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void aVoid) {
                                ((SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout)).setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                                if (ActionStates.isSaved(s)) {
                                    ((ImageView) rootView.findViewById(R.id.save)).setColorFilter(ContextCompat.getColor(c, R.color.md_amber_500), PorterDuff.Mode.SRC_ATOP);
                                    AnimateHelper.setFlashAnimation(rootView, rootView.findViewById(R.id.save), ContextCompat.getColor(c, R.color.md_amber_500));
                                } else {
                                    ((ImageView) rootView.findViewById(R.id.save)).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                                }
                            }
                        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    }
                });
            }
            if (!Authentication.isLoggedIn || !Authentication.didOnline) {
                rootView.findViewById(R.id.save).setVisibility(View.GONE);
            }
            try {
                final TextView points = ((TextView) rootView.findViewById(R.id.score));
                final TextView comments = ((TextView) rootView.findViewById(R.id.comments));
                if (Authentication.isLoggedIn && Authentication.didOnline) {
                    {
                        downvotebutton.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                ((SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout)).setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                                if (ActionStates.getVoteDirection(s) != VoteDirection.DOWNVOTE) {
                                    // has not been downvoted
                                    points.setTextColor(ContextCompat.getColor(c, R.color.md_blue_500));
                                    downvotebutton.setColorFilter(ContextCompat.getColor(c, R.color.md_blue_500), PorterDuff.Mode.SRC_ATOP);
                                    upvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                                    AnimateHelper.setFlashAnimation(rootView, downvotebutton, ContextCompat.getColor(c, R.color.md_blue_500));
                                    ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.BOLD);
                                    // if a post is at 0 votes, keep it at 0 when downvoting
                                    final int downvoteScore = (s.getScore() == 0) ? 0 : s.getScore() - 1;
                                    ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", downvoteScore));
                                    new Vote(false, points, c).execute(s);
                                    ActionStates.setVoteDirection(s, VoteDirection.DOWNVOTE);
                                } else {
                                    points.setTextColor(comments.getCurrentTextColor());
                                    new Vote(points, c).execute(s);
                                    ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.NORMAL);
                                    ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", s.getScore()));
                                    ActionStates.setVoteDirection(s, VoteDirection.NO_VOTE);
                                    downvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                                }
                            }
                        });
                    }
                    {
                        upvotebutton.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                ((SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout)).setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
                                if (ActionStates.getVoteDirection(s) != VoteDirection.UPVOTE) {
                                    // has not been upvoted
                                    points.setTextColor(ContextCompat.getColor(c, R.color.md_orange_500));
                                    upvotebutton.setColorFilter(ContextCompat.getColor(c, R.color.md_orange_500), PorterDuff.Mode.SRC_ATOP);
                                    downvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                                    AnimateHelper.setFlashAnimation(rootView, upvotebutton, ContextCompat.getColor(c, R.color.md_orange_500));
                                    ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.BOLD);
                                    ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", s.getScore() + 1));
                                    new Vote(true, points, c).execute(s);
                                    ActionStates.setVoteDirection(s, VoteDirection.UPVOTE);
                                } else {
                                    points.setTextColor(comments.getCurrentTextColor());
                                    new Vote(points, c).execute(s);
                                    ((TextView) rootView.findViewById(R.id.score)).setTypeface(null, Typeface.NORMAL);
                                    ((TextView) rootView.findViewById(R.id.score)).setText(String.format(Locale.getDefault(), "%d", s.getScore()));
                                    ActionStates.setVoteDirection(s, VoteDirection.NO_VOTE);
                                    upvotebutton.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                                }
                            }
                        });
                    }
                } else {
                    upvotebutton.setVisibility(View.GONE);
                    downvotebutton.setVisibility(View.GONE);
                }
            } catch (Exception ignored) {
                ignored.printStackTrace();
            }
        }
    }
}
Also used : Comment(net.dean.jraw.models.Comment) Vote(me.ccrama.redditslide.Vote) SlidingUpPanelLayout(com.sothree.slidinguppanel.SlidingUpPanelLayout) ForegroundColorSpan(android.text.style.ForegroundColorSpan) AsyncTask(android.os.AsyncTask) TitleTextView(me.ccrama.redditslide.Views.TitleTextView) RelativeSizeSpan(android.text.style.RelativeSizeSpan) ImageView(android.widget.ImageView) TitleTextView(me.ccrama.redditslide.Views.TitleTextView) View(android.view.View) TextView(android.widget.TextView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) ApiException(net.dean.jraw.ApiException) RoundedBackgroundSpan(me.ccrama.redditslide.Views.RoundedBackgroundSpan) TitleTextView(me.ccrama.redditslide.Views.TitleTextView) TextView(android.widget.TextView) AccountManager(net.dean.jraw.managers.AccountManager) ImageView(android.widget.ImageView) SpannableStringBuilder(android.text.SpannableStringBuilder) ApiException(net.dean.jraw.ApiException)

Example 8 with AccountManager

use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.

the class Vote method doInBackground.

@Override
protected Void doInBackground(PublicContribution... sub) {
    if (Authentication.isLoggedIn) {
        try {
            new AccountManager(Authentication.reddit).vote(sub[0], direction);
        } catch (ApiException | RuntimeException e) {
            ((Activity) c).runOnUiThread(new Runnable() {

                public void run() {
                    try {
                        if (v != null && c != null && v.getContext() != null) {
                            Snackbar s = Snackbar.make(v, R.string.vote_err, Snackbar.LENGTH_SHORT);
                            View view = s.getView();
                            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                            tv.setTextColor(Color.WHITE);
                            s.show();
                        }
                    } catch (Exception ignored) {
                    }
                    c = null;
                    v = null;
                }
            });
            e.printStackTrace();
        }
    } else {
        ((Activity) c).runOnUiThread(new Runnable() {

            public void run() {
                try {
                    if (v != null && c != null && v.getContext() != null) {
                        Snackbar s = Snackbar.make(v, R.string.vote_err_login, Snackbar.LENGTH_SHORT);
                        View view = s.getView();
                        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                        tv.setTextColor(Color.WHITE);
                        s.show();
                    }
                } catch (Exception ignored) {
                }
                c = null;
                v = null;
            }
        });
    }
    return null;
}
Also used : Activity(android.app.Activity) AccountManager(net.dean.jraw.managers.AccountManager) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View) ApiException(net.dean.jraw.ApiException) ApiException(net.dean.jraw.ApiException) Snackbar(android.support.design.widget.Snackbar)

Example 9 with AccountManager

use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.

the class MainActivity method onPause.

@Override
public void onPause() {
    super.onPause();
    changed = false;
    if (!SettingValues.synccitName.isEmpty()) {
        new MySynccitUpdateTask().execute(SynccitRead.newVisited.toArray(new String[SynccitRead.newVisited.size()]));
    }
    if (Authentication.isLoggedIn && Authentication.me != null && Authentication.me.hasGold() && !SynccitRead.newVisited.isEmpty()) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    String[] returned = new String[SynccitRead.newVisited.size()];
                    int i = 0;
                    for (String s : SynccitRead.newVisited) {
                        if (!s.contains("t3_")) {
                            s = "t3_" + s;
                        }
                        returned[i] = s;
                        i++;
                    }
                    new AccountManager(Authentication.reddit).storeVisits(returned);
                    SynccitRead.newVisited = new ArrayList<>();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
    // Upon leaving MainActivity--hide the toolbar search if it is visible
    if (findViewById(R.id.toolbar_search).getVisibility() == View.VISIBLE) {
        findViewById(R.id.close_search_toolbar).performClick();
    }
}
Also used : ArrayList(java.util.ArrayList) CaseInsensitiveArrayList(me.ccrama.redditslide.CaseInsensitiveArrayList) MySynccitUpdateTask(me.ccrama.redditslide.Synccit.MySynccitUpdateTask) AccountManager(net.dean.jraw.managers.AccountManager) ApiException(net.dean.jraw.ApiException) ActivityNotFoundException(android.content.ActivityNotFoundException) NetworkException(net.dean.jraw.http.NetworkException)

Example 10 with AccountManager

use of net.dean.jraw.managers.AccountManager in project Slide by ccrama.

the class SubredditView method doSubSidebar.

public void doSubSidebar(final String subOverride) {
    findViewById(R.id.loader).setVisibility(View.VISIBLE);
    invalidateOptionsMenu();
    if (!subOverride.equalsIgnoreCase("all") && !subOverride.equalsIgnoreCase("frontpage") && !subOverride.equalsIgnoreCase("random") && !subOverride.equalsIgnoreCase("popular") && !subOverride.equalsIgnoreCase("myrandom") && !subOverride.equalsIgnoreCase("randnsfw") && !subOverride.equalsIgnoreCase("friends") && !subOverride.equalsIgnoreCase("mod") && !subOverride.contains("+") && !subOverride.contains(".") && !subOverride.contains("/m/")) {
        if (drawerLayout != null) {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.END);
        }
        loaded = true;
        final View dialoglayout = findViewById(R.id.sidebarsub);
        {
            View submit = (dialoglayout.findViewById(R.id.submit));
            if (!Authentication.isLoggedIn || !Authentication.didOnline) {
                submit.setVisibility(View.GONE);
            }
            if (SettingValues.fab && SettingValues.fabType == Constants.FAB_POST) {
                submit.setVisibility(View.GONE);
            }
            submit.setOnClickListener(new OnSingleClickListener() {

                @Override
                public void onSingleClick(View view) {
                    Intent inte = new Intent(SubredditView.this, Submit.class);
                    if (!subOverride.contains("/m/") && canSubmit) {
                        inte.putExtra(Submit.EXTRA_SUBREDDIT, subOverride);
                    }
                    SubredditView.this.startActivity(inte);
                }
            });
        }
        dialoglayout.findViewById(R.id.wiki).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(SubredditView.this, Wiki.class);
                i.putExtra(Wiki.EXTRA_SUBREDDIT, subOverride);
                startActivity(i);
            }
        });
        dialoglayout.findViewById(R.id.syncflair).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ImageFlairs.syncFlairs(SubredditView.this, subreddit);
            }
        });
        dialoglayout.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(SubredditView.this, Submit.class);
                if ((!subOverride.contains("/m/") || !subOverride.contains(".")) && canSubmit) {
                    i.putExtra(Submit.EXTRA_SUBREDDIT, subOverride);
                }
                startActivity(i);
            }
        });
        final TextView sort = (TextView) dialoglayout.findViewById(R.id.sort);
        Sorting sortingis = Sorting.HOT;
        if (SettingValues.hasSort(subreddit)) {
            sortingis = SettingValues.getBaseSubmissionSort(subreddit);
            sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
        } else {
            sort.setText("Set default sorting");
        }
        final int sortid = Reddit.getSortingId(sortingis);
        dialoglayout.findViewById(R.id.sorting).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final DialogInterface.OnClickListener l2 = new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        switch(i) {
                            case 0:
                                sorts = Sorting.HOT;
                                break;
                            case 1:
                                sorts = Sorting.NEW;
                                break;
                            case 2:
                                sorts = Sorting.RISING;
                                break;
                            case 3:
                                sorts = Sorting.TOP;
                                askTimePeriod(sorts, subreddit, dialoglayout);
                                return;
                            case 4:
                                sorts = Sorting.CONTROVERSIAL;
                                askTimePeriod(sorts, subreddit, dialoglayout);
                                return;
                        }
                        SettingValues.setSubSorting(sorts, time, subreddit);
                        Sorting sortingis = SettingValues.getBaseSubmissionSort(subreddit);
                        sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
                        reloadSubs();
                    }
                };
                AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(SubredditView.this);
                builder.setTitle(R.string.sorting_choose);
                builder.setSingleChoiceItems(Reddit.getSortingStrings(getBaseContext()), sortid, l2);
                builder.setNegativeButton("Reset default sorting", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SettingValues.prefs.edit().remove("defaultSort" + subreddit.toLowerCase(Locale.ENGLISH)).apply();
                        SettingValues.prefs.edit().remove("defaultTime" + subreddit.toLowerCase(Locale.ENGLISH)).apply();
                        final TextView sort = (TextView) dialoglayout.findViewById(R.id.sort);
                        if (SettingValues.hasSort(subreddit)) {
                            Sorting sortingis = SettingValues.getBaseSubmissionSort(subreddit);
                            sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
                        } else {
                            sort.setText("Set default sorting");
                        }
                        reloadSubs();
                    }
                });
                builder.show();
            }
        });
        dialoglayout.findViewById(R.id.theme).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int style = new ColorPreferences(SubredditView.this).getThemeSubreddit(subOverride);
                final Context contextThemeWrapper = new ContextThemeWrapper(SubredditView.this, style);
                LayoutInflater localInflater = getLayoutInflater().cloneInContext(contextThemeWrapper);
                final View dialoglayout = localInflater.inflate(R.layout.colorsub, null);
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add(subOverride);
                SettingsSubAdapter.showSubThemeEditor(arrayList, SubredditView.this, dialoglayout);
            }
        });
        dialoglayout.findViewById(R.id.mods).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final Dialog d = new MaterialDialog.Builder(SubredditView.this).title(R.string.sidebar_findingmods).cancelable(true).content(R.string.misc_please_wait).progress(true, 100).show();
                new AsyncTask<Void, Void, Void>() {

                    ArrayList<UserRecord> mods;

                    @Override
                    protected Void doInBackground(Void... params) {
                        mods = new ArrayList<>();
                        UserRecordPaginator paginator = new UserRecordPaginator(Authentication.reddit, subOverride, "moderators");
                        paginator.setSorting(Sorting.HOT);
                        paginator.setTimePeriod(TimePeriod.ALL);
                        while (paginator.hasNext()) {
                            mods.addAll(paginator.next());
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        final ArrayList<String> names = new ArrayList<>();
                        for (UserRecord rec : mods) {
                            names.add(rec.getFullName());
                        }
                        d.dismiss();
                        new MaterialDialog.Builder(SubredditView.this).title(getString(R.string.sidebar_submods, subreddit)).items(names).itemsCallback(new MaterialDialog.ListCallback() {

                            @Override
                            public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                Intent i = new Intent(SubredditView.this, Profile.class);
                                i.putExtra(Profile.EXTRA_PROFILE, names.get(which));
                                startActivity(i);
                            }
                        }).positiveText(R.string.btn_message).onPositive(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                Intent i = new Intent(SubredditView.this, SendMessage.class);
                                i.putExtra(SendMessage.EXTRA_NAME, "/r/" + subOverride);
                                startActivity(i);
                            }
                        }).show();
                    }
                }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        });
        dialoglayout.findViewById(R.id.flair).setVisibility(View.GONE);
        if (Authentication.didOnline && Authentication.isLoggedIn) {
            new AsyncTask<View, Void, View>() {

                List<FlairTemplate> flairs;

                ArrayList<String> flairText;

                String current;

                AccountManager m;

                @Override
                protected View doInBackground(View... params) {
                    try {
                        m = new AccountManager(Authentication.reddit);
                        JsonNode node = m.getFlairChoicesRootNode(subOverride, null);
                        flairs = m.getFlairChoices(subOverride, node);
                        FlairTemplate currentF = m.getCurrentFlair(subOverride, node);
                        if (currentF != null) {
                            if (currentF.getText().isEmpty()) {
                                current = ("[" + currentF.getCssClass() + "]");
                            } else {
                                current = (currentF.getText());
                            }
                        }
                        flairText = new ArrayList<>();
                        for (FlairTemplate temp : flairs) {
                            if (temp.getText().isEmpty()) {
                                flairText.add("[" + temp.getCssClass() + "]");
                            } else {
                                flairText.add(temp.getText());
                            }
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    return params[0];
                }

                @Override
                protected void onPostExecute(View flair) {
                    if (flairs != null && !flairs.isEmpty() && flairText != null && !flairText.isEmpty()) {
                        flair.setVisibility(View.VISIBLE);
                        if (current != null) {
                            ((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
                        }
                        flair.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                new MaterialDialog.Builder(SubredditView.this).items(flairText).title(R.string.sidebar_select_flair).itemsCallback(new MaterialDialog.ListCallback() {

                                    @Override
                                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                        final FlairTemplate t = flairs.get(which);
                                        if (t.isTextEditable()) {
                                            new MaterialDialog.Builder(SubredditView.this).title(R.string.sidebar_select_flair_text).input(getString(R.string.mod_flair_hint), t.getText(), true, new MaterialDialog.InputCallback() {

                                                @Override
                                                public void onInput(MaterialDialog dialog, CharSequence input) {
                                                }
                                            }).positiveText(R.string.btn_set).onPositive(new MaterialDialog.SingleButtonCallback() {

                                                @Override
                                                public void onClick(MaterialDialog dialog, DialogAction which) {
                                                    final String flair = dialog.getInputEditText().getText().toString();
                                                    new AsyncTask<Void, Void, Boolean>() {

                                                        @Override
                                                        protected Boolean doInBackground(Void... params) {
                                                            try {
                                                                new ModerationManager(Authentication.reddit).setFlair(subOverride, t, flair, Authentication.name);
                                                                FlairTemplate currentF = m.getCurrentFlair(subOverride);
                                                                if (currentF.getText().isEmpty()) {
                                                                    current = ("[" + currentF.getCssClass() + "]");
                                                                } else {
                                                                    current = (currentF.getText());
                                                                }
                                                                return true;
                                                            } catch (Exception e) {
                                                                e.printStackTrace();
                                                                return false;
                                                            }
                                                        }

                                                        @Override
                                                        protected void onPostExecute(Boolean done) {
                                                            Snackbar s;
                                                            if (done) {
                                                                if (current != null) {
                                                                    ((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
                                                                }
                                                                s = Snackbar.make(mToolbar, R.string.snackbar_flair_success, Snackbar.LENGTH_SHORT);
                                                            } else {
                                                                s = Snackbar.make(mToolbar, R.string.snackbar_flair_error, Snackbar.LENGTH_SHORT);
                                                            }
                                                            if (s != null) {
                                                                View view = s.getView();
                                                                TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                                                                tv.setTextColor(Color.WHITE);
                                                                s.show();
                                                            }
                                                        }
                                                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                                                }
                                            }).negativeText(R.string.btn_cancel).show();
                                        } else {
                                            new AsyncTask<Void, Void, Boolean>() {

                                                @Override
                                                protected Boolean doInBackground(Void... params) {
                                                    try {
                                                        new ModerationManager(Authentication.reddit).setFlair(subOverride, t, null, Authentication.name);
                                                        FlairTemplate currentF = m.getCurrentFlair(subOverride);
                                                        if (currentF.getText().isEmpty()) {
                                                            current = ("[" + currentF.getCssClass() + "]");
                                                        } else {
                                                            current = (currentF.getText());
                                                        }
                                                        return true;
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                        return false;
                                                    }
                                                }

                                                @Override
                                                protected void onPostExecute(Boolean done) {
                                                    Snackbar s;
                                                    if (done) {
                                                        if (current != null) {
                                                            ((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
                                                        }
                                                        s = Snackbar.make(mToolbar, R.string.snackbar_flair_success, Snackbar.LENGTH_SHORT);
                                                    } else {
                                                        s = Snackbar.make(mToolbar, R.string.snackbar_flair_error, Snackbar.LENGTH_SHORT);
                                                    }
                                                    if (s != null) {
                                                        View view = s.getView();
                                                        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                                                        tv.setTextColor(Color.WHITE);
                                                        s.show();
                                                    }
                                                }
                                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                                        }
                                    }
                                }).show();
                            }
                        });
                    }
                }
            }.execute(dialoglayout.findViewById(R.id.flair));
        }
    } else {
        if (drawerLayout != null) {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
        }
    }
}
Also used : UserRecordPaginator(net.dean.jraw.paginators.UserRecordPaginator) DialogInterface(android.content.DialogInterface) OnSingleClickListener(me.ccrama.redditslide.util.OnSingleClickListener) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView) Context(android.content.Context) ModerationManager(net.dean.jraw.managers.ModerationManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ColorPreferences(me.ccrama.redditslide.ColorPreferences) FlairTemplate(net.dean.jraw.models.FlairTemplate) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) ImageView(android.widget.ImageView) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) HorizontalScrollView(android.widget.HorizontalScrollView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) View(android.view.View) TextView(android.widget.TextView) ApiException(net.dean.jraw.ApiException) ActivityNotFoundException(android.content.ActivityNotFoundException) NetworkException(net.dean.jraw.http.NetworkException) Sorting(net.dean.jraw.paginators.Sorting) ContextThemeWrapper(android.support.v7.view.ContextThemeWrapper) UserRecord(net.dean.jraw.models.UserRecord) DialogAction(com.afollestad.materialdialogs.DialogAction) LayoutInflater(android.view.LayoutInflater) AccountManager(net.dean.jraw.managers.AccountManager) Snackbar(android.support.design.widget.Snackbar)

Aggregations

AccountManager (net.dean.jraw.managers.AccountManager)16 View (android.view.View)14 TextView (android.widget.TextView)14 ApiException (net.dean.jraw.ApiException)14 AsyncTask (android.os.AsyncTask)13 ImageView (android.widget.ImageView)12 ArrayList (java.util.ArrayList)12 Snackbar (android.support.design.widget.Snackbar)11 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)11 DialogInterface (android.content.DialogInterface)9 DialogAction (com.afollestad.materialdialogs.DialogAction)9 Intent (android.content.Intent)8 SpannableStringBuilder (android.text.SpannableStringBuilder)8 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)8 SubmissionsView (me.ccrama.redditslide.Fragments.SubmissionsView)8 SubredditView (me.ccrama.redditslide.Activities.SubredditView)7 Dialog (android.app.Dialog)6 ActivityNotFoundException (android.content.ActivityNotFoundException)5 List (java.util.List)5 NetworkException (net.dean.jraw.http.NetworkException)5