From 30515a53848ebca887659a2ac9599face7351fd6 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 17 Apr 2015 12:00:30 +0200 Subject: [PATCH] Fixed playerList, now the players can be deleted and added Editor still missing --- .../kauron/dungeonmanager/Introduction.java | 64 +++++---- .../kauron/dungeonmanager/MainActivity.java | 60 ++++----- .../dungeonmanager/PlayerDBHandler.java | 48 +++++++ .../com/kauron/dungeonmanager/Welcome.java | 126 +++++++++++++----- .../main/res/layout/activity_introduction.xml | 8 -- app/src/main/res/layout/activity_welcome.xml | 7 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 211 insertions(+), 103 deletions(-) create mode 100644 app/src/main/java/com/kauron/dungeonmanager/PlayerDBHandler.java diff --git a/app/src/main/java/com/kauron/dungeonmanager/Introduction.java b/app/src/main/java/com/kauron/dungeonmanager/Introduction.java index e045b4d..6fde151 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/Introduction.java +++ b/app/src/main/java/com/kauron/dungeonmanager/Introduction.java @@ -114,9 +114,6 @@ public class Introduction extends ActionBarActivity { } private boolean finished() { - SharedPreferences p = getSharedPreferences(Welcome.PREFERENCES, MODE_PRIVATE); - int i = p.getInt("players", 0); - SharedPreferences.Editor ed = getSharedPreferences("player" + i, MODE_PRIVATE).edit(); String nameString = name.getText().toString().trim(); int classInt = classSpinner.getSelectedItemPosition(); int raceInt = raceSpinner.getSelectedItemPosition(); @@ -139,19 +136,28 @@ public class Introduction extends ActionBarActivity { if (!this.sab.getText().toString().isEmpty()) sab = Integer.parseInt(this.sab.getText().toString()); - if(first) { +// if(first) { if ( !nameString.isEmpty() && - classInt != Player.NULL && - raceInt != Player.NULL && - pxInt != -1 && - car != 0 && - fue != 0 && - con != 0 && - des != 0 && - intel != 0 && - sab != 0 - ) { + classInt != Player.NULL && + raceInt != Player.NULL && + pxInt != -1 && + car != 0 && + fue != 0 && + con != 0 && + des != 0 && + intel != 0 && + sab != 0 + ) { + SharedPreferences p = getSharedPreferences(Welcome.PREFERENCES, MODE_PRIVATE); + int i = p.getInt("players", 0); + String saveName = nameString; + for (int j = 0; j < i; j++) { + saveName += p.getString("player" + j, "").equals(saveName) ? "2" : ""; + } + p.edit().putString("player" + i, saveName).putInt("players", i + 1).apply(); + SharedPreferences.Editor ed = getSharedPreferences(saveName, MODE_PRIVATE).edit(); + //first save it all ed.putString("playerName", nameString); ed.putInt("classInt", classInt); @@ -164,25 +170,25 @@ public class Introduction extends ActionBarActivity { ed.putInt("sab", sab); ed.putInt("con", con); ed.putInt("des", des); + //TEMP + ed.apply(); } else { return false; } - } else { - if (!nameString.isEmpty()) ed.putString("playerName", nameString); - if (classInt != Player.NULL) ed.putInt("classInt", classInt); - if (raceInt != Player.NULL) ed.putInt("raceInt", raceInt); - if (pxInt != -1) ed.putInt("px", pxInt); +// } else { +// if (!nameString.isEmpty()) ed.putString("playerName", nameString); +// if (classInt != Player.NULL) ed.putInt("classInt", classInt); +// if (raceInt != Player.NULL) ed.putInt("raceInt", raceInt); +// if (pxInt != -1) ed.putInt("px", pxInt); +// +// if (fue != 0) ed.putInt("fue", fue); +// if (car != 0) ed.putInt("car", car); +// if (intel != 0) ed.putInt("int", intel); +// if (sab != 0) ed.putInt("sab", sab); +// if (con != 0) ed.putInt("con", con); +// if (des != 0) ed.putInt("des", des); +// } - if (fue != 0) ed.putInt("fue", fue); - if (car != 0) ed.putInt("car", car); - if (intel != 0) ed.putInt("int", intel); - if (sab != 0) ed.putInt("sab", sab); - if (con != 0) ed.putInt("con", con); - if (des != 0) ed.putInt("des", des); - } - ed.apply(); - getSharedPreferences(Welcome.PREFERENCES, MODE_PRIVATE).edit() - .putInt("players", i + 1).apply(); return true; } } diff --git a/app/src/main/java/com/kauron/dungeonmanager/MainActivity.java b/app/src/main/java/com/kauron/dungeonmanager/MainActivity.java index dd36625..1692541 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/MainActivity.java +++ b/app/src/main/java/com/kauron/dungeonmanager/MainActivity.java @@ -1,5 +1,6 @@ package com.kauron.dungeonmanager; +import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; @@ -44,10 +45,22 @@ public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + //Initializing activity (setting toolbar as actionbar) setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); - p = getSharedPreferences("player" + getIntent().getIntExtra("player", 0), MODE_PRIVATE); + + //Loading player + try{ + String name = getSharedPreferences(Welcome.PREFERENCES, MODE_PRIVATE) + .getString("player" + getIntent().getIntExtra("player", 0), "null"); + p = getSharedPreferences(name, MODE_PRIVATE); + } catch (Exception e) { + Toast.makeText(getApplicationContext(), "Error loading character", Toast.LENGTH_LONG).show(); + finish(); + } + + //Performing all the findViewById commands xpBar = (ProgressBar) findViewById(R.id.xpBar); curativeEffortsBar = (ProgressBar) findViewById(R.id.curativeEffortsBar); pgBar = (ProgressBar) findViewById(R.id.pgBar); @@ -66,12 +79,8 @@ public class MainActivity extends ActionBarActivity { curativeEffortsBar.getProgressDrawable() .setColorFilter(getResources().getColor(R.color.surges_bar), PorterDuff.Mode.SRC_IN); undo = false; - //begin + restoreData(); - pgUpdate(); - ceUpdate(); - pxUpdate(); - //end invalidateOptionsMenu(); } @@ -84,11 +93,12 @@ public class MainActivity extends ActionBarActivity { @Override public boolean onPrepareOptionsMenu (Menu menu) { - menu.findItem(R.id.action_undo).setEnabled(undo); + MenuItem menuUndo = menu.findItem(R.id.action_undo); + menuUndo.setEnabled(undo); if (undo) - menu.findItem(R.id.action_undo).getIcon().setAlpha(255); + menuUndo.getIcon().setAlpha(255); else - menu.findItem(R.id.action_undo).getIcon().setAlpha(128); + menuUndo.getIcon().setAlpha(128); return true; } @@ -148,7 +158,9 @@ public class MainActivity extends ActionBarActivity { // Player.LEVEL_PX[player.getLevel() - 1], // true, player.getPx() - Player.LEVEL_PX[player.getLevel() - 1] // ); - } catch(Exception e) {} + } catch(Exception e) { + Toast.makeText(getApplicationContext(), "There was an error leveling up", Toast.LENGTH_LONG).show(); + } } }); alert.show(); @@ -337,17 +349,14 @@ public class MainActivity extends ActionBarActivity { undo(); } }); - + final Activity activity = this; alert.setNegativeButton(R.string.die, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { - //TODO: fix snackbar on death - Toast.makeText(getApplicationContext(), - R.string.message_death, Toast.LENGTH_LONG).show(); -// SnackbarManager.show( -// Snackbar -// .with(this) -// .text(R.string.message_death) -// ); + SnackbarManager.show( + Snackbar + .with(activity) + .text(R.string.message_death) + ); p.edit().clear().apply(); restoreData(); } @@ -387,15 +396,6 @@ public class MainActivity extends ActionBarActivity { } private void restoreData(){ - int i = getIntent().getIntExtra("player", 0); - if (!p.getBoolean("saved", false)) { - if (i == -1) { - Intent intent = new Intent(this, Introduction.class); - startActivity(intent.putExtra( - "first_time", - !p.getBoolean("saved", false) - )); - } if (player == null) { player = new Player( p.getString("playerName", getString(R.string.adventurer_name)), @@ -481,7 +481,7 @@ public class MainActivity extends ActionBarActivity { //defenses ((TextView) findViewById(R.id.CA)).setText( - getString(R.string.CA) + ": " + player.getCa() + getString(R.string.CA) + ":" + player.getCa() ); ((TextView) findViewById(R.id.FORT)).setText( getString(R.string.FORT) + ":" + player.getFort() @@ -490,7 +490,7 @@ public class MainActivity extends ActionBarActivity { getString(R.string.REF) + ":" + player.getRef() ); ((TextView) findViewById(R.id.VOL)).setText( - getString(R.string.VOL) + ": " + player.getVol() + getString(R.string.VOL) + ":" + player.getVol() ); } diff --git a/app/src/main/java/com/kauron/dungeonmanager/PlayerDBHandler.java b/app/src/main/java/com/kauron/dungeonmanager/PlayerDBHandler.java new file mode 100644 index 0000000..ec1960b --- /dev/null +++ b/app/src/main/java/com/kauron/dungeonmanager/PlayerDBHandler.java @@ -0,0 +1,48 @@ +package com.kauron.dungeonmanager; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class PlayerDBHandler extends SQLiteOpenHelper { + + private static final int DB_VERSION = 1; + private static final String DB_NAME = "dnd.db"; + + public static final String TABLE_PLAYERS = "players"; + public static final String C_ID="_id", C_NAME="name", C_CLASS="class", C_RACE="race"; + public static final String C_PG="pg", C_PG_MAX="pgMax", C_SURGES="surges", C_SURGES_MAX="surgesMax"; + public static final String C_PX = "px", C_ATK = "atk", C_ABILITY = "ability", C_POWER_ID="powerID"; + + //TODO: complete class and implement database to save the players instead of sharedprefs + //TODO: not using it at the moment + public PlayerDBHandler(Context context, SQLiteDatabase.CursorFactory factory) { + super(context, DB_NAME, factory, DB_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYERS); + onCreate(db); + } + + public void addPlayer(Player p) { + + } + + public void updatePlayer (Player p) { + + } + + public void deletePlayer (Player p) { + SQLiteDatabase db = getWritableDatabase(); + db.execSQL( + "DELETE FROM " + TABLE_PLAYERS + " WTHERE " //TODO: complete + ); + } +} diff --git a/app/src/main/java/com/kauron/dungeonmanager/Welcome.java b/app/src/main/java/com/kauron/dungeonmanager/Welcome.java index 69475c9..cffc331 100644 --- a/app/src/main/java/com/kauron/dungeonmanager/Welcome.java +++ b/app/src/main/java/com/kauron/dungeonmanager/Welcome.java @@ -1,16 +1,21 @@ package com.kauron.dungeonmanager; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; -import android.widget.ListAdapter; import android.widget.ListView; +import android.widget.Toast; + +import java.io.File; public class Welcome extends ActionBarActivity { @@ -18,6 +23,7 @@ public class Welcome extends ActionBarActivity { private SharedPreferences p; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -55,6 +61,7 @@ public class Welcome extends ActionBarActivity { load(); } + //TODO: load all players with secondary constructor (except the ones created by introduction and haven't passed by MainActivity) private void load() { int n = p.getInt("players",0); ListView playerList = (ListView) findViewById(R.id.listView); @@ -64,50 +71,54 @@ public class Welcome extends ActionBarActivity { elements = adapter.getCount(); if ( elements < n && adapter != null ) { playerList.setVisibility(View.VISIBLE); + findViewById(R.id.help_text).setVisibility(View.VISIBLE); findViewById(R.id.no_players_text).setVisibility(View.GONE); for ( int i = elements; i < n; i++ ) { - SharedPreferences sav = getSharedPreferences("player" + i, MODE_PRIVATE); - adapter.add( - new Player( - sav.getString(Player.NAME, "player" + i), - sav.getInt(Player.CLASS, 0), - sav.getInt(Player.RACE, 0), - sav.getInt(Player.PX, 0), - new int[] { - sav.getInt("fue", 10), - sav.getInt("con", 10), - sav.getInt("des", 10), - sav.getInt("int", 10), - sav.getInt("sab", 10), - sav.getInt("car", 10) - }, - new int[18], - new Power[4] - )); + SharedPreferences sav = getSharedPreferences(p.getString("player" + i, ""), MODE_PRIVATE); + if (sav.contains(Player.NAME)) + adapter.add( + new Player( + sav.getString(Player.NAME, "player" + i), + sav.getInt(Player.CLASS, 0), + sav.getInt(Player.RACE, 0), + sav.getInt(Player.PX, 0), + new int[] { + sav.getInt("fue", 10), + sav.getInt("con", 10), + sav.getInt("des", 10), + sav.getInt("int", 10), + sav.getInt("sab", 10), + sav.getInt("car", 10) + }, + new int[18], + new Power[4] + )); } } else if ( n != 0 ) { playerList.setVisibility(View.VISIBLE); + findViewById(R.id.help_text).setVisibility(View.VISIBLE); findViewById(R.id.no_players_text).setVisibility(View.GONE); Player[] players = new Player[n]; for ( int i = 0; i < n; i++ ) { //TODO: fill the information for the player creation - SharedPreferences sav = getSharedPreferences("player" + i, MODE_PRIVATE); - players[i] = new Player( - sav.getString(Player.NAME, "player" + i), - sav.getInt(Player.CLASS, 0), - sav.getInt(Player.RACE, 0), - sav.getInt(Player.PX, 0), - new int[] { - sav.getInt("fue", 10), - sav.getInt("con", 10), - sav.getInt("des", 10), - sav.getInt("int", 10), - sav.getInt("sab", 10), - sav.getInt("car", 10) - }, - new int[18], - new Power[4] - ); + SharedPreferences sav = getSharedPreferences(p.getString("player" + i, ""), MODE_PRIVATE); + if (sav.contains(Player.NAME)) + players[i] = new Player( + sav.getString(Player.NAME, "player" + i), + sav.getInt(Player.CLASS, 0), + sav.getInt(Player.RACE, 0), + sav.getInt(Player.PX, 0), + new int[] { + sav.getInt("fue", 10), + sav.getInt("con", 10), + sav.getInt("des", 10), + sav.getInt("int", 10), + sav.getInt("sab", 10), + sav.getInt("car", 10) + }, + new int[18], + new Power[4] + ); } playerList.setAdapter(new PlayerAdapter(this, players)); @@ -120,8 +131,51 @@ public class Welcome extends ActionBarActivity { ).putExtra("player", position)); } }); + final ActionBarActivity activity = this; + playerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(final AdapterView parent, View view, final int position, long id) { + AlertDialog.Builder alert = new AlertDialog.Builder(activity); + 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("player" + position, ""); + if (name != null && !name.isEmpty()) { + getSharedPreferences(name, MODE_PRIVATE).edit().clear().apply(); + Log.d("Tag", activity.getApplicationContext().getFilesDir().getParent() + + File.separator + "shared_prefs" + File.separator + name + ".xml"); + try { + if(!new File(activity.getApplicationContext().getFilesDir().getParent() + + File.separator + "shared_prefs" + File.separator + name + ".xml").delete()) + throw new Exception(); + } catch (Exception e) { + Toast.makeText(getApplicationContext(), "Error deleting player files", Toast.LENGTH_SHORT).show(); + } + int max = p.getInt("players", 0); + SharedPreferences.Editor ed = p.edit(); + for (int i = position; i < max - 1; i++) + ed.putString("player" + i, p.getString("player" + (i + 1), "max")); + ed.putInt("players", max - 1).apply(); + load(); + ed.remove("player" + (max - 1)).apply(); + } + } else { + //edit the item + Toast.makeText( + activity, "Editor not implemented yet", Toast.LENGTH_LONG) + .show(); + } + } + }); + alert.show(); + return true; + } + }); } else { playerList.setVisibility(View.GONE); + findViewById(R.id.help_text).setVisibility(View.GONE); findViewById(R.id.no_players_text).setVisibility(View.VISIBLE); } } diff --git a/app/src/main/res/layout/activity_introduction.xml b/app/src/main/res/layout/activity_introduction.xml index e221901..711099b 100644 --- a/app/src/main/res/layout/activity_introduction.xml +++ b/app/src/main/res/layout/activity_introduction.xml @@ -25,14 +25,6 @@ -