Skip to content
Snippets Groups Projects
Commit 05e3e041 authored by Ilham Firdausi Putra's avatar Ilham Firdausi Putra
Browse files

finish bot chat room section

parent 22282f2c
No related merge requests found
...@@ -3,7 +3,11 @@ package com.chatman; ...@@ -3,7 +3,11 @@ package com.chatman;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -12,12 +16,19 @@ import android.widget.EditText; ...@@ -12,12 +16,19 @@ import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.Toast; import android.widget.Toast;
import com.chatman.adapter.ChatAdapter;
import com.chatman.adapter.ChatListAdapter;
import com.chatman.helper.FirebaseHelper; import com.chatman.helper.FirebaseHelper;
import com.chatman.helper.PreferencesHelper; import com.chatman.helper.PreferencesHelper;
import com.chatman.model.Chat; import com.chatman.model.Chat;
import com.chatman.model.User; import com.chatman.model.User;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
/** /**
...@@ -34,6 +45,9 @@ public class BotFragment extends Fragment { ...@@ -34,6 +45,9 @@ public class BotFragment extends Fragment {
private ImageButton sentButton; private ImageButton sentButton;
private EditText message; private EditText message;
private Context context; private Context context;
private RecyclerView recycler;
private ChatAdapter adapter;
private List<Chat> chatList;
private static final String BOT_KEY = "BOT_KEY"; private static final String BOT_KEY = "BOT_KEY";
public BotFragment() { public BotFragment() {
...@@ -69,15 +83,56 @@ public class BotFragment extends Fragment { ...@@ -69,15 +83,56 @@ public class BotFragment extends Fragment {
Chat sendMessage = new Chat(PreferencesHelper.getUserFirebaseKey(context), PreferencesHelper.getUserName(context), BOT_KEY, Calendar.getInstance().getTime(), message.getText().toString()); Chat sendMessage = new Chat(PreferencesHelper.getUserFirebaseKey(context), PreferencesHelper.getUserName(context), BOT_KEY, Calendar.getInstance().getTime(), message.getText().toString());
String key = FirebaseHelper.dbMessage.push().getKey(); String key = FirebaseHelper.dbMessage.push().getKey();
FirebaseHelper.dbMessage.child(key).setValue(sendMessage); FirebaseHelper.dbMessage.child(key).setValue(sendMessage);
// BALASAN DARI BOT
Chat botMessage = new Chat(BOT_KEY, "ChatMan Bot", PreferencesHelper.getUserFirebaseKey(context), Calendar.getInstance().getTime(), "Halo! Selamat datang di ChatMan");
String botKey = FirebaseHelper.dbMessage.push().getKey();
FirebaseHelper.dbMessage.child(botKey).setValue(botMessage);
} }
} }
}); });
// Inflate the layout for this fragment // Inflate the layout for this fragment
return view; return view;
} }
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// Recycler View
recycler = view.findViewById(R.id.chat_bot_room_rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setReverseLayout(false);
linearLayoutManager.setStackFromEnd(false);
recycler.setLayoutManager(linearLayoutManager);
adapter = new ChatAdapter(chatList);
recycler.setAdapter(adapter);
FirebaseHelper.dbMessage.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
getAllChat(dataSnapshot);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void getAllChat(DataSnapshot dataSnapshot) {
chatList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if (ds.getValue(Chat.class).getIdSender().equals(PreferencesHelper.getUserFirebaseKey(context)) || ds.getValue(Chat.class).getIdReceiver().equals(PreferencesHelper.getUserFirebaseKey(context))) {
chatList.add(ds.getValue(Chat.class));
}
}
adapter = new ChatAdapter(chatList);
recycler.setAdapter(adapter);
}
public void onButtonPressed(Uri uri) { public void onButtonPressed(Uri uri) {
if (mListener != null) { if (mListener != null) {
......
package com.chatman.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.chatman.R;
import com.chatman.helper.PreferencesHelper;
import com.chatman.model.Chat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class ChatAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private List<Chat> items = new ArrayList<>();
private int itemLayoutSent = R.layout.item_message_sent;
private int itemLayoutReceived = R.layout.item_message_received;
private Context context;
private String currentUser;
public ChatAdapter(List<Chat> items) {
this.items = items;
}
// @Override
// public SentMessageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// return new SentMessageHolder(LayoutInflater.from(parent.getContext()).inflate(itemLayoutSent, parent, false));
// }
// @Override
// public void onBindViewHolder(SentMessageHolder holder, int position) {
// holder.bind(items.get(position));
// }
public void addItems(List<Chat> items) {
this.items.addAll(items);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (items == null) {
return 0;
}
return items.size();
}
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
Chat message = (Chat) items.get(position);
if (message.getIdSender().equals(currentUser)) {
// If the current user is the sender of the message
return VIEW_TYPE_MESSAGE_SENT;
} else {
// If some other user sent the message
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
// Inflates the appropriate layout according to the ViewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
currentUser = PreferencesHelper.getUserFirebaseKey(context);
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_received, parent, false);
return new ReceivedMessageHolder(view);
}
return null;
}
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Chat message = (Chat) items.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
}
}
private class SentMessageHolder extends RecyclerView.ViewHolder {
private TextView messageText;
private TextView timeText;
SentMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.sent_text_message_body);
timeText = (TextView) itemView.findViewById(R.id.sent_text_message_time);
}
void bind(Chat message) {
messageText.setText(message.getMessage());
// Format the stored timestamp into a readable String using method.
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
String strDate = dateFormat.format(message.getDate());
timeText.setText(strDate);
}
}
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
private TextView messageText, timeText, nameText;
private ImageView profileImage;
ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.received_text_message_body);
timeText = (TextView) itemView.findViewById(R.id.received_text_message_time);
nameText = (TextView) itemView.findViewById(R.id.received_text_message_name);
profileImage = (ImageView) itemView.findViewById(R.id.received_image_message_profile);
}
void bind(Chat message) {
messageText.setText(message.getMessage());
// Format the stored timestamp into a readable String using method.
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
String strDate = dateFormat.format(message.getDate());
timeText.setText(strDate);
nameText.setText(message.getNameSender());
// Insert the profile image from the URL into the ImageView.
Glide.with(itemView).load(R.drawable.robotic).into(profileImage);
}
}
// public int getImage(String imageName) {
//
// int drawableResourceId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
//
// return drawableResourceId;
// }
}
\ No newline at end of file
...@@ -9,6 +9,9 @@ public class Chat { ...@@ -9,6 +9,9 @@ public class Chat {
private Date date; private Date date;
private String message; private String message;
public Chat() {
}
public Chat(String idSender, String nameSender, String idReceiver, Date date, String message) { public Chat(String idSender, String nameSender, String idReceiver, Date date, String message) {
this.idSender = idSender; this.idSender = idSender;
this.nameSender = nameSender; this.nameSender = nameSender;
......
...@@ -6,6 +6,8 @@ public class ChatList { ...@@ -6,6 +6,8 @@ public class ChatList {
private String lastMessage; private String lastMessage;
// Todo : tambahin atribut KEY atau apalah supaya ntar waktu ditekan, ke fragmentnya bisa dipass KEY itu terus data di dalam fragmentnya diambil berdasarkan KEY tersebut // Todo : tambahin atribut KEY atau apalah supaya ntar waktu ditekan, ke fragmentnya bisa dipass KEY itu terus data di dalam fragmentnya diambil berdasarkan KEY tersebut
public ChatList() {
}
public ChatList(int avatarResource, String name, String lastMessage) { public ChatList(int avatarResource, String name, String lastMessage) {
this.avatarResource = avatarResource; this.avatarResource = avatarResource;
......
...@@ -6,6 +6,9 @@ public class ChatRoom { ...@@ -6,6 +6,9 @@ public class ChatRoom {
private String idSender; private String idSender;
private List<Chat> messages; private List<Chat> messages;
public ChatRoom() {
}
public ChatRoom(String idSender, List<Chat> messages) { public ChatRoom(String idSender, List<Chat> messages) {
this.idSender = idSender; this.idSender = idSender;
this.messages = messages; this.messages = messages;
......
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