37 /** |
37 /** |
38 * This fragment displays a chatlog and text input field for chatting in either |
38 * This fragment displays a chatlog and text input field for chatting in either |
39 * the lobby or a room. |
39 * the lobby or a room. |
40 */ |
40 */ |
41 public class ChatFragment extends Fragment { |
41 public class ChatFragment extends Fragment { |
42 private ChatlogAdapter adapter; |
42 private ChatlogAdapter adapter; |
43 private Netplay netplay; |
43 private Netplay netplay; |
44 private MessageLog messageLog; |
44 private MessageLog messageLog; |
45 private boolean inRoom; |
45 private boolean inRoom; |
46 |
46 |
47 public void setInRoom(boolean inRoom) { |
47 public void setInRoom(boolean inRoom) { |
48 this.inRoom = inRoom; |
48 this.inRoom = inRoom; |
49 } |
49 } |
50 |
50 |
51 @Override |
51 @Override |
52 public void onCreate(Bundle savedInstanceState) { |
52 public void onCreate(Bundle savedInstanceState) { |
53 super.onCreate(savedInstanceState); |
53 super.onCreate(savedInstanceState); |
54 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
54 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
55 adapter = new ChatlogAdapter(getActivity()); |
55 adapter = new ChatlogAdapter(getActivity()); |
56 } |
56 } |
57 |
57 |
58 @Override |
58 @Override |
59 public void onStart() { |
59 public void onStart() { |
60 super.onStart(); |
60 super.onStart(); |
61 messageLog = inRoom ? netplay.roomChatlog : netplay.lobbyChatlog; |
61 messageLog = inRoom ? netplay.roomChatlog : netplay.lobbyChatlog; |
62 adapter.setLog(messageLog.getLog()); |
62 adapter.setLog(messageLog.getLog()); |
63 messageLog.addListener(adapter); |
63 messageLog.addListener(adapter); |
64 } |
64 } |
65 |
65 |
66 @Override |
66 @Override |
67 public void onStop() { |
67 public void onStop() { |
68 super.onStop(); |
68 super.onStop(); |
69 messageLog.removeListener(adapter); |
69 messageLog.removeListener(adapter); |
70 } |
70 } |
71 |
71 |
72 @Override |
72 @Override |
73 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
73 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
74 Bundle savedInstanceState) { |
74 Bundle savedInstanceState) { |
75 View view = inflater.inflate(R.layout.fragment_chat, container, false); |
75 View view = inflater.inflate(R.layout.fragment_chat, container, false); |
76 |
76 |
77 ListView listView = (ListView) view.findViewById(R.id.chatConsole); |
77 ListView listView = (ListView) view.findViewById(R.id.chatConsole); |
78 listView.setAdapter(adapter); |
78 listView.setAdapter(adapter); |
79 listView.setDivider(null); |
79 listView.setDivider(null); |
80 listView.setDividerHeight(0); |
80 listView.setDividerHeight(0); |
81 listView.setVerticalFadingEdgeEnabled(true); |
81 listView.setVerticalFadingEdgeEnabled(true); |
82 |
82 |
83 EditText editText = (EditText) view.findViewById(R.id.chatInput); |
83 EditText editText = (EditText) view.findViewById(R.id.chatInput); |
84 editText.setOnEditorActionListener(new ChatSendListener()); |
84 editText.setOnEditorActionListener(new ChatSendListener()); |
85 |
85 |
86 return view; |
86 return view; |
87 } |
87 } |
88 |
88 |
89 private final class ChatSendListener implements OnEditorActionListener { |
89 private final class ChatSendListener implements OnEditorActionListener { |
90 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
90 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
91 String text = v.getText().toString(); |
91 String text = v.getText().toString(); |
92 if(text.length()>0) { |
92 if(text.length()>0) { |
93 v.setText(""); |
93 v.setText(""); |
94 netplay.sendChat(text); |
94 netplay.sendChat(text); |
95 } |
95 } |
96 return true; |
96 return true; |
97 } |
97 } |
98 } |
98 } |
99 } |
99 } |