Commit c4227f0b authored by Kevin Whitaker's avatar Kevin Whitaker

Add in player management and improve use interaction cases.

parent f134e20b
......@@ -25,6 +25,9 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.eyecreate.miceandmystics.miceandmystics.CampaignActivity" />
</activity>
<activity
android:name=".PlayerManagerActivity">
</activity>
</application>
</manifest>
......@@ -3,7 +3,9 @@ package com.eyecreate.miceandmystics.miceandmystics;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.*;
import android.widget.ArrayAdapter;
......@@ -25,11 +27,20 @@ public class CampaignDetailsActivity extends RecyclerViewActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Game Details");
campaign = MiceAndMysticsApplication.getRealmInstance().where(Campaign.class).equalTo("campaignName",getIntent().getStringExtra("campaignName")).findFirst();
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();
}
setLayoutManager(new LinearLayoutManager(this));
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) {
......@@ -50,31 +61,38 @@ public class CampaignDetailsActivity extends RecyclerViewActivity {
newCharacterDialog();
return true;
} else if (id == R.id.action_add_player) {
newPlayerDialog();
newPlayerDialog(this);
return true;
} else if (id == R.id.action_manage_players) {
Intent managePlayers = new Intent(this,PlayerManagerActivity.class);
startActivity(managePlayers);
return true;
}
return super.onOptionsItemSelected(item);
}
public void newPlayerDialog() {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(this, R.style.dialogTheme)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public static void newPlayerDialog(final Context ctx) {
LayoutInflater inflator = (LayoutInflater)(new ContextThemeWrapper(ctx, 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)
AlertDialog addDialog = new AlertDialog.Builder(ctx,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) {
RealmResults<Player> matchingPlayers = MiceAndMysticsApplication.getRealmInstance().where(Player.class).equalTo("playerName", playerEdit.getText().toString()).findAll();
if (playerEdit.getText().length() > 0 && matchingPlayers.size() == 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();
} 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();
}
}
})
......@@ -82,6 +100,12 @@ public class CampaignDetailsActivity extends RecyclerViewActivity {
addDialog.show();
}
@Override
protected void onResume() {
super.onResume();
getAdapter().notifyDataSetChanged();
}
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);
......
package com.eyecreate.miceandmystics.miceandmystics;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import com.eyecreate.miceandmystics.miceandmystics.adapters.PlayerManagerAdapter;
public class PlayerManagerActivity extends RecyclerViewActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Manage Players");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setLayoutManager(new LinearLayoutManager(this));
setAdapter(new PlayerManagerAdapter());
}
@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_player_manager, 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
if (id == R.id.action_add_player) {
CampaignDetailsActivity.newPlayerDialog(this);
return true;
} else if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
package com.eyecreate.miceandmystics.miceandmystics.adapters;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
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.Character;
import com.eyecreate.miceandmystics.miceandmystics.viewholders.PlayerViewHolder;
import io.realm.RealmResults;
import java.util.Iterator;
public class PlayerManagerAdapter extends RecyclerView.Adapter<PlayerViewHolder> {
LayoutInflater inflater;
RealmResults<Player> playerList;
public PlayerManagerAdapter() {
playerList = MiceAndMysticsApplication.getRealmInstance().where(Player.class).findAll();
}
@Override
public PlayerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(inflater == null) inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return new PlayerViewHolder(inflater.inflate(R.layout.item_player,parent,false),this);
}
@Override
public void onBindViewHolder(PlayerViewHolder holder, int position) {
holder.bindHolder(playerList.get(position));
}
@Override
public int getItemCount() {
return playerList.size();
}
public void removePlayer(final String playerName,Context ctx) {
//This part checks to see if any characters are attached to this player.
final RealmResults<Character> playerCharacters = MiceAndMysticsApplication.getRealmInstance().where(com.eyecreate.miceandmystics.miceandmystics.model.Character.class)
.equalTo("controllingPlayer.playerName", playerName).findAll();
if(playerCharacters.size()>0) {
AlertDialog removeDialog = new AlertDialog.Builder(ctx,R.style.dialogTheme)
.setMessage("Removing player "+playerName+" must first remove all characters owned by player. Do you want to continue still?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
for(Iterator iter = playerCharacters.iterator();playerCharacters.iterator().hasNext();) {
CampaignDetailsAdapter.removeCharacterFromDB((Character)iter.next());
}
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
MiceAndMysticsApplication.getRealmInstance().where(Player.class).equalTo("playerName",playerName).findFirst().removeFromRealm();
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
notifyDataSetChanged();
}
})
.create();
removeDialog.show();
} else {
AlertDialog removeDialog = new AlertDialog.Builder(ctx, R.style.dialogTheme)
.setMessage("Do you want to remove player: " + playerName + "?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MiceAndMysticsApplication.getRealmInstance().beginTransaction();
MiceAndMysticsApplication.getRealmInstance().where(Player.class).equalTo("playerName",playerName).findFirst().removeFromRealm();
MiceAndMysticsApplication.getRealmInstance().commitTransaction();
notifyDataSetChanged();
}
})
.create();
removeDialog.show();
}
}
}
package com.eyecreate.miceandmystics.miceandmystics.viewholders;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.eyecreate.miceandmystics.miceandmystics.R;
import com.eyecreate.miceandmystics.miceandmystics.adapters.PlayerManagerAdapter;
import com.eyecreate.miceandmystics.miceandmystics.model.Player;
public class PlayerViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
TextView playerName;
PlayerManagerAdapter adapter;
public PlayerViewHolder(View itemView,PlayerManagerAdapter adapter) {
super(itemView);
this.adapter = adapter;
playerName = (TextView)itemView.findViewById(R.id.player_name);
itemView.setOnLongClickListener(this);
}
public void bindHolder(Player player) {
playerName.setText(player.getPlayerName());
}
@Override
public boolean onLongClick(View view) {
adapter.removePlayer(playerName.getText().toString(),view.getContext());
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
app:cardUseCompatPadding="true"
app:cardElevation="4dp"
android:foreground="?android:attr/selectableItemBackground"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/player_list"
android:padding="8dp"
android:background="@color/parchment_yellow">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="@android:color/black"
android:layout_gravity="center_vertical"
android:id="@+id/player_name"/>
</LinearLayout>
</android.support.v7.widget.CardView>
\ No newline at end of file
......@@ -9,5 +9,13 @@
<item android:id="@+id/action_add_player"
android:title="Add Player"
android:icon="@mipmap/ic_menu_invite"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_add_party_achievement"
android:title="Add Achievment"
android:icon="@mipmap/ic_menu_star"
app:showAsAction="always"/>
<item android:id="@+id/action_manage_players"
android:title="Manage Players"
android:icon="@mipmap/ic_menu_friendslist"
app:showAsAction="ifRoom"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_add_player"
android:title="Add Player"
android:icon="@mipmap/ic_menu_invite"
app:showAsAction="always"/>
</menu>
\ No newline at end of file
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