Search in sources :

Example 16 with AccountManager

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

the class PopulateSubmissionViewHolder method categorizeSaved.

private void categorizeSaved(final Submission submission, View itemView, final Context mContext) {
    new AsyncTask<Void, Void, List<String>>() {

        Dialog d;

        @Override
        public void onPreExecute() {
            d = new MaterialDialog.Builder(mContext).progress(true, 100).title(R.string.profile_category_loading).content(R.string.misc_please_wait).show();
        }

        @Override
        protected List<String> doInBackground(Void... params) {
            try {
                List<String> categories = new ArrayList<String>(new AccountManager(Authentication.reddit).getSavedCategories());
                categories.add("New category");
                return categories;
            } catch (Exception e) {
                e.printStackTrace();
                return new ArrayList<String>() {

                    {
                        add("New category");
                    }
                };
            // sub probably has no flairs?
            }
        }

        @Override
        public void onPostExecute(final List<String> data) {
            try {
                new MaterialDialog.Builder(mContext).items(data).title(R.string.sidebar_select_flair).itemsCallback(new MaterialDialog.ListCallback() {

                    @Override
                    public void onSelection(MaterialDialog dialog, final View itemView, int which, CharSequence text) {
                        final String t = data.get(which);
                        if (which == data.size() - 1) {
                            new MaterialDialog.Builder(mContext).title(R.string.category_set_name).input(mContext.getString(R.string.category_set_name_hint), null, false, 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 AccountManager(Authentication.reddit).save(submission, flair);
                                                return true;
                                            } catch (ApiException e) {
                                                e.printStackTrace();
                                                return false;
                                            }
                                        }

                                        @Override
                                        protected void onPostExecute(Boolean done) {
                                            Snackbar s;
                                            if (done) {
                                                if (itemView != null) {
                                                    s = Snackbar.make(itemView, R.string.submission_info_saved, Snackbar.LENGTH_SHORT);
                                                    View view = s.getView();
                                                    TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                                                    tv.setTextColor(Color.WHITE);
                                                    s.show();
                                                }
                                            } else {
                                                if (itemView != null) {
                                                    s = Snackbar.make(itemView, R.string.category_set_error, Snackbar.LENGTH_SHORT);
                                                    View view = s.getView();
                                                    TextView tv = 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 AccountManager(Authentication.reddit).save(submission, t);
                                        return true;
                                    } catch (ApiException e) {
                                        e.printStackTrace();
                                        return false;
                                    }
                                }

                                @Override
                                protected void onPostExecute(Boolean done) {
                                    Snackbar s;
                                    if (done) {
                                        if (itemView != null) {
                                            s = Snackbar.make(itemView, R.string.submission_info_saved, Snackbar.LENGTH_SHORT);
                                            View view = s.getView();
                                            TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                                            tv.setTextColor(Color.WHITE);
                                            s.show();
                                        }
                                    } else {
                                        if (itemView != null) {
                                            s = Snackbar.make(itemView, R.string.category_set_error, Snackbar.LENGTH_SHORT);
                                            View view = s.getView();
                                            TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
                                            tv.setTextColor(Color.WHITE);
                                            s.show();
                                        }
                                    }
                                }
                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    }
                }).show();
                if (d != null) {
                    d.dismiss();
                }
            } catch (Exception ignored) {
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) ArrayList(java.util.ArrayList) Dialog(android.app.Dialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArrayList(java.util.ArrayList) List(java.util.List) TextView(android.widget.TextView) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) AsyncTask(android.os.AsyncTask) 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) ApiException(net.dean.jraw.ApiException) 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