Added keyword title edit

pull/199/head
M66B 4 years ago
parent e6deeb4ea6
commit db1bf7652d

@ -24,13 +24,18 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -49,6 +54,7 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
private Context context; private Context context;
private LifecycleOwner owner; private LifecycleOwner owner;
private LayoutInflater inflater; private LayoutInflater inflater;
private SharedPreferences prefs;
private boolean pro; private boolean pro;
private long id; private long id;
@ -57,32 +63,48 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener, View.OnClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
private View view; private View view;
private CheckBox cbKeyword; private CheckBox cbKeyword;
private ImageButton ibEdit;
private EditText etKeyword;
private ImageButton ibSave;
private ViewButtonColor btnColor; private ViewButtonColor btnColor;
private Group grpNotEdit;
private Group grpEdit;
ViewHolder(View itemView) { ViewHolder(View itemView) {
super(itemView); super(itemView);
view = itemView.findViewById(R.id.clItem); view = itemView.findViewById(R.id.clItem);
cbKeyword = itemView.findViewById(R.id.cbKeyword); cbKeyword = itemView.findViewById(R.id.cbKeyword);
ibEdit = itemView.findViewById(R.id.ibEdit);
etKeyword = itemView.findViewById(R.id.etKeyword);
ibSave = itemView.findViewById(R.id.ibSave);
btnColor = itemView.findViewById(R.id.btnColor); btnColor = itemView.findViewById(R.id.btnColor);
grpNotEdit = itemView.findViewById(R.id.grpNotEdit);
grpEdit = itemView.findViewById(R.id.grpEdit);
} }
private void wire() { private void wire() {
cbKeyword.setOnCheckedChangeListener(this); cbKeyword.setOnCheckedChangeListener(this);
ibEdit.setOnClickListener(this);
ibSave.setOnClickListener(this);
btnColor.setOnClickListener(this); btnColor.setOnClickListener(this);
} }
private void unwire() { private void unwire() {
cbKeyword.setOnCheckedChangeListener(null); cbKeyword.setOnCheckedChangeListener(null);
ibEdit.setOnClickListener(null);
ibSave.setOnClickListener(null);
btnColor.setOnClickListener(null); btnColor.setOnClickListener(null);
} }
private void bindTo(TupleKeyword keyword) { private void bindTo(TupleKeyword keyword) {
cbKeyword.setText(EntityMessage.getKeywordAlias(context, keyword.name)); cbKeyword.setText(getTitle(keyword.name));
cbKeyword.setChecked(keyword.selected); cbKeyword.setChecked(keyword.selected);
cbKeyword.setEnabled(pro); cbKeyword.setEnabled(pro);
btnColor.setColor(keyword.color); btnColor.setColor(keyword.color);
btnColor.setEnabled(pro); btnColor.setEnabled(pro);
grpNotEdit.setVisibility(View.VISIBLE);
grpEdit.setVisibility(View.GONE);
} }
@Override @Override
@ -132,44 +154,87 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
final TupleKeyword keyword = all.get(pos); final TupleKeyword keyword = all.get(pos);
int editTextColor = Helper.resolveColor(context, android.R.attr.editTextColor); int itemId = view.getId();
if (itemId == R.id.ibEdit) {
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder String key = "kwtitle." + keyword.name;
.with(context) etKeyword.setText(prefs.getString(key, null));
.setTitle(context.getString(R.string.title_color)) etKeyword.setHint(keyword.name);
.showColorEdit(true) grpNotEdit.setVisibility(View.GONE);
.setColorEditTextColor(editTextColor) grpEdit.setVisibility(View.VISIBLE);
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER) etKeyword.post(new Runnable() {
.density(6) @Override
.lightnessSliderOnly() public void run() {
.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() { etKeyword.requestFocus();
@Override InputMethodManager imm =
public void onClick(DialogInterface dialog, int which) { (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
update(keyword, null); if (imm != null)
} imm.showSoftInput(etKeyword, InputMethodManager.SHOW_IMPLICIT);
}) }
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() { });
@Override } else if (itemId == R.id.ibSave) {
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) { updateTitle(keyword, etKeyword.getText().toString().trim());
update(keyword, selectedColor); Helper.hideKeyboard(etKeyword);
} grpNotEdit.setVisibility(View.VISIBLE);
}); grpEdit.setVisibility(View.GONE);
} else if (itemId == R.id.btnColor) {
if (keyword.color != null) int editTextColor = Helper.resolveColor(context, android.R.attr.editTextColor);
builder.initialColor(keyword.color);
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder
builder.build().show(); .with(context)
.setTitle(context.getString(R.string.title_color))
.showColorEdit(true)
.setColorEditTextColor(editTextColor)
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(6)
.lightnessSliderOnly()
.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
updateColor(keyword, null);
}
})
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
updateColor(keyword, selectedColor);
}
});
if (keyword.color != null)
builder.initialColor(keyword.color);
builder.build().show();
}
}
private String getTitle(String keyword) {
String keyTitle = "kwtitle." + keyword;
String def = TupleKeyword.getDefaultKeywordAlias(context, keyword);
return prefs.getString(keyTitle, def);
}
private void updateTitle(TupleKeyword keyword, String title) {
String key = "kwtitle." + keyword.name;
if (TextUtils.isEmpty(title))
prefs.edit().remove(key).apply();
else
prefs.edit().putString(key, title).apply();
cbKeyword.setText(getTitle(keyword.name));
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(new Intent(FragmentMessages.ACTION_KEYWORDS));
} }
private void update(TupleKeyword keyword, Integer color) { private void updateColor(TupleKeyword keyword, Integer color) {
btnColor.setColor(color); btnColor.setColor(color);
keyword.color = color; keyword.color = color;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String key = "kwcolor." + keyword.name;
if (color == null) if (color == null)
prefs.edit().remove("keyword." + keyword.name).apply(); prefs.edit().remove(key).apply();
else else
prefs.edit().putInt("keyword." + keyword.name, keyword.color).apply(); prefs.edit().putInt(key, keyword.color).apply();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(new Intent(FragmentMessages.ACTION_KEYWORDS)); lbm.sendBroadcast(new Intent(FragmentMessages.ACTION_KEYWORDS));
@ -180,6 +245,7 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
this.context = context; this.context = context;
this.owner = owner; this.owner = owner;
this.inflater = LayoutInflater.from(context); this.inflater = LayoutInflater.from(context);
this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.pro = ActivityBilling.isPro(context); this.pro = ActivityBilling.isPro(context);
setHasStableIds(false); setHasStableIds(false);

@ -5216,27 +5216,31 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
private SpannableStringBuilder getKeywords(TupleMessageEx message) { private SpannableStringBuilder getKeywords(TupleMessageEx message) {
SpannableStringBuilder keywords = new SpannableStringBuilder(); SpannableStringBuilder ssb = new SpannableStringBuilder();
if (message.keyword_titles == null || message.keyword_colors == null) {
ssb.append("Keywords missing!");
return ssb;
}
for (int i = 0; i < message.keywords.length; i++) { for (int i = 0; i < message.keywords.length; i++) {
if (MessageHelper.showKeyword(message.keywords[i])) { if (MessageHelper.showKeyword(message.keywords[i])) {
if (keywords.length() > 0) if (ssb.length() > 0)
keywords.append(" "); ssb.append(' ');
// Thunderbird String keyword = message.keyword_titles[i];
String keyword = EntityMessage.getKeywordAlias(context, message.keywords[i]); ssb.append(keyword);
keywords.append(keyword);
if (message.keyword_colors != null && if (message.keyword_colors[i] != null) {
message.keyword_colors[i] != null) { int len = ssb.length();
int len = keywords.length(); ssb.setSpan(
keywords.setSpan(
new ForegroundColorSpan(message.keyword_colors[i]), new ForegroundColorSpan(message.keyword_colors[i]),
len - keyword.length(), len, len - keyword.length(), len,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
} }
} }
return keywords; return ssb;
} }
ItemDetailsLookup.ItemDetails<Long> getItemDetails(@NonNull MotionEvent motionEvent) { ItemDetailsLookup.ItemDetails<Long> getItemDetails(@NonNull MotionEvent motionEvent) {
@ -6796,6 +6800,14 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
.create(); .create();
} }
@Override
public void onResume() {
super.onResume();
Dialog dialog = getDialog();
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
} }
public static class FragmentDialogKeywordAdd extends FragmentDialogBase { public static class FragmentDialogKeywordAdd extends FragmentDialogBase {

@ -507,23 +507,6 @@ public class EntityMessage implements Serializable {
} }
} }
static String getKeywordAlias(Context context, String keyword) {
switch (keyword) {
case "$label1": // Important
return context.getString(R.string.title_keyword_label1);
case "$label2": // Work
return context.getString(R.string.title_keyword_label2);
case "$label3": // Personal
return context.getString(R.string.title_keyword_label3);
case "$label4": // To do
return context.getString(R.string.title_keyword_label4);
case "$label5": // Later
return context.getString(R.string.title_keyword_label5);
default:
return keyword;
}
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof EntityMessage) { if (obj instanceof EntityMessage) {

@ -21,6 +21,7 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -81,13 +82,33 @@ public class TupleKeyword {
k.name = keyword; k.name = keyword;
k.selected = Arrays.asList(data.selected).contains(keyword); k.selected = Arrays.asList(data.selected).contains(keyword);
String c = "keyword." + keyword; String c1 = "kwcolor." + keyword;
if (prefs.contains(c)) String c2 = "keyword." + keyword; // legacy
k.color = prefs.getInt(c, -1); if (prefs.contains(c1))
k.color = prefs.getInt(c1, Color.GRAY);
else if (prefs.contains(c2))
k.color = prefs.getInt(c2, Color.GRAY);
result.add(k); result.add(k);
} }
return result; return result;
} }
static String getDefaultKeywordAlias(Context context, String keyword) {
switch (keyword) {
case "$label1": // Important
return context.getString(R.string.title_keyword_label1);
case "$label2": // Work
return context.getString(R.string.title_keyword_label2);
case "$label3": // Personal
return context.getString(R.string.title_keyword_label3);
case "$label4": // To do
return context.getString(R.string.title_keyword_label4);
case "$label5": // Later
return context.getString(R.string.title_keyword_label5);
default:
return keyword;
}
}
} }

@ -68,6 +68,8 @@ public class TupleMessageEx extends EntityMessage {
@Ignore @Ignore
public Integer[] keyword_colors; public Integer[] keyword_colors;
@Ignore
public String[] keyword_titles;
String getFolderName(Context context) { String getFolderName(Context context) {
return (folderDisplay == null return (folderDisplay == null
@ -77,17 +79,28 @@ public class TupleMessageEx extends EntityMessage {
void resolveKeywordColors(Context context) { void resolveKeywordColors(Context context) {
List<Integer> color = new ArrayList<>(); List<Integer> color = new ArrayList<>();
List<String> titles = new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (int i = 0; i < this.keywords.length; i++) { for (int i = 0; i < this.keywords.length; i++) {
String key = "keyword." + this.keywords[i]; String keyword = this.keywords[i];
if (prefs.contains(key))
color.add(prefs.getInt(key, Color.GRAY)); String keyColor1 = "kwcolor." + keyword;
String keyColor2 = "keyword." + keyword; // legacy
if (prefs.contains(keyColor1))
color.add(prefs.getInt(keyColor1, Color.GRAY));
else if (prefs.contains(keyColor2))
color.add(prefs.getInt(keyColor2, Color.GRAY));
else else
color.add(null); color.add(null);
String keyTitle = "kwtitle." + keyword;
String def = TupleKeyword.getDefaultKeywordAlias(context, keyword);
titles.add(prefs.getString(keyTitle, def));
} }
this.keyword_colors = color.toArray(new Integer[0]); this.keyword_colors = color.toArray(new Integer[0]);
this.keyword_titles = titles.toArray(new String[0]);
} }
String getRemark() { String getRemark() {

@ -18,19 +18,76 @@
android:text="Keyword" android:text="Keyword"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toBottomOf="@+id/btnColor" app:layout_constraintBottom_toBottomOf="@+id/btnColor"
app:layout_constraintEnd_toStartOf="@+id/btnColor" app:layout_constraintEnd_toStartOf="@+id/ibEdit"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/btnColor" /> app:layout_constraintTop_toTopOf="@+id/btnColor" />
<ImageButton
android:id="@+id/ibEdit"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_legend_edit"
android:padding="6dp"
android:scaleType="fitCenter"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:tooltipText="@string/title_legend_edit"
app:layout_constraintBottom_toBottomOf="@+id/btnColor"
app:layout_constraintStart_toEndOf="@id/cbKeyword"
app:layout_constraintTop_toTopOf="@+id/btnColor"
app:srcCompat="@drawable/twotone_edit_24" />
<EditText
android:id="@+id/etKeyword"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="text"
android:text="Title"
app:layout_constraintBottom_toBottomOf="@+id/btnColor"
app:layout_constraintEnd_toStartOf="@+id/ibSave"
app:layout_constraintStart_toEndOf="@id/ibEdit"
app:layout_constraintTop_toTopOf="@+id/btnColor" />
<ImageButton
android:id="@+id/ibSave"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_save"
android:padding="6dp"
android:scaleType="fitCenter"
android:tooltipText="@string/title_save"
app:layout_constraintBottom_toBottomOf="@+id/btnColor"
app:layout_constraintStart_toEndOf="@id/etKeyword"
app:layout_constraintTop_toTopOf="@+id/btnColor"
app:srcCompat="@drawable/twotone_save_24" />
<eu.faircode.email.ViewButtonColor <eu.faircode.email.ViewButtonColor
android:id="@+id/btnColor" android:id="@+id/btnColor"
style="?android:attr/buttonStyleSmall" style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:paddingHorizontal="6dp" android:paddingHorizontal="6dp"
android:text="@string/title_select" android:text="@string/title_select"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/cbKeyword" app:layout_constraintStart_toEndOf="@+id/ibSave"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpNotEdit"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
app:constraint_referenced_ids="cbKeyword,ibEdit" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpEdit"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="visible"
app:constraint_referenced_ids="etKeyword,ibSave" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout> </FrameLayout>

Loading…
Cancel
Save