PowerEditor: made attack details optional and repeated attacks impossible
This commit is contained in:
parent
c1ce2a5a4e
commit
3d794a925a
3 changed files with 63 additions and 52 deletions
|
@ -3,7 +3,6 @@ package com.kauron.dungeonmanager;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.app.ActionBarActivity;
|
import android.support.v7.app.ActionBarActivity;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
|
@ -13,6 +12,9 @@ import android.widget.ArrayAdapter;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
|
|
||||||
|
import com.nispok.snackbar.Snackbar;
|
||||||
|
import com.nispok.snackbar.SnackbarManager;
|
||||||
|
|
||||||
|
|
||||||
public class PowerEditor extends ActionBarActivity {
|
public class PowerEditor extends ActionBarActivity {
|
||||||
|
|
||||||
|
@ -25,17 +27,18 @@ public class PowerEditor extends ActionBarActivity {
|
||||||
private String originalName;
|
private String originalName;
|
||||||
private int power;
|
private int power;
|
||||||
private SharedPreferences p;
|
private SharedPreferences p;
|
||||||
private Drawable background;
|
|
||||||
|
private boolean changed;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
||||||
alert.setTitle(R.string.sure);
|
alert.setTitle(R.string.save_changes);
|
||||||
alert.setMessage(R.string.progress_lost);
|
alert.setMessage(R.string.progress_lost);
|
||||||
|
|
||||||
alert.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
alert.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
public void onClick(DialogInterface dialog, int whichButton) {
|
||||||
PowerEditor.super.onBackPressed();
|
save();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -44,6 +47,12 @@ public class PowerEditor extends ActionBarActivity {
|
||||||
// Canceled.
|
// Canceled.
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
alert.setNeutralButton(R.string.no, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
|
PowerEditor.super.onBackPressed();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
alert.show();
|
alert.show();
|
||||||
|
|
||||||
|
@ -58,6 +67,8 @@ public class PowerEditor extends ActionBarActivity {
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
|
changed = false;
|
||||||
|
|
||||||
power = getIntent().getIntExtra("power", -1);
|
power = getIntent().getIntExtra("power", -1);
|
||||||
|
|
||||||
p = getSharedPreferences(getIntent().getStringExtra("player"), MODE_PRIVATE);
|
p = getSharedPreferences(getIntent().getStringExtra("player"), MODE_PRIVATE);
|
||||||
|
@ -111,8 +122,6 @@ public class PowerEditor extends ActionBarActivity {
|
||||||
Power.ACTIONS
|
Power.ACTIONS
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
background = spinners[0].getBackground();
|
|
||||||
|
|
||||||
|
|
||||||
if (power != -1) {
|
if (power != -1) {
|
||||||
SharedPreferences powerPrefs = getSharedPreferences(p.getString("power" + power, ""), MODE_PRIVATE);
|
SharedPreferences powerPrefs = getSharedPreferences(p.getString("power" + power, ""), MODE_PRIVATE);
|
||||||
|
@ -123,60 +132,56 @@ public class PowerEditor extends ActionBarActivity {
|
||||||
originalName = edits[0].getText().toString();
|
originalName = edits[0].getText().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: detect changes in edits and spinners
|
||||||
|
// for (int i = 0; i < edits.length; i++) {
|
||||||
|
// edits[i].;
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
boolean readyToSave = true;
|
for (int i = 0; i < strings.length; i++)
|
||||||
|
strings[i] = edits[i].getText().toString();
|
||||||
|
for (int i = 0; i < spinners.length; i++)
|
||||||
|
ints[i] = spinners[i].getSelectedItemPosition();
|
||||||
|
|
||||||
for ( int i = 0; i < edits.length; i++ ) {
|
if (strings[0].length() == 0) {
|
||||||
String s = edits[i].getText().toString().trim();
|
edits[0].setError(getString(R.string.required));
|
||||||
if (s.length() == 0) {
|
return;
|
||||||
edits[i].setError(getString(R.string.required));
|
|
||||||
readyToSave = false;
|
|
||||||
} else {
|
|
||||||
strings[i] = s;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int i = 0; i < spinners.length; i++) {
|
int powers = p.getInt("powers", 0);
|
||||||
int n = spinners[i].getSelectedItemPosition();
|
|
||||||
if ( n == 0) {
|
|
||||||
spinners[i].setBackgroundColor(getResources().getColor(R.color.red));
|
|
||||||
readyToSave = false;
|
|
||||||
//TODO: TEST THIS remove the color when the user has made a choice
|
|
||||||
} else {
|
|
||||||
spinners[i].setBackground(background);
|
|
||||||
ints[i] = n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( readyToSave ) {
|
String saveName;
|
||||||
int powers = p.getInt("powers", 0);
|
if ( originalName == null ) {
|
||||||
|
saveName = strings[0];
|
||||||
String saveName;
|
for (int i = 0; i < powers; i++) {
|
||||||
if ( originalName == null ) {
|
if (p.getString("power" + power, "").equals(saveName)) {
|
||||||
saveName = strings[0];
|
SnackbarManager.show(Snackbar
|
||||||
for (int i = 0; i < powers; i++) {
|
.with(this)
|
||||||
if (p.getString("power" + power, "").equals(saveName)) saveName += "2";
|
.text(R.string.power_same_name)
|
||||||
|
.duration(Snackbar.SnackbarDuration.LENGTH_INDEFINITE));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
p.edit().putString("power" + powers, saveName)
|
|
||||||
.putInt("powers", powers + 1)
|
|
||||||
.apply();
|
|
||||||
} else {
|
|
||||||
saveName = originalName;
|
|
||||||
}
|
}
|
||||||
|
p.edit().putString("power" + powers, saveName)
|
||||||
SharedPreferences.Editor ed = getSharedPreferences( saveName, MODE_PRIVATE).edit();
|
.putInt("powers", powers + 1)
|
||||||
|
.apply();
|
||||||
for (int i = 0; i < strings.length; i++)
|
} else {
|
||||||
ed.putString("s" + i, strings[i]);
|
saveName = originalName;
|
||||||
for (int i = 0; i < ints.length; i++)
|
|
||||||
ed.putInt("i" + i, ints[i]);
|
|
||||||
ed.apply();
|
|
||||||
|
|
||||||
finish();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
SharedPreferences.Editor ed = getSharedPreferences( saveName, MODE_PRIVATE).edit();
|
||||||
|
|
||||||
|
for (int i = 0; i < strings.length; i++)
|
||||||
|
if (!strings[i].isEmpty())
|
||||||
|
ed.putString("s" + i, strings[i]);
|
||||||
|
for (int i = 0; i < ints.length; i++)
|
||||||
|
ed.putInt("i" + i, ints[i]);
|
||||||
|
ed.apply();
|
||||||
|
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
|
|
@ -187,7 +187,7 @@
|
||||||
<string name="title_section1">Sección 1</string>
|
<string name="title_section1">Sección 1</string>
|
||||||
<string name="title_section2">Sección 2</string>
|
<string name="title_section2">Sección 2</string>
|
||||||
<string name="title_section3">Sección 3</string>
|
<string name="title_section3">Sección 3</string>
|
||||||
<string name="progress_lost">Tus cambios se perderán</string>
|
<string name="progress_lost">Tus cambios se perderán si no lo haces</string>
|
||||||
<string name="yes">Sí</string>
|
<string name="yes">Sí</string>
|
||||||
<string name="add_existing_player">Añadir un aventurero existente</string>
|
<string name="add_existing_player">Añadir un aventurero existente</string>
|
||||||
<string name="add_import">Importar un aventurero</string>
|
<string name="add_import">Importar un aventurero</string>
|
||||||
|
@ -195,4 +195,7 @@
|
||||||
<string name="import_confirmation">Importar</string>
|
<string name="import_confirmation">Importar</string>
|
||||||
<string name="paste_here">Pega el código de personaje aquí</string>
|
<string name="paste_here">Pega el código de personaje aquí</string>
|
||||||
<string name="attack">Ataque</string>
|
<string name="attack">Ataque</string>
|
||||||
|
<string name="save_changes">¿Guardar los cambios?</string>
|
||||||
|
<string name="no">No</string>
|
||||||
|
<string name="power_same_name">No puede ser guardado. Otro poder ya tiene ese nombre</string>
|
||||||
</resources>
|
</resources>
|
|
@ -200,7 +200,7 @@
|
||||||
<string name="hello_world">Hello world!</string>
|
<string name="hello_world">Hello world!</string>
|
||||||
|
|
||||||
<string name="action_settings">Settings</string>
|
<string name="action_settings">Settings</string>
|
||||||
<string name="progress_lost">Your changes will be lost</string>
|
<string name="progress_lost">Your changes will be lost otherwise</string>
|
||||||
<string name="yes">Yes</string>
|
<string name="yes">Yes</string>
|
||||||
<string name="add_new_player">Add a new adventurer</string>
|
<string name="add_new_player">Add a new adventurer</string>
|
||||||
<string name="add_existing_player">Add an existing adventurer</string>
|
<string name="add_existing_player">Add an existing adventurer</string>
|
||||||
|
@ -208,5 +208,8 @@
|
||||||
<string name="paste_here">Paste here</string>
|
<string name="paste_here">Paste here</string>
|
||||||
<string name="import_confirmation">Import</string>
|
<string name="import_confirmation">Import</string>
|
||||||
<string name="attack">Attack</string>
|
<string name="attack">Attack</string>
|
||||||
|
<string name="save_changes">Save changes?</string>
|
||||||
|
<string name="no">No</string>
|
||||||
|
<string name="power_same_name">Can\'t be saved. Another power already has that name.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Reference in a new issue