Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GroupFragment extends Fragment {
- // TODO: Rename parameter arguments, choose names that match
- // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
- private static final String ARG_PARAM1 = "param1";
- private static final String ARG_PARAM2 = "param2";
- // TODO: Rename and change types of parameters
- private String mParam1;
- private String mParam2;
- static ArrayList<Group> groupNameList = new ArrayList<Group>();
- static ArrayList<String> groupKeyValues = new ArrayList<String>();
- GroupAdapter groupAdapter;
- ListView lstViewGroup;
- private Button RegisterNewGroup;
- private OnFragmentInteractionListener mListener;
- /**
- * Use this factory method to create a new instance of
- * this fragment using the provided parameters.
- *
- * @param param1 Parameter 1.
- * @param param2 Parameter 2.
- * @return A new instance of fragment GroupFragment.
- */
- // TODO: Rename and change types and number of parameters
- public static GroupFragment newInstance(String param1, String param2) {
- GroupFragment fragment = new GroupFragment();
- Bundle args = new Bundle();
- args.putString(ARG_PARAM1, param1);
- args.putString(ARG_PARAM2, param2);
- fragment.setArguments(args);
- return fragment;
- }
- public GroupFragment() {
- // Required empty public constructor
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getArguments() != null) {
- mParam1 = getArguments().getString(ARG_PARAM1);
- mParam2 = getArguments().getString(ARG_PARAM2);
- }
- groupKeyValues.clear();
- ReadGroupData();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_group, container, false);
- RegisterNewGroup = (Button)view.findViewById(R.id.btn_reg_new_group);
- RegisterNewGroup.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- EditText editRegNewGr = (EditText) getView().findViewById(R.id.txt_group_name);
- String groupName = editRegNewGr.getText().toString();
- if (groupName != "") {
- CreateNewGroup(groupName);
- }
- }
- });
- ReadGroupData();
- return view;
- }
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- // TODO, Skapa ett testmeddelande!
- public void CreateNewGroup(String groupName) {
- Map<String, Group> newGroup = new HashMap<>();
- Firebase firebaserootRef = new Firebase("https://luminous-heat-420.firebaseio.com");
- Firebase firebaseGroup = firebaserootRef.child("").push();
- Group group = new Group();
- group.SetID(firebaseGroup.getKey());
- group.SetName(groupName);
- String id = firebaseGroup.getKey();
- firebaseGroup.child("id").setValue(group.GetId());
- firebaseGroup.child("name").setValue(group.GetName());
- newGroup.put(id, group);
- ChangeToChatFragment(group.GetId());
- }
- public void ReadGroupData() {
- Firebase firebaserootRef = new Firebase("https://luminous-heat-000.firebaseio.com");
- firebaserootRef.addChildEventListener(new ChildEventListener() {
- @Override
- public void onChildAdded(DataSnapshot snapshot, String s) {
- if (snapshot.getValue() != null) {
- Group newGroup = new Group((String) snapshot.child("name").getValue(), (String) snapshot.child("id").getValue());
- if(!groupKeyValues.contains(newGroup.GetId())) {
- groupKeyValues.add(newGroup.GetId());
- AddToLstViewGroup(newGroup);
- System.out.println("Read group data from firebase and inserted in listView");
- }
- }
- }
- @Override
- public void onChildChanged(DataSnapshot snapshot, String s) {
- }
- @Override
- public void onChildRemoved(DataSnapshot snapshot) {
- }
- @Override
- public void onChildMoved(DataSnapshot snapshot, String s) {
- }
- @Override
- public void onCancelled(FirebaseError firebaseError) {
- }
- });
- }
- public void AddToLstViewGroup(Group newGroup) {
- groupNameList.add(newGroup);
- if(groupAdapter == null) {
- groupAdapter = new GroupAdapter(getActivity(), groupNameList);
- }
- groupAdapter.notifyDataSetChanged();
- if (lstViewGroup == null) {
- lstViewGroup = (ListView) getView().findViewById(R.id.listView_group);
- }
- lstViewGroup.setOnItemClickListener(onItemClickListener);
- lstViewGroup.setOnItemLongClickListener(onItemLongClickListener);
- groupAdapter.notifyDataSetChanged();
- lstViewGroup.setAdapter(groupAdapter);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mListener = (OnFragmentInteractionListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnFragmentInteractionListener");
- }
- }
- @Override
- public void onDetach() {
- super.onDetach();
- mListener = null;
- }
- public void ChangeToChatFragment(String groupId) {
- ChatFragment groupFragment = ChatFragment.newInstance("", "");
- groupFragment.SetGroupID(groupId);
- FragmentManager fM = getFragmentManager();
- FragmentTransaction fT = fM.beginTransaction();
- fT.replace(R.id.container_chat, groupFragment, null);
- fT.addToBackStack("go to chat fragmement");
- fT.commit();
- }
- private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Log.d("Short clicked on group item in listView!", "GOTO Chatfragment");
- Group group = groupAdapter.getItem(position);
- ChangeToChatFragment(group.GetId());
- }
- };
- private AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
- Log.d("Long clicked on group item in listView!", "GOTO Chatfragment");
- Group group = groupAdapter.getItem(position);
- ChangeToChatFragment(group.GetId());
- return true;
- }
- };
- public interface OnFragmentInteractionListener {
- // TODO: Update argument type and name
- public void onFragmentInteraction(Uri uri);
- }
- }
- public class ChatFragment extends Fragment implements
- View.OnClickListener {
- // TODO: Rename parameter arguments, choose names that match
- // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
- private static final String ARG_PARAM1 = "param1";
- private static final String ARG_PARAM2 = "param2";
- // TODO: Rename and change types of parameters
- private String mParam1;
- private String mParam2;
- private OnFragmentInteractionListener mListener;
- static ArrayList<Message> chatMsgList = new ArrayList<>();
- static ArrayList<String> msgKeyValues = new ArrayList<>();
- ChatAdapter chatAdapter;
- ListView lstViewChat = null;
- private String groupId;
- Firebase firebaserootRef = new Firebase("https://luminous-heat-000.firebaseio.com");
- static String username = "Me";
- EditText editMsg;
- Button btnSend;
- /**
- * Use this factory method to create a new instance of
- * this fragment using the provided parameters.
- *
- * @param param1 Parameter 1.
- * @param param2 Parameter 2.
- * @return A new instance of fragment ChatFragment.
- */
- // TODO: Rename and change types and number of parameters
- public static ChatFragment newInstance(String param1, String param2) {
- ChatFragment fragment = new ChatFragment();
- Bundle args = new Bundle();
- args.putString(ARG_PARAM1, param1);
- args.putString(ARG_PARAM2, param2);
- fragment.setArguments(args);
- return fragment;
- }
- public ChatFragment() {
- // Required empty public constructor
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getArguments() != null) {
- mParam1 = getArguments().getString(ARG_PARAM1);
- mParam2 = getArguments().getString(ARG_PARAM2);
- }
- msgKeyValues.clear();
- ReadChatMessages(firebaserootRef);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_chat, container, false);
- editMsg = (EditText) view.findViewById(R.id.txt_message_input);
- btnSend = (Button) view.findViewById(R.id.btnSend);
- btnSend.setOnClickListener(this);
- return view;
- }
- @Override
- public void onClick(View v) {
- String txtMsg = editMsg.getText().toString();
- if (txtMsg != "") {
- System.out.println("GOTO CreateNewMessage!");
- CreateNewMessage(firebaserootRef, txtMsg);
- }
- }
- public void CreateNewMessage(Firebase firebaserootRef, String textMsg) {
- Map<String, Message> chatMessages = new HashMap<>();
- String msg = textMsg;
- String from = username;
- String time = TimeStamp();
- String id = "";
- //Message
- if (groupId != "") {
- Firebase firebaseParentMsg = firebaserootRef.child(GetGroupId()).child("messages");
- Firebase firebaseMsg = firebaseParentMsg.push();
- Message cm = new Message(id, from, msg, time);
- id = firebaseMsg.getKey();
- firebaseMsg.child("from").setValue(cm.GetFrom());
- firebaseMsg.child("message").setValue(cm.GetMsg());
- firebaseMsg.child("time").setValue(cm.GetTime());
- chatMessages.put(id,cm);
- System.out.println("Succesfully created a new message.");
- }
- ReadChatMessages(firebaserootRef);
- }
- public void ReadChatMessages(Firebase firebaseRootRef) {
- firebaseRootRef.addChildEventListener(new ChildEventListener() {
- @Override
- public void onChildAdded(DataSnapshot snapshot, String s) {
- if (snapshot.child(GetGroupId()).child("messages").getChildren() != null) {
- for (DataSnapshot c : snapshot.child(GetGroupId()).child("messages").getChildren()) {
- String key = c.getKey();
- Message newMessage = new Message();
- newMessage.SetFrom((String) c.child("from").getValue());
- newMessage.SetMsg((String) c.child("message").getValue());
- newMessage.SetTime((String) c.child("time").getValue());
- newMessage.SetId((String) c.child("id").getValue());
- if ((!msgKeyValues.contains(key)) || newMessage.GetFrom() != "") {
- msgKeyValues.add(key);
- AddToLstViewChat(newMessage);
- //Automatic scrolls to last line in listView.
- lstViewChat.setSelection(chatAdapter.getCount() -1);
- }
- }
- }
- }
- @Override
- public void onChildChanged(DataSnapshot snapshot, String s) {
- }
- @Override
- public void onChildRemoved(DataSnapshot snapshot) {
- }
- @Override
- public void onChildMoved(DataSnapshot snapshot, String s) {
- }
- @Override
- public void onCancelled(FirebaseError firebaseError) {
- }
- });
- }
- public boolean IsMsgFromMe(Message message) {
- boolean isSenderMe = username.equals((CharSequence) message.GetFrom());
- return isSenderMe;
- }
- public void AddToLstViewChat(Message newMessage) {
- chatMsgList.add(newMessage);
- if (chatAdapter == null) {
- chatAdapter = new ChatAdapter(getActivity(), chatMsgList);
- }
- if(IsMsgFromMe(newMessage)) {
- lstViewChat = (ListView) getView().findViewById(R.id.listView_chat_message_me);
- } else {
- lstViewChat = (ListView)getView().findViewById(R.id.listView_chat_message_others);
- }
- chatAdapter.notifyDataSetChanged();
- lstViewChat.setAdapter(chatAdapter);
- }
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mListener = (OnFragmentInteractionListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnFragmentInteractionListener");
- }
- }
- @Override
- public void onDetach() {
- super.onDetach();
- mListener = null;
- }
- public String GetGroupId() {
- System.out.println(groupId);
- return groupId;
- }
- public void SetGroupID(String groupId) {
- this.groupId = groupId;
- }
- public interface OnFragmentInteractionListener {
- // TODO: Update argument type and name
- public void onFragmentInteraction(Uri uri);
- }
- public String TimeStamp() {
- String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
- return timeStamp;
- }
- }
- public class ChatActivity extends ActionBarActivity implements
- GroupFragment.OnFragmentInteractionListener,
- ChatFragment.OnFragmentInteractionListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_chat);
- Firebase.setAndroidContext(this);
- GroupFragment groupFragment = GroupFragment.newInstance("", "");
- FragmentManager fM = getFragmentManager();
- FragmentTransaction fT = fM.beginTransaction();
- fT.replace(R.id.container_chat, groupFragment, null);
- fT.addToBackStack("go to group fragmement");
- fT.commit();
- }
- @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_chat, 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_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onFragmentInteraction(Uri uri) {
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
- public PlaceholderFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.activity_chat, container, false);
- return rootView;
- }
- }
- @Override
- public void onBackPressed() {
- if (getFragmentManager().getBackStackEntryCount() > 0) {
- getFragmentManager().popBackStack();
- } else {
- finish();
- }
- }
- }
- public class Message {
- String from = "";
- String message = "";
- String time = "";
- String id = "";
- public Message() {}
- public Message(String id, String from, String message, String time) {
- this.from = from;
- this.time = time;
- this.message = message;
- this.id = id;
- }
- public String GetMsg() {
- return this.message;
- }
- public String SetMsg(String message) {
- return this.message = message;
- }
- public String GetFrom() {
- return this.from;
- }
- public String SetFrom(String from) {
- return this.from = from;
- }
- public String GetTime() {
- return this.time;
- }
- public String SetTime(String time) {
- return this.time = time;
- }
- public String GetId() {
- return id;
- }
- public String SetId(String id) {
- return this.id = id;
- }
- }
- public class Group {
- String name = "";
- String id = "";
- public Group() {}
- public Group(String name, String id) {
- this.name = name;
- this.id = id;
- }
- public String GetName() {
- return this.name;
- }
- public String SetName(String name) {
- return this.name = name;
- }
- public String GetId() {
- return this.id;
- }
- public String SetID(String id) {
- return this.id = id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement