1
0
Fork 0

PowerEditor: made attack details optional and repeated attacks impossible

This commit is contained in:
kauron 2015-07-19 18:25:37 +02:00
parent c1ce2a5a4e
commit 3d794a925a
3 changed files with 63 additions and 52 deletions

View file

@ -3,7 +3,6 @@ package com.kauron.dungeonmanager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
@ -13,6 +12,9 @@ import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
public class PowerEditor extends ActionBarActivity {
@ -25,17 +27,18 @@ public class PowerEditor extends ActionBarActivity {
private String originalName;
private int power;
private SharedPreferences p;
private Drawable background;
private boolean changed;
@Override
public void onBackPressed() {
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.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
PowerEditor.super.onBackPressed();
save();
}
});
@ -44,6 +47,12 @@ public class PowerEditor extends ActionBarActivity {
// Canceled.
}
});
alert.setNeutralButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
PowerEditor.super.onBackPressed();
}
});
alert.show();
@ -58,6 +67,8 @@ public class PowerEditor extends ActionBarActivity {
setSupportActionBar(toolbar);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
changed = false;
power = getIntent().getIntExtra("power", -1);
p = getSharedPreferences(getIntent().getStringExtra("player"), MODE_PRIVATE);
@ -111,8 +122,6 @@ public class PowerEditor extends ActionBarActivity {
Power.ACTIONS
)
);
background = spinners[0].getBackground();
if (power != -1) {
SharedPreferences powerPrefs = getSharedPreferences(p.getString("power" + power, ""), MODE_PRIVATE);
@ -123,60 +132,56 @@ public class PowerEditor extends ActionBarActivity {
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() {
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++ ) {
String s = edits[i].getText().toString().trim();
if (s.length() == 0) {
edits[i].setError(getString(R.string.required));
readyToSave = false;
} else {
strings[i] = s;
}
if (strings[0].length() == 0) {
edits[0].setError(getString(R.string.required));
return;
}
for ( int i = 0; i < spinners.length; i++) {
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;
}
}
int powers = p.getInt("powers", 0);
if ( readyToSave ) {
int powers = p.getInt("powers", 0);
String saveName;
if ( originalName == null ) {
saveName = strings[0];
for (int i = 0; i < powers; i++) {
if (p.getString("power" + power, "").equals(saveName)) saveName += "2";
String saveName;
if ( originalName == null ) {
saveName = strings[0];
for (int i = 0; i < powers; i++) {
if (p.getString("power" + power, "").equals(saveName)) {
SnackbarManager.show(Snackbar
.with(this)
.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;
}
SharedPreferences.Editor ed = getSharedPreferences( saveName, MODE_PRIVATE).edit();
for (int i = 0; i < strings.length; i++)
ed.putString("s" + i, strings[i]);
for (int i = 0; i < ints.length; i++)
ed.putInt("i" + i, ints[i]);
ed.apply();
finish();
p.edit().putString("power" + powers, saveName)
.putInt("powers", powers + 1)
.apply();
} else {
saveName = originalName;
}
}
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
public boolean onOptionsItemSelected(MenuItem item) {

View file

@ -187,7 +187,7 @@
<string name="title_section1">Sección 1</string>
<string name="title_section2">Sección 2</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"></string>
<string name="add_existing_player">Añadir un aventurero existente</string>
<string name="add_import">Importar un aventurero</string>
@ -195,4 +195,7 @@
<string name="import_confirmation">Importar</string>
<string name="paste_here">Pega el código de personaje aquí</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>

View file

@ -200,7 +200,7 @@
<string name="hello_world">Hello world!</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="add_new_player">Add a new 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="import_confirmation">Import</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>