Newer
Older
package com.eyecreate.miceandmystics.miceandmystics;
Kevin Whitaker
committed
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
Kevin Whitaker
committed
import android.support.v7.widget.LinearLayoutManager;
Kevin Whitaker
committed
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
Kevin Whitaker
committed
import com.eyecreate.miceandmystics.miceandmystics.adapters.CampaignDetailsAdapter;
Kevin Whitaker
committed
import com.eyecreate.miceandmystics.miceandmystics.model.Campaign;
Kevin Whitaker
committed
import com.eyecreate.miceandmystics.miceandmystics.model.Enums.CharacterNames;
import com.eyecreate.miceandmystics.miceandmystics.model.Player;
import io.realm.RealmResults;
Kevin Whitaker
committed
public class CampaignDetailsActivity extends RecyclerViewActivity {
Campaign campaign;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Game Details");
if(getIntent().hasExtra("campaignName")) {
campaign = MiceAndMysticsApplication.getRealmInstance().where(Campaign.class).equalTo("campaignName",getIntent().getStringExtra("campaignName")).findFirst();
} else if(savedInstanceState.containsKey("campaignName")) {
campaign = MiceAndMysticsApplication.getRealmInstance().where(Campaign.class).equalTo("campaignName",savedInstanceState.getString("campaignName")).findFirst();
}
Kevin Whitaker
committed
setLayoutManager(new LinearLayoutManager(this));
Kevin Whitaker
committed
setAdapter(new CampaignDetailsAdapter(campaign));
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("campaignName", campaign.getCampaignName());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_campaign_details, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
Kevin Whitaker
committed
if (id == R.id.action_add_character) {
Kevin Whitaker
committed
newCharacterDialog();
return true;
} else if (id == R.id.action_add_player) {
newPlayerDialog(this);
return true;
} else if (id == R.id.action_manage_players) {
Intent managePlayers = new Intent(this,PlayerManagerActivity.class);
startActivity(managePlayers);
} else if (id == R.id.action_add_party_achievement) {
return true;
}
return super.onOptionsItemSelected(item);
}
Kevin Whitaker
committed
public static void newPlayerDialog(final Context ctx) {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(ctx, R.style.dialogTheme)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Kevin Whitaker
committed
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(ctx,R.style.dialogTheme)
Kevin Whitaker
committed
.setMessage("Please name the new player:")
.setView(dialogView)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
RealmResults<Player> matchingPlayers = MiceAndMysticsApplication.getRealmInstance().where(Player.class).equalTo("playerName", playerEdit.getText().toString()).findAll();
if (playerEdit.getText().length() > 0 && matchingPlayers.size() == 0) {
Kevin Whitaker
committed
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
Player player = MiceAndMysticsApplication.getRealmInstance().createObject(Player.class);
player.setPlayerName(playerEdit.getText().toString());
MiceAndMysticsApplication.getRealmInstance().copyToRealmOrUpdate(player);
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
} else if (playerEdit.getText().length() == 0) {
Toast.makeText(ctx, "Can not have a blank name!", Toast.LENGTH_LONG).show();
} else if (matchingPlayers.size() > 0) {
Toast.makeText(ctx, "Can not have same name as another player!", Toast.LENGTH_LONG).show();
Kevin Whitaker
committed
}
}
})
.create();
addDialog.show();
}
@Override
protected void onResume() {
super.onResume();
getAdapter().notifyDataSetChanged();
}
Kevin Whitaker
committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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();
}