From 943d98f867ef35e5bf3856b8eb3e05b61e428948 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 23 Apr 2015 19:25:35 +0200 Subject: [PATCH] Better colors display, edition and deletion --- .../kauron/dungeonmanager/AttackAdapter.java | 3 + .../com/kauron/dungeonmanager/Player.java | 12 ++ .../kauron/dungeonmanager/PlayerAdapter.java | 13 +- .../kauron/dungeonmanager/PlayerEditor.java | 3 +- .../java/com/kauron/dungeonmanager/Power.java | 14 ++ .../kauron/dungeonmanager/PowerEditor.java | 54 +++++-- .../com/kauron/dungeonmanager/ShowPlayer.java | 148 ++++++++++-------- .../main/res/layout/activity_power_editor.xml | 5 +- .../main/res/layout/activity_show_player.xml | 2 +- app/src/main/res/layout/attack_display.xml | 42 +++-- app/src/main/res/layout/attack_row.xml | 10 +- 11 files changed, 191 insertions(+), 115 deletions(-) diff --git a/app/src/main/java/com/kauron/dungeonmanager/AttackAdapter.java b/app/src/main/java/com/kauron/dungeonmanager/AttackAdapter.java index cb569d7..7a8212a 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/AttackAdapter.java +++ b/app/src/main/java/com/kauron/dungeonmanager/AttackAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -36,6 +37,8 @@ class AttackAdapter extends ArrayAdapter { ((TextView) mView.findViewById(R.id.keywords)).setText(attack.getKeywords()); ((TextView) mView.findViewById(R.id.frequency)).setText(attack.getFrequencyString()); ((TextView) mView.findViewById(R.id.extra)).setText(attack.getRangeString() + " " + attack.getDistance()); + mView.setBackgroundColor(attack.getFreqColor(getContext())); + mView.getBackground().setAlpha((position % 2) * 127 + 128); } return mView; } diff --git a/app/src/main/java/com/kauron/dungeonmanager/Player.java b/app/src/main/java/com/kauron/dungeonmanager/Player.java index 86feef3..bd1ccd1 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/Player.java +++ b/app/src/main/java/com/kauron/dungeonmanager/Player.java @@ -1,5 +1,6 @@ package com.kauron.dungeonmanager; +import android.content.Context; import android.content.SharedPreferences; import java.io.Serializable; @@ -236,4 +237,15 @@ class Player implements Serializable { static int getModifier(int i) { return i / 2 - 5; } + + int getStatusColor(Context context) { + if (pg > maxPg / 2) + return context.getResources().getColor(R.color.green); + else if (pg > 0) + return context.getResources().getColor(R.color.yellow); + else if (pg > - maxPg / 2) + return context.getResources().getColor(R.color.red); + else + return context.getResources().getColor(R.color.black); + } } \ No newline at end of file diff --git a/app/src/main/java/com/kauron/dungeonmanager/PlayerAdapter.java b/app/src/main/java/com/kauron/dungeonmanager/PlayerAdapter.java index 04f6013..dbffd91 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/PlayerAdapter.java +++ b/app/src/main/java/com/kauron/dungeonmanager/PlayerAdapter.java @@ -42,16 +42,9 @@ class PlayerAdapter extends ArrayAdapter { neg.setProgress(pg < 0 ? -pg : 0); pos.setProgress(pg > 0 ? pg : 0); - int c; - if ( pg <= 0 ) - c = getContext().getResources().getColor(R.color.red); - else if ( pg <= maxPg / 2 ) - c = getContext().getResources().getColor(R.color.yellow); - else - c = getContext().getResources().getColor(R.color.green); - - neg.getProgressDrawable().setColorFilter(c, PorterDuff.Mode.SRC_IN); - pos.getProgressDrawable().setColorFilter(c, PorterDuff.Mode.SRC_IN); + int color = player.getStatusColor(getContext()); + neg.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); + pos.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); } return mView; diff --git a/app/src/main/java/com/kauron/dungeonmanager/PlayerEditor.java b/app/src/main/java/com/kauron/dungeonmanager/PlayerEditor.java index 3d99b2d..9925e56 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/PlayerEditor.java +++ b/app/src/main/java/com/kauron/dungeonmanager/PlayerEditor.java @@ -136,7 +136,8 @@ public class PlayerEditor extends ActionBarActivity { int i = p.getInt("players", 0); String saveName = nameString; for (int j = 0; j < i; j++) { - saveName += p.getString("player" + j, "").equals(saveName) ? "2" : ""; + if (p.getString("player" + j, "").equals(saveName)) + saveName += "2"; } p.edit().putString("player" + i, saveName).putInt("players", i + 1).apply(); SharedPreferences.Editor ed = getSharedPreferences(saveName, MODE_PRIVATE).edit(); diff --git a/app/src/main/java/com/kauron/dungeonmanager/Power.java b/app/src/main/java/com/kauron/dungeonmanager/Power.java index e05881a..b60a4ee 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/Power.java +++ b/app/src/main/java/com/kauron/dungeonmanager/Power.java @@ -1,5 +1,6 @@ package com.kauron.dungeonmanager; +import android.content.Context; import android.content.SharedPreferences; import android.graphics.Color; @@ -77,4 +78,17 @@ class Power implements Serializable{ void recover(int type){ if(this.freq <= type) used = false; } + + int getFreqColor(Context context) { + switch (freq) { + case DIARIO: + return context.getResources().getColor(R.color.daily); + case ENCUENTRO: + return context.getResources().getColor(R.color.encounter); + case A_VOLUNTAD: + return context.getResources().getColor(R.color.at_will); + default: + return context.getResources().getColor(R.color.green); //TODO: find other color + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/kauron/dungeonmanager/PowerEditor.java b/app/src/main/java/com/kauron/dungeonmanager/PowerEditor.java index 3dbad31..709c4a0 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/PowerEditor.java +++ b/app/src/main/java/com/kauron/dungeonmanager/PowerEditor.java @@ -1,6 +1,7 @@ package com.kauron.dungeonmanager; import android.content.SharedPreferences; +import android.support.annotation.NonNull; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; @@ -20,13 +21,19 @@ public class PowerEditor extends ActionBarActivity { private String[] strings = new String[5]; private int[] ints = new int[5]; + private String originalName; + private int power; + private SharedPreferences p; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_power_editor); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); + power = getIntent().getIntExtra("power", -1); + p = getSharedPreferences(getIntent().getStringExtra("player"), MODE_PRIVATE); //EditText edits[0] = (EditText) findViewById(R.id.nameEdit); @@ -78,6 +85,16 @@ public class PowerEditor extends ActionBarActivity { ) ); + + if (power != -1) { + SharedPreferences pp = getSharedPreferences(p.getString("power" + power, ""), MODE_PRIVATE); + for ( int i = 0; i < spinners.length; i++ ) + spinners[i].setSelection(pp.getInt("i" + i, 0)); + for ( int i = 0; i < edits.length; i++ ) + edits[i].setText(pp.getString("s" + i, "")); + originalName = edits[0].getText().toString(); + } + } @@ -85,42 +102,45 @@ public class PowerEditor extends ActionBarActivity { //TODO: change strings per resources boolean readyToSave = true; - for ( int i = 0; i < edits.length; i++ ) - if ( edits[i].getText().toString().trim().length() == 0 ) { + for ( int i = 0; i < edits.length; i++ ) { + String s = edits[i].getText().toString().trim(); + if (s.length() == 0) { edits[i].setError("This field is required"); readyToSave = false; } else { - strings[i] = edits[i].getText().toString().trim(); + strings[i] = s; } + } - - for ( int i = 0; i < spinners.length; i++) - if ( spinners[i].getSelectedItemPosition() == 0 ) { + 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: remove the color when the user has made a choice } else { - ints[i] = spinners[i].getSelectedItemPosition(); - spinners[i].setBackgroundColor(getResources().getColor(R.color.green)); + ints[i] = n; } + } if ( readyToSave ) { - String player = getIntent().getStringExtra("player"); - SharedPreferences p = getSharedPreferences(player, MODE_PRIVATE); - int powers = p.getInt("powers", 0); - for (int i = 0; i < powers; i++) { - if ( p.getString("power" + i, "") == strings[0] ) { - edits[0].setError("This power has already been defined. Use another name."); - return; + String saveName; + if ( originalName == null ) { + saveName = strings[0]; + for (int i = 0; i < powers; i++) { + if (p.getString("power" + power, "").equals(saveName)) saveName += "2"; } + } else { + saveName = originalName; } - p.edit().putString("power" + powers, strings[0]) + p.edit().putString("power" + powers, saveName) .putInt("powers", powers + 1) .apply(); - SharedPreferences.Editor ed = getSharedPreferences(strings[0], MODE_PRIVATE).edit(); + SharedPreferences.Editor ed = getSharedPreferences( saveName, MODE_PRIVATE).edit(); for (int i = 0; i < strings.length; i++) ed.putString("s" + i, strings[i]); diff --git a/app/src/main/java/com/kauron/dungeonmanager/ShowPlayer.java b/app/src/main/java/com/kauron/dungeonmanager/ShowPlayer.java index 91fb68d..0807bfc 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/ShowPlayer.java +++ b/app/src/main/java/com/kauron/dungeonmanager/ShowPlayer.java @@ -7,17 +7,19 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.Color; import android.graphics.PorterDuff; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.InputType; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.Window; import android.view.inputmethod.EditorInfo; import android.widget.AdapterView; +import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.ProgressBar; @@ -28,6 +30,8 @@ import com.nispok.snackbar.Snackbar; import com.nispok.snackbar.SnackbarManager; import com.nispok.snackbar.listeners.ActionClickListener; +import java.io.File; + public class ShowPlayer extends ActionBarActivity { public static final int CURRENT_PG = 1, NULL = 0; @@ -37,7 +41,7 @@ public class ShowPlayer extends ActionBarActivity { //TODO: fix undo (show snackbar with button in each case, without timing). private int undoObject, undoPreviousValue; - private ProgressBar pgBar, negPgBar, xpBar, curativeEffortsBar; + private ProgressBar posPgBar, negPgBar, xpBar, curativeEffortsBar; private TextView currentPg, currentXp, currentCurativeEfforts; private SharedPreferences p; private Toolbar toolbar; @@ -64,7 +68,7 @@ public class ShowPlayer extends ActionBarActivity { //Performing all the findViewById commands xpBar = (ProgressBar) findViewById(R.id.xpBar); curativeEffortsBar = (ProgressBar) findViewById(R.id.curativeEffortsBar); - pgBar = (ProgressBar) findViewById(R.id.pgBar); + posPgBar = (ProgressBar) findViewById(R.id.pgBar); negPgBar = (ProgressBar) findViewById(R.id.negPgBar); currentPg = (TextView) findViewById(R.id.currentPg); @@ -151,7 +155,8 @@ public class ShowPlayer extends ActionBarActivity { alert.show(); input.requestFocus(); return true; - //TODO: the player no longer contains the powers, therefore the + //TODO: the player no longer contains the powers, therefore the resting action affects the array of powers + //TODO: fix restoring powers // } else if (id == R.id.action_time_long_rest) { // player.rest(true); // SnackbarManager.show( @@ -299,31 +304,14 @@ public class ShowPlayer extends ActionBarActivity { private void pgUpdate() { int status = player.getState(); int pg = player.getPg(); - if (pg < 0) { - if (status == Player.MUERTO) - negPgBar.setProgress(negPgBar.getMax()); - else - negPgBar.setProgress(-pg); - pgBar.setProgress(0); - } else { - pgBar.setProgress(pg); - negPgBar.setProgress(0); - } + negPgBar.setProgress(pg < 0 ? -pg : 0); + posPgBar.setProgress(pg > 0 ? pg : 0); currentPg.setText(player.getPg() + " / " + player.getMaxPg()); - if (status == Player.MUERTO) { - pgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_IN); - } else if (status == Player.DEBILITADO) { - pgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_IN); - negPgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_IN); - } else if (status == Player.MALHERIDO) { - pgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.yellow), PorterDuff.Mode.SRC_IN); - negPgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.yellow), PorterDuff.Mode.SRC_IN); - } else { - pgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.green), PorterDuff.Mode.SRC_IN); - negPgBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.green), PorterDuff.Mode.SRC_IN); - } + int color = player.getStatusColor(getApplicationContext()); + posPgBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); + negPgBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); } private void restoreData(){ @@ -343,7 +331,7 @@ public class ShowPlayer extends ActionBarActivity { player.setCurativeEffort(p.getInt("curativeEfforts", player.getMaxCurativeEfforts())); player.setPg(p.getInt("pg", player.getMaxPg())); - pgBar.setMax(player.getMaxPg()); + posPgBar.setMax(player.getMaxPg()); negPgBar.setMax(player.getMaxPg() / 2); curativeEffortsBar.setMax(player.getMaxCurativeEfforts()); pgUpdate(); @@ -455,7 +443,16 @@ public class ShowPlayer extends ActionBarActivity { input.requestFocus(); } - public void addToList (View view) { startActivity(new Intent(this, PowerEditor.class).putExtra("player", player.getName())); } + public void addToList (View view) { + startActivity( + new Intent( + this, + PowerEditor.class + ) + .putExtra("player", player.getName()) + .putExtra("power", -1) + ); + } private void refreshList() { //TODO: check which is active (now there is only a power list), so there is only one possibility @@ -480,12 +477,12 @@ public class ShowPlayer extends ActionBarActivity { } attackList.setAdapter(new AttackAdapter(this, powers)); - final Activity thisActivity = this; attackList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { final Dialog dialog = new Dialog(ShowPlayer.this); + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.attack_display); // set the custom dialog components - text, image and button // Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); @@ -498,21 +495,10 @@ public class ShowPlayer extends ActionBarActivity { // }); //identify all the elements from the VIEW and then add LISTENERS - Power power = (Power) parent.getItemAtPosition(position); + final Power power = (Power) parent.getItemAtPosition(position); View nameText = dialog.findViewById(R.id.nameText); - switch(power.getFreq()){ - case Power.A_VOLUNTAD: - nameText.setBackgroundColor(getResources().getColor(R.color.at_will)); - break; - case Power.ENCUENTRO: - nameText.setBackgroundColor(getResources().getColor(R.color.encounter)); - break; - case Power.DIARIO: - nameText.setBackgroundColor(getResources().getColor(R.color.daily)); - break; - default: - nameText.setBackgroundColor(getResources().getColor(R.color.green)); - } + int color = power.getFreqColor(getApplicationContext()); + nameText.setBackgroundColor(color); //TODO: fix the title gap ((TextView) nameText) .setText(power.getName()); ((TextView) dialog.findViewById(R.id.typeText)) .setText(power.getTypeString()); @@ -521,43 +507,75 @@ public class ShowPlayer extends ActionBarActivity { ((TextView) dialog.findViewById(R.id.keywordsText)) .setText(power.getKeywords()); ((TextView) dialog.findViewById(R.id.distanceText)) .setText(String.valueOf(power.getDistance())); ((TextView) dialog.findViewById(R.id.objectiveText)).setText(power.getObjective()); - ((TextView) dialog.findViewById(R.id.impactText)) .setText(power.getImpact()); + ((TextView) dialog.findViewById(R.id.impactText)).setText(power.getImpact()); ((TextView) dialog.findViewById(R.id.otherText)) .setText(power.getOther()); String[] attack = getResources().getStringArray(R.array.attack_array); String[] defense = getResources().getStringArray(R.array.defense_array); - //TODO: add attack and defense array - ((TextView) dialog.findViewById(R.id.attackText)) .setText(attack[power.getAtk()] + ((TextView) dialog.findViewById(R.id.attackText)).setText(attack[power.getAtk()] + " " + getResources().getString(R.string.vs) + " " + defense[power.getDef()]); - dialog.findViewById(R.id.useButton).setOnClickListener(new View.OnClickListener() { + Button useButton = (Button) dialog.findViewById(R.id.useButton); + useButton.setBackgroundColor(color); + useButton.setTextColor(getResources().getColor(R.color.white)); + useButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //TODO: use power - Toast.makeText(getApplicationContext(), "Use it", Toast.LENGTH_LONG).show(); + Toast.makeText(getApplicationContext(), "Power used!", Toast.LENGTH_LONG).show(); + power.use(); } }); - - dialog.findViewById(R.id.deleteButton).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - //TODO: delete power - Toast.makeText(getApplicationContext(), "Delete it", Toast.LENGTH_LONG).show(); - } - }); - - dialog.findViewById(R.id.editButton).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - //TODO: edit power - Toast.makeText(getApplicationContext(), "Edit it", Toast.LENGTH_LONG).show(); - } - }); - + //TODO: edit power dialog.show(); } }); } + final Activity thisActivity = this; + + attackList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(final AdapterView parent, View view, final int position, long id) { + AlertDialog.Builder alert = new AlertDialog.Builder(thisActivity); + alert.setItems(new String[]{"Delete", "Edit"}, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if ( which == 0 ) { + //delete the item + String name = p.getString("power" + position, ""); + if (name != null && !name.isEmpty()) { + getSharedPreferences(name, MODE_PRIVATE).edit().clear().apply(); + Log.d("Tag", thisActivity.getApplicationContext().getFilesDir().getParent() + + File.separator + "shared_prefs" + File.separator + name + ".xml"); + try { + if(!new File(thisActivity.getApplicationContext().getFilesDir().getParent() + + File.separator + "shared_prefs" + File.separator + name + ".xml").delete()) + throw new Exception(); + } catch (Exception e) { + Toast.makeText(getApplicationContext(), "Error deleting attack files", Toast.LENGTH_SHORT).show(); + } + int max = p.getInt("powers", 0); + SharedPreferences.Editor ed = p.edit(); + for (int i = position; i < max - 1; i++) + ed.putString("power" + i, p.getString("power" + (i + 1), "max")); + ed.putInt("powers", max - 1).apply(); + refreshList(); + ed.remove("power" + (max - 1)).apply(); + } + } else { + //edit the item + startActivity( + new Intent(getApplicationContext(), PowerEditor.class) + .putExtra("power", position) + .putExtra("player", player.getName()) + ); + } + } + }); + alert.show(); + return true; + } + }); } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_power_editor.xml b/app/src/main/res/layout/activity_power_editor.xml index e58e04e..e811f96 100644 --- a/app/src/main/res/layout/activity_power_editor.xml +++ b/app/src/main/res/layout/activity_power_editor.xml @@ -5,6 +5,8 @@ android:layout_height="match_parent" android:orientation="vertical"> + + + android:text="Go to abilities"/> diff --git a/app/src/main/res/layout/attack_display.xml b/app/src/main/res/layout/attack_display.xml index 2365bf0..a321b3f 100644 --- a/app/src/main/res/layout/attack_display.xml +++ b/app/src/main/res/layout/attack_display.xml @@ -9,7 +9,7 @@ +