From c9b0760efad231d5c9bf4c9a21253396f714891d Mon Sep 17 00:00:00 2001 From: nuclearfog Date: Sat, 2 Dec 2023 22:13:38 +0100 Subject: [PATCH] added new color picker dialog, bug fix --- app/build.gradle | 3 +- app/src/main/assets/licenses.html | 4 +- .../ui/activities/SettingsActivity.java | 250 +++++++----------- .../adapter/recyclerview/ReactionAdapter.java | 2 - .../twidda/ui/dialogs/ColorPickerDialog.java | 157 +++++++++++ .../main/res/layout/dialog_colorpicker.xml | 66 +++++ app/src/main/res/layout/item_announcement.xml | 1 - app/src/main/res/values/dimens.xml | 7 +- app/src/main/res/values/strings.xml | 2 + 9 files changed, 336 insertions(+), 156 deletions(-) create mode 100644 app/src/main/java/org/nuclearfog/twidda/ui/dialogs/ColorPickerDialog.java create mode 100644 app/src/main/res/layout/dialog_colorpicker.xml diff --git a/app/build.gradle b/app/build.gradle index 80849b93..8bb8a7e9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/assets/licenses.html b/app/src/main/assets/licenses.html index bcf59354..55c04d07 100644 --- a/app/src/main/assets/licenses.html +++ b/app/src/main/assets/licenses.html @@ -102,10 +102,10 @@

Notices for libraries:

-            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
diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java b/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java
index cee4e721..bbe79204 100644
--- a/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java
+++ b/app/src/main/java/org/nuclearfog/twidda/ui/activities/SettingsActivity.java
@@ -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 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
 	 */
diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/adapter/recyclerview/ReactionAdapter.java b/app/src/main/java/org/nuclearfog/twidda/ui/adapter/recyclerview/ReactionAdapter.java
index 8f2cbbd0..ce540c55 100644
--- a/app/src/main/java/org/nuclearfog/twidda/ui/adapter/recyclerview/ReactionAdapter.java
+++ b/app/src/main/java/org/nuclearfog/twidda/ui/adapter/recyclerview/ReactionAdapter.java
@@ -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;
 
diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/ColorPickerDialog.java b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/ColorPickerDialog.java
new file mode 100644
index 00000000..e9e15771
--- /dev/null
+++ b/app/src/main/java/org/nuclearfog/twidda/ui/dialogs/ColorPickerDialog.java
@@ -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);
+	}
+}
\ No newline at end of file
diff --git a/app/src/main/res/layout/dialog_colorpicker.xml b/app/src/main/res/layout/dialog_colorpicker.xml
new file mode 100644
index 00000000..dd8d09e6
--- /dev/null
+++ b/app/src/main/res/layout/dialog_colorpicker.xml
@@ -0,0 +1,66 @@
+
+
+
+	
+
+	
+
+		
+
+		
+
+