LayoutsPreference: Modify custom layout

Clicking on a custom layout opens a dialog for modifying the layout
description instead of the dialog for selecting a layout.
This commit is contained in:
Jules Aguillon 2023-08-10 20:48:24 +02:00
parent 4584e8289b
commit ddceb69d4e
2 changed files with 25 additions and 7 deletions

View File

@ -146,7 +146,7 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
callback.select(new SystemLayout()); callback.select(new SystemLayout());
break; break;
case "custom": case "custom":
select_custom(callback); select_custom(callback, "");
break; break;
default: default:
callback.select(new NamedLayout(name)); callback.select(new NamedLayout(name));
@ -157,15 +157,18 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
.show(); .show();
} }
void select_custom(final SelectionCallback callback) /** Dialog for specifying a custom layout. [initial_text] is the layout
description when modifying a layout. */
void select_custom(final SelectionCallback callback, String initial_text)
{ {
final EditText input = new EditText(getContext());
input.setText(initial_text);
new AlertDialog.Builder(getContext()) new AlertDialog.Builder(getContext())
.setView(R.layout.dialog_edit_text) .setView(input)
.setTitle(R.string.pref_custom_layout_title) .setTitle(R.string.pref_custom_layout_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){ .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int _which) public void onClick(DialogInterface _dialog, int _which)
{ {
EditText input = (EditText)((AlertDialog)dialog).findViewById(R.id.text);
callback.select(new CustomLayout(input.getText().toString())); callback.select(new CustomLayout(input.getText().toString()));
} }
}) })
@ -173,6 +176,16 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
.show(); .show();
} }
/** Called when modifying a layout. Custom layouts behave differently. */
@Override
void select(final SelectionCallback callback, Layout prev_layout)
{
if (prev_layout instanceof CustomLayout)
select_custom(callback, ((CustomLayout)prev_layout).xml);
else
select(callback);
}
class LayoutsAddButton extends AddButton class LayoutsAddButton extends AddButton
{ {
public LayoutsAddButton(Context ctx) public LayoutsAddButton(Context ctx)

View File

@ -53,10 +53,15 @@ public abstract class ListGroupPreference<E> extends PreferenceGroup
return true; return true;
} }
/** Called when an item is added or modified. Returns [null] to cancel the /** Called when an item is added or modified. */
action. */
abstract void select(SelectionCallback<E> callback); abstract void select(SelectionCallback<E> callback);
/** Called when an item is modified. */
void select(SelectionCallback<E> callback, E _old_value)
{
select(callback);
}
/** A separate class is used as the same serializer must be used in the /** A separate class is used as the same serializer must be used in the
static context. See [Serializer] below. */ static context. See [Serializer] below. */
abstract Serializer<E> get_serializer(); abstract Serializer<E> get_serializer();