added new color picker dialog, bug fix

This commit is contained in:
nuclearfog 2023-12-02 22:13:38 +01:00
parent 3a95cacfa2
commit c9b0760efa
No known key found for this signature in database
GPG Key ID: 43E45B82006BC9D5
9 changed files with 336 additions and 156 deletions

View File

@ -60,7 +60,8 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
//noinspection GradleDependency
implementation 'com.squareup.picasso:picasso:2.8'
implementation 'com.github.QuadFlask:colorpicker:0.0.15'
implementation 'com.github.duanhong169:colorpicker:1.1.6'
implementation 'com.github.duanhong169:checkerboarddrawable:1.0.2'
implementation 'com.kyleduo.switchbutton:library:2.1.0'
implementation 'com.github.UnifiedPush:android-connector:2.1.1'
implementation 'com.google.android.material:material:1.9.0'

View File

@ -102,10 +102,10 @@
<h3>Notices for libraries:</h3>
<ul>
<li>Colorpicker</li>
<li>ColorPicker</li>
</ul>
<pre>
Copyright 2014-2017 QuadFlask
Copyright 2018 Hong Duan
Licensed under the Apache License,
Version 2.0 (the "License");
you may not use this file except

View File

@ -4,8 +4,6 @@ import static android.Manifest.permission.POST_NOTIFICATIONS;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
@ -27,15 +25,11 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorChangedListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import com.kyleduo.switchbutton.SwitchButton;
import org.nuclearfog.twidda.R;
@ -46,6 +40,8 @@ import org.nuclearfog.twidda.config.Configuration;
import org.nuclearfog.twidda.config.GlobalSettings;
import org.nuclearfog.twidda.notification.PushSubscription;
import org.nuclearfog.twidda.ui.adapter.listview.DropdownAdapter;
import org.nuclearfog.twidda.ui.dialogs.ColorPickerDialog;
import org.nuclearfog.twidda.ui.dialogs.ColorPickerDialog.OnColorSelectedListener;
import org.nuclearfog.twidda.ui.dialogs.ConfirmDialog;
import org.nuclearfog.twidda.ui.dialogs.ConfirmDialog.OnConfirmListener;
import org.nuclearfog.twidda.ui.dialogs.InfoDialog;
@ -59,8 +55,8 @@ import java.util.regex.Matcher;
*
* @author nuclearfog
*/
public class SettingsActivity extends AppCompatActivity implements OnClickListener, OnDismissListener, OnSeekBarChangeListener,
OnCheckedChangeListener, OnItemSelectedListener, OnConfirmListener, OnColorChangedListener {
public class SettingsActivity extends AppCompatActivity implements OnClickListener, OnSeekBarChangeListener,
OnCheckedChangeListener, OnItemSelectedListener, OnConfirmListener, OnColorSelectedListener {
/**
* return code to recognize {@link MainActivity} that the current account was removed from login
@ -96,7 +92,8 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
private DropdownAdapter fontAdapter, scaleAdapter;
private Dialog color_dialog_selector, appInfo, license, pushDialog;
private Dialog appInfo, license, pushDialog;
private ColorPickerDialog colorPickerDialog;
private ConfirmDialog confirmDialog;
private View enable_auth_label;
@ -106,10 +103,6 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
private ViewGroup root;
private Button[] colorButtons = new Button[COLOR_COUNT];
@IntRange(from = 0, to = COLOR_COUNT - 1)
private int mode = 0;
private int color = 0;
private AsyncCallback<DatabaseAction.Result> databaseResult = this::onDatabaseResult;
@ -165,6 +158,7 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
license = new LicenseDialog(this);
pushDialog = new WebPushDialog(this);
databaseAction = new DatabaseAction(this);
colorPickerDialog = new ColorPickerDialog(this, this);
fontAdapter = new DropdownAdapter(getApplicationContext());
scaleAdapter = new DropdownAdapter(getApplicationContext());
@ -210,6 +204,7 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
proxy_user.setVisibility(View.GONE);
proxy_pass.setVisibility(View.GONE);
}
if (settings.likeEnabled()) {
colorButtons[COLOR_FAVORITE].setText(R.string.settings_color_like);
} else {
@ -345,129 +340,47 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
else if (v.getId() == R.id.page_settings_button_logout) {
confirmDialog.show(ConfirmDialog.APP_LOG_OUT);
}
// set background color
else if (v.getId() == R.id.page_settings_color_background) {
mode = COLOR_BACKGROUND;
color = settings.getBackgroundColor();
showColorPicker(color, false);
}
// set font color
else if (v.getId() == R.id.page_settings_color_text) {
mode = COLOR_TEXT;
color = settings.getTextColor();
showColorPicker(color, false);
}
// set popup color
else if (v.getId() == R.id.page_settings_color_window) {
mode = COLOR_WINDOW;
color = settings.getPopupColor();
showColorPicker(color, false);
}
// set highlight color
else if (v.getId() == R.id.page_settings_highlight_color) {
mode = COLOR_HIGHLIGHT;
color = settings.getHighlightColor();
showColorPicker(color, false);
}
// set card color
else if (v.getId() == R.id.page_settings_color_card) {
mode = COLOR_CARD;
color = settings.getCardColor();
showColorPicker(color, true);
}
// set icon color
else if (v.getId() == R.id.page_settings_color_icon) {
mode = COLOR_ICON;
color = settings.getIconColor();
showColorPicker(color, false);
}
// set repost icon color
else if (v.getId() == R.id.page_settings_color_repost) {
mode = COLOR_REPOST;
color = settings.getRepostIconColor();
showColorPicker(color, false);
}
// set favorite icon color
else if (v.getId() == R.id.page_settings_color_favorite) {
mode = COLOR_FAVORITE;
color = settings.getFavoriteIconColor();
showColorPicker(color, false);
}
// set follow icon color
else if (v.getId() == R.id.page_settings_color_follow) {
mode = COLOR_FOLLOWING;
color = settings.getFollowIconColor();
showColorPicker(color, false);
}
// show push configuration dialog
else if (v.getId() == R.id.page_settings_enable_push_label) {
if (enablePush.isChecked()) {
pushDialog.show();
}
}
}
@Override
public void onDismiss(DialogInterface d) {
if (d == color_dialog_selector) {
switch (mode) {
case COLOR_BACKGROUND:
settings.setBackgroundColor(color);
fontAdapter.notifyDataSetChanged();
scaleAdapter.notifyDataSetChanged();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_TEXT:
settings.setTextColor(color);
fontAdapter.notifyDataSetChanged();
scaleAdapter.notifyDataSetChanged();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_WINDOW:
settings.setPopupColor(color);
AppStyles.setColorButton(colorButtons[COLOR_WINDOW], color);
break;
case COLOR_HIGHLIGHT:
settings.setHighlightColor(color);
AppStyles.setColorButton(colorButtons[COLOR_HIGHLIGHT], color);
break;
case COLOR_CARD:
settings.setCardColor(color);
fontAdapter.notifyDataSetChanged();
scaleAdapter.notifyDataSetChanged();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_ICON:
settings.setIconColor(color);
invalidateOptionsMenu();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_REPOST:
settings.setRepostIconColor(color);
AppStyles.setColorButton(colorButtons[COLOR_REPOST], color);
break;
case COLOR_FAVORITE:
settings.setFavoriteIconColor(color);
AppStyles.setColorButton(colorButtons[COLOR_FAVORITE], color);
break;
case COLOR_FOLLOWING:
settings.setFollowIconColor(color);
AppStyles.setColorButton(colorButtons[COLOR_FOLLOWING], color);
break;
}
// set background color
else if (v.getId() == R.id.page_settings_color_background) {
colorPickerDialog.show(settings.getBackgroundColor(), COLOR_BACKGROUND, false);
}
// set font color
else if (v.getId() == R.id.page_settings_color_text) {
colorPickerDialog.show(settings.getTextColor(), COLOR_TEXT, false);
}
// set popup color
else if (v.getId() == R.id.page_settings_color_window) {
colorPickerDialog.show(settings.getPopupColor(), COLOR_WINDOW, false);
}
// set highlight color
else if (v.getId() == R.id.page_settings_highlight_color) {
colorPickerDialog.show(settings.getHighlightColor(), COLOR_HIGHLIGHT, false);
}
// set card color
else if (v.getId() == R.id.page_settings_color_card) {
colorPickerDialog.show(settings.getCardColor(), COLOR_CARD, true);
}
// set icon color
else if (v.getId() == R.id.page_settings_color_icon) {
colorPickerDialog.show(settings.getIconColor(), COLOR_ICON, false);
}
// set repost icon color
else if (v.getId() == R.id.page_settings_color_repost) {
colorPickerDialog.show(settings.getRepostIconColor(), COLOR_REPOST, false);
}
// set favorite icon color
else if (v.getId() == R.id.page_settings_color_favorite) {
colorPickerDialog.show(settings.getFavoriteIconColor(), COLOR_FAVORITE, false);
}
// set follow icon color
else if (v.getId() == R.id.page_settings_color_follow) {
colorPickerDialog.show(settings.getFollowIconColor(), COLOR_FOLLOWING, false);
}
}
@ -575,8 +488,64 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
@Override
public void onColorChanged(int i) {
color = i;
public void onColorSelected(int type, int color) {
switch (type) {
case COLOR_BACKGROUND:
settings.setBackgroundColor(color);
fontAdapter.notifyDataSetChanged();
scaleAdapter.notifyDataSetChanged();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_TEXT:
settings.setTextColor(color);
fontAdapter.notifyDataSetChanged();
scaleAdapter.notifyDataSetChanged();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_WINDOW:
settings.setPopupColor(color);
AppStyles.setColorButton(colorButtons[COLOR_WINDOW], color);
break;
case COLOR_HIGHLIGHT:
settings.setHighlightColor(color);
AppStyles.setColorButton(colorButtons[COLOR_HIGHLIGHT], color);
break;
case COLOR_CARD:
settings.setCardColor(color);
fontAdapter.notifyDataSetChanged();
scaleAdapter.notifyDataSetChanged();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_ICON:
settings.setIconColor(color);
invalidateOptionsMenu();
AppStyles.setTheme(root);
setButtonColors();
break;
case COLOR_REPOST:
settings.setRepostIconColor(color);
AppStyles.setColorButton(colorButtons[COLOR_REPOST], color);
break;
case COLOR_FAVORITE:
settings.setFavoriteIconColor(color);
AppStyles.setColorButton(colorButtons[COLOR_FAVORITE], color);
break;
case COLOR_FOLLOWING:
settings.setFollowIconColor(color);
AppStyles.setColorButton(colorButtons[COLOR_FOLLOWING], color);
break;
}
}
@ -618,23 +587,6 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
}
}
/**
* show color picker dialog with preselected color
*
* @param preColor preselected color
* @param enableAlpha true to enable alpha slider
*/
private void showColorPicker(int preColor, boolean enableAlpha) {
if (color_dialog_selector == null || !color_dialog_selector.isShowing()) {
color_dialog_selector = ColorPickerDialogBuilder.with(this)
.showAlphaSlider(enableAlpha).initialColor(preColor)
.wheelType(ColorPickerView.WHEEL_TYPE.CIRCLE)
.setOnColorChangedListener(this).density(15).build();
color_dialog_selector.setOnDismissListener(this);
color_dialog_selector.show();
}
}
/**
* setup all color buttons color
*/

View File

@ -10,8 +10,6 @@ import org.nuclearfog.twidda.ui.adapter.recyclerview.holder.OnHolderClickListene
import org.nuclearfog.twidda.ui.adapter.recyclerview.holder.ReactionHolder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

View File

@ -0,0 +1,157 @@
package org.nuclearfog.twidda.ui.dialogs;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.NonNull;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.backend.utils.AppStyles;
import top.defaults.colorpicker.ColorObserver;
import top.defaults.colorpicker.ColorPickerView;
/**
* Dialog to show a color selector
*
* @author nuclearfog
*/
public class ColorPickerDialog extends Dialog implements OnClickListener, ColorObserver, TextWatcher {
private static final String KEY_COLOR = "color";
private static final String KEY_MODE = "mode";
private ColorPickerView colorPickerView;
private EditText hexCode;
private ViewGroup root;
private OnColorSelectedListener listener;
private int mode;
/**
* @param listener callback listener to set color
*/
public ColorPickerDialog(Activity activity, OnColorSelectedListener listener) {
super(activity, R.style.DefaultDialog);
this.listener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_colorpicker);
colorPickerView = findViewById(R.id.dialog_colorpicker_selector);
root = findViewById(R.id.dialog_colorpicker_root);
hexCode = findViewById(R.id.dialog_colorpicker_hex);
View confirm = findViewById(R.id.dialog_colorpicker_ok);
View cancel = findViewById(R.id.dialog_colorpicker_cancel);
hexCode.addTextChangedListener(this);
colorPickerView.subscribe(this);
confirm.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@NonNull
@Override
public Bundle onSaveInstanceState() {
Bundle bundle = super.onSaveInstanceState();
bundle.putInt(KEY_COLOR, colorPickerView.getColor());
bundle.putInt(KEY_MODE, mode);
return bundle;
}
@Override
public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mode = savedInstanceState.getInt(KEY_MODE);
colorPickerView.setInitialColor(savedInstanceState.getInt(KEY_COLOR));
}
@Override
public void show() {
// using show(int, int) instead
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.dialog_colorpicker_ok) {
listener.onColorSelected(mode, colorPickerView.getColor());
dismiss();
} else if (v.getId() == R.id.dialog_colorpicker_cancel) {
dismiss();
}
}
@Override
public void onColor(int color, boolean fromUser, boolean shouldPropagate) {
if (fromUser) {
hexCode.setText(String.format("%08X", color));
}
}
/**
* @param color start color
* @param type color type
* @param enableAlpha true to enable alpha slider
*/
public void show(int color, int type, boolean enableAlpha) {
if (!isShowing()) {
super.show();
this.mode = type;
colorPickerView.setInitialColor(color);
colorPickerView.setEnabledAlpha(enableAlpha);
hexCode.setText(String.format("%08X", color));
AppStyles.setTheme(root);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// only handle user input
if (hexCode.hasFocus()) {
String hex = s.toString();
if (hex.matches("[0123456789ABCDEFabcdef]{1,8}")) {
colorPickerView.setInitialColor(Integer.parseUnsignedInt(hex, 16));
}
}
}
/**
* callback listener to set selected color
*/
public interface OnColorSelectedListener {
/**
* called to set selected color
*
* @param type color type
* @param color color value
*/
void onColorSelected(int type, int color);
}
}

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_colorpicker_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<top.defaults.colorpicker.ColorPickerView
android:id="@+id/dialog_colorpicker_selector"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="@dimen/dialog_colorpicker_margin_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dialog_colorpicker_margin_layout"
android:text="@string/hash"
android:lines="1"
android:textSize="@dimen/dialog_colorpicker_textsize_textview" />
<EditText
android:id="@+id/dialog_colorpicker_hex"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_marginEnd="@dimen/dialog_colorpicker_margin_layout"
android:layout_weight="1"
android:maxLength="8"
android:lines="1"
android:hint="@string/hash_hint"
android:digits="0123456789ABCDEFabcdef"
android:textSize="@dimen/dialog_colorpicker_textsize_textview"
android:inputType="textNoSuggestions"
android:importantForAutofill="no"/>
<Button
android:id="@+id/dialog_colorpicker_cancel"
android:layout_width="wrap_content"
android:layout_height="@dimen/dialog_colorpicker_height_button"
android:layout_margin="@dimen/dialog_colorpicker_margin_layout"
android:lines="1"
android:text="@android:string/cancel"
android:textSize="@dimen/dialog_colorpicker_textsize_button"
style="@style/FeedbackButton" />
<Button
android:id="@+id/dialog_colorpicker_ok"
android:layout_width="wrap_content"
android:layout_height="@dimen/dialog_colorpicker_height_button"
android:layout_margin="@dimen/dialog_colorpicker_margin_layout"
android:lines="1"
android:text="@android:string/ok"
android:textSize="@dimen/dialog_colorpicker_textsize_button"
style="@style/FeedbackButton" />
</LinearLayout>
</LinearLayout>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/CardView">

View File

@ -328,7 +328,6 @@
<dimen name="item_filter_button_size">20sp</dimen>
<!--dimens of item_announcement.xml-->
<dimen name="item_announcement_list_reaction_height">26dp</dimen>
<dimen name="item_announcement_card_padding">8dp</dimen>
<dimen name="item_announcement_view_margin">5dp</dimen>
<dimen name="item_announcement_textsize_content">14sp</dimen>
@ -371,6 +370,12 @@
<dimen name="dialog_report_button_padding">5dp</dimen>
<dimen name="dialog_report_button_height">24sp</dimen>
<!--dimens of dialog_colorpicker.xml-->
<dimen name="dialog_colorpicker_margin_layout">5dp</dimen>
<dimen name="dialog_colorpicker_textsize_button">14sp</dimen>
<dimen name="dialog_colorpicker_textsize_textview">16sp</dimen>
<dimen name="dialog_colorpicker_height_button">24sp</dimen>
<!--dimens of dialog_description.xml-->
<dimen name="dialog_description_textsize_title">22sp</dimen>
<dimen name="dialog_description_button_padding">5dp</dimen>

View File

@ -13,6 +13,8 @@
<string name="login_sec_opt" translatable="false">2.</string>
<string name="login_trd_opt" translatable="false">3.</string>
<string name="login_4th_opt" translatable="false">4.</string>
<string name="hash" translatable="false">#</string>
<string name="hash_hint" translatable="false">#FFFFFFFF</string>
<!-- toast messages to inform user -->
<string name="info_list_removed">Userlist removed</string>