mirror of
https://github.com/TimeCrafters/TimeCraftersConfigurationTool.git
synced 2025-12-15 05:02:33 +00:00
Action presets can now be properly edited, renamed, and deleted.
This commit is contained in:
@@ -10,17 +10,17 @@ import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.R;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Config;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Action;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Group;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersDialog;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.ui.editor.ActionsFragment;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.ui.editor.GroupsFragment;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.ui.settings.presets.PresetsFragment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -63,9 +63,9 @@ public class PresetDialog extends TimeCraftersDialog {
|
||||
}
|
||||
});
|
||||
|
||||
if (group != null) {
|
||||
title.setText("Editing " + group.name);
|
||||
name.setText(group.name);
|
||||
if (!isNewPreset) {
|
||||
title.setText("Editing " + action.name);
|
||||
name.setText(action.name);
|
||||
mutate.setText(getResources().getString(R.string.dialog_update));
|
||||
} else {
|
||||
title.setText("Add Preset");
|
||||
@@ -93,16 +93,30 @@ public class PresetDialog extends TimeCraftersDialog {
|
||||
public void onClick(View v) {
|
||||
final String presetName = name.getText().toString().trim();
|
||||
Action actionClone = deepCopyAction(action);
|
||||
// if (group != null && group.name.equals(groupName)) {
|
||||
// dismiss();
|
||||
// }
|
||||
if (!isNewPreset && actionClone.name.equals(presetName)) {
|
||||
dismiss();
|
||||
}
|
||||
|
||||
if (validated(presetName)) {
|
||||
if (action.name != presetName) {
|
||||
actionClone.name = presetName;
|
||||
if (isNewPreset) {
|
||||
actionClone.name = presetName;
|
||||
} else {
|
||||
action.name = presetName;
|
||||
}
|
||||
}
|
||||
|
||||
Backend.instance().getConfig().getPresets().getActions().add(actionClone);
|
||||
if (isNewPreset) {
|
||||
Backend.instance().getConfig().getPresets().getActions().add(actionClone);
|
||||
|
||||
ActionsFragment fragment = (ActionsFragment) getFragmentManager().getPrimaryNavigationFragment();
|
||||
Snackbar.make(fragment.getActivity().findViewById(R.id.snackbar_host), "Saved preset: " + presetName, Snackbar.LENGTH_LONG).show();
|
||||
} else { // Don't repopulate presets when it is not possible
|
||||
PresetsFragment fragment = (PresetsFragment) getFragmentManager().getPrimaryNavigationFragment();
|
||||
if (fragment != null) {
|
||||
fragment.populatePresets();
|
||||
}
|
||||
}
|
||||
|
||||
Backend.instance().configChanged();
|
||||
|
||||
@@ -126,7 +140,6 @@ public class PresetDialog extends TimeCraftersDialog {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: fix editing preset name impossible
|
||||
if (!nameUnique) {
|
||||
message += "Name is not unique!";
|
||||
|
||||
|
||||
@@ -45,8 +45,12 @@ public class VariableDialog extends TimeCraftersDialog {
|
||||
View root = super.onCreateView(inflater, container, savedInstanceState);
|
||||
|
||||
if (getArguments() != null) {
|
||||
Group group = Backend.instance().getConfig().getGroups().get(getArguments().getInt("group_index"));
|
||||
action = group.getActions().get(getArguments().getInt("action_index"));
|
||||
if (getArguments().getBoolean("action_is_preset")) {
|
||||
action = Backend.instance().getConfig().getPresets().getActions().get(getArguments().getInt("action_index"));
|
||||
} else {
|
||||
Group group = Backend.instance().getConfig().getGroups().get(getArguments().getInt("group_index"));
|
||||
action = group.getActions().get(getArguments().getInt("action_index"));
|
||||
}
|
||||
|
||||
if (getArguments().getInt("variable_index", -1) != -1) {
|
||||
variable = action.getVariables().get(getArguments().getInt("variable_index"));
|
||||
|
||||
@@ -33,6 +33,7 @@ public class VariablesFragment extends TimeCraftersFragment {
|
||||
|
||||
private Config config;
|
||||
private LinearLayout container;
|
||||
private boolean actionIsPreset = false;
|
||||
private Group group;
|
||||
private Action action;
|
||||
|
||||
@@ -44,8 +45,13 @@ public class VariablesFragment extends TimeCraftersFragment {
|
||||
final ScrollView scrollView = root.findViewById(R.id.scrollview);
|
||||
|
||||
this.config = Backend.instance().getConfig();
|
||||
this.group = config.getGroups().get(getArguments().getInt("group_index"));
|
||||
this.action = group.getActions().get(getArguments().getInt("action_index"));
|
||||
this.actionIsPreset = getArguments().getBoolean("action_is_preset", false);
|
||||
if (actionIsPreset) {
|
||||
this.action = config.getPresets().getActions().get(getArguments().getInt("action_index"));
|
||||
} else {
|
||||
this.group = config.getGroups().get(getArguments().getInt("group_index"));
|
||||
this.action = group.getActions().get(getArguments().getInt("action_index"));
|
||||
}
|
||||
|
||||
if (config != null) {
|
||||
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Action: " + action.name);
|
||||
@@ -59,7 +65,11 @@ public class VariablesFragment extends TimeCraftersFragment {
|
||||
public void onClick(View v) {
|
||||
VariableDialog dialog = new VariableDialog();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt("group_index", getArguments().getInt("group_index"));
|
||||
if (actionIsPreset) {
|
||||
bundle.putBoolean("action_is_preset", actionIsPreset);
|
||||
} else {
|
||||
bundle.putInt("group_index", getArguments().getInt("group_index"));
|
||||
}
|
||||
bundle.putInt("action_index", getArguments().getInt("action_index"));
|
||||
dialog.setArguments(bundle);
|
||||
dialog.show(getFragmentManager(), "add_variable");
|
||||
@@ -94,7 +104,11 @@ public class VariablesFragment extends TimeCraftersFragment {
|
||||
public void onClick(View v) {
|
||||
VariableDialog dialog = new VariableDialog();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt("group_index", getArguments().getInt("group_index"));
|
||||
if (actionIsPreset) {
|
||||
bundle.putBoolean("action_is_preset", actionIsPreset);
|
||||
} else {
|
||||
bundle.putInt("group_index", getArguments().getInt("group_index"));
|
||||
}
|
||||
bundle.putInt("action_index", getArguments().getInt("action_index"));
|
||||
bundle.putInt("variable_index", action.getVariables().indexOf(variable));
|
||||
dialog.setArguments(bundle);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.timecrafters.TimeCraftersConfigurationTool.ui.settings.presets;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -10,17 +11,25 @@ import android.widget.LinearLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.navigation.Navigation;
|
||||
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.R;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Action;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.config.Group;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.dialogs.ConfirmationDialog;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.dialogs.PresetDialog;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersDialog;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersDialogRunnable;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersFragment;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.ui.editor.ActionsFragment;
|
||||
import org.timecrafters.TimeCraftersConfigurationTool.ui.settings.configurations.ConfigurationsFragment;
|
||||
|
||||
public class PresetsFragment extends TimeCraftersFragment {
|
||||
private LayoutInflater inflater;
|
||||
private LinearLayout groupsContainer, actionsContainer;
|
||||
private View root;
|
||||
private String deletePresetKey = "deletePresetKey";
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@@ -35,7 +44,7 @@ public class PresetsFragment extends TimeCraftersFragment {
|
||||
return root;
|
||||
}
|
||||
|
||||
void populatePresets() {
|
||||
public void populatePresets() {
|
||||
groupsContainer.removeAllViews();
|
||||
actionsContainer.removeAllViews();
|
||||
|
||||
@@ -55,6 +64,7 @@ public class PresetsFragment extends TimeCraftersFragment {
|
||||
|
||||
i = 0;
|
||||
for (Action action : Backend.instance().getConfig().getPresets().getActions()) {
|
||||
final int action_index = i;
|
||||
View view = inflater.inflate(R.layout.fragment_part_presets, null);
|
||||
|
||||
if (i % 2 == 0) { // even
|
||||
@@ -68,6 +78,58 @@ public class PresetsFragment extends TimeCraftersFragment {
|
||||
ImageButton delete = view.findViewById(R.id.delete);
|
||||
|
||||
name.setText(action.name);
|
||||
name.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putBoolean("action_is_preset", true);
|
||||
bundle.putInt("action_index", action_index);
|
||||
Navigation.findNavController(v).navigate(R.id.variables_fragment, bundle);
|
||||
}
|
||||
});
|
||||
|
||||
rename.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
PresetDialog dialog = new PresetDialog();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt("action_index", action_index);
|
||||
dialog.setArguments(bundle);
|
||||
dialog.show(getFragmentManager(), "preset_dialog");
|
||||
}
|
||||
});
|
||||
|
||||
delete.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
ConfirmationDialog dialog = new ConfirmationDialog();
|
||||
Bundle bundle = new Bundle();
|
||||
|
||||
bundle.putString("message", "Delete Preset?");
|
||||
bundle.putString("action", deletePresetKey);
|
||||
TimeCraftersDialogRunnable action = new TimeCraftersDialogRunnable() {
|
||||
@Override
|
||||
public void run(TimeCraftersDialog dialog) {
|
||||
Backend.getStorage().remove(deletePresetKey);
|
||||
|
||||
if (Backend.instance().getConfig().getPresets().getActions().get(action_index) != null) {
|
||||
Backend.instance().getConfig().getPresets().getActions().remove(action_index);
|
||||
|
||||
Backend.instance().configChanged();
|
||||
}
|
||||
|
||||
PresetsFragment fragment = (PresetsFragment) dialog.getFragmentManager().getPrimaryNavigationFragment();
|
||||
if (fragment != null) {
|
||||
fragment.populatePresets();
|
||||
}
|
||||
}
|
||||
} ;
|
||||
Backend.getStorage().put(deletePresetKey, action);
|
||||
dialog.setArguments(bundle);
|
||||
|
||||
dialog.show(getFragmentManager(), deletePresetKey);
|
||||
}
|
||||
});
|
||||
|
||||
actionsContainer.addView(view);
|
||||
i++;
|
||||
|
||||
Reference in New Issue
Block a user