Commit f2cc55ca authored by Kevin Whitaker's avatar Kevin Whitaker

Do a better job at fully removing things when deleted. Add more edge case...

Do a better job at fully removing things when deleted. Add more edge case logic. Clean up messy functions. Have players and characters able to be added. Stole an icon from AOSP. ;)
parent 8103e300
......@@ -10,6 +10,7 @@ import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.eyecreate.miceandmystics.miceandmystics.adapters.CampaignAdapter;
import com.eyecreate.miceandmystics.miceandmystics.model.Enums.CampaignType;
......@@ -42,25 +43,33 @@ public class CampaignActivity extends RecyclerViewActivity {
//noinspection SimplifiableIfStatement
if (id == R.id.action_add_campaign) {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(this, R.style.dialogTheme)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = inflator.inflate(R.layout.dialog_new_campaign, null, false);
final Spinner typeSpinner = ((Spinner)dialogView.findViewById(R.id.campaign_type));
typeSpinner.setAdapter(new ArrayAdapter<CampaignType>(this, R.layout.simple_spinner_item, CampaignType.values()));
typeSpinner.setSelection(0);
AlertDialog addDialog = new AlertDialog.Builder(this,R.style.dialogTheme)
.setMessage("Please give your new campaign a unique name:")
.setView(dialogView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
((CampaignAdapter) getAdapter()).addItem(((EditText) dialogView.findViewById(R.id.campaign_name)).getText().toString(), CampaignType.valueOfDisplayName(typeSpinner.getSelectedItem().toString()));
}
})
.create();
addDialog.show();
newCampaignDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
public void newCampaignDialog() {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(this, R.style.dialogTheme)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = inflator.inflate(R.layout.dialog_new_campaign, null, false);
final Spinner typeSpinner = ((Spinner)dialogView.findViewById(R.id.campaign_type));
typeSpinner.setAdapter(new ArrayAdapter<CampaignType>(this, R.layout.simple_spinner_item, CampaignType.values()));
typeSpinner.setSelection(0);
AlertDialog addDialog = new AlertDialog.Builder(this,R.style.dialogTheme)
.setMessage("Please give your new campaign a unique name:")
.setView(dialogView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(((EditText) dialogView.findViewById(R.id.campaign_name)).getText().length()>0) {
((CampaignAdapter) getAdapter()).addItem(((EditText) dialogView.findViewById(R.id.campaign_name)).getText().toString(), CampaignType.valueOfDisplayName(typeSpinner.getSelectedItem().toString()));
} else {
Toast.makeText(CampaignActivity.this,"Can not have blank name!",Toast.LENGTH_LONG).show();
}
}
})
.create();
addDialog.show();
}
}
package com.eyecreate.miceandmystics.miceandmystics;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.eyecreate.miceandmystics.miceandmystics.adapters.CampaignDetailsAdapter;
import com.eyecreate.miceandmystics.miceandmystics.model.Campaign;
import com.eyecreate.miceandmystics.miceandmystics.model.Enums.CharacterNames;
import com.eyecreate.miceandmystics.miceandmystics.model.Player;
import io.realm.RealmResults;
public class CampaignDetailsActivity extends RecyclerViewActivity {
......@@ -38,9 +47,65 @@ public class CampaignDetailsActivity extends RecyclerViewActivity {
//noinspection SimplifiableIfStatement
if (id == R.id.action_add_character) {
newCharacterDialog();
return true;
} else if (id == R.id.action_add_player) {
newPlayerDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
public void newPlayerDialog() {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(this, R.style.dialogTheme)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = inflator.inflate(R.layout.dialog_new_player, null, false);
final EditText playerEdit = ((EditText)dialogView.findViewById(R.id.player_name));
AlertDialog addDialog = new AlertDialog.Builder(this,R.style.dialogTheme)
.setMessage("Please name the new player:")
.setView(dialogView)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (playerEdit.getText().length()>0) {
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
Player player = MiceAndMysticsApplication.getRealmInstance().createObject(Player.class);
player.setPlayerName(playerEdit.getText().toString());
MiceAndMysticsApplication.getRealmInstance().copyToRealmOrUpdate(player);
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
} else {
Toast.makeText(CampaignDetailsActivity.this,"Can not have a blank name!",Toast.LENGTH_LONG).show();
}
}
})
.create();
addDialog.show();
}
public void newCharacterDialog() {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(this, R.style.dialogTheme)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = inflator.inflate(R.layout.dialog_new_character, null, false);
final Spinner charSpinner = ((Spinner)dialogView.findViewById(R.id.character_type));
charSpinner.setAdapter(new ArrayAdapter<CharacterNames>(this, R.layout.simple_spinner_item, CharacterNames.values()));
charSpinner.setSelection(0);
final Spinner playerSpinner = ((Spinner)dialogView.findViewById(R.id.player_name));
RealmResults<Player> players = MiceAndMysticsApplication.getRealmInstance().where(Player.class).findAll();
playerSpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_spinner_item, Player.convertPlayerListToStringArray(players.subList(0,players.size()))));
playerSpinner.setSelection(0);
AlertDialog addDialog = new AlertDialog.Builder(this,R.style.dialogTheme)
.setMessage("Please select your character and controlling player:")
.setView(dialogView)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(playerSpinner.getSelectedItem() != null) {
((CampaignDetailsAdapter) getAdapter()).addCharacter(CharacterNames.valueOf(charSpinner.getSelectedItem().toString()), MiceAndMysticsApplication.getRealmInstance().where(Player.class).equalTo("playerName", playerSpinner.getSelectedItem().toString()).findFirst());
} else {
Toast.makeText(CampaignDetailsActivity.this,"No player selected. Maybe try making one first.",Toast.LENGTH_LONG).show();
}
}
})
.create();
addDialog.show();
}
}
......@@ -6,6 +6,7 @@ import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.eyecreate.miceandmystics.miceandmystics.MiceAndMysticsApplication;
import com.eyecreate.miceandmystics.miceandmystics.R;
import com.eyecreate.miceandmystics.miceandmystics.model.*;
import com.eyecreate.miceandmystics.miceandmystics.model.Enums.CampaignType;
import com.eyecreate.miceandmystics.miceandmystics.viewholders.CampaignViewHolder;
import io.realm.RealmResults;
......@@ -49,7 +50,13 @@ public class CampaignAdapter extends RecyclerView.Adapter<CampaignViewHolder> {
.where(com.eyecreate.miceandmystics.miceandmystics.model.Campaign.class)
.equalTo("campaignName",campaignName)
.findFirst();
for(com.eyecreate.miceandmystics.miceandmystics.model.Character character:campaign.getCurrentCharacters()) {
CampaignDetailsAdapter.removeCharacterFromDB(character);
}
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
for(Achievement achievement:campaign.getPartyStoryAchievements()) {
achievement.removeFromRealm();
}
campaign.removeFromRealm();
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
fullRefresh();
......
......@@ -4,6 +4,7 @@ import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;
import com.eyecreate.miceandmystics.miceandmystics.MiceAndMysticsApplication;
import com.eyecreate.miceandmystics.miceandmystics.R;
import com.eyecreate.miceandmystics.miceandmystics.model.*;
......@@ -47,21 +48,51 @@ public class CampaignDetailsAdapter extends RecyclerView.Adapter<RecyclerView.Vi
notifyDataSetChanged();
}
private boolean areThereDuplicates(Campaign campaign, CharacterNames character) {
for(Character chr:campaign.getCurrentCharacters()) {
if(chr.getCharacterName().equals(character.name())){
return true;
}
}
return false;
}
public void addCharacter(CharacterNames name, Player player) {
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
Character character = MiceAndMysticsApplication.getRealmInstance().createObject(Character.class);
character.setUuid(UUID.randomUUID().toString());
character.setCharacterName(name.name());
character.setControllingPlayer(player);
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
fullRefresh();
if(!areThereDuplicates(currentCampaign,name)) {
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
Character character = MiceAndMysticsApplication.getRealmInstance().createObject(Character.class);
character.setUuid(UUID.randomUUID().toString());
character.setCharacterName(name.name());
character.setControllingPlayer(player);
currentCampaign.getCurrentCharacters().add(character);
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
fullRefresh();
} else {
Toast.makeText(inflater.getContext(),"Can not have two of same character in game.",Toast.LENGTH_LONG).show();
}
}
public void removeCharacter(Character character) {
removeCharacterFromDB(character);
fullRefresh();
}
public static void removeCharacterFromDB(Character character) {
removeItemsAndAbilities(character);
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
character.removeFromRealm();
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
fullRefresh();
}
private static void removeItemsAndAbilities(Character character) {
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
for(Ability ability:character.getAbilities()) {
ability.removeFromRealm();
}
for(BackpackItem item:character.getStoredItems()) {
item.removeFromRealm();
}
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
}
@Override
......
......@@ -3,16 +3,16 @@ package com.eyecreate.miceandmystics.miceandmystics.model;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import java.util.List;
import java.util.UUID;
public class Player extends RealmObject {
@PrimaryKey
private String uuid;
private String playerName;
public Player() {}
public Player(String playerName) {setPlayerName(playerName);setUuid(UUID.randomUUID().toString());}
public Player(String playerName) {setPlayerName(playerName);}
public String getPlayerName() {
return playerName;
......@@ -22,11 +22,13 @@ public class Player extends RealmObject {
this.playerName = playerName;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
public static String[] convertPlayerListToStringArray(List<Player> playerList) {
String[] outputPlayers = new String[playerList.size()];
int iter = 0;
for(Player player:playerList){
outputPlayers[iter] = player.getPlayerName();
iter++;
}
return outputPlayers;
}
}
......@@ -23,6 +23,8 @@ public class CampaignDetailsViewHolder extends RecyclerView.ViewHolder implement
characterName = (TextView)itemView.findViewById(R.id.character_name);
playerName = (TextView)itemView.findViewById(R.id.character_player);
characterIcon = (ImageView)itemView.findViewById(R.id.character_icon);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void bindModel(Character character) {
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/dark_red"
android:text="Character:"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/character_type"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/dark_red"
android:text="Player:"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player_name"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/player_name"/>
</LinearLayout>
\ No newline at end of file
......@@ -6,4 +6,8 @@
android:title="Add Character"
android:icon="@android:drawable/ic_menu_add"
app:showAsAction="always"/>
<item android:id="@+id/action_add_player"
android:title="Add Player"
android:icon="@mipmap/ic_menu_invite"
app:showAsAction="always"/>
</menu>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment