7485
|
1 |
package org.hedgewars.hedgeroid.netplay;
|
|
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.Collection;
|
|
5 |
import java.util.Collections;
|
|
6 |
import java.util.List;
|
|
7 |
|
|
8 |
import org.hedgewars.hedgeroid.R;
|
|
9 |
import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
|
|
10 |
import org.hedgewars.hedgeroid.Datastructures.Team;
|
|
11 |
|
|
12 |
import android.app.Activity;
|
|
13 |
import android.app.AlertDialog;
|
|
14 |
import android.app.Dialog;
|
|
15 |
import android.content.DialogInterface;
|
|
16 |
import android.content.DialogInterface.OnClickListener;
|
|
17 |
import android.os.Bundle;
|
|
18 |
import android.support.v4.app.DialogFragment;
|
|
19 |
|
|
20 |
public class TeamAddDialog extends DialogFragment {
|
|
21 |
private static final String STATE_TEAMS_ALREADY_IN_GAME = "teamAlreadyInGame";
|
|
22 |
private ArrayList<String> teamsAlreadyInGame;
|
|
23 |
private List<Team> availableTeams;
|
|
24 |
private Listener listener;
|
|
25 |
|
|
26 |
public static interface Listener {
|
|
27 |
void onTeamAddDialogSubmitted(Team newTeam);
|
|
28 |
}
|
|
29 |
|
|
30 |
public TeamAddDialog() {
|
|
31 |
// Only for reflection-based instantiation by the framework
|
|
32 |
}
|
|
33 |
|
|
34 |
TeamAddDialog(Collection<String> teamsAlreadyInGame) {
|
|
35 |
this.teamsAlreadyInGame = new ArrayList<String>(teamsAlreadyInGame);
|
|
36 |
}
|
|
37 |
|
|
38 |
@Override
|
|
39 |
public void onAttach(Activity activity) {
|
|
40 |
super.onAttach(activity);
|
|
41 |
try {
|
|
42 |
listener = (Listener) activity;
|
|
43 |
} catch(ClassCastException e) {
|
|
44 |
throw new ClassCastException("Activity " + activity + " must implement TeamAddDialog.Listener to use TeamAddDialog.");
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
@Override
|
|
49 |
public void onDetach() {
|
|
50 |
super.onDetach();
|
|
51 |
listener = null;
|
|
52 |
}
|
|
53 |
|
|
54 |
@Override
|
|
55 |
public void onCreate(Bundle savedInstanceState) {
|
|
56 |
super.onCreate(savedInstanceState);
|
|
57 |
if(savedInstanceState != null) {
|
|
58 |
teamsAlreadyInGame = savedInstanceState.getStringArrayList(STATE_TEAMS_ALREADY_IN_GAME);
|
|
59 |
}
|
|
60 |
availableTeams = new ArrayList<Team>();
|
|
61 |
List<Team> teams = FrontendDataUtils.getTeams(getActivity());
|
|
62 |
for(Team team : teams) {
|
|
63 |
if(!teamsAlreadyInGame.contains(team.name)) {
|
|
64 |
availableTeams.add(team);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
Collections.sort(availableTeams, Team.NAME_ORDER);
|
|
68 |
}
|
|
69 |
|
|
70 |
@Override
|
|
71 |
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
72 |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
73 |
builder.setTitle(R.string.dialog_addteam_title);
|
|
74 |
builder.setIcon(R.drawable.human);
|
|
75 |
String[] teamNames = new String[availableTeams.size()];
|
|
76 |
for(int i=0; i<availableTeams.size(); i++) {
|
|
77 |
teamNames[i] = availableTeams.get(i).name;
|
|
78 |
}
|
|
79 |
builder.setItems(teamNames, new OnClickListener() {
|
|
80 |
public void onClick(DialogInterface dialog, int which) {
|
|
81 |
listener.onTeamAddDialogSubmitted(availableTeams.get(which));
|
|
82 |
}
|
|
83 |
});
|
|
84 |
return builder.create();
|
|
85 |
}
|
|
86 |
|
|
87 |
@Override
|
|
88 |
public void onSaveInstanceState(Bundle outState) {
|
|
89 |
super.onSaveInstanceState(outState);
|
|
90 |
outState.putStringArrayList(STATE_TEAMS_ALREADY_IN_GAME, teamsAlreadyInGame);
|
|
91 |
}
|
|
92 |
}
|