Search in sources :

Example 1 with Account

use of net.dean.jraw.models.Account in project Slide by ccrama.

the class Profile method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case (android.R.id.home):
            onBackPressed();
            break;
        case (R.id.category):
            new AsyncTask<Void, Void, List<String>>() {

                Dialog d;

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

                @Override
                protected List<String> doInBackground(Void... params) {
                    try {
                        List<String> categories = new ArrayList<>(new AccountManager(Authentication.reddit).getSavedCategories());
                        categories.add(0, "No category");
                        return categories;
                    } catch (Exception e) {
                        e.printStackTrace();
                        // probably has no categories?
                        return new ArrayList<String>() {

                            {
                                add(0, "No category");
                            }
                        };
                    }
                }

                @Override
                public void onPostExecute(final List<String> data) {
                    try {
                        new MaterialDialog.Builder(Profile.this).items(data).title(R.string.profile_category_select).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 == 0)
                                    category = null;
                                else
                                    category = t;
                                int current = pager.getCurrentItem();
                                ProfilePagerAdapter adapter = new ProfilePagerAdapter(getSupportFragmentManager());
                                pager.setAdapter(adapter);
                                pager.setOffscreenPageLimit(1);
                                tabs.setupWithViewPager(pager);
                                pager.setCurrentItem(current);
                            }
                        }).show();
                        if (d != null) {
                            d.dismiss();
                        }
                    } catch (Exception ignored) {
                    }
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            break;
        case (R.id.info):
            if (account != null && trophyCase != null) {
                LayoutInflater inflater = getLayoutInflater();
                final View dialoglayout = inflater.inflate(R.layout.colorprofile, null);
                final TextView title = dialoglayout.findViewById(R.id.title);
                title.setText(name);
                if (account.getDataNode().has("is_employee") && account.getDataNode().get("is_employee").asBoolean()) {
                    SpannableStringBuilder admin = new SpannableStringBuilder("[A]");
                    admin.setSpan(new RelativeSizeSpan(.67f), 0, admin.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    title.append(" ");
                    title.append(admin);
                }
                dialoglayout.findViewById(R.id.share).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Reddit.defaultShareText(getString(R.string.profile_share, name), "https://www.reddit.com/u/" + name, Profile.this);
                    }
                });
                final int currentColor = Palette.getColorUser(name);
                title.setBackgroundColor(currentColor);
                String info = getString(R.string.profile_age, TimeUtils.getTimeSince(account.getCreated().getTime(), Profile.this));
                /*todo better if (account.hasGold() &&account.getDataNode().has("gold_expiration") ) {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(account.getDataNode().get("gold_expiration").asLong());
                    info.append("Gold expires on " + new SimpleDateFormat("dd/MM/yy").format(c.getTime()));
                }*/
                ((TextView) dialoglayout.findViewById(R.id.moreinfo)).setText(info);
                String tag = UserTags.getUserTag(name);
                if (tag.isEmpty()) {
                    tag = getString(R.string.profile_tag_user);
                } else {
                    tag = getString(R.string.profile_tag_user_existing, tag);
                }
                ((TextView) dialoglayout.findViewById(R.id.tagged)).setText(tag);
                LinearLayout l = dialoglayout.findViewById(R.id.trophies_inner);
                dialoglayout.findViewById(R.id.tag).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        MaterialDialog.Builder b = new MaterialDialog.Builder(Profile.this).title(getString(R.string.profile_tag_set, name)).input(getString(R.string.profile_tag), UserTags.getUserTag(name), false, (dialog, input) -> {
                        }).positiveText(R.string.profile_btn_tag).neutralText(R.string.btn_cancel);
                        if (UserTags.isUserTagged(name)) {
                            b.negativeText(R.string.profile_btn_untag);
                        }
                        b.onPositive(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(MaterialDialog dialog, DialogAction which) {
                                UserTags.setUserTag(name, dialog.getInputEditText().getText().toString());
                                String tag = UserTags.getUserTag(name);
                                if (tag.isEmpty()) {
                                    tag = getString(R.string.profile_tag_user);
                                } else {
                                    tag = getString(R.string.profile_tag_user_existing, tag);
                                }
                                ((TextView) dialoglayout.findViewById(R.id.tagged)).setText(tag);
                            }
                        }).onNeutral(null).onNegative(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(MaterialDialog dialog, DialogAction which) {
                                UserTags.removeUserTag(name);
                                String tag = UserTags.getUserTag(name);
                                if (tag.isEmpty()) {
                                    tag = getString(R.string.profile_tag_user);
                                } else {
                                    tag = getString(R.string.profile_tag_user_existing, tag);
                                }
                                ((TextView) dialoglayout.findViewById(R.id.tagged)).setText(tag);
                            }
                        }).show();
                    }
                });
                if (trophyCase.isEmpty()) {
                    dialoglayout.findViewById(R.id.trophies).setVisibility(View.GONE);
                } else {
                    for (final Trophy t : trophyCase) {
                        View view = getLayoutInflater().inflate(R.layout.trophy, null);
                        ((Reddit) getApplicationContext()).getImageLoader().displayImage(t.getIcon(), ((ImageView) view.findViewById(R.id.image)));
                        ((TextView) view.findViewById(R.id.trophyTitle)).setText(t.getFullName());
                        if (t.getAboutUrl() != null && !t.getAboutUrl().equalsIgnoreCase("null")) {
                            view.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    LinkUtil.openUrl(LinkUtil.formatURL(t.getAboutUrl()).toString(), Palette.getColorUser(account.getFullName()), Profile.this);
                                }
                            });
                        }
                        l.addView(view);
                    }
                }
                if (Authentication.isLoggedIn) {
                    dialoglayout.findViewById(R.id.pm).setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(Profile.this, SendMessage.class);
                            i.putExtra(SendMessage.EXTRA_NAME, name);
                            startActivity(i);
                        }
                    });
                    friend = account.isFriend();
                    if (friend) {
                        ((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_remove_friend);
                    } else {
                        ((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_add_friend);
                    }
                    dialoglayout.findViewById(R.id.friend_body).setOnClickListener(new View.OnClickListener() {

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

                                @Override
                                protected Void doInBackground(Void... params) {
                                    if (friend) {
                                        try {
                                            new AccountManager(Authentication.reddit).deleteFriend(name);
                                        } catch (Exception ignored) {
                                        // Will throw java.lang.IllegalStateException: No Content-Type header was found, but it still works.
                                        }
                                        friend = false;
                                    } else {
                                        new AccountManager(Authentication.reddit).updateFriend(name);
                                        friend = true;
                                    }
                                    return null;
                                }

                                @Override
                                public void onPostExecute(Void voids) {
                                    if (friend) {
                                        ((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_remove_friend);
                                    } else {
                                        ((TextView) dialoglayout.findViewById(R.id.friend)).setText(R.string.profile_add_friend);
                                    }
                                }
                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    });
                    dialoglayout.findViewById(R.id.block_body).setOnClickListener(new View.OnClickListener() {

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

                                @Override
                                protected Boolean doInBackground(Void... params) {
                                    Map<String, String> map = new HashMap();
                                    map.put("account_id", "t2_" + account.getId());
                                    try {
                                        Authentication.reddit.execute(Authentication.reddit.request().post(map).path("/api/block_user").build());
                                    } catch (Exception ex) {
                                        return false;
                                    }
                                    return true;
                                }

                                @Override
                                public void onPostExecute(Boolean blocked) {
                                    if (!blocked) {
                                        Toast.makeText(getBaseContext(), getString(R.string.err_block_user), Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(getBaseContext(), getString(R.string.success_block_user), Toast.LENGTH_LONG).show();
                                    }
                                }
                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    });
                } else {
                    dialoglayout.findViewById(R.id.pm).setVisibility(View.GONE);
                }
                dialoglayout.findViewById(R.id.multi_body).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent inte = new Intent(Profile.this, MultiredditOverview.class);
                        inte.putExtra(EXTRA_PROFILE, name);
                        Profile.this.startActivity(inte);
                    }
                });
                final View body = dialoglayout.findViewById(R.id.body2);
                body.setVisibility(View.INVISIBLE);
                final View center = dialoglayout.findViewById(R.id.colorExpandFrom);
                dialoglayout.findViewById(R.id.color).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        int cx = center.getWidth() / 2;
                        int cy = center.getHeight() / 2;
                        int finalRadius = Math.max(body.getWidth(), body.getHeight());
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            Animator anim = ViewAnimationUtils.createCircularReveal(body, cx, cy, 0, finalRadius);
                            body.setVisibility(View.VISIBLE);
                            anim.start();
                        } else {
                            body.setVisibility(View.VISIBLE);
                        }
                    }
                });
                LineColorPicker colorPicker = dialoglayout.findViewById(R.id.picker);
                final LineColorPicker colorPicker2 = dialoglayout.findViewById(R.id.picker2);
                colorPicker.setColors(ColorPreferences.getBaseColors(Profile.this));
                colorPicker.setOnColorChangedListener(new OnColorChangedListener() {

                    @Override
                    public void onColorChanged(int c) {
                        colorPicker2.setColors(ColorPreferences.getColors(getBaseContext(), c));
                        colorPicker2.setSelectedColor(c);
                    }
                });
                for (int i : colorPicker.getColors()) {
                    for (int i2 : ColorPreferences.getColors(getBaseContext(), i)) {
                        if (i2 == currentColor) {
                            colorPicker.setSelectedColor(i);
                            colorPicker2.setColors(ColorPreferences.getColors(getBaseContext(), i));
                            colorPicker2.setSelectedColor(i2);
                            break;
                        }
                    }
                }
                colorPicker2.setOnColorChangedListener(new OnColorChangedListener() {

                    @Override
                    public void onColorChanged(int i) {
                        findViewById(R.id.header).setBackgroundColor(colorPicker2.getColor());
                        if (mToolbar != null)
                            mToolbar.setBackgroundColor(colorPicker2.getColor());
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            Window window = getWindow();
                            window.setStatusBarColor(Palette.getDarkerColor(colorPicker2.getColor()));
                        }
                        title.setBackgroundColor(colorPicker2.getColor());
                    }
                });
                {
                    TextView dialogButton = dialoglayout.findViewById(R.id.ok);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Palette.setColorUser(name, colorPicker2.getColor());
                            int cx = center.getWidth() / 2;
                            int cy = center.getHeight() / 2;
                            int initialRadius = body.getWidth();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                Animator anim = ViewAnimationUtils.createCircularReveal(body, cx, cy, initialRadius, 0);
                                anim.addListener(new AnimatorListenerAdapter() {

                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        super.onAnimationEnd(animation);
                                        body.setVisibility(View.GONE);
                                    }
                                });
                                anim.start();
                            } else {
                                body.setVisibility(View.GONE);
                            }
                        }
                    });
                }
                {
                    final TextView dialogButton = dialoglayout.findViewById(R.id.reset);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Palette.removeUserColor(name);
                            Snackbar.make(dialogButton, "User color removed", Snackbar.LENGTH_SHORT).show();
                            int cx = center.getWidth() / 2;
                            int cy = center.getHeight() / 2;
                            int initialRadius = body.getWidth();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                Animator anim = ViewAnimationUtils.createCircularReveal(body, cx, cy, initialRadius, 0);
                                anim.addListener(new AnimatorListenerAdapter() {

                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        super.onAnimationEnd(animation);
                                        body.setVisibility(View.GONE);
                                    }
                                });
                                anim.start();
                            } else {
                                body.setVisibility(View.GONE);
                            }
                        }
                    });
                }
                ((TextView) dialoglayout.findViewById(R.id.commentkarma)).setText(String.format(Locale.getDefault(), "%d", account.getCommentKarma()));
                ((TextView) dialoglayout.findViewById(R.id.linkkarma)).setText(String.format(Locale.getDefault(), "%d", account.getLinkKarma()));
                ((TextView) dialoglayout.findViewById(R.id.totalKarma)).setText(String.format(Locale.getDefault(), "%d", account.getCommentKarma() + account.getLinkKarma()));
                new AlertDialog.Builder(Profile.this).setOnDismissListener(dialogInterface -> {
                    findViewById(R.id.header).setBackgroundColor(currentColor);
                    if (mToolbar != null)
                        mToolbar.setBackgroundColor(currentColor);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        Window window = getWindow();
                        window.setStatusBarColor(Palette.getDarkerColor(currentColor));
                    }
                }).setView(dialoglayout).show();
            }
            return true;
        case (R.id.sort):
            openPopup();
            return true;
    }
    return false;
}
Also used : LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) R(me.ccrama.redditslide.R) TimeUtils(me.ccrama.redditslide.util.TimeUtils) NonNull(androidx.annotation.NonNull) Spannable(android.text.Spannable) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) Animator(android.animation.Animator) Locale(java.util.Locale) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Sorting(net.dean.jraw.paginators.Sorting) FragmentStatePagerAdapter(androidx.fragment.app.FragmentStatePagerAdapter) Log(android.util.Log) AsyncTask(android.os.AsyncTask) TabLayout(com.google.android.material.tabs.TabLayout) Authentication(me.ccrama.redditslide.Authentication) ViewAnimationUtils(android.view.ViewAnimationUtils) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) Palette(me.ccrama.redditslide.Visuals.Palette) List(java.util.List) TextView(android.widget.TextView) SortingUtil(me.ccrama.redditslide.util.SortingUtil) LinearInterpolator(android.view.animation.LinearInterpolator) LineColorPicker(uz.shift.colorpicker.LineColorPicker) ContributionsView(me.ccrama.redditslide.Fragments.ContributionsView) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Window(android.view.Window) Snackbar(com.google.android.material.snackbar.Snackbar) LayoutUtils(me.ccrama.redditslide.util.LayoutUtils) Spanned(android.text.Spanned) AlertDialog(androidx.appcompat.app.AlertDialog) ViewPager(androidx.viewpager.widget.ViewPager) HistoryView(me.ccrama.redditslide.Fragments.HistoryView) Dialog(android.app.Dialog) Intent(android.content.Intent) HashMap(java.util.HashMap) LogUtil(me.ccrama.redditslide.util.LogUtil) MenuItem(android.view.MenuItem) AccountManager(net.dean.jraw.managers.AccountManager) ArrayList(java.util.ArrayList) SpannableStringBuilder(android.text.SpannableStringBuilder) MenuInflater(android.view.MenuInflater) Toast(android.widget.Toast) Menu(android.view.Menu) Build(android.os.Build) TimePeriod(net.dean.jraw.paginators.TimePeriod) OnColorChangedListener(uz.shift.colorpicker.OnColorChangedListener) FragmentManager(androidx.fragment.app.FragmentManager) FluentRedditClient(net.dean.jraw.fluent.FluentRedditClient) UserTags(me.ccrama.redditslide.UserTags) LayoutInflater(android.view.LayoutInflater) Reddit(me.ccrama.redditslide.Reddit) RelativeSizeSpan(android.text.style.RelativeSizeSpan) DialogAction(com.afollestad.materialdialogs.DialogAction) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) Trophy(net.dean.jraw.models.Trophy) PopupMenu(androidx.appcompat.widget.PopupMenu) Gravity(android.view.Gravity) Account(net.dean.jraw.models.Account) LinkUtil(me.ccrama.redditslide.util.LinkUtil) AlertDialog(androidx.appcompat.app.AlertDialog) Trophy(net.dean.jraw.models.Trophy) HashMap(java.util.HashMap) SpannableStringBuilder(android.text.SpannableStringBuilder) ArrayList(java.util.ArrayList) RelativeSizeSpan(android.text.style.RelativeSizeSpan) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) AlertDialog(androidx.appcompat.app.AlertDialog) Dialog(android.app.Dialog) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) List(java.util.List) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) ImageView(android.widget.ImageView) Window(android.view.Window) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContributionsView(me.ccrama.redditslide.Fragments.ContributionsView) HistoryView(me.ccrama.redditslide.Fragments.HistoryView) LineColorPicker(uz.shift.colorpicker.LineColorPicker) Animator(android.animation.Animator) DialogAction(com.afollestad.materialdialogs.DialogAction) OnColorChangedListener(uz.shift.colorpicker.OnColorChangedListener) LayoutInflater(android.view.LayoutInflater) AccountManager(net.dean.jraw.managers.AccountManager) SpannableStringBuilder(android.text.SpannableStringBuilder) LinearLayout(android.widget.LinearLayout)

Aggregations

Animator (android.animation.Animator)1 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 Dialog (android.app.Dialog)1 Intent (android.content.Intent)1 AsyncTask (android.os.AsyncTask)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 Spannable (android.text.Spannable)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 RelativeSizeSpan (android.text.style.RelativeSizeSpan)1 Log (android.util.Log)1 Gravity (android.view.Gravity)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewAnimationUtils (android.view.ViewAnimationUtils)1 Window (android.view.Window)1