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

@ -5216,27 +5216,31 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
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++) {
if (MessageHelper.showKeyword(message.keywords[i])) {
if (keywords.length() > 0)
keywords.append(" ");
if (ssb.length() > 0)
ssb.append(' ');
// Thunderbird
String keyword = EntityMessage.getKeywordAlias(context, message.keywords[i]);
keywords.append(keyword);
String keyword = message.keyword_titles[i];
ssb.append(keyword);
if (message.keyword_colors != null &&
message.keyword_colors[i] != null) {
int len = keywords.length();
keywords.setSpan(
if (message.keyword_colors[i] != null) {
int len = ssb.length();
ssb.setSpan(
new ForegroundColorSpan(message.keyword_colors[i]),
len - keyword.length(), len,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return keywords;
return ssb;
}
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)
.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 {

@ -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
public boolean equals(Object obj) {
if (obj instanceof EntityMessage) {

@ -21,6 +21,7 @@ package eu.faircode.email;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
@ -81,13 +82,33 @@ public class TupleKeyword {
k.name = keyword;
k.selected = Arrays.asList(data.selected).contains(keyword);
String c = "keyword." + keyword;
if (prefs.contains(c))
k.color = prefs.getInt(c, -1);
String c1 = "kwcolor." + keyword;
String c2 = "keyword." + keyword; // legacy
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);
}
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
public Integer[] keyword_colors;
@Ignore
public String[] keyword_titles;
String getFolderName(Context context) {
return (folderDisplay == null
@ -77,17 +79,28 @@ public class TupleMessageEx extends EntityMessage {
void resolveKeywordColors(Context context) {
List<Integer> color = new ArrayList<>();
List<String> titles = new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (int i = 0; i < this.keywords.length; i++) {
String key = "keyword." + this.keywords[i];
if (prefs.contains(key))
color.add(prefs.getInt(key, Color.GRAY));
String keyword = this.keywords[i];
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
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_titles = titles.toArray(new String[0]);
}
String getRemark() {

@ -18,19 +18,76 @@
android:text="Keyword"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
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_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
android:id="@+id/btnColor"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:paddingHorizontal="6dp"
android:text="@string/title_select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/cbKeyword"
app:layout_constraintStart_toEndOf="@+id/ibSave"
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>
</FrameLayout>

Loading…
Cancel
Save