Search in sources :

Example 1 with CommentCacheAsync

use of me.ccrama.redditslide.CommentCacheAsync in project Slide by ccrama.

the class ManageOfflineContent method onCreate.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    applyColorTheme();
    setContentView(R.layout.activity_manage_history);
    setupAppBar(R.id.toolbar, R.string.manage_offline_content, true, true);
    if (!NetworkUtil.isConnected(this))
        SettingsTheme.changed = true;
    findViewById(R.id.clear_all).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean wifi = Reddit.cachedData.getBoolean("wifiOnly", false);
            String sync = Reddit.cachedData.getString("toCache", "");
            int hour = (Reddit.cachedData.getInt("hour", 0));
            int minute = (Reddit.cachedData.getInt("minute", 0));
            Reddit.cachedData.edit().clear().apply();
            Reddit.cachedData.edit().putBoolean("wifiOnly", wifi).putString("toCache", sync).putInt("hour", hour).putInt("minute", minute).apply();
            finish();
        }
    });
    if (NetworkUtil.isConnectedNoOverride(this)) {
        findViewById(R.id.sync_now).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new CommentCacheAsync(ManageOfflineContent.this, Reddit.cachedData.getString("toCache", "").split(",")).execute();
            }
        });
    } else {
        findViewById(R.id.sync_now).setVisibility(View.GONE);
    }
    {
        SwitchCompat single = (SwitchCompat) findViewById(R.id.wifi);
        single.setChecked(Reddit.cachedData.getBoolean("wifiOnly", false));
        single.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Reddit.cachedData.edit().putBoolean("wifiOnly", isChecked).apply();
            }
        });
    }
    updateBackup();
    updateFilters();
    final List<String> commentDepths = ImmutableList.of("2", "4", "6", "8", "10");
    final String[] commentDepthArray = new String[commentDepths.size()];
    findViewById(R.id.comments_depth).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final String commentDepth = SettingValues.prefs.getString(SettingValues.COMMENT_DEPTH, "2");
            AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(ManageOfflineContent.this);
            builder.setTitle(R.string.comments_depth);
            builder.setSingleChoiceItems(commentDepths.toArray(commentDepthArray), commentDepths.indexOf(commentDepth), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SettingValues.prefs.edit().putString(SettingValues.COMMENT_DEPTH, commentDepths.get(which)).apply();
                }
            });
            builder.show();
        }
    });
    final List<String> commentCounts = ImmutableList.of("20", "40", "60", "80", "100");
    final String[] commentCountArray = new String[commentCounts.size()];
    findViewById(R.id.comments_count).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final String commentCount = SettingValues.prefs.getString(SettingValues.COMMENT_COUNT, "2");
            AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(ManageOfflineContent.this);
            builder.setTitle(R.string.comments_count);
            builder.setSingleChoiceItems(commentCounts.toArray(commentCountArray), commentCounts.indexOf(commentCount), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SettingValues.prefs.edit().putString(SettingValues.COMMENT_COUNT, commentCounts.get(which)).apply();
                }
            });
            builder.show();
        }
    });
    findViewById(R.id.autocache).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<String> sorted = UserSubscriptions.sort(UserSubscriptions.getSubscriptions(ManageOfflineContent.this));
            final String[] all = new String[sorted.size()];
            boolean[] checked = new boolean[all.length];
            int i = 0;
            List<String> s2 = new ArrayList<>();
            Collections.addAll(s2, Reddit.cachedData.getString("toCache", "").split(","));
            for (String s : sorted) {
                all[i] = s;
                if (s2.contains(s)) {
                    checked[i] = true;
                }
                i++;
            }
            final ArrayList<String> toCheck = new ArrayList<>();
            toCheck.addAll(s2);
            new AlertDialogWrapper.Builder(ManageOfflineContent.this).alwaysCallMultiChoiceCallback().setMultiChoiceItems(all, checked, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if (!isChecked) {
                        toCheck.remove(all[which]);
                    } else {
                        toCheck.add(all[which]);
                    }
                }
            }).setTitle(R.string.multireddit_selector).setPositiveButton(getString(R.string.btn_add).toUpperCase(), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Reddit.cachedData.edit().putString("toCache", Reddit.arrayToString(toCheck)).apply();
                    updateBackup();
                }
            }).show();
        }
    });
    updateTime();
    findViewById(R.id.autocache_time_touch).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final TimePickerDialog d = new TimePickerDialog(ManageOfflineContent.this);
            d.hour(Reddit.cachedData.getInt("hour", 0));
            d.minute(Reddit.cachedData.getInt("minute", 0));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                d.applyStyle(new ColorPreferences(ManageOfflineContent.this).getFontStyle().getBaseId());
            d.positiveAction("SET");
            TypedValue typedValue = new TypedValue();
            Resources.Theme theme = getTheme();
            theme.resolveAttribute(R.attr.activity_background, typedValue, true);
            int color = typedValue.data;
            d.backgroundColor(color);
            d.actionTextColor(getResources().getColor(new ColorPreferences(ManageOfflineContent.this).getFontStyle().getColor()));
            d.positiveActionClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Reddit.cachedData.edit().putInt("hour", d.getHour()).putInt("minute", d.getMinute()).commit();
                    Reddit.autoCache = new AutoCacheScheduler(ManageOfflineContent.this);
                    Reddit.autoCache.start(getApplicationContext());
                    updateTime();
                    d.dismiss();
                }
            });
            theme.resolveAttribute(R.attr.fontColor, typedValue, true);
            int color2 = typedValue.data;
            d.setTitle(getString(R.string.choose_sync_time));
            d.titleColor(color2);
            d.show();
        }
    });
}
Also used : AutoCacheScheduler(me.ccrama.redditslide.Autocache.AutoCacheScheduler) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TypedValue(android.util.TypedValue) CommentCacheAsync(me.ccrama.redditslide.CommentCacheAsync) ColorPreferences(me.ccrama.redditslide.ColorPreferences) TimePickerDialog(com.rey.material.app.TimePickerDialog) View(android.view.View) TextView(android.widget.TextView) CompoundButton(android.widget.CompoundButton) SwitchCompat(android.support.v7.widget.SwitchCompat)

Example 2 with CommentCacheAsync

use of me.ccrama.redditslide.CommentCacheAsync in project Slide by ccrama.

the class MainActivity method saveOffline.

public void saveOffline(final List<Submission> submissions, final String subreddit) {
    final boolean[] chosen = new boolean[2];
    new AlertDialogWrapper.Builder(this).setTitle(R.string.save_for_offline_viewing).setMultiChoiceItems(new String[] { getString(R.string.type_gifs) }, new boolean[] { false }, new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            chosen[which] = isChecked;
        }
    }).setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            caching = new CommentCacheAsync(submissions, MainActivity.this, subreddit, chosen).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    }).setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            ExecutorService service = Executors.newSingleThreadExecutor();
            new CommentCacheAsync(submissions, MainActivity.this, subreddit, chosen).executeOnExecutor(service);
        }
    }).show();
}
Also used : CommentCacheAsync(me.ccrama.redditslide.CommentCacheAsync) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) DialogInterface(android.content.DialogInterface) ExecutorService(java.util.concurrent.ExecutorService) Point(android.graphics.Point)

Example 3 with CommentCacheAsync

use of me.ccrama.redditslide.CommentCacheAsync in project Slide by ccrama.

the class PopulateNewsViewHolder method showBottomSheet.

public <T extends Contribution> void showBottomSheet(final Activity mContext, final Submission submission, final NewsViewHolder holder, final List<T> posts, final String baseSub, final RecyclerView recyclerview, final boolean full) {
    int[] attrs = new int[] { R.attr.tintColor };
    TypedArray ta = mContext.obtainStyledAttributes(attrs);
    int color = ta.getColor(0, Color.WHITE);
    Drawable profile = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.profile, null);
    final Drawable sub = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.sub, null);
    Drawable saved = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.iconstarfilled, null);
    Drawable hide = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.hide, null);
    final Drawable report = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.report, null);
    Drawable copy = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_content_copy, null);
    final Drawable readLater = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.save, null);
    Drawable open = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.openexternal, null);
    Drawable link = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.link, null);
    Drawable reddit = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.commentchange, null);
    Drawable filter = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.filter, null);
    profile.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    sub.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    saved.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    hide.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);
    readLater.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    filter.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    ta.recycle();
    final BottomSheet.Builder b = new BottomSheet.Builder(mContext).title(Html.fromHtml(submission.getTitle()));
    final boolean isReadLater = mContext instanceof PostReadLater;
    final boolean isAddedToReadLaterList = ReadLater.isToBeReadLater(submission);
    if (Authentication.didOnline) {
        b.sheet(1, profile, "/u/" + submission.getAuthor()).sheet(2, sub, "/r/" + submission.getSubredditName());
        String save = mContext.getString(R.string.btn_save);
        if (ActionStates.isSaved(submission)) {
            save = mContext.getString(R.string.comment_unsave);
        }
        if (Authentication.isLoggedIn) {
            b.sheet(3, saved, save);
        }
    }
    if (isAddedToReadLaterList) {
        b.sheet(28, readLater, "Mark As Read");
    } else {
        b.sheet(28, readLater, "Read later");
    }
    if (Authentication.didOnline) {
        if (Authentication.isLoggedIn) {
            b.sheet(12, report, mContext.getString(R.string.btn_report));
        }
    }
    if (submission.getSelftext() != null && !submission.getSelftext().isEmpty() && full) {
        b.sheet(25, copy, mContext.getString(R.string.submission_copy_text));
    }
    boolean hidden = submission.isHidden();
    if (!full && Authentication.didOnline) {
        if (!hidden) {
            b.sheet(5, hide, mContext.getString(R.string.submission_hide));
        } else {
            b.sheet(5, hide, mContext.getString(R.string.submission_unhide));
        }
    }
    b.sheet(7, open, mContext.getString(R.string.submission_link_extern));
    b.sheet(4, link, mContext.getString(R.string.submission_share_permalink)).sheet(8, reddit, mContext.getString(R.string.submission_share_reddit_url));
    if ((mContext instanceof MainActivity) || (mContext instanceof SubredditView)) {
        b.sheet(10, filter, mContext.getString(R.string.filter_content));
    }
    b.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 10:
                    String[] choices;
                    final String flair = submission.getSubmissionFlair().getText() != null ? submission.getSubmissionFlair().getText() : "";
                    if (flair.isEmpty()) {
                        choices = new String[] { mContext.getString(R.string.filter_posts_sub, submission.getSubredditName()), mContext.getString(R.string.filter_posts_user, submission.getAuthor()), mContext.getString(R.string.filter_posts_urls, submission.getDomain()), mContext.getString(R.string.filter_open_externally, submission.getDomain()) };
                        chosen = new boolean[] { Arrays.asList(SettingValues.subredditFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getSubredditName().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.userFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getAuthor().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.domainFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.alwaysExternal.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)) };
                        oldChosen = chosen.clone();
                    } else {
                        choices = new String[] { mContext.getString(R.string.filter_posts_sub, submission.getSubredditName()), mContext.getString(R.string.filter_posts_user, submission.getAuthor()), mContext.getString(R.string.filter_posts_urls, submission.getDomain()), mContext.getString(R.string.filter_open_externally, submission.getDomain()), mContext.getString(R.string.filter_posts_flair, flair, baseSub) };
                    }
                    ;
                    chosen = new boolean[] { Arrays.asList(SettingValues.subredditFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getSubredditName().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.userFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getAuthor().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.domainFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.alwaysExternal.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.flairFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(baseSub + ":" + flair) };
                    oldChosen = chosen.clone();
                    new AlertDialogWrapper.Builder(mContext).setTitle(R.string.filter_title).alwaysCallMultiChoiceCallback().setMultiChoiceItems(choices, chosen, new DialogInterface.OnMultiChoiceClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                            chosen[which] = isChecked;
                        }
                    }).setPositiveButton(R.string.filter_btn, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            boolean filtered = false;
                            SharedPreferences.Editor e = SettingValues.prefs.edit();
                            if (chosen[0] && chosen[0] != oldChosen[0]) {
                                SettingValues.subredditFilters = SettingValues.subredditFilters + ((SettingValues.subredditFilters.isEmpty() || SettingValues.subredditFilters.endsWith(",")) ? "" : ",") + submission.getSubredditName();
                                filtered = true;
                                e.putString(SettingValues.PREF_SUBREDDIT_FILTERS, SettingValues.subredditFilters);
                                PostMatch.subreddits = null;
                            } else if (!chosen[0] && chosen[0] != oldChosen[0]) {
                                SettingValues.subredditFilters = SettingValues.subredditFilters.replace(submission.getSubredditName(), "");
                                filtered = false;
                                e.putString(SettingValues.PREF_SUBREDDIT_FILTERS, SettingValues.subredditFilters);
                                e.apply();
                                PostMatch.subreddits = null;
                            }
                            if (chosen[1] && chosen[1] != oldChosen[1]) {
                                SettingValues.userFilters = SettingValues.userFilters + ((SettingValues.userFilters.isEmpty() || SettingValues.userFilters.endsWith(",")) ? "" : ",") + submission.getAuthor();
                                filtered = true;
                                e.putString(SettingValues.PREF_USER_FILTERS, SettingValues.userFilters);
                                PostMatch.users = null;
                            } else if (!chosen[1] && chosen[1] != oldChosen[1]) {
                                SettingValues.userFilters = SettingValues.userFilters.replace(submission.getAuthor(), "");
                                filtered = false;
                                e.putString(SettingValues.PREF_USER_FILTERS, SettingValues.userFilters);
                                e.apply();
                                PostMatch.users = null;
                            }
                            if (chosen[2] && chosen[2] != oldChosen[2]) {
                                SettingValues.domainFilters = SettingValues.domainFilters + ((SettingValues.domainFilters.isEmpty() || SettingValues.domainFilters.endsWith(",")) ? "" : ",") + submission.getDomain();
                                filtered = true;
                                e.putString(SettingValues.PREF_DOMAIN_FILTERS, SettingValues.domainFilters);
                                PostMatch.domains = null;
                            } else if (!chosen[2] && chosen[2] != oldChosen[2]) {
                                SettingValues.domainFilters = SettingValues.domainFilters.replace(submission.getDomain(), "");
                                filtered = false;
                                e.putString(SettingValues.PREF_DOMAIN_FILTERS, SettingValues.domainFilters);
                                e.apply();
                                PostMatch.domains = null;
                            }
                            if (chosen[3] && chosen[3] != oldChosen[3]) {
                                SettingValues.alwaysExternal = SettingValues.alwaysExternal + ((SettingValues.alwaysExternal.isEmpty() || SettingValues.alwaysExternal.endsWith(",")) ? "" : ",") + submission.getDomain();
                                e.putString(SettingValues.PREF_ALWAYS_EXTERNAL, SettingValues.alwaysExternal);
                                e.apply();
                            } else if (!chosen[3] && chosen[3] != oldChosen[3]) {
                                SettingValues.alwaysExternal = SettingValues.alwaysExternal.replace(submission.getDomain(), "");
                                e.putString(SettingValues.PREF_ALWAYS_EXTERNAL, SettingValues.alwaysExternal);
                                e.apply();
                            }
                            if (chosen.length > 4) {
                                if (chosen[4] && chosen[4] != oldChosen[4]) {
                                    SettingValues.flairFilters = SettingValues.flairFilters + ((SettingValues.flairFilters.isEmpty() || SettingValues.flairFilters.endsWith(",")) ? "" : ",") + (baseSub + ":" + flair);
                                    e.putString(SettingValues.PREF_FLAIR_FILTERS, SettingValues.flairFilters);
                                    e.apply();
                                    PostMatch.flairs = null;
                                    filtered = true;
                                } else if (!chosen[4] && chosen[4] != oldChosen[4]) {
                                    SettingValues.flairFilters = SettingValues.flairFilters.toLowerCase(Locale.ENGLISH).replace((baseSub + ":" + flair).toLowerCase(Locale.ENGLISH), "");
                                    e.putString(SettingValues.PREF_FLAIR_FILTERS, SettingValues.flairFilters);
                                    e.apply();
                                    PostMatch.flairs = null;
                                }
                            }
                            if (filtered) {
                                e.apply();
                                PostMatch.domains = null;
                                PostMatch.subreddits = null;
                                PostMatch.users = null;
                                ArrayList<Contribution> toRemove = new ArrayList<>();
                                for (Contribution s : posts) {
                                    if (s instanceof Submission && PostMatch.doesMatch((Submission) s)) {
                                        toRemove.add(s);
                                    }
                                }
                                OfflineSubreddit s = OfflineSubreddit.getSubreddit(baseSub, false, mContext);
                                for (Contribution remove : toRemove) {
                                    final int pos = posts.indexOf(remove);
                                    posts.remove(pos);
                                    if (baseSub != null) {
                                        s.hideMulti(pos);
                                    }
                                }
                                s.writeToMemoryNoStorage();
                                recyclerview.getAdapter().notifyDataSetChanged();
                            }
                        }
                    }).setNegativeButton(R.string.btn_cancel, null).show();
                    break;
                case 5:
                    {
                        hideSubmission(submission, posts, baseSub, recyclerview, mContext);
                    }
                    break;
                case 7:
                    LinkUtil.openExternally(submission.getUrl(), mContext);
                    if (submission.isNsfw() && !SettingValues.storeNSFWHistory) {
                    // Do nothing if the post is NSFW and storeNSFWHistory is not enabled
                    } else if (SettingValues.storeHistory) {
                        HasSeen.addSeen(submission.getFullName());
                    }
                    break;
                case 28:
                    if (!isAddedToReadLaterList) {
                        ReadLater.setReadLater(submission, true);
                        Snackbar s = Snackbar.make(holder.itemView, "Added to read later!", Snackbar.LENGTH_SHORT);
                        View view = s.getView();
                        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                        tv.setTextColor(Color.WHITE);
                        s.setAction(R.string.btn_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                ReadLater.setReadLater(submission, false);
                                Snackbar s2 = Snackbar.make(holder.itemView, "Removed from read later", Snackbar.LENGTH_SHORT);
                                View view2 = s2.getView();
                                TextView tv2 = (TextView) view2.findViewById(android.support.design.R.id.snackbar_text);
                                tv2.setTextColor(Color.WHITE);
                                s2.show();
                            }
                        });
                        if (NetworkUtil.isConnected(mContext)) {
                            new CommentCacheAsync(Arrays.asList(submission), mContext, CommentCacheAsync.SAVED_SUBMISSIONS, new boolean[] { true, true }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                        s.show();
                    } else {
                        ReadLater.setReadLater(submission, false);
                        if (isReadLater || !Authentication.didOnline) {
                            final int pos = posts.indexOf(submission);
                            posts.remove(submission);
                            recyclerview.getAdapter().notifyItemRemoved(holder.getAdapterPosition());
                            Snackbar s2 = Snackbar.make(holder.itemView, "Removed from read later", Snackbar.LENGTH_SHORT);
                            View view2 = s2.getView();
                            TextView tv2 = (TextView) view2.findViewById(android.support.design.R.id.snackbar_text);
                            tv2.setTextColor(Color.WHITE);
                            s2.setAction(R.string.btn_undo, new View.OnClickListener() {

                                @Override
                                public void onClick(View view) {
                                    posts.add(pos, (T) submission);
                                    recyclerview.getAdapter().notifyDataSetChanged();
                                }
                            });
                        } else {
                            Snackbar s2 = Snackbar.make(holder.itemView, "Removed from read later", Snackbar.LENGTH_SHORT);
                            View view2 = s2.getView();
                            TextView tv2 = (TextView) view2.findViewById(android.support.design.R.id.snackbar_text);
                            s2.show();
                        }
                        OfflineSubreddit.newSubreddit(CommentCacheAsync.SAVED_SUBMISSIONS).deleteFromMemory(submission.getFullName());
                    }
                    break;
                case 4:
                    Reddit.defaultShareText(Html.fromHtml(submission.getTitle()).toString(), StringEscapeUtils.escapeHtml4(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();
                        }
                    }).title(R.string.report_post).alwaysCallInputCallback().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) {
                                    if (holder.itemView != null) {
                                        try {
                                            Snackbar s = Snackbar.make(holder.itemView, 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();
                                        } catch (Exception ignored) {
                                        }
                                    }
                                }
                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    }).show();
                    break;
                case 8:
                    Reddit.defaultShareText(Html.fromHtml(submission.getTitle()).toString(), "https://reddit.com" + submission.getPermalink(), 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;
                case 25:
                    final TextView showText = new TextView(mContext);
                    showText.setText(StringEscapeUtils.unescapeHtml4(submission.getTitle() + "\n\n" + submission.getSelftext()));
                    showText.setTextIsSelectable(true);
                    int sixteen = Reddit.dpToPxVertical(24);
                    showText.setPadding(sixteen, 0, sixteen, 0);
                    AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(mContext);
                    builder.setView(showText).setTitle("Select text to copy").setCancelable(true).setPositiveButton("COPY SELECTED", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String selected = showText.getText().toString().substring(showText.getSelectionStart(), showText.getSelectionEnd());
                            if (!selected.isEmpty()) {
                                ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("Selftext", selected);
                                clipboard.setPrimaryClip(clip);
                                Toast.makeText(mContext, R.string.submission_comment_copied, Toast.LENGTH_SHORT).show();
                            } else {
                                ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("Selftext", Html.fromHtml(submission.getTitle() + "\n\n" + submission.getSelftext()));
                                clipboard.setPrimaryClip(clip);
                                Toast.makeText(mContext, R.string.submission_comment_copied, Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).setNegativeButton(R.string.btn_cancel, null).setNeutralButton("COPY ALL", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clip = ClipData.newPlainText("Selftext", Html.fromHtml(submission.getTitle() + "\n\n" + submission.getSelftext()));
                            clipboard.setPrimaryClip(clip);
                            Toast.makeText(mContext, R.string.submission_comment_copied, Toast.LENGTH_SHORT).show();
                        }
                    }).show();
                    break;
            }
        }
    });
    b.show();
}
Also used : DialogInterface(android.content.DialogInterface) SpannableStringBuilder(android.text.SpannableStringBuilder) ArrayList(java.util.ArrayList) PostReadLater(me.ccrama.redditslide.Activities.PostReadLater) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TypedArray(android.content.res.TypedArray) TextView(android.widget.TextView) ClipboardManager(android.content.ClipboardManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Submission(net.dean.jraw.models.Submission) CommentCacheAsync(me.ccrama.redditslide.CommentCacheAsync) SharedPreferences(android.content.SharedPreferences) Drawable(android.graphics.drawable.Drawable) OfflineSubreddit(me.ccrama.redditslide.OfflineSubreddit) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) MediaView(me.ccrama.redditslide.Activities.MediaView) ImageView(android.widget.ImageView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) 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) ApiException(net.dean.jraw.ApiException) DialogAction(com.afollestad.materialdialogs.DialogAction) AccountManager(net.dean.jraw.managers.AccountManager) BottomSheet(com.cocosw.bottomsheet.BottomSheet) ClipData(android.content.ClipData) Contribution(net.dean.jraw.models.Contribution) Snackbar(android.support.design.widget.Snackbar) ApiException(net.dean.jraw.ApiException)

Example 4 with CommentCacheAsync

use of me.ccrama.redditslide.CommentCacheAsync in project Slide by ccrama.

the class PopulateSubmissionViewHolder method showBottomSheet.

public <T extends Contribution> void showBottomSheet(final Activity mContext, final Submission submission, final SubmissionViewHolder holder, final List<T> posts, final String baseSub, final RecyclerView recyclerview, final boolean full) {
    int[] attrs = new int[] { R.attr.tintColor };
    TypedArray ta = mContext.obtainStyledAttributes(attrs);
    int color = ta.getColor(0, Color.WHITE);
    Drawable profile = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.profile, null);
    final Drawable sub = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.sub, null);
    Drawable saved = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.iconstarfilled, null);
    Drawable hide = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.hide, null);
    final Drawable report = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.report, null);
    Drawable copy = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_content_copy, null);
    final Drawable readLater = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.save, null);
    Drawable open = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.openexternal, null);
    Drawable link = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.link, null);
    Drawable reddit = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.commentchange, null);
    Drawable filter = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.filter, null);
    Drawable crosspost = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.forward, null);
    profile.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    sub.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    saved.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    hide.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);
    readLater.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    filter.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    crosspost.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    ta.recycle();
    final BottomSheet.Builder b = new BottomSheet.Builder(mContext).title(Html.fromHtml(submission.getTitle()));
    final boolean isReadLater = mContext instanceof PostReadLater;
    final boolean isAddedToReadLaterList = ReadLater.isToBeReadLater(submission);
    if (Authentication.didOnline) {
        b.sheet(1, profile, "/u/" + submission.getAuthor()).sheet(2, sub, "/r/" + submission.getSubredditName());
        String save = mContext.getString(R.string.btn_save);
        if (ActionStates.isSaved(submission)) {
            save = mContext.getString(R.string.comment_unsave);
        }
        if (Authentication.isLoggedIn) {
            b.sheet(3, saved, save);
        }
    }
    if (isAddedToReadLaterList) {
        b.sheet(28, readLater, "Mark As Read");
    } else {
        b.sheet(28, readLater, "Read later");
    }
    if (Authentication.didOnline) {
        if (Authentication.isLoggedIn) {
            b.sheet(12, report, mContext.getString(R.string.btn_report));
            b.sheet(13, crosspost, mContext.getString(R.string.btn_crosspost));
        }
    }
    if (submission.getSelftext() != null && !submission.getSelftext().isEmpty() && full) {
        b.sheet(25, copy, mContext.getString(R.string.submission_copy_text));
    }
    boolean hidden = submission.isHidden();
    if (!full && Authentication.didOnline) {
        if (!hidden) {
            b.sheet(5, hide, mContext.getString(R.string.submission_hide));
        } else {
            b.sheet(5, hide, mContext.getString(R.string.submission_unhide));
        }
    }
    b.sheet(7, open, mContext.getString(R.string.submission_link_extern));
    b.sheet(4, link, mContext.getString(R.string.submission_share_permalink)).sheet(8, reddit, mContext.getString(R.string.submission_share_reddit_url));
    if ((mContext instanceof MainActivity) || (mContext instanceof SubredditView)) {
        b.sheet(10, filter, mContext.getString(R.string.filter_content));
    }
    b.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 10:
                    String[] choices;
                    final String flair = submission.getSubmissionFlair().getText() != null ? submission.getSubmissionFlair().getText() : "";
                    if (flair.isEmpty()) {
                        choices = new String[] { mContext.getString(R.string.filter_posts_sub, submission.getSubredditName()), mContext.getString(R.string.filter_posts_user, submission.getAuthor()), mContext.getString(R.string.filter_posts_urls, submission.getDomain()), mContext.getString(R.string.filter_open_externally, submission.getDomain()) };
                        chosen = new boolean[] { Arrays.asList(SettingValues.subredditFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getSubredditName().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.userFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getAuthor().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.domainFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.alwaysExternal.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)) };
                        oldChosen = chosen.clone();
                    } else {
                        choices = new String[] { mContext.getString(R.string.filter_posts_sub, submission.getSubredditName()), mContext.getString(R.string.filter_posts_user, submission.getAuthor()), mContext.getString(R.string.filter_posts_urls, submission.getDomain()), mContext.getString(R.string.filter_open_externally, submission.getDomain()), mContext.getString(R.string.filter_posts_flair, flair, baseSub) };
                    }
                    chosen = new boolean[] { Arrays.asList(SettingValues.subredditFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getSubredditName().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.userFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getAuthor().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.domainFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.alwaysExternal.toLowerCase(Locale.ENGLISH).split(",")).contains(submission.getDomain().toLowerCase(Locale.ENGLISH)), Arrays.asList(SettingValues.flairFilters.toLowerCase(Locale.ENGLISH).split(",")).contains(baseSub + ":" + flair) };
                    oldChosen = chosen.clone();
                    new AlertDialogWrapper.Builder(mContext).setTitle(R.string.filter_title).alwaysCallMultiChoiceCallback().setMultiChoiceItems(choices, chosen, new DialogInterface.OnMultiChoiceClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                            chosen[which] = isChecked;
                        }
                    }).setPositiveButton(R.string.filter_btn, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            boolean filtered = false;
                            SharedPreferences.Editor e = SettingValues.prefs.edit();
                            if (chosen[0] && chosen[0] != oldChosen[0]) {
                                SettingValues.subredditFilters = SettingValues.subredditFilters + ((SettingValues.subredditFilters.isEmpty() || SettingValues.subredditFilters.endsWith(",")) ? "" : ",") + submission.getSubredditName();
                                filtered = true;
                                e.putString(SettingValues.PREF_SUBREDDIT_FILTERS, SettingValues.subredditFilters);
                                PostMatch.subreddits = null;
                            } else if (!chosen[0] && chosen[0] != oldChosen[0]) {
                                SettingValues.subredditFilters = SettingValues.subredditFilters.replace(submission.getSubredditName(), "");
                                filtered = false;
                                e.putString(SettingValues.PREF_SUBREDDIT_FILTERS, SettingValues.subredditFilters);
                                e.apply();
                                PostMatch.subreddits = null;
                            }
                            if (chosen[1] && chosen[1] != oldChosen[1]) {
                                SettingValues.userFilters = SettingValues.userFilters + ((SettingValues.userFilters.isEmpty() || SettingValues.userFilters.endsWith(",")) ? "" : ",") + submission.getAuthor();
                                filtered = true;
                                e.putString(SettingValues.PREF_USER_FILTERS, SettingValues.userFilters);
                                PostMatch.users = null;
                            } else if (!chosen[1] && chosen[1] != oldChosen[1]) {
                                SettingValues.userFilters = SettingValues.userFilters.replace(submission.getAuthor(), "");
                                filtered = false;
                                e.putString(SettingValues.PREF_USER_FILTERS, SettingValues.userFilters);
                                e.apply();
                                PostMatch.users = null;
                            }
                            if (chosen[2] && chosen[2] != oldChosen[2]) {
                                SettingValues.domainFilters = SettingValues.domainFilters + ((SettingValues.domainFilters.isEmpty() || SettingValues.domainFilters.endsWith(",")) ? "" : ",") + submission.getDomain();
                                filtered = true;
                                e.putString(SettingValues.PREF_DOMAIN_FILTERS, SettingValues.domainFilters);
                                PostMatch.domains = null;
                            } else if (!chosen[2] && chosen[2] != oldChosen[2]) {
                                SettingValues.domainFilters = SettingValues.domainFilters.replace(submission.getDomain(), "");
                                filtered = false;
                                e.putString(SettingValues.PREF_DOMAIN_FILTERS, SettingValues.domainFilters);
                                e.apply();
                                PostMatch.domains = null;
                            }
                            if (chosen[3] && chosen[3] != oldChosen[3]) {
                                SettingValues.alwaysExternal = SettingValues.alwaysExternal + ((SettingValues.alwaysExternal.isEmpty() || SettingValues.alwaysExternal.endsWith(",")) ? "" : ",") + submission.getDomain();
                                e.putString(SettingValues.PREF_ALWAYS_EXTERNAL, SettingValues.alwaysExternal);
                                e.apply();
                            } else if (!chosen[3] && chosen[3] != oldChosen[3]) {
                                SettingValues.alwaysExternal = SettingValues.alwaysExternal.replace(submission.getDomain(), "");
                                e.putString(SettingValues.PREF_ALWAYS_EXTERNAL, SettingValues.alwaysExternal);
                                e.apply();
                            }
                            if (chosen.length > 4) {
                                if (chosen[4] && chosen[4] != oldChosen[4]) {
                                    SettingValues.flairFilters = SettingValues.flairFilters + ((SettingValues.flairFilters.isEmpty() || SettingValues.flairFilters.endsWith(",")) ? "" : ",") + (baseSub + ":" + flair);
                                    e.putString(SettingValues.PREF_FLAIR_FILTERS, SettingValues.flairFilters);
                                    e.apply();
                                    PostMatch.flairs = null;
                                    filtered = true;
                                } else if (!chosen[4] && chosen[4] != oldChosen[4]) {
                                    SettingValues.flairFilters = SettingValues.flairFilters.toLowerCase(Locale.ENGLISH).replace((baseSub + ":" + flair).toLowerCase(Locale.ENGLISH), "");
                                    e.putString(SettingValues.PREF_FLAIR_FILTERS, SettingValues.flairFilters);
                                    e.apply();
                                    PostMatch.flairs = null;
                                }
                            }
                            if (filtered) {
                                e.apply();
                                PostMatch.domains = null;
                                PostMatch.subreddits = null;
                                PostMatch.users = null;
                                ArrayList<Contribution> toRemove = new ArrayList<>();
                                for (Contribution s : posts) {
                                    if (s instanceof Submission && PostMatch.doesMatch((Submission) s)) {
                                        toRemove.add(s);
                                    }
                                }
                                OfflineSubreddit s = OfflineSubreddit.getSubreddit(baseSub, false, mContext);
                                for (Contribution remove : toRemove) {
                                    final int pos = posts.indexOf(remove);
                                    posts.remove(pos);
                                    if (baseSub != null) {
                                        s.hideMulti(pos);
                                    }
                                }
                                s.writeToMemoryNoStorage();
                                recyclerview.getAdapter().notifyDataSetChanged();
                            }
                        }
                    }).setNegativeButton(R.string.btn_cancel, null).show();
                    break;
                case 3:
                    saveSubmission(submission, mContext, holder, full);
                    break;
                case 5:
                    {
                        hideSubmission(submission, posts, baseSub, recyclerview, mContext);
                    }
                    break;
                case 7:
                    LinkUtil.openExternally(submission.getUrl(), mContext);
                    if (submission.isNsfw() && !SettingValues.storeNSFWHistory) {
                    // Do nothing if the post is NSFW and storeNSFWHistory is not enabled
                    } else if (SettingValues.storeHistory) {
                        HasSeen.addSeen(submission.getFullName());
                    }
                    break;
                case 13:
                    LinkUtil.crosspost(submission, mContext);
                    break;
                case 28:
                    if (!isAddedToReadLaterList) {
                        ReadLater.setReadLater(submission, true);
                        Snackbar s = Snackbar.make(holder.itemView, "Added to read later!", Snackbar.LENGTH_SHORT);
                        View view = s.getView();
                        TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                        tv.setTextColor(Color.WHITE);
                        s.setAction(R.string.btn_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                ReadLater.setReadLater(submission, false);
                                Snackbar s2 = Snackbar.make(holder.itemView, "Removed from read later", Snackbar.LENGTH_SHORT);
                                View view2 = s2.getView();
                                TextView tv2 = view2.findViewById(android.support.design.R.id.snackbar_text);
                                tv2.setTextColor(Color.WHITE);
                                s2.show();
                            }
                        });
                        if (NetworkUtil.isConnected(mContext)) {
                            new CommentCacheAsync(Arrays.asList(submission), mContext, CommentCacheAsync.SAVED_SUBMISSIONS, new boolean[] { true, true }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                        s.show();
                    } else {
                        ReadLater.setReadLater(submission, false);
                        if (isReadLater || !Authentication.didOnline) {
                            final int pos = posts.indexOf(submission);
                            posts.remove(submission);
                            recyclerview.getAdapter().notifyItemRemoved(holder.getAdapterPosition());
                            Snackbar s2 = Snackbar.make(holder.itemView, "Removed from read later", Snackbar.LENGTH_SHORT);
                            View view2 = s2.getView();
                            TextView tv2 = view2.findViewById(android.support.design.R.id.snackbar_text);
                            tv2.setTextColor(Color.WHITE);
                            s2.setAction(R.string.btn_undo, new View.OnClickListener() {

                                @Override
                                public void onClick(View view) {
                                    posts.add(pos, (T) submission);
                                    recyclerview.getAdapter().notifyDataSetChanged();
                                }
                            });
                        } else {
                            Snackbar s2 = Snackbar.make(holder.itemView, "Removed from read later", Snackbar.LENGTH_SHORT);
                            View view2 = s2.getView();
                            TextView tv2 = view2.findViewById(android.support.design.R.id.snackbar_text);
                            s2.show();
                        }
                        OfflineSubreddit.newSubreddit(CommentCacheAsync.SAVED_SUBMISSIONS).deleteFromMemory(submission.getFullName());
                    }
                    break;
                case 4:
                    Reddit.defaultShareText(Html.fromHtml(submission.getTitle()).toString(), StringEscapeUtils.escapeHtml4(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();
                        }
                    }).title(R.string.report_post).alwaysCallInputCallback().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) {
                                    if (holder.itemView != null) {
                                        try {
                                            Snackbar s = Snackbar.make(holder.itemView, R.string.msg_report_sent, Snackbar.LENGTH_SHORT);
                                            View view = s.getView();
                                            TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                                            tv.setTextColor(Color.WHITE);
                                            s.show();
                                        } catch (Exception ignored) {
                                        }
                                    }
                                }
                            }.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;
                case 25:
                    final TextView showText = new TextView(mContext);
                    showText.setText(StringEscapeUtils.unescapeHtml4(submission.getTitle() + "\n\n" + submission.getSelftext()));
                    showText.setTextIsSelectable(true);
                    int sixteen = Reddit.dpToPxVertical(24);
                    showText.setPadding(sixteen, 0, sixteen, 0);
                    AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(mContext);
                    builder.setView(showText).setTitle("Select text to copy").setCancelable(true).setPositiveButton("COPY SELECTED", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String selected = showText.getText().toString().substring(showText.getSelectionStart(), showText.getSelectionEnd());
                            if (!selected.isEmpty()) {
                                ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("Selftext", selected);
                                clipboard.setPrimaryClip(clip);
                                Toast.makeText(mContext, R.string.submission_comment_copied, Toast.LENGTH_SHORT).show();
                            } else {
                                ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("Selftext", Html.fromHtml(submission.getTitle() + "\n\n" + submission.getSelftext()));
                                clipboard.setPrimaryClip(clip);
                                Toast.makeText(mContext, R.string.submission_comment_copied, Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).setNegativeButton(R.string.btn_cancel, null).setNeutralButton("COPY ALL", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clip = ClipData.newPlainText("Selftext", Html.fromHtml(submission.getTitle() + "\n\n" + submission.getSelftext()));
                            clipboard.setPrimaryClip(clip);
                            Toast.makeText(mContext, R.string.submission_comment_copied, Toast.LENGTH_SHORT).show();
                        }
                    }).show();
                    break;
            }
        }
    });
    b.show();
}
Also used : DialogInterface(android.content.DialogInterface) SpannableStringBuilder(android.text.SpannableStringBuilder) ArrayList(java.util.ArrayList) PostReadLater(me.ccrama.redditslide.Activities.PostReadLater) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TypedArray(android.content.res.TypedArray) TextView(android.widget.TextView) ClipboardManager(android.content.ClipboardManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Submission(net.dean.jraw.models.Submission) CommentCacheAsync(me.ccrama.redditslide.CommentCacheAsync) SharedPreferences(android.content.SharedPreferences) Drawable(android.graphics.drawable.Drawable) OfflineSubreddit(me.ccrama.redditslide.OfflineSubreddit) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) MediaView(me.ccrama.redditslide.Activities.MediaView) ImageView(android.widget.ImageView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) RecyclerView(android.support.v7.widget.RecyclerView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) View(android.view.View) TextView(android.widget.TextView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) InvalidScopeException(net.dean.jraw.http.oauth.InvalidScopeException) ApiException(net.dean.jraw.ApiException) DialogAction(com.afollestad.materialdialogs.DialogAction) AccountManager(net.dean.jraw.managers.AccountManager) BottomSheet(com.cocosw.bottomsheet.BottomSheet) ClipData(android.content.ClipData) Contribution(net.dean.jraw.models.Contribution) Snackbar(android.support.design.widget.Snackbar) ApiException(net.dean.jraw.ApiException)

Aggregations

DialogInterface (android.content.DialogInterface)4 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)4 CommentCacheAsync (me.ccrama.redditslide.CommentCacheAsync)4 View (android.view.View)3 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 ClipData (android.content.ClipData)2 ClipboardManager (android.content.ClipboardManager)2 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 TypedArray (android.content.res.TypedArray)2 Drawable (android.graphics.drawable.Drawable)2 AsyncTask (android.os.AsyncTask)2 Snackbar (android.support.design.widget.Snackbar)2 RecyclerView (android.support.v7.widget.RecyclerView)2 SpannableStringBuilder (android.text.SpannableStringBuilder)2 ImageView (android.widget.ImageView)2 DialogAction (com.afollestad.materialdialogs.DialogAction)2 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)2 BottomSheet (com.cocosw.bottomsheet.BottomSheet)2