Skip to content

Commit

Permalink
Merge branch 'dont_allow_deleting_of_sole_active_preset'
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Dec 15, 2024
2 parents 62a5776 + 76cb2f6 commit dcabe80
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public void downloadPreset() {
menu = entry.getParent().getParent().findObject(By.res(device.getCurrentPackageName() + ":id/listItemMenu"));
menu.click();
TestUtils.clickText(device, false, main.getString(R.string.delete), true);
TestUtils.clickText(device, false, main.getString(R.string.yes), true);
TestUtils.clickHome(device, true);
App.resetPresets();
}
Expand Down
53 changes: 35 additions & 18 deletions src/main/java/de/blau/android/prefs/PresetEditorActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.blau.android.prefs;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -54,8 +56,8 @@
/** Provides an activity to edit the preset list. Downloads preset data when necessary. */
public class PresetEditorActivity extends URLListEditActivity {

private static final String DEBUG_TAG = PresetEditorActivity.class.getSimpleName().substring(0,
Math.min(23, PresetEditorActivity.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, PresetEditorActivity.class.getSimpleName().length());
private static final String DEBUG_TAG = PresetEditorActivity.class.getSimpleName().substring(0, TAG_LEN);

private AdvancedPrefDatabase db;

Expand Down Expand Up @@ -171,9 +173,7 @@ protected void onLoadList(List<ListEditItem> items) {

@Override
protected void onItemClicked(ListEditItem item) {
if (item.active && db.getActivePresets().length == 1) { // at least one item needs to be selected
updateAdapter();
ScreenMessage.barWarning(this, R.string.toast_min_one_preset);
if (!activePresetEnsured(item)) {
return;
}
item.active = !item.active;
Expand Down Expand Up @@ -207,8 +207,29 @@ protected void onItemEdited(ListEditItem item) {

@Override
protected void onItemDeleted(ListEditItem item) {
db.deletePreset(item.id);
App.resetPresets();
if (!activePresetEnsured(item)) {
return;
}
new AlertDialog.Builder(this).setTitle(R.string.delete).setMessage(R.string.preset_management_delete)
.setPositiveButton(R.string.Yes, (dialog, which) -> {
db.deletePreset(item.id);
App.resetPresets();
}).setNegativeButton(R.string.cancel, null).show();
}

/**
* Check that we have at least one active preset
*
* @param item the current item
* @return true if there will be at least one active item after item is de-activated or deleted
*/
private boolean activePresetEnsured(@NonNull ListEditItem item) {
if (item.active && db.getActivePresets().length == 1) { // at least one item needs to be selected
updateAdapter();
ScreenMessage.barWarning(this, R.string.toast_min_one_preset);
return false;
}
return true;
}

@Override
Expand Down Expand Up @@ -276,12 +297,8 @@ protected void onPreExecute() {
protected Integer doInBackground(Void args) {
Uri uri = Uri.parse(item.value);
final String scheme = uri.getScheme();
int loadResult;
if (Schemes.FILE.equals(scheme) || Schemes.CONTENT.equals(scheme)) {
loadResult = PresetLoader.load(activity, uri, presetDir, Preset.PRESETXML);
} else {
loadResult = PresetLoader.download(item.value, presetDir, Preset.PRESETXML);
}
int loadResult = Schemes.FILE.equals(scheme) || Schemes.CONTENT.equals(scheme) ? PresetLoader.load(activity, uri, presetDir, Preset.PRESETXML)
: PresetLoader.download(item.value, presetDir, Preset.PRESETXML);

if (loadResult == PresetLoader.DOWNLOADED_PRESET_ERROR) {
return RESULT_TOTAL_FAILURE;
Expand Down Expand Up @@ -381,25 +398,25 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {

final PresetEditorActivity activity = (PresetEditorActivity) getActivity();

if (item != null) {
final boolean itemExists = item != null;
if (itemExists) {
editName.setText(item.name);
editValue.setText(item.value);
useTranslations.setChecked(item.boolean0);

} else if (activity.isAddingViaIntent()) {
String tmpName = activity.getIntent().getExtras().getString(EXTRA_NAME);
String tmpValue = activity.getIntent().getExtras().getString(EXTRA_VALUE);
editName.setText(tmpName == null ? "" : tmpName);
editValue.setText(tmpValue == null ? "" : tmpValue);
useTranslations.setChecked(true);
}
if (item != null && item.value3 != null) {
if (itemExists && item.value3 != null) {
version.setText(item.value3);
} else {
versionLabel.setVisibility(View.GONE);
version.setVisibility(View.GONE);
}
if (item != null && LISTITEM_ID_DEFAULT.equals(item.id)) {
if (itemExists && LISTITEM_ID_DEFAULT.equals(item.id)) {
// name and value are not editable
editName.setInputType(InputType.TYPE_NULL);
editName.setBackground(null);
Expand Down Expand Up @@ -447,7 +464,7 @@ public boolean read(FragmentActivity currentActivity, Uri fileUri) {

// save or display toast, exception for localhost is needed for testing
if (validPresetURL || presetURL.startsWith(Schemes.FILE) || presetURL.startsWith(Schemes.CONTENT)
|| (url != null && "localhost".equals(url.getHost())) || (item != null && item.id.equals(LISTITEM_ID_DEFAULT))) {
|| (url != null && "localhost".equals(url.getHost())) || (itemExists && item.id.equals(LISTITEM_ID_DEFAULT))) {
if (item == null) {
// new item
activity.finishCreateItem(new ListEditItem(name, presetURL, null, null, useTranslationsEnabled, null));
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,8 @@
<string name="preset_download_parse_failed">The preset file is invalid. The preset will not work.</string>
<string name="preset_download_missing_images">Preset file downloaded. However, some missing icons were not.</string>
<string name="preset_download_title">Preset downloader</string>
<!-- preset management -->
<string name="preset_management_delete">Really delete Preset?</string>
<!-- url handler activity -->
<string name="urldialog_add_preset">Add preset</string>
<string name="urldialog_preset_exists">This preset already exists</string>
Expand Down

0 comments on commit dcabe80

Please sign in to comment.