Newer
Older
package com.eyecreate.miceandmystics.miceandmystics;
Kevin Whitaker
committed
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
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);
Kevin Whitaker
committed
setTitle(getIntent().getStringExtra("campaignName"));
campaign = MiceAndMysticsApplication.getRealmInstance().where(Campaign.class).equalTo("campaignName",getIntent().getStringExtra("campaignName")).findFirst();
setLayoutManager(new LinearLayoutManager(this));
Kevin Whitaker
committed
setAdapter(new CampaignDetailsAdapter(campaign));
}
@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();
return true;
}
return super.onOptionsItemSelected(item);
}
Kevin Whitaker
committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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();
}