Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
qtinsider committed Jan 3, 2022
1 parent 32be20c commit fd92236
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 207 deletions.
9 changes: 6 additions & 3 deletions resources/qml/ArchivedChatPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ Page {

BusyIndicator {
anchors.centerIn: listView
running: populateTimer.running
visible: populateTimer.running
running: populateTimer.running || myChatModel.loading
visible: running
platformStyle: BusyIndicatorStyle { size: "large" }
}

ChatModel {
id: myChatModel
chatList: TdApi.ChatListArchive

Component.onCompleted: populateTimer.restart()
onLoadingChanged: {
if (!loading)
populateTimer.restart()
}
}

ContextMenu {
Expand Down
41 changes: 32 additions & 9 deletions resources/qml/AuthenticationPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Sheet {
property alias __nextTypeString: nextTypeLabel.text
property int __length

property bool loading: false

rejectButtonText: "Cancel"

state: "Phone"
Expand Down Expand Up @@ -102,7 +104,7 @@ Sheet {

Button {
anchors.horizontalCenter: parent.horizontalCenter
enabled: !tdapi.busy
enabled: !sheet.loading
text: qsTr("Next")
width: parent.width / 2

Expand All @@ -111,11 +113,13 @@ Sheet {
onClicked: {
if (phoneNumber.text.length > 0) {
tdapi.setPhoneNumber(countryCodeButton.text + phoneNumber.text)

sheet.loading = true;
}
}

BusyIndicator {
visible: tdapi.busy
visible: sheet.loading
running: visible
anchors.centerIn: parent
}
Expand Down Expand Up @@ -160,7 +164,7 @@ Sheet {

Button {
anchors.horizontalCenter: parent.horizontalCenter
enabled: !tdapi.busy
enabled: !sheet.loading
text: qsTr("Next")
width: parent.width / 2

Expand All @@ -171,7 +175,7 @@ Sheet {
}

BusyIndicator {
visible: tdapi.busy
visible: sheet.loading
running: visible
anchors.centerIn: parent
}
Expand Down Expand Up @@ -278,7 +282,7 @@ Sheet {

Button {
anchors.horizontalCenter: parent.horizontalCenter
enabled: !tdapi.busy
enabled: !sheet.loading
text: qsTr("Next")
width: parent.width / 2

Expand All @@ -291,7 +295,7 @@ Sheet {
}

BusyIndicator {
visible: tdapi.busy
visible: sheet.loading
running: visible
anchors.centerIn: parent
}
Expand Down Expand Up @@ -345,7 +349,7 @@ Sheet {

Button {
anchors.horizontalCenter: parent.horizontalCenter
enabled: !tdapi.busy
enabled: !sheet.loading
width: parent.width / 2
text: qsTr("Done")

Expand All @@ -356,7 +360,7 @@ Sheet {
}

BusyIndicator {
visible: tdapi.busy
visible: sheet.loading
running: visible
anchors.centerIn: parent
}
Expand Down Expand Up @@ -427,16 +431,22 @@ Sheet {
__isNextTypeSms = codeInfo.isNextTypeSms

__length = codeInfo.length

sheet.loading = false
}

onRegistrationRequested: {
state = "Registration";
termsOfServiceString = termsOfService.text

sheet.loading = false
}

onPasswordRequested: {
state = "Password"
passwordHint = passwordInfo.passwordHint

sheet.loading = false
}

onIsAuthorizedChanged: {
Expand All @@ -445,10 +455,23 @@ Sheet {
}
}

Timer {
id: loadingTimer

interval: 10000
repeat: false
onTriggered: { sheet.loading = false; }
}

onLoadingChanged: {
if (loading)
loadingTimer.restart()
}

onTimeoutChanged: {
codeExpireTimer.start()
codeTimeText.text = Utils.formatTime(timeout / 1000)
}

onRejected: tdapi.busy = false
onRejected: sheet.loading = false
}
13 changes: 8 additions & 5 deletions resources/qml/MainPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ Page {
font.pixelSize: 60
color: "gray"
text: qsTr("NoChats")
visible: myChatModel.count === 0 && !populateTimer.running && tdapi.isAuthorized
visible: myChatModel.count === 0 && !populateTimer.running && !myChatModel.loading
}

BusyIndicator {
anchors.centerIn: listView
running: populateTimer.running
visible: populateTimer.running
running: populateTimer.running || myChatModel.loading
visible: running
platformStyle: BusyIndicatorStyle { size: "large" }
}

Expand Down Expand Up @@ -174,7 +174,10 @@ Page {
id: myChatModel
chatList: TdApi.ChatListMain

onChatListChanged: { populateTimer.restart() }
onLoadingChanged: {
if (!loading)
populateTimer.restart()
}
}

ContextMenu {
Expand All @@ -197,7 +200,7 @@ Page {

onIsAuthorizedChanged: {
if (tdapi.isAuthorized)
populateTimer.restart()
myChatModel.refresh()
}
}

Expand Down
8 changes: 4 additions & 4 deletions resources/qml/MessagePage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "components"
Page {
id: root

property variant chat: null
property string chatId: ""

property string messageThreadId: ""
property string replyToMessageId: ""
Expand Down Expand Up @@ -81,7 +81,7 @@ Page {
height: 50
width: 50

source: chat.photo ? "image://telegram/" + chat.photo.small.local.path : "image://theme/icon-l-content-avatar-placeholder"
source: myMessageModel.chat.photo ? "image://telegram/" + myMessageModel.chat.photo.small.local.path : "image://theme/icon-l-content-avatar-placeholder"

MouseArea {
anchors.fill: parent
Expand Down Expand Up @@ -360,10 +360,10 @@ Page {
text: message
}
}
tdapi.sendMessage(chat.id, messageThreadId, replyToMessageId, options, replyMarkup, inputMessageContent)
tdapi.sendMessage(myMessageModel.chat.id, messageThreadId, replyToMessageId, options, replyMarkup, inputMessageContent)
}
}

Component.onCompleted: myMessageModel.openChat(chat.id)
Component.onCompleted: myMessageModel.openChat(chatId)
Component.onDestruction: myMessageModel.closeChat()
}
2 changes: 1 addition & 1 deletion resources/qml/components/AboutDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Dialog {
wrapMode: Text.WordWrap
horizontalAlignment: Text.AlignHCenter
color: "white"
text: "Copyright \u00a9 2021 Chukwudi Nwutobo<br/>Contact: <a href='mailto:[email protected]?subject=Meegram'>[email protected]</a>"
text: "Copyright \u00a9 2022 Chukwudi Nwutobo<br/>Contact: <a href='mailto:[email protected]?subject=Meegram'>[email protected]</a>"
onLinkActivated: Qt.openUrlExternally(link)
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/qml/components/ChatItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Item {
anchors.fill: parent

onClicked: {
pageStack.push(Qt.createComponent("qrc:/qml/MessagePage.qml"), { chat: myChatModel.get(index) })
pageStack.push(Qt.createComponent("qrc:/qml/MessagePage.qml"), { chatId: myChatModel.get(index).id })
}
onPressAndHold: root.pressAndHold()
}
Expand Down
2 changes: 1 addition & 1 deletion resources/qml/components/MessageBubble.qml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Item {
color: isOutgoing ? "black" : "white"
font.pixelSize: 16
font.weight: Font.Light
horizontalAlignment: isOutgoing ? Text.AlignLeft : Text.AlignRight
horizontalAlignment: isServiceMessage ? Text.AlignHCenter : isOutgoing ? Text.AlignLeft : Text.AlignRight
visible: !isServiceMessage
}

Expand Down
4 changes: 1 addition & 3 deletions resources/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ PageStackWindow {
Connections {
target: tdapi
onError: {
banner.text = data.message

if (data.code !== "404") {
banner.text = data.message
banner.show()
console.log(data.message, data.code)
}
}
}
Expand Down
45 changes: 23 additions & 22 deletions src/ChatModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@

#include <fnv-cpp/fnv.h>

#include <QDebug>

#include <algorithm>

ChatModel::ChatModel(QObject *parent)
: QAbstractListModel(parent)
, m_sortTimer(new QTimer(this))
, m_loadingTimer(new QTimer(this))
{
connect(TdApi::getInstance().chatStore, SIGNAL(updateChatItem(qint64)), SLOT(handleChatItem(qint64)));
connect(TdApi::getInstance().chatStore, SIGNAL(updateChatPosition(qint64)), SLOT(handleChatPosition(qint64)));

connect(&TdApi::getInstance(), SIGNAL(error(const QVariantMap &)), SLOT(handleError(const QVariantMap &)));
connect(this, SIGNAL(chatListChanged()), SLOT(refresh()));

connect(m_sortTimer, SIGNAL(timeout()), SLOT(sortChats()));
connect(this, SIGNAL(chatListChanged()), this, SLOT(refresh()));

connect(m_sortTimer, SIGNAL(timeout()), this, SLOT(sortChats()));
connect(m_loadingTimer, SIGNAL(timeout()), this, SLOT(loadChats()));

m_sortTimer->setInterval(1000);
m_sortTimer->setSingleShot(true);

m_loadingTimer->setInterval(500);

setRoleNames(roleNames());
loadChats();
}

ChatModel::~ChatModel()
{
if (m_sortTimer)
delete m_sortTimer;
delete m_sortTimer;
delete m_loadingTimer;
}

int ChatModel::rowCount(const QModelIndex &parent) const
Expand All @@ -53,9 +57,6 @@ void ChatModel::fetchMore(const QModelIndex &parent)
if (parent.isValid())
return;

if (m_loading)
loadChats();

const int remainder = m_chatIds.size() - m_count;
const auto itemsToFetch = qMin(ChatSliceLimit, remainder);

Expand All @@ -68,8 +69,6 @@ void ChatModel::fetchMore(const QModelIndex &parent)

endInsertRows();

populate();

emit countChanged();
}

Expand Down Expand Up @@ -229,14 +228,19 @@ void ChatModel::populate()
if (!chat.value("photo").isNull() &&
!chatPhoto.value("small").toMap().value("local").toMap().value("is_downloading_completed").toBool())
{
TdApi::getInstance().downloadFile(chatPhoto.value("small").toMap().value("id").toInt(), 1, 0, 0, false);
QVariantMap result;
result.insert("@type", "downloadFile");
result.insert("file_id", chatPhoto.value("small").toMap().value("id").toInt());
result.insert("priority", 1);

TdApi::getInstance().sendRequest(result);
}
}
}

sortChats();

if (m_count == 0 && !m_chatIds.isEmpty())
if (!m_chatIds.isEmpty())
fetchMore();
}

Expand All @@ -256,7 +260,8 @@ void ChatModel::refresh()
m_loading = true;

clear();
loadChats();

m_loadingTimer->start();

emit loadingChanged();
}
Expand All @@ -279,16 +284,10 @@ void ChatModel::handleChatItem(qint64 chatId)
auto index = std::distance(m_chatIds.begin(), it);
QModelIndex modelIndex = createIndex(static_cast<int>(index), 0);

qDebug() << chatId;

emit dataChanged(modelIndex, modelIndex);
}
}

void ChatModel::handleChatPhoto(int fileId)
{
}

void ChatModel::handleChatPosition(qint64 chatId)
{
auto it = std::ranges::find_if(m_chatIds, [chatId](qint64 id) { return id == chatId; });
Expand All @@ -303,9 +302,11 @@ void ChatModel::handleChatPosition(qint64 chatId)

void ChatModel::handleError(const QVariantMap &error)
{
if (error.value("@extra").toString() == "load_chats_error" && error.value("@extra").toInt() == 404)
if (error.value("@extra").toByteArray() == "load_chats_error" && error.value("code").toInt() == 404)
{
m_loading = false;
m_loadingTimer->stop();

emit loadingChanged();
}
}
Expand Down
Loading

0 comments on commit fd92236

Please sign in to comment.