From 00817b79bea00f872b9621d8dbe7bcd053a25f1e Mon Sep 17 00:00:00 2001 From: kosyak Date: Mon, 2 Sep 2024 00:12:42 +0200 Subject: [PATCH] account color selector --- build.gradle | 1 + .../ui/ManageAccountActivity.java | 36 +++++++++++++++++- .../ui/adapter/AccountAdapter.java | 32 ++++++++++++++-- .../ui/adapter/ConversationAdapter.java | 2 +- .../ui/adapter/ListItemAdapter.java | 2 +- .../siacs/conversations/utils/UIHelper.java | 18 +++++++++ src/main/res/layout/account_row.xml | 38 ++++++++++++------- 7 files changed, 107 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index c6e5f252d..c9c8eabb7 100644 --- a/build.gradle +++ b/build.gradle @@ -83,6 +83,7 @@ dependencies { implementation 'com.github.singpolyma:TokenAutoComplete:bfa93780e0' + implementation 'com.github.kizitonwose.colorpreference:core:1.1.0' implementation 'com.github.kizitonwose.colorpreference:support:1.1.0' implementation 'com.caverock:androidsvg-aar:1.4' implementation 'com.github.singpolyma:Better-Link-Movement-Method:4df081e1e4' diff --git a/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java b/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java index aab32220d..67803b387 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java @@ -36,6 +36,7 @@ import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate; import eu.siacs.conversations.ui.adapter.AccountAdapter; import eu.siacs.conversations.ui.util.MenuDoubleTabUtil; +import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.XmppConnection; @@ -44,8 +45,14 @@ import static eu.siacs.conversations.utils.PermissionUtils.writeGranted; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.navigation.NavigationBarView; +import com.kizitonwose.colorpreference.ColorDialog; +import com.kizitonwose.colorpreference.ColorShape; -public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate, KeyChainAliasCallback, XmppConnectionService.OnAccountCreated, AccountAdapter.OnTglAccountState { +public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate, + KeyChainAliasCallback, + XmppConnectionService.OnAccountCreated, + AccountAdapter.OnTglAccountState, + ColorDialog.OnColorSelectedListener { private final String STATE_SELECTED_ACCOUNT = "selected_account"; @@ -61,6 +68,18 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda protected Pair mPostponedActivityResult = null; + private AccountAdapter.ColorSelectorListener colorSelectorListener = new AccountAdapter.ColorSelectorListener() { + @Override + public void onColorPickerRequested(Jid accountJid, int currentColor) { + new ColorDialog.Builder(ManageAccountActivity.this) + .setColorShape(ColorShape.CIRCLE) + .setColorChoices(R.array.themeColorsOverride) + .setSelectedColor(currentColor) + .setTag(accountJid.asBareJid().toEscapedString()) + .show(); + } + }; + @Override public void onAccountUpdate() { refreshUi(); @@ -102,7 +121,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda } accountListView = findViewById(R.id.account_list); - this.mAccountAdapter = new AccountAdapter(this, accountList); + this.mAccountAdapter = new AccountAdapter(this, accountList, colorSelectorListener); accountListView.setAdapter(this.mAccountAdapter); accountListView.setOnItemClickListener((arg0, view, position, arg3) -> switchToAccount(accountList.get(position))); registerForContextMenu(accountListView); @@ -158,6 +177,13 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda super.onSaveInstanceState(savedInstanceState); } + @Override + protected void onDestroy() { + super.onDestroy(); + colorSelectorListener = null; + mAccountAdapter.colorSelectorListener = null; + } + @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); @@ -349,6 +375,12 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda } } + @Override + public void onColorSelected(int newColor, String tag) { + UIHelper.overrideAccountColor(this, tag, newColor); + refreshUiReal(); + } + private void addAccountFromKey() { try { KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null); diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java index a074fd6c8..6a23b2cf8 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java @@ -9,6 +9,11 @@ import android.widget.ArrayAdapter; import androidx.annotation.NonNull; import androidx.databinding.DataBindingUtil; +import com.kizitonwose.colorpreference.ColorDialog; +import com.kizitonwose.colorpreference.ColorPreference; +import com.kizitonwose.colorpreference.ColorShape; +import com.kizitonwose.colorpreference.ColorUtils; + import java.util.List; import eu.siacs.conversations.Config; @@ -19,22 +24,29 @@ import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.util.AvatarWorkerTask; import eu.siacs.conversations.ui.util.StyledAttributes; import eu.siacs.conversations.utils.UIHelper; +import eu.siacs.conversations.xmpp.Jid; public class AccountAdapter extends ArrayAdapter { private final XmppActivity activity; private final boolean showStateButton; + private final boolean showColorSelector; + + public ColorSelectorListener colorSelectorListener = null; public AccountAdapter(XmppActivity activity, List objects, boolean showStateButton) { super(activity, 0, objects); this.activity = activity; this.showStateButton = showStateButton; + this.showColorSelector = false; } - public AccountAdapter(XmppActivity activity, List objects) { + public AccountAdapter(XmppActivity activity, List objects, ColorSelectorListener listener) { super(activity, 0, objects); this.activity = activity; this.showStateButton = true; + this.showColorSelector = true; + colorSelectorListener = listener; } @Override @@ -77,18 +89,27 @@ public class AccountAdapter extends ArrayAdapter { } else { viewHolder.binding.tglAccountStatus.setVisibility(View.GONE); } + viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener((compoundButton, b) -> { if (b == isDisabled && activity instanceof OnTglAccountState) { ((OnTglAccountState) activity).onClickTglAccountState(account, b); } }); - if (activity.xmppConnectionService.getAccounts().size() > 1) { - viewHolder.binding.accountIndicator.setBackgroundColor(UIHelper.getColorForName(account.getJid().asBareJid().getEscapedLocal())); + if (this.showColorSelector && activity.xmppConnectionService.getAccounts().size() > 1) { + int color = UIHelper.getAccountColor(activity, account.getJid()); + viewHolder.binding.colorView.setVisibility(View.VISIBLE); + ColorUtils.setColorViewValue(viewHolder.binding.colorView, color, false, ColorShape.CIRCLE); + viewHolder.binding.colorView.setOnClickListener(v -> { + if (colorSelectorListener != null) { + colorSelectorListener.onColorPickerRequested(account.getJid(), color); + } + }); } else { - viewHolder.binding.accountIndicator.setBackgroundColor(Color.TRANSPARENT); + viewHolder.binding.colorView.setVisibility(View.GONE); } + return view; } @@ -106,4 +127,7 @@ public class AccountAdapter extends ArrayAdapter { void onClickTglAccountState(Account account, boolean state); } + public interface ColorSelectorListener { + void onColorPickerRequested(Jid accountJid, int currentColor); + } } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java index 69d4c7df4..22a41b59b 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java @@ -401,7 +401,7 @@ public class ConversationAdapter Account account = conversation.getAccount(); if (account != null && activity.xmppConnectionService.getAccounts().size() > 1) { - viewHolder.binding.accountIndicator.setBackgroundColor(UIHelper.getColorForName(account.getJid().asBareJid().getEscapedLocal())); + viewHolder.binding.accountIndicator.setBackgroundColor(UIHelper.getAccountColor(activity, account.getJid())); } else { viewHolder.binding.accountIndicator.setBackgroundColor(Color.TRANSPARENT); } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java index b342ccf0d..940d5cb2b 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java @@ -108,7 +108,7 @@ public class ListItemAdapter extends ArrayAdapter { } if (account != null && activity.xmppConnectionService.getAccounts().size() > 1) { - viewHolder.accountIndicator.setBackgroundColor(UIHelper.getColorForName(account.getJid().asBareJid().getEscapedLocal())); + viewHolder.accountIndicator.setBackgroundColor(UIHelper.getAccountColor(activity, account.getJid())); } else { viewHolder.accountIndicator.setBackgroundColor(Color.TRANSPARENT); } diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index 5680a5e60..ea674c893 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.utils; import android.content.Context; +import android.content.SharedPreferences; import android.text.SpannableStringBuilder; import android.text.format.DateFormat; import android.text.format.DateUtils; @@ -231,6 +232,23 @@ public class UIHelper { return getColorForName(name, false); } + public static int getAccountColor(Context context, Jid accountJid) { + SharedPreferences prefs = context.getSharedPreferences("accountColorsPrefs", Context.MODE_PRIVATE); + String name = accountJid.asBareJid().toEscapedString(); + int overrideColor = prefs.getInt(name, -1); + + if (overrideColor != -1) { + return overrideColor; + } else { + return getColorForName(name); + } + } + + public static void overrideAccountColor(Context context, String accountName, int color) { + SharedPreferences prefs = context.getSharedPreferences("accountColorsPrefs", Context.MODE_PRIVATE); + prefs.edit().putInt(accountName, color).apply(); + } + public static int getColorForName(String name, boolean safe) { if (Config.XEP_0392) { return XEP0392Helper.rgbFromNick(name); diff --git a/src/main/res/layout/account_row.xml b/src/main/res/layout/account_row.xml index ee3f7c138..93d6bd8d7 100644 --- a/src/main/res/layout/account_row.xml +++ b/src/main/res/layout/account_row.xml @@ -6,17 +6,10 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?selectableItemBackground" - android:clipToPadding="false" android:paddingLeft="8dp" android:paddingBottom="8dp" android:paddingTop="8dp"> - - + android:layout_toLeftOf="@+id/controls" + android:layout_toStartOf="@+id/controls"> - + android:layout_alignParentRight="true" + android:layout_centerVertical="true"> + + + + + + \ No newline at end of file