1
0
Fork 0

Fixed playerList, now the players can be deleted and added

Editor still missing
This commit is contained in:
Carlos Galindo 2015-04-17 12:00:30 +02:00
parent aebb20542e
commit 30515a5384
7 changed files with 211 additions and 103 deletions

View file

@ -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;
}
}

View file

@ -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()
);
}

View file

@ -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
);
}
}

View file

@ -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);
}
}

View file

@ -25,14 +25,6 @@
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/finish_button"
android:onClick="finishButton"
android:layout_gravity="center_horizontal"
android:text="Save character" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"

View file

@ -45,6 +45,13 @@
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/help_text"
android:text="@string/help_welcome_text"
android:layout_gravity="center"/>
</LinearLayout>

View file

@ -90,4 +90,5 @@
<string name="add_player">Add player</string>
<string name="zero" translatable="false">0</string>
<string name="no_players">No players, please add one</string>
<string name="help_welcome_text">Press a player to open, long press for options</string>
</resources>