-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupmessagemanager.cpp
129 lines (114 loc) · 4.33 KB
/
groupmessagemanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// GroupMessageManager.cpp
#include "GroupMessageManager.h"
#include <QDebug>
GroupMessageManager::GroupMessageManager(QObject *parent)
: QObject(parent)
{}
void GroupMessageManager::addMessage(int groupId,
int ms_id,
const QString &sender,
const QString &message,
const QString &time,
const QString &messageType,
int userId,
const QString &createdAt)
{
if (!m_groupMessages.contains(groupId)) {
m_groupMessages.insert(groupId, new GroupData(this));
}
Message *newMessage = new Message(this);
newMessage->setMsId(ms_id);
newMessage->setSender(sender);
newMessage->setMessage(message);
newMessage->setTime(time);
newMessage->setImage("https://placehold.co/50x50");
newMessage->setMessageType(messageType);
newMessage->setUserId(userId);
newMessage->setCreatedAt(createdAt);
m_groupMessages[groupId]->messages().append(newMessage);
// qDebug() << "void GroupMessageManager::addMessage: groupId: " << groupId;
// qDebug() << "void GroupMessageManager::addMessage: group messages: "
// << m_groupMessages[groupId]->messages().size();
emit groupMessagesChanged();
}
void GroupMessageManager::pushFrontMessage(int groupId,
int ms_id,
const QString &sender,
const QString &message,
const QString &time,
const QString &messageType,
int userId,
const QString &createdAt)
{
if (!m_groupMessages.contains(groupId)) {
m_groupMessages.insert(groupId, new GroupData(this));
}
Message *newMessage = new Message(this);
newMessage->setMsId(ms_id);
newMessage->setSender(sender);
newMessage->setMessage(message);
newMessage->setTime(time);
newMessage->setImage("https://placehold.co/50x50");
newMessage->setMessageType(messageType);
newMessage->setUserId(userId);
newMessage->setCreatedAt(createdAt);
m_groupMessages[groupId]->messages().push_front(newMessage);
qDebug() << "void GroupMessageManager::addMessage: groupId: " << groupId;
qDebug() << "void GroupMessageManager::addMessage: group messages: "
<< m_groupMessages[groupId]->messages().size();
emit groupMessagesChanged();
}
QList<Message *> GroupMessageManager::getMessages(int groupId) const
{
if (m_groupMessages.contains(groupId)) {
return m_groupMessages[groupId]->messages();
}
return QList<Message *>();
}
void GroupMessageManager::removeMessage(int groupId, int ms_id)
{
if (m_groupMessages.contains(groupId)) {
QList<Message *> messages = m_groupMessages[groupId]->messages();
for (int i = 0; i < messages.size(); ++i) {
if (messages[i]->ms_id() == ms_id) {
m_groupMessages[groupId]->messages().removeAt(i);
break;
}
}
emit groupMessagesChanged();
}
}
void GroupMessageManager::setCurrentPage(int groupId, int page)
{
if (m_groupMessages.contains(groupId)) {
m_groupMessages[groupId]->setCurrentPage(page);
}
}
int GroupMessageManager::getCurrentPage(int groupId) const
{
if (m_groupMessages.contains(groupId)) {
return m_groupMessages[groupId]->currentPage();
}
return 1; // Or another value to indicate non-existent group
}
void GroupMessageManager::setCurrentScrollIndex(int groupId, int currentIndex)
{
if (m_groupMessages.contains(groupId)) {
m_groupMessages[groupId]->setCurrentScrollIndex(currentIndex);
}
}
int GroupMessageManager::getCurrentScrollIndex(int groupId) const
{
if (m_groupMessages.contains(groupId)) {
return m_groupMessages[groupId]->currentScrollIndex();
}
return 1; // Or another value to indicate non-existent group
}
int GroupMessageManager::getSize(int groupId) const
{
return m_groupMessages[groupId]->messages().size();
}
QHash<int, GroupData *> GroupMessageManager::groupMessages() const
{
return m_groupMessages;
}