Apply a dumb filter on contacts

This commit is contained in:
Sebastian Wolf 2020-11-24 17:28:41 +01:00
parent 86599b2859
commit 4713fbfba6
14 changed files with 300 additions and 192 deletions

View file

@ -36,6 +36,11 @@ Page {
}
}
function resetFocus() {
contactsSearchField.focus = false;
newChatPage.focus = true;
}
SilicaFlickable {
id: newChatContainer
contentHeight: newChatPage.height
@ -57,11 +62,28 @@ Page {
width: newChatPageColumn.width
height: newChatPageColumn.height - newChatPageHeader.height
Column {
visible: !newChatPage.isLoading
width: parent.width
height: parent.height
SearchField {
id: contactsSearchField
width: parent.width
placeholderText: qsTr("Search a contact")
active: !newChatPage.isLoading
onTextChanged: contactsModel.applyFilter(text);
EnterKey.iconSource: "image://theme/icon-m-enter-close"
EnterKey.onClicked: {
resetFocus();
}
}
SilicaListView {
id: contactsListView
clip: true
width: parent.width
height: parent.height
height: parent.height - contactsSearchField.height
visible: !newChatPage.isLoading
opacity: visible ? 1 : 0
Behavior on opacity { FadeAnimation {} }
@ -103,6 +125,7 @@ Page {
Connections {
target: contactsListView
onNewChatInitiated: {
if (index === currentIndex) {
contactListItem.visible = false;
@ -111,6 +134,15 @@ Page {
}
}
}
Connections {
target: contactsSearchField
onFocusChanged: {
if (contactsSearchField.focus) {
contactListItem.visible = true;
}
}
}
}
Column {
@ -296,6 +328,8 @@ Page {
VerticalScrollDecorator {}
}
}
Column {
opacity: visible ? 1 : 0

View file

@ -44,13 +44,13 @@ ContactsModel::ContactsModel(TDLibWrapper *tdLibWrapper, QObject *parent)
int ContactsModel::rowCount(const QModelIndex &) const
{
return this->contacts.size();
return this->filter.isEmpty() ? this->contacts.size() : this->filteredContacts.size();
}
QVariant ContactsModel::data(const QModelIndex &index, int role) const
{
if (index.isValid() && role == Qt::DisplayRole) {
return QVariant(contacts.value(index.row()));
return this->filter.isEmpty() ? QVariant(contacts.value(index.row())) : QVariant(filteredContacts.value(index.row())) ;
}
return QVariant();
}
@ -116,3 +116,30 @@ void ContactsModel::hydrateContacts()
LOG("Hydrated contacts:" << this->contacts.size());
std::sort(this->contacts.begin(), this->contacts.end(), compareUsers);
}
void ContactsModel::applyFilter(const QString &filter)
{
LOG("Applying filter:" << filter);
beginResetModel();
this->filter = filter;
this->filteredContacts.clear();
if (!this->filter.isEmpty()) {
QListIterator<QVariant> contactIterator(this->contacts);
while (contactIterator.hasNext()) {
QVariantMap contact = contactIterator.next().toMap();
if (contact.value(LAST_NAME).toString().contains(this->filter, Qt::CaseInsensitive)) {
this->filteredContacts.append(contact);
continue;
}
if (contact.value(FIRST_NAME).toString().contains(this->filter, Qt::CaseInsensitive)) {
this->filteredContacts.append(contact);
continue;
}
if (contact.value(USERNAME).toString().contains(this->filter, Qt::CaseInsensitive)) {
this->filteredContacts.append(contact);
continue;
}
}
}
endResetModel();
}

View file

@ -35,6 +35,7 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE void hydrateContacts();
Q_INVOKABLE void applyFilter(const QString &filter);
public slots:
void handleUsersReceived(const QString &extra, const QVariantList &userIds, int totalUsers);
@ -42,7 +43,9 @@ public slots:
private:
TDLibWrapper *tdLibWrapper;
QVariantList contacts;
QVariantList filteredContacts;
QList<QString> contactIds;
QString filter;
};
#endif // CONTACTSMODEL_H

View file

@ -920,6 +920,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -920,6 +920,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -910,6 +910,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -921,6 +921,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -910,6 +910,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -920,6 +920,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -930,6 +930,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -930,6 +930,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -920,6 +920,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -910,6 +910,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>

View file

@ -920,6 +920,10 @@
<source>End-to-end-encrypted, accessible on this device only</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Search a contact</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationManager</name>