add custom color preference for primary and accent colors

This commit is contained in:
dkanada 2021-05-30 11:58:05 +09:00
commit 56298b08c3
11 changed files with 150 additions and 72 deletions

View file

@ -80,6 +80,7 @@ dependencies {
implementation 'com.afollestad:material-cab:0.1.12' implementation 'com.afollestad:material-cab:0.1.12'
implementation 'me.zhanghai.android.materialprogressbar:library:1.6.1' implementation 'me.zhanghai.android.materialprogressbar:library:1.6.1'
implementation 'com.github.QuadFlask:colorpicker:ef73ced217'
implementation 'com.github.codekidX:storage-chooser:2.0.4.4' implementation 'com.github.codekidX:storage-chooser:2.0.4.4'
implementation 'com.pacioianu.david:ink-page-indicator:1.3.0' implementation 'com.pacioianu.david:ink-page-indicator:1.3.0'
implementation 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:1.0.0' implementation 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:1.0.0'

View file

@ -5,7 +5,6 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.Preference; import androidx.preference.Preference;
@ -13,11 +12,8 @@ import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.preference.TwoStatePreference; import androidx.preference.TwoStatePreference;
import com.afollestad.materialdialogs.color.ColorChooserDialog;
import com.dkanada.gramophone.databinding.ActivitySettingsBinding; import com.dkanada.gramophone.databinding.ActivitySettingsBinding;
import com.kabouzeid.appthemehelper.ThemeStore; import com.kabouzeid.appthemehelper.ThemeStore;
import com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEColorPreference;
import com.kabouzeid.appthemehelper.util.ColorUtil;
import com.dkanada.gramophone.R; import com.dkanada.gramophone.R;
import com.dkanada.gramophone.views.shortcuts.DynamicShortcutManager; import com.dkanada.gramophone.views.shortcuts.DynamicShortcutManager;
import com.dkanada.gramophone.dialogs.preferences.CategoryPreferenceDialog; import com.dkanada.gramophone.dialogs.preferences.CategoryPreferenceDialog;
@ -25,7 +21,7 @@ import com.dkanada.gramophone.dialogs.preferences.NowPlayingPreferenceDialog;
import com.dkanada.gramophone.activities.base.AbsBaseActivity; import com.dkanada.gramophone.activities.base.AbsBaseActivity;
import com.dkanada.gramophone.util.PreferenceUtil; import com.dkanada.gramophone.util.PreferenceUtil;
public class SettingsActivity extends AbsBaseActivity implements ColorChooserDialog.ColorCallback { public class SettingsActivity extends AbsBaseActivity {
private ActivitySettingsBinding binding; private ActivitySettingsBinding binding;
@Override @Override
@ -53,30 +49,6 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
} }
} }
@Override
public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) {
switch (dialog.getTitle()) {
case R.string.pref_title_primary_color:
ThemeStore.editTheme(this).primaryColor(selectedColor).commit();
PreferenceUtil.getInstance(this).setPrimaryColor(selectedColor);
break;
case R.string.pref_title_accent_color:
ThemeStore.editTheme(this).accentColor(selectedColor).commit();
PreferenceUtil.getInstance(this).setAccentColor(selectedColor);
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
new DynamicShortcutManager(this).updateDynamicShortcuts();
}
recreate();
}
@Override
public void onColorChooserDismissed(@NonNull ColorChooserDialog dialog) {
}
public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override @Override
public void onCreatePreferences(Bundle bundle, String s) { public void onCreatePreferences(Bundle bundle, String s) {
@ -106,38 +78,12 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
private void invalidateSettings() { private void invalidateSettings() {
final ATEColorPreference primaryColorPref = findPreference(PreferenceUtil.PRIMARY_COLOR);
final ATEColorPreference accentColorPref = findPreference(PreferenceUtil.ACCENT_COLOR);
final TwoStatePreference classicNotification = findPreference(PreferenceUtil.CLASSIC_NOTIFICATION); final TwoStatePreference classicNotification = findPreference(PreferenceUtil.CLASSIC_NOTIFICATION);
final TwoStatePreference coloredNotification = findPreference(PreferenceUtil.COLORED_NOTIFICATION); final TwoStatePreference coloredNotification = findPreference(PreferenceUtil.COLORED_NOTIFICATION);
final TwoStatePreference colorAppShortcuts = findPreference(PreferenceUtil.COLORED_SHORTCUTS); final TwoStatePreference colorAppShortcuts = findPreference(PreferenceUtil.COLORED_SHORTCUTS);
final Preference categoryPreference = findPreference(PreferenceUtil.CATEGORIES); final Preference categoryPreference = findPreference(PreferenceUtil.CATEGORIES);
final Preference nowPlayingPreference = findPreference(PreferenceUtil.NOW_PLAYING_SCREEN); final Preference nowPlayingPreference = findPreference(PreferenceUtil.NOW_PLAYING_SCREEN);
final int primaryColor = ThemeStore.primaryColor(requireActivity());
primaryColorPref.setColor(primaryColor, ColorUtil.darkenColor(primaryColor));
primaryColorPref.setOnPreferenceClickListener(preference -> {
new ColorChooserDialog.Builder(requireActivity(), R.string.pref_title_primary_color)
.accentMode(false)
.allowUserColorInput(true)
.allowUserColorInputAlpha(false)
.preselect(primaryColor)
.show(requireActivity());
return true;
});
final int accentColor = ThemeStore.accentColor(requireActivity());
accentColorPref.setColor(accentColor, ColorUtil.darkenColor(accentColor));
accentColorPref.setOnPreferenceClickListener(preference -> {
new ColorChooserDialog.Builder(requireActivity(), R.string.pref_title_accent_color)
.accentMode(true)
.allowUserColorInput(true)
.allowUserColorInputAlpha(false)
.preselect(accentColor)
.show(requireActivity());
return true;
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
classicNotification.setEnabled(false); classicNotification.setEnabled(false);
} }

View file

@ -178,12 +178,12 @@ public final class PreferenceUtil {
return Theme.valueOf(mPreferences.getString(GENERAL_THEME, Theme.DARK.toString())); return Theme.valueOf(mPreferences.getString(GENERAL_THEME, Theme.DARK.toString()));
} }
public void setPrimaryColor(int color) { public int getPrimaryColor() {
mPreferences.edit().putInt(PRIMARY_COLOR, color).apply(); return mPreferences.getInt(PRIMARY_COLOR, mContext.getResources().getColor(R.color.color_primary));
} }
public void setAccentColor(int color) { public int getAccentColor() {
mPreferences.edit().putInt(ACCENT_COLOR, color).apply(); return mPreferences.getInt(ACCENT_COLOR, mContext.getResources().getColor(R.color.color_accent));
} }
public final int getPageSize() { public final int getPageSize() {

View file

@ -0,0 +1,45 @@
package com.dkanada.gramophone.views;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class ColorCircleDrawable extends View {
Paint circle = new Paint();
Paint border = new Paint();
public ColorCircleDrawable(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray attributes = getContext().obtainStyledAttributes(new int[]{android.R.attr.divider});
int colorCircle = getContext().getResources().getColor(android.R.color.black);
int colorBorder = getContext().getResources().getColor(android.R.color.darker_gray);
circle.setAntiAlias(true);
circle.setColor(colorCircle);
border.setAntiAlias(true);
border.setColor(attributes.getColor(0, colorBorder));
attributes.recycle();
}
public void setColor(int color) {
circle.setColor(color);
}
@Override
@SuppressLint("CanvasSize")
protected void onDraw(Canvas canvas) {
int size = canvas.getHeight() / 2;
canvas.drawCircle(size, size, size, border);
canvas.drawCircle(size, size, size - 4f, circle);
}
}

View file

@ -0,0 +1,78 @@
package com.dkanada.gramophone.views.settings;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.dkanada.gramophone.R;
import com.dkanada.gramophone.util.PreferenceUtil;
import com.dkanada.gramophone.views.ColorCircleDrawable;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import com.kabouzeid.appthemehelper.ThemeStore;
public class ColorPreference extends Preference implements View.OnClickListener, ColorPickerClickListener {
private SharedPreferences preferences;
private ColorCircleDrawable colorView;
private int defaultValue;
public ColorPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setWidgetLayoutResource(R.layout.preference_color);
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
defaultValue = a.getInt(index, getContext().getResources().getColor(android.R.color.white));
return super.onGetDefaultValue(a, index);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
preferences = PreferenceUtil.getInstance(getContext()).getPreferences();
colorView = holder.itemView.findViewById(R.id.color);
holder.itemView.setOnClickListener(this);
colorView.setColor(preferences.getInt(getKey(), defaultValue));
}
@Override
public void onClick(DialogInterface dialog, int color, Integer[] allColors) {
preferences.edit().putInt(getKey(), color).apply();
colorView.setColor(color);
// TODO remove this when the theme helper library is removed
if (getKey().equals(PreferenceUtil.PRIMARY_COLOR)) {
ThemeStore.editTheme(getContext()).primaryColor(color).commit();
} else if (getKey().equals(PreferenceUtil.ACCENT_COLOR)) {
ThemeStore.editTheme(getContext()).accentColor(color).commit();
}
}
@Override
public void onClick(View v) {
ColorPickerDialogBuilder
.with(getContext())
.setTitle(getTitle().toString())
.initialColor(preferences.getInt(getKey(), defaultValue))
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.showColorEdit(true)
.showAlphaSlider(false)
.density(8)
.setPositiveButton(android.R.string.ok, this)
.build()
.show();
}
}

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<com.dkanada.gramophone.views.ColorCircleDrawable
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="8dp"
android:layout_gravity="end" />

View file

@ -13,21 +13,21 @@
android:positiveButtonText="@null" android:positiveButtonText="@null"
android:title="@string/pref_title_theme" /> android:title="@string/pref_title_theme" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEColorPreference <com.dkanada.gramophone.views.settings.ColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="@color/color_primary"
android:key="primary_color" android:key="primary_color"
android:persistent="false"
android:summary="@string/pref_summary_primary_color" android:summary="@string/pref_summary_primary_color"
android:title="@string/pref_title_primary_color" /> android:title="@string/pref_title_primary_color" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEColorPreference <com.dkanada.gramophone.views.settings.ColorPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="@color/color_accent"
android:key="accent_color" android:key="accent_color"
android:persistent="false"
android:summary="@string/pref_summary_accent_color" android:summary="@string/pref_summary_accent_color"
android:title="@string/pref_title_accent_color" /> android:title="@string/pref_title_accent_color" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="colored_shortcuts" android:key="colored_shortcuts"

View file

@ -19,7 +19,7 @@
android:positiveButtonText="@null" android:positiveButtonText="@null"
android:title="@string/pref_title_page_size" /> android:title="@string/pref_title_page_size" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="remember_last_tab" android:key="remember_last_tab"

View file

@ -3,14 +3,14 @@
<com.dkanada.gramophone.views.settings.JellyPreferenceCategory android:title="@string/pref_header_lock_screen"> <com.dkanada.gramophone.views.settings.JellyPreferenceCategory android:title="@string/pref_header_lock_screen">
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="show_album_cover" android:key="show_album_cover"
android:summary="@string/pref_summary_show_album_art" android:summary="@string/pref_summary_show_album_art"
android:title="@string/pref_title_show_album_art" /> android:title="@string/pref_title_show_album_art" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:dependency="show_album_cover" android:dependency="show_album_cover"

View file

@ -3,14 +3,14 @@
<com.dkanada.gramophone.views.settings.JellyPreferenceCategory android:title="@string/pref_header_notification"> <com.dkanada.gramophone.views.settings.JellyPreferenceCategory android:title="@string/pref_header_notification">
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="classic_notification" android:key="classic_notification"
android:summary="@string/pref_summary_classic_notification" android:summary="@string/pref_summary_classic_notification"
android:title="@string/pref_title_classic_notification" /> android:title="@string/pref_title_classic_notification" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="colored_notification" android:key="colored_notification"

View file

@ -29,21 +29,21 @@
android:positiveButtonText="@null" android:positiveButtonText="@null"
android:title="@string/pref_title_maximum_bitrate" /> android:title="@string/pref_title_maximum_bitrate" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="audio_ducking" android:key="audio_ducking"
android:summary="@string/pref_summary_audio_ducking" android:summary="@string/pref_summary_audio_ducking"
android:title="@string/pref_title_audio_ducking" /> android:title="@string/pref_title_audio_ducking" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="remember_shuffle" android:key="remember_shuffle"
android:summary="@string/pref_summary_remember_shuffle" android:summary="@string/pref_summary_remember_shuffle"
android:title="@string/pref_title_remember_shuffle" /> android:title="@string/pref_title_remember_shuffle" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <SwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="true" android:defaultValue="true"
android:key="remember_queue" android:key="remember_queue"