35 import android.content.DialogInterface.OnClickListener; |
35 import android.content.DialogInterface.OnClickListener; |
36 import android.os.Bundle; |
36 import android.os.Bundle; |
37 import android.support.v4.app.DialogFragment; |
37 import android.support.v4.app.DialogFragment; |
38 |
38 |
39 public class TeamAddDialog extends DialogFragment { |
39 public class TeamAddDialog extends DialogFragment { |
40 private static final String STATE_TEAMS_ALREADY_IN_GAME = "teamAlreadyInGame"; |
40 private static final String STATE_TEAMS_ALREADY_IN_GAME = "teamAlreadyInGame"; |
41 private ArrayList<String> teamsAlreadyInGame; |
41 private ArrayList<String> teamsAlreadyInGame; |
42 private List<Team> availableTeams; |
42 private List<Team> availableTeams; |
43 private Listener listener; |
43 private Listener listener; |
44 |
44 |
45 public static interface Listener { |
45 public static interface Listener { |
46 void onTeamAddDialogSubmitted(Team newTeam); |
46 void onTeamAddDialogSubmitted(Team newTeam); |
47 } |
47 } |
48 |
48 |
49 public TeamAddDialog() { |
49 public TeamAddDialog() { |
50 // Only for reflection-based instantiation by the framework |
50 // Only for reflection-based instantiation by the framework |
51 } |
51 } |
52 |
52 |
53 TeamAddDialog(Collection<String> teamsAlreadyInGame) { |
53 TeamAddDialog(Collection<String> teamsAlreadyInGame) { |
54 this.teamsAlreadyInGame = new ArrayList<String>(teamsAlreadyInGame); |
54 this.teamsAlreadyInGame = new ArrayList<String>(teamsAlreadyInGame); |
55 } |
55 } |
56 |
56 |
57 @Override |
57 @Override |
58 public void onAttach(Activity activity) { |
58 public void onAttach(Activity activity) { |
59 super.onAttach(activity); |
59 super.onAttach(activity); |
60 try { |
60 try { |
61 listener = (Listener) activity; |
61 listener = (Listener) activity; |
62 } catch(ClassCastException e) { |
62 } catch(ClassCastException e) { |
63 throw new ClassCastException("Activity " + activity + " must implement TeamAddDialog.Listener to use TeamAddDialog."); |
63 throw new ClassCastException("Activity " + activity + " must implement TeamAddDialog.Listener to use TeamAddDialog."); |
64 } |
64 } |
65 } |
65 } |
66 |
66 |
67 @Override |
67 @Override |
68 public void onDetach() { |
68 public void onDetach() { |
69 super.onDetach(); |
69 super.onDetach(); |
70 listener = null; |
70 listener = null; |
71 } |
71 } |
72 |
72 |
73 @Override |
73 @Override |
74 public void onCreate(Bundle savedInstanceState) { |
74 public void onCreate(Bundle savedInstanceState) { |
75 super.onCreate(savedInstanceState); |
75 super.onCreate(savedInstanceState); |
76 if(savedInstanceState != null) { |
76 if(savedInstanceState != null) { |
77 teamsAlreadyInGame = savedInstanceState.getStringArrayList(STATE_TEAMS_ALREADY_IN_GAME); |
77 teamsAlreadyInGame = savedInstanceState.getStringArrayList(STATE_TEAMS_ALREADY_IN_GAME); |
78 } |
78 } |
79 availableTeams = new ArrayList<Team>(); |
79 availableTeams = new ArrayList<Team>(); |
80 List<Team> teams = FrontendDataUtils.getTeams(getActivity()); |
80 List<Team> teams = FrontendDataUtils.getTeams(getActivity()); |
81 for(Team team : teams) { |
81 for(Team team : teams) { |
82 if(!teamsAlreadyInGame.contains(team.name)) { |
82 if(!teamsAlreadyInGame.contains(team.name)) { |
83 availableTeams.add(team); |
83 availableTeams.add(team); |
84 } |
84 } |
85 } |
85 } |
86 Collections.sort(availableTeams, Team.NAME_ORDER); |
86 Collections.sort(availableTeams, Team.NAME_ORDER); |
87 } |
87 } |
88 |
88 |
89 // TODO use icons for the teams (corresponding to botlevel) |
89 // TODO use icons for the teams (corresponding to botlevel) |
90 @Override |
90 @Override |
91 public Dialog onCreateDialog(Bundle savedInstanceState) { |
91 public Dialog onCreateDialog(Bundle savedInstanceState) { |
92 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
92 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
93 builder.setTitle(R.string.dialog_addteam_title); |
93 builder.setTitle(R.string.dialog_addteam_title); |
94 builder.setIcon(R.drawable.human); |
94 builder.setIcon(R.drawable.human); |
95 String[] teamNames = new String[availableTeams.size()]; |
95 String[] teamNames = new String[availableTeams.size()]; |
96 for(int i=0; i<availableTeams.size(); i++) { |
96 for(int i=0; i<availableTeams.size(); i++) { |
97 teamNames[i] = availableTeams.get(i).name; |
97 teamNames[i] = availableTeams.get(i).name; |
98 } |
98 } |
99 builder.setItems(teamNames, new OnClickListener() { |
99 builder.setItems(teamNames, new OnClickListener() { |
100 public void onClick(DialogInterface dialog, int which) { |
100 public void onClick(DialogInterface dialog, int which) { |
101 listener.onTeamAddDialogSubmitted(availableTeams.get(which)); |
101 listener.onTeamAddDialogSubmitted(availableTeams.get(which)); |
102 } |
102 } |
103 }); |
103 }); |
104 return builder.create(); |
104 return builder.create(); |
105 } |
105 } |
106 |
106 |
107 @Override |
107 @Override |
108 public void onSaveInstanceState(Bundle outState) { |
108 public void onSaveInstanceState(Bundle outState) { |
109 super.onSaveInstanceState(outState); |
109 super.onSaveInstanceState(outState); |
110 outState.putStringArrayList(STATE_TEAMS_ALREADY_IN_GAME, teamsAlreadyInGame); |
110 outState.putStringArrayList(STATE_TEAMS_ALREADY_IN_GAME, teamsAlreadyInGame); |
111 } |
111 } |
112 } |
112 } |