parent
a319f90bad
commit
99460aefeb
@ -1,17 +1,121 @@
|
|||||||
package com.example.leudaemialikeme.Activity;
|
package com.example.leudaemialikeme.Activity;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.inputmethod.EditorInfo;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.ImageButton;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.example.leudaemialikeme.R;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
public class ChatActivity extends BaseActivity {
|
|
||||||
|
|
||||||
|
import com.example.leudaemialikeme.Adapter.ChatAdapter;
|
||||||
|
import com.example.leudaemialikeme.Dao.FriendDao;
|
||||||
|
import com.example.leudaemialikeme.Dao.MessageDao;
|
||||||
|
import com.example.leudaemialikeme.Model.Friend;
|
||||||
|
import com.example.leudaemialikeme.Model.Message;
|
||||||
|
import com.example.leudaemialikeme.Model.MessageItemView;
|
||||||
|
import com.example.leudaemialikeme.R;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class ChatActivity extends BaseActivity {
|
||||||
|
private MessageDao messageDao = new MessageDao();
|
||||||
|
private FriendDao friendDao = new FriendDao();
|
||||||
|
private ImageButton bt_chat_to_chat_list;
|
||||||
|
private RecyclerView msg_recycler_view;
|
||||||
|
private TextView text_chat_name;
|
||||||
|
private EditText edit_content;
|
||||||
|
private Button bt_send_msg;
|
||||||
|
public int friendNetId;
|
||||||
|
private Friend currentFriend;
|
||||||
|
private ArrayList<Message> messageList = new ArrayList<Message>();
|
||||||
|
private MessageItemView msgItemView;
|
||||||
|
private ChatAdapter chatAdapter;
|
||||||
|
private Gson gson = new Gson();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_chat);
|
setContentView(R.layout.activity_chat);
|
||||||
|
bt_chat_to_chat_list = findViewById(R.id.bt_chat_to_chat_list);
|
||||||
|
msg_recycler_view = findViewById(R.id.msg_recycler_view);
|
||||||
|
text_chat_name = findViewById(R.id.text_chat_name);
|
||||||
|
edit_content = findViewById(R.id.edit_content);
|
||||||
|
bt_send_msg = findViewById(R.id.bt_send_msg);
|
||||||
|
|
||||||
|
initData();
|
||||||
|
text_chat_name.setText(currentFriend.getUsername());
|
||||||
|
|
||||||
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
||||||
|
msg_recycler_view.setLayoutManager(layoutManager);
|
||||||
|
chatAdapter = new ChatAdapter(msgItemView);
|
||||||
|
msg_recycler_view.setAdapter(chatAdapter);
|
||||||
|
msg_recycler_view.scrollToPosition(messageList.size() - 1);
|
||||||
|
edit_content.setImeOptions(EditorInfo.IME_ACTION_SEND);
|
||||||
|
edit_content.setInputType(EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
|
||||||
|
edit_content.setMaxLines(2);
|
||||||
|
edit_content.setSingleLine(false);
|
||||||
|
edit_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_SEND
|
||||||
|
|| actionId == EditorInfo.IME_ACTION_DONE
|
||||||
|
|| (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
|
||||||
|
String content=edit_content.getText().toString();
|
||||||
|
if (!content.equals("")){
|
||||||
|
Message message =new Message(owner.getNetId(),currentFriend.getNetId(),content);
|
||||||
|
addMsg(message,1);
|
||||||
|
edit_content.setText("");
|
||||||
|
wsClient.send(gson.toJson(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
bt_send_msg.setOnClickListener(new View.OnClickListener(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
String content=edit_content.getText().toString();
|
||||||
|
if (!content.equals("")){
|
||||||
|
Message message =new Message(owner.getNetId(),currentFriend.getNetId(),content);
|
||||||
|
addMsg(message,1);
|
||||||
|
edit_content.setText("");
|
||||||
|
wsClient.send(gson.toJson(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
bt_chat_to_chat_list.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initData() {
|
||||||
|
//从intent中取出currentNetId并查出对象
|
||||||
|
Intent intent = getIntent();
|
||||||
|
friendNetId = intent.getIntExtra("friendNetId", -1);
|
||||||
|
currentFriend = friendDao.findByNetId(friendNetId, owner.getNetId());
|
||||||
|
//从数据库中查找聊天记录-默认最近20条记录
|
||||||
|
messageList = messageDao.findNearlyMessage(friendNetId, owner.getNetId());
|
||||||
|
//组装成MessageItemViw
|
||||||
|
msgItemView = new MessageItemView(currentFriend, owner, messageList);
|
||||||
|
}
|
||||||
|
|
||||||
|
//消息添加方法
|
||||||
|
public void addMsg(Message message, int isRead) {
|
||||||
|
msgItemView.messages.add(message);
|
||||||
|
chatAdapter.notifyItemInserted(messageList.size() - 1);
|
||||||
|
msg_recycler_view.scrollToPosition(messageList.size() - 1);
|
||||||
|
message.setFlag(isRead);
|
||||||
|
message.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.example.leudaemialikeme.Dao;
|
||||||
|
|
||||||
|
import com.example.leudaemialikeme.Model.Friend;
|
||||||
|
|
||||||
|
import org.litepal.LitePal;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class FriendDao extends BaseDao{
|
||||||
|
|
||||||
|
public ArrayList<Friend> findFriendListByOwnerNetId(int ownerNetId){
|
||||||
|
ArrayList<Friend> friendList=(ArrayList<Friend>)LitePal.where("ownerNetId = ?", String.valueOf(ownerNetId)).find(Friend.class);
|
||||||
|
return friendList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Friend> findChatFriendList(int ownerNetId){
|
||||||
|
ArrayList<Friend> friendList=(ArrayList<Friend>)LitePal.where("ownerNetId = ? AND isChat = ?", String.valueOf(ownerNetId),"1").find(Friend.class);
|
||||||
|
return friendList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Friend findByNetId(int netId,int ownerNetId){
|
||||||
|
ArrayList<Friend> friendList=(ArrayList<Friend>)LitePal.where("netId = ? and ownerNetId = ?", String.valueOf(netId),String.valueOf(ownerNetId)).find(Friend.class);
|
||||||
|
if (friendList.size()==0){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return friendList.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeChatStatus(Friend friend, int netId, int ownerNetId){
|
||||||
|
friend.updateAll("netId = ? and ownerNetId = ?",String.valueOf(netId),String.valueOf(ownerNetId));
|
||||||
|
}
|
||||||
|
|
||||||
|
// public ArrayList<Friend> findByKeyword(String keyword,int ownerNetId){
|
||||||
|
// String keywordCop="%"+keyword+"%";
|
||||||
|
// ArrayList<Friend> friends=(ArrayList<Friend>) LitePal.where("name like ? AND isfriend=1 AND ownerNetId=?",keywordCop,String.valueOf(ownerNetId)).find(Friend.class);
|
||||||
|
// return friends;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public ArrayList<Friend> findNewFriendList(int ownerNetId){
|
||||||
|
// ArrayList<Friend> friendList=(ArrayList<Friend>)LitePal.where("ownerNetId = ? AND isfriend=0", String.valueOf(ownerNetId)).find(Friend.class);
|
||||||
|
// return friendList;
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.example.leudaemialikeme.Model;
|
||||||
|
|
||||||
|
import org.litepal.crud.LitePalSupport;
|
||||||
|
|
||||||
|
public class Friend extends LitePalSupport {
|
||||||
|
private int id;
|
||||||
|
private String username;
|
||||||
|
private int netId;
|
||||||
|
private int ownerNetId;
|
||||||
|
private Boolean isChat;
|
||||||
|
|
||||||
|
public Friend(){
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
public Friend(int netId, String username){
|
||||||
|
this.netId = netId;
|
||||||
|
this.username = username;
|
||||||
|
this.isChat=false;
|
||||||
|
}
|
||||||
|
public Friend(int netId, String username, int ownerNetId){
|
||||||
|
this.netId = netId; //聊天人的ID
|
||||||
|
this.username = username; //聊天人的用户名
|
||||||
|
this.ownerNetId = ownerNetId; //本机用户的ID
|
||||||
|
this.isChat=false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNetId() {
|
||||||
|
return netId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNetId(int netId) {
|
||||||
|
this.netId = netId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOwnerNetId() {
|
||||||
|
return ownerNetId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwnerNetId(int ownerNetId) {
|
||||||
|
this.ownerNetId = ownerNetId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getChat() {
|
||||||
|
return isChat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChat(Boolean chat) {
|
||||||
|
isChat = chat;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.leudaemialikeme.Model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class MessageItemView {
|
||||||
|
public Friend friend;
|
||||||
|
public Owner owner;
|
||||||
|
public ArrayList<Message> messages;
|
||||||
|
|
||||||
|
public MessageItemView(Friend friend,Owner owner,ArrayList<Message> messages){
|
||||||
|
this.friend=friend;
|
||||||
|
this.owner=owner;
|
||||||
|
this.messages=messages;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.example.leudaemialikeme.Utils;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class TimeUtil {
|
||||||
|
public static String pattern="yyyy-MM-dd HH:mm";
|
||||||
|
|
||||||
|
public static String timeToString(Timestamp timestamp){
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
||||||
|
return format.format(timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Timestamp stringToTime(String string){
|
||||||
|
return Timestamp.valueOf(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date stringToDate(String string) throws ParseException {
|
||||||
|
SimpleDateFormat format=new SimpleDateFormat(pattern);
|
||||||
|
return format.parse(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String dateToString(Date date){
|
||||||
|
SimpleDateFormat format=new SimpleDateFormat(pattern);
|
||||||
|
return format.format(date);
|
||||||
|
}
|
||||||
|
}
|
After Width: | Height: | Size: 231 B |
Loading…
Reference in new issue