mirror of
https://github.com/TimeCrafters/TimeCraftersConfigurationTool.git
synced 2025-12-15 21:12:34 +00:00
Finished* ConfigurationDialog, refactored Backend#configsList to remove '.json' file extension
This commit is contained in:
@@ -218,7 +218,7 @@ public class Backend {
|
|||||||
};
|
};
|
||||||
File fileList[] = directory.listFiles(filter);
|
File fileList[] = directory.listFiles(filter);
|
||||||
for (File file : fileList) {
|
for (File file : fileList) {
|
||||||
list.add(file.getName());
|
list.add(file.getName().replace(".json", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package org.timecrafters.TimeCraftersConfigurationTool.dialogs;
|
package org.timecrafters.TimeCraftersConfigurationTool.dialogs;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.Editable;
|
||||||
|
import android.text.TextWatcher;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -14,11 +16,10 @@ import org.timecrafters.TimeCraftersConfigurationTool.R;
|
|||||||
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
import org.timecrafters.TimeCraftersConfigurationTool.backend.Backend;
|
||||||
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersDialog;
|
import org.timecrafters.TimeCraftersConfigurationTool.library.TimeCraftersDialog;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class ConfigurationDialog extends TimeCraftersDialog {
|
public class ConfigurationDialog extends TimeCraftersDialog {
|
||||||
private static final String TAG = "ConfigurationDialog";
|
private static final String TAG = "ConfigurationDialog";
|
||||||
private String configName;
|
private String configName;
|
||||||
|
private TextView nameError;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
@@ -29,6 +30,7 @@ public class ConfigurationDialog extends TimeCraftersDialog {
|
|||||||
|
|
||||||
final TextView title = root.findViewById(R.id.dialogTitle);
|
final TextView title = root.findViewById(R.id.dialogTitle);
|
||||||
final EditText name = view.findViewById(R.id.name);
|
final EditText name = view.findViewById(R.id.name);
|
||||||
|
this.nameError = view.findViewById(R.id.name_error);
|
||||||
final Button cancel = view.findViewById(R.id.cancel);
|
final Button cancel = view.findViewById(R.id.cancel);
|
||||||
final Button mutate = view.findViewById(R.id.mutate);
|
final Button mutate = view.findViewById(R.id.mutate);
|
||||||
|
|
||||||
@@ -40,6 +42,24 @@ public class ConfigurationDialog extends TimeCraftersDialog {
|
|||||||
} else {
|
} else {
|
||||||
title.setText("Create Configuration");
|
title.setText("Create Configuration");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name.addTextChangedListener(new TextWatcher() {
|
||||||
|
@Override
|
||||||
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
|
validated(name.getText().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterTextChanged(Editable s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
cancel.setOnClickListener(new View.OnClickListener() {
|
cancel.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
@@ -52,12 +72,18 @@ public class ConfigurationDialog extends TimeCraftersDialog {
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
final String newConfigName = name.getText().toString();
|
final String newConfigName = name.getText().toString();
|
||||||
|
|
||||||
if (isValid(newConfigName)) {
|
if (newConfigName.equals(configName)) {
|
||||||
|
dismiss();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (validated(newConfigName)) {
|
||||||
if (configName != null) {
|
if (configName != null) {
|
||||||
Backend.instance().moveConfig(configName, newConfigName);
|
Backend.instance().moveConfig(configName, newConfigName);
|
||||||
} else {
|
} else {
|
||||||
Backend.instance().writeNewConfig(newConfigName);
|
Backend.instance().writeNewConfig(newConfigName);
|
||||||
}
|
}
|
||||||
|
|
||||||
dismiss();
|
dismiss();
|
||||||
} else {
|
} else {
|
||||||
// TODO: Show friendly error message
|
// TODO: Show friendly error message
|
||||||
@@ -69,7 +95,25 @@ public class ConfigurationDialog extends TimeCraftersDialog {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValid(String name) {
|
private boolean validated(String name) {
|
||||||
return name.length() > 0 && name.matches("^[A-Za-z0-9\\._\\-]+$");
|
String message = "";
|
||||||
|
if (Backend.instance().configsList().contains(name)) {
|
||||||
|
message += "Name is not unique!";
|
||||||
|
|
||||||
|
} else if (name.length() <= 0) {
|
||||||
|
message += "Name cannot be blank!";
|
||||||
|
|
||||||
|
} else if (!name.matches("^[A-Za-z0-9\\._\\-]+$")) {
|
||||||
|
message += "Name can only contain alphanumeric characters, dashes, underscores, periods, and no spaces!";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.length() > 0) {
|
||||||
|
nameError.setVisibility(View.VISIBLE);
|
||||||
|
nameError.setText(message);
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
nameError.setVisibility(View.GONE);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public class PacketHandler {
|
|||||||
|
|
||||||
Log.i(TAG, "Got request for config: " + packet.getContent());
|
Log.i(TAG, "Got request for config: " + packet.getContent());
|
||||||
Packet pkt;
|
Packet pkt;
|
||||||
if (Backend.instance().configsList().contains("" + configName + ".json")) {
|
if (Backend.instance().configsList().contains(configName)) {
|
||||||
final String path = TAC.CONFIGS_PATH + File.separator + configName + ".json";
|
final String path = TAC.CONFIGS_PATH + File.separator + configName + ".json";
|
||||||
|
|
||||||
String content = Backend.instance().readFromFile(path);
|
String content = Backend.instance().readFromFile(path);
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ public class ConfigurationsFragment extends TimeCraftersFragment {
|
|||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (final String configFile : Backend.instance().configsList()) {
|
for (final String configFile : Backend.instance().configsList()) {
|
||||||
final String config = configFile.replace(".json", "");
|
|
||||||
View view = inflater.inflate(R.layout.fragment_part_configuration, null);
|
View view = inflater.inflate(R.layout.fragment_part_configuration, null);
|
||||||
|
|
||||||
if (i % 2 == 0) { // even
|
if (i % 2 == 0) { // even
|
||||||
@@ -66,20 +65,20 @@ public class ConfigurationsFragment extends TimeCraftersFragment {
|
|||||||
final Button configName = view.findViewById(R.id.name);
|
final Button configName = view.findViewById(R.id.name);
|
||||||
final ImageButton rename = view.findViewById(R.id.rename);
|
final ImageButton rename = view.findViewById(R.id.rename);
|
||||||
final ImageButton delete = view.findViewById(R.id.delete);
|
final ImageButton delete = view.findViewById(R.id.delete);
|
||||||
configName.setText(config);
|
configName.setText(configFile);
|
||||||
configName.setOnClickListener(new View.OnClickListener() {
|
configName.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (Backend.instance().getSettings().config.equals(config)) {
|
if (Backend.instance().getSettings().config.equals(configFile)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Backend.instance().getSettings().config = config;
|
Backend.instance().getSettings().config = configFile;
|
||||||
Backend.instance().loadConfig(config);
|
Backend.instance().loadConfig(configFile);
|
||||||
Backend.instance().saveSettings();
|
Backend.instance().saveSettings();
|
||||||
|
|
||||||
View snackbarHost = getActivity().findViewById(R.id.snackbar_host);
|
View snackbarHost = getActivity().findViewById(R.id.snackbar_host);
|
||||||
Snackbar.make(snackbarHost, "Loaded config: " + config, Snackbar.LENGTH_LONG).show();
|
Snackbar.make(snackbarHost, "Loaded config: " + configFile, Snackbar.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -88,7 +87,7 @@ public class ConfigurationsFragment extends TimeCraftersFragment {
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
ConfigurationDialog dialog = new ConfigurationDialog();
|
ConfigurationDialog dialog = new ConfigurationDialog();
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
bundle.putString("config_name", config);
|
bundle.putString("config_name", configFile);
|
||||||
dialog.setArguments(bundle);
|
dialog.setArguments(bundle);
|
||||||
dialog.show(getFragmentManager(), null);
|
dialog.show(getFragmentManager(), null);
|
||||||
}
|
}
|
||||||
@@ -101,13 +100,13 @@ public class ConfigurationsFragment extends TimeCraftersFragment {
|
|||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
final String actionKey = "delete_configuration";
|
final String actionKey = "delete_configuration";
|
||||||
bundle.putString("title", "Are you sure?");
|
bundle.putString("title", "Are you sure?");
|
||||||
bundle.putString("message", "Destroy configuration " + config + "?");
|
bundle.putString("message", "Destroy configuration " + configFile + "?");
|
||||||
bundle.putString("action", actionKey);
|
bundle.putString("action", actionKey);
|
||||||
bundle.putBoolean("extreme_danger", true);
|
bundle.putBoolean("extreme_danger", true);
|
||||||
Runnable action = new Runnable() {
|
Runnable action = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Backend.instance().deleteConfig(config);
|
Backend.instance().deleteConfig(configFile);
|
||||||
}
|
}
|
||||||
} ;
|
} ;
|
||||||
Backend.getStorage().put(actionKey, action);
|
Backend.getStorage().put(actionKey, action);
|
||||||
|
|||||||
@@ -21,6 +21,15 @@
|
|||||||
android:hint="@string/variable_name"
|
android:hint="@string/variable_name"
|
||||||
android:inputType="textPersonName" />
|
android:inputType="textPersonName" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/name_error"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginLeft="@dimen/button_margin_left"
|
||||||
|
android:layout_marginRight="@dimen/button_margin_right"
|
||||||
|
android:textColor="@color/dialogError"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|||||||
Reference in New Issue
Block a user