Search in sources :

Example 1 with ColorPickerPagerAdapter

use of com.moez.QKSMS.ui.dialog.ColorPickerPagerAdapter in project qksms by moezbhatti.

the class ThemeManager method showColorPicker.

private static void showColorPicker(QKActivity context, View.OnClickListener saveListener) {
    final QKDialog dialog = new QKDialog();
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_color_picker, null, false);
    ColorPickerViewHolder holder = new ColorPickerViewHolder(view);
    holder.mTab1.setBackgroundDrawable(getRippleBackground());
    holder.mTab2.setBackgroundDrawable(getRippleBackground());
    holder.mTab1.setOnClickListener(v -> holder.mPager.setCurrentItem(0));
    holder.mTab2.setOnClickListener(v -> holder.mPager.setCurrentItem(1));
    ColorPickerPagerAdapter adapter = new ColorPickerPagerAdapter(context);
    holder.mPager.setAdapter(adapter);
    holder.mPager.setOffscreenPageLimit(1);
    holder.mPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            holder.mTab1.setTextColor(position == 0 ? getColor() : getTextOnBackgroundPrimary());
            holder.mTab2.setTextColor(position == 1 ? getColor() : getTextOnBackgroundPrimary());
        }
    });
    int swatchColor = getSwatchColor(getColor());
    holder.mPalette.init(19, 4, color -> {
        holder.mPalette.init(getSwatch(color).length, 4, color2 -> {
            setActiveColor(color2);
            saveListener.onClick(null);
            dialog.dismiss();
        });
        holder.mPalette.drawPalette(getSwatch(color), getColor());
    });
    holder.mPalette.drawPalette(PALETTE, swatchColor);
    if (swatchColor == getColor()) {
        // If the current theme was set from the RGB picker, show that page instead
        holder.mPager.setCurrentItem(1);
    }
    SeekBar.OnSeekBarChangeListener seekListener = new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int color = getColor();
            color = Color.rgb(seekBar == holder.mRed ? progress : Color.red(color), seekBar == holder.mGreen ? progress : Color.green(color), seekBar == holder.mBlue ? progress : Color.blue(color));
            if (seekBar == holder.mRed)
                holder.mRedValue.setText(String.valueOf(progress));
            if (seekBar == holder.mGreen)
                holder.mGreenValue.setText(String.valueOf(progress));
            if (seekBar == holder.mBlue)
                holder.mBlueValue.setText(String.valueOf(progress));
            setActiveColor(color);
            String colorString = Integer.toHexString(color);
            holder.mHex.setTextChangedListenerEnabled(false);
            holder.mHex.setText(colorString.substring(colorString.length() > 6 ? colorString.length() - 6 : 0));
            holder.mHex.setTextChangedListenerEnabled(true);
            holder.mError.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    };
    Drawable thumbRed = ContextCompat.getDrawable(mContext, R.drawable.seek_thumb);
    Drawable thumbGreen = ContextCompat.getDrawable(mContext, R.drawable.seek_thumb);
    Drawable thumbBlue = ContextCompat.getDrawable(mContext, R.drawable.seek_thumb);
    LiveViewManager.registerView(QKPreference.THEME, holder.mPreview, key -> {
        holder.mPreview.setBackgroundColor(getColor());
        holder.mRed.getProgressDrawable().setColorFilter(getColor(), PorterDuff.Mode.MULTIPLY);
        holder.mGreen.getProgressDrawable().setColorFilter(getColor(), PorterDuff.Mode.MULTIPLY);
        holder.mBlue.getProgressDrawable().setColorFilter(getColor(), PorterDuff.Mode.MULTIPLY);
        if (holder.mPager.getCurrentItem() == 0) {
            holder.mTab1.setTextColor(getColor());
        } else {
            holder.mTab2.setTextColor(getColor());
        }
    });
    LiveViewManager.registerView(QKPreference.BACKGROUND, holder.mHex, key -> {
        holder.mHex.getBackground().setColorFilter(0x22888888, PorterDuff.Mode.MULTIPLY);
        holder.mError.setColorFilter(getTextOnBackgroundSecondary(), PorterDuff.Mode.SRC_ATOP);
    });
    holder.mRed.setThumb(thumbRed);
    holder.mRed.setOnSeekBarChangeListener(seekListener);
    holder.mGreen.setThumb(thumbGreen);
    holder.mGreen.setOnSeekBarChangeListener(seekListener);
    holder.mBlue.setThumb(thumbBlue);
    holder.mBlue.setOnSeekBarChangeListener(seekListener);
    holder.mRed.setProgress(Color.red(getColor()));
    holder.mGreen.setProgress(Color.green(getColor()));
    holder.mBlue.setProgress(Color.blue(getColor()));
    String colorString = Integer.toHexString(getColor());
    holder.mHex.setText(colorString.substring(colorString.length() > 6 ? colorString.length() - 6 : 0));
    holder.mHex.setOnEditorActionListener((v, actionId, event) -> {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            holder.mHex.clearFocus();
            KeyboardUtils.hide(mContext, holder.mHex);
        }
        return false;
    });
    holder.mHex.setTextChangedListener(s -> {
        if (s.length() == 6) {
            int color = Color.parseColor("#" + s.toString());
            holder.mError.setVisibility(View.INVISIBLE);
            if (color != getColor()) {
                holder.mRed.setProgress(Color.red(color));
                holder.mGreen.setProgress(Color.green(color));
                holder.mBlue.setProgress(Color.blue(color));
            }
        } else {
            holder.mError.setVisibility(View.VISIBLE);
        }
    });
    dialog.setContext(context).setCustomView(view).setNegativeButton(R.string.cancel, v -> setActiveColor(getThemeColor())).setPositiveButton(R.string.save, saveListener).show();
}
Also used : SeekBar(android.widget.SeekBar) Drawable(android.graphics.drawable.Drawable) QKTextView(com.moez.QKSMS.ui.view.QKTextView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) ViewPager(android.support.v4.view.ViewPager) QKDialog(com.moez.QKSMS.ui.dialog.QKDialog) ColorPickerPagerAdapter(com.moez.QKSMS.ui.dialog.ColorPickerPagerAdapter)

Aggregations

Drawable (android.graphics.drawable.Drawable)1 ViewPager (android.support.v4.view.ViewPager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 SeekBar (android.widget.SeekBar)1 ColorPickerPagerAdapter (com.moez.QKSMS.ui.dialog.ColorPickerPagerAdapter)1 QKDialog (com.moez.QKSMS.ui.dialog.QKDialog)1 QKTextView (com.moez.QKSMS.ui.view.QKTextView)1