Custom placeholder improvements

pull/209/head
M66B 2 years ago
parent 2727932089
commit cf5538abb0

@ -99,7 +99,7 @@ public class EntityAnswer implements Serializable {
public Integer applied = 0;
public Long last_applied;
static final String PREF_PLACEHOLDER = "answer.value.";
private static final String PREF_PLACEHOLDER = "answer.value.";
String getHtml(Context context, Address[] address) {
return replacePlaceholders(context, text, address);
@ -191,6 +191,39 @@ public class EntityAnswer implements Serializable {
return text;
}
static void setCustomPlaceholder(Context context, String name, String value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (TextUtils.isEmpty(value))
prefs.edit().remove(EntityAnswer.PREF_PLACEHOLDER + name).apply();
else
prefs.edit().putString(EntityAnswer.PREF_PLACEHOLDER + name, value).apply();
}
static String getCustomPlaceholder(Context context, String name) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString(EntityAnswer.PREF_PLACEHOLDER + name, null);
}
static List<String> getCustomPlaceholders(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<String> names = new ArrayList<>();
for (String key : prefs.getAll().keySet())
if (key.startsWith(EntityAnswer.PREF_PLACEHOLDER))
names.add(key.substring(EntityAnswer.PREF_PLACEHOLDER.length()));
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String n1, String n2) {
return collator.compare(n1, n2);
}
});
return names;
}
static void fillMenu(Menu main, boolean compose, List<EntityAnswer> answers, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean sort_answers = prefs.getBoolean("sort_answers", false);

@ -56,12 +56,7 @@ import com.google.android.material.snackbar.Snackbar;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class FragmentAnswer extends FragmentBase {
private ViewGroup view;
@ -287,22 +282,9 @@ public class FragmentAnswer extends FragmentBase {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_answer, menu);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
List<String> names = new ArrayList<>();
for (String key : prefs.getAll().keySet())
if (key.startsWith(EntityAnswer.PREF_PLACEHOLDER))
names.add(key.substring(EntityAnswer.PREF_PLACEHOLDER.length()));
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String n1, String n2) {
return collator.compare(n1, n2);
}
});
Menu smenu = menu.findItem(R.id.menu_placeholders).getSubMenu();
List<String> names = EntityAnswer.getCustomPlaceholders(getContext());
for (int i = 0; i < names.size(); i++)
smenu.add(Menu.FIRST, i + 1, i + 1, names.get(i));

@ -279,27 +279,36 @@ public class FragmentAnswers extends FragmentBase {
}
});
Menu smenu = menu.findItem(R.id.menu_placeholders).getSubMenu();
List<String> names = EntityAnswer.getCustomPlaceholders(getContext());
for (int i = 0; i < names.size(); i++)
smenu.add(Menu.FIRST, i + 1, i + 1, names.get(i));
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_define) {
onDefine();
if (item.getGroupId() == Menu.FIRST) {
onDefine(item.getTitle().toString());
return true;
} else
return super.onOptionsItemSelected(item);
} else {
int id = item.getItemId();
if (id == R.id.menu_define) {
onDefine(null);
return true;
} else
return super.onOptionsItemSelected(item);
}
}
private void onDefine() {
private void onDefine(String name) {
final Context context = getContext();
View view = LayoutInflater.from(context).inflate(R.layout.dialog_placeholder, null);
final EditText etName = view.findViewById(R.id.etName);
final EditText etValue = view.findViewById(R.id.etValue);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
etName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@ -313,12 +322,14 @@ public class FragmentAnswers extends FragmentBase {
@Override
public void afterTextChanged(Editable s) {
String value = prefs.getString(EntityAnswer.PREF_PLACEHOLDER + s.toString().trim(), null);
String value = EntityAnswer.getCustomPlaceholder(context, s.toString().trim());
if (!TextUtils.isEmpty(value))
etValue.setText(value);
}
});
etName.setText(name);
new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@ -328,10 +339,7 @@ public class FragmentAnswers extends FragmentBase {
String value = etValue.getText().toString();
if (TextUtils.isEmpty(name))
return;
if (TextUtils.isEmpty(value))
prefs.edit().remove(EntityAnswer.PREF_PLACEHOLDER + name).apply();
else
prefs.edit().putString(EntityAnswer.PREF_PLACEHOLDER + name, value).apply();
EntityAnswer.setCustomPlaceholder(context, name, value);
}
})
.setNegativeButton(android.R.string.cancel, null)

@ -10,7 +10,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="@+id/etName"
android:text="@string/title_answer_define"
android:text="@string/title_answer_placeholder"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

@ -9,7 +9,13 @@
app:showAsAction="collapseActionView|always" />
<item
android:id="@+id/menu_define"
android:icon="@drawable/twotone_add_24"
android:title="@string/title_answer_define" />
android:id="@+id/menu_placeholders"
android:title="@string/title_answer_placeholder">
<menu>
<item
android:id="@+id/menu_define"
android:icon="@drawable/twotone_add_24"
android:title="@string/title_add" />
</menu>
</item>
</menu>

@ -1652,7 +1652,6 @@
So, don\'t delete the image file!
</string>
<string name="title_answer_define">Define placeholder</string>
<string name="title_answer_define_name">Name</string>
<string name="title_answer_define_value">Value</string>
<string name="title_answer_caption">Edit template</string>

Loading…
Cancel
Save