logos-execution-zone-wallet-ui/src/LEZWalletAccountModel.cpp

145 lines
5.2 KiB
C++
Raw Normal View History

2026-02-22 02:03:13 +05:30
#include "LEZWalletAccountModel.h"
2026-06-16 21:02:54 -03:00
#include <QHash>
#include <QPair>
#include <QVariantMap>
#include <algorithm>
2026-02-22 02:03:13 +05:30
LEZWalletAccountModel::LEZWalletAccountModel(QObject* parent)
: QAbstractListModel(parent)
{
}
int LEZWalletAccountModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return m_entries.size();
}
QVariant LEZWalletAccountModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
return QVariant();
const LEZWalletAccountEntry& e = m_entries.at(index.row());
switch (role) {
case NameRole: return e.name;
2026-06-06 01:24:27 -03:00
case AccountIdRole: return e.accountId;
2026-02-22 02:03:13 +05:30
case BalanceRole: return e.balance;
2026-06-16 21:02:54 -03:00
case VaultBalanceRole: return e.vaultBalance;
2026-02-22 02:03:13 +05:30
case IsPublicRole: return e.isPublic;
2026-06-16 21:02:54 -03:00
case SectionKeyRole: return e.sectionKey;
case KeysJsonRole: return e.keysJson;
case IsFirstInGroupRole: return e.isFirstInGroup;
2026-07-03 23:43:48 -03:00
case IsInitializedRole: return e.isInitialized;
2026-02-22 02:03:13 +05:30
default: return QVariant();
}
}
QHash<int, QByteArray> LEZWalletAccountModel::roleNames() const
{
return {
{ NameRole, "name" },
2026-06-06 01:24:27 -03:00
{ AccountIdRole, "accountId" },
2026-02-22 02:03:13 +05:30
{ BalanceRole, "balance" },
2026-06-16 21:02:54 -03:00
{ VaultBalanceRole, "vaultBalance" },
{ IsPublicRole, "isPublic" },
{ SectionKeyRole, "sectionKey" },
{ KeysJsonRole, "keysJson" },
2026-07-03 23:43:48 -03:00
{ IsFirstInGroupRole, "isFirstInGroup" },
{ IsInitializedRole, "isInitialized" }
2026-02-22 02:03:13 +05:30
};
}
void LEZWalletAccountModel::replaceFromVariantList(const QVariantList& list)
{
2026-06-16 21:02:54 -03:00
// Rebuilding from scratch loses any balance/vaultBalance already fetched for an
// account that's still present — carry those over so periodic re-listing (e.g. to
// pick up newly discovered private accounts) doesn't make the claimable list and
// balances flicker empty until the next refresh repopulates them.
QHash<QString, QPair<QString, QString>> previousBalances;
previousBalances.reserve(m_entries.size());
for (const LEZWalletAccountEntry& e : m_entries)
previousBalances.insert(e.accountId, qMakePair(e.balance, e.vaultBalance));
beginResetModel();
int oldCount = m_entries.size();
m_entries.clear();
for (const QVariant& v : list) {
LEZWalletAccountEntry e;
e.balance = QString();
2026-06-16 21:02:54 -03:00
e.name = QString();
if (v.type() == QVariant::Map) {
const QVariantMap map = v.toMap();
e.accountId = map.value(QStringLiteral("account_id")).toString();
e.isPublic = map.value(QStringLiteral("is_public"), true).toBool();
2026-07-03 23:43:48 -03:00
e.isInitialized = map.value(QStringLiteral("is_initialized"), false).toBool();
2026-06-16 21:02:54 -03:00
if (e.isPublic) {
e.sectionKey = PublicSectionKey;
} else {
e.sectionKey = map.value(QStringLiteral("npk")).toString();
e.keysJson = map.value(QStringLiteral("keys_json")).toString();
}
} else {
e.accountId = v.toString();
e.isPublic = true;
2026-06-16 21:02:54 -03:00
e.sectionKey = PublicSectionKey;
}
const auto previous = previousBalances.find(e.accountId);
if (previous != previousBalances.end()) {
e.balance = previous->first;
e.vaultBalance = previous->second;
}
m_entries.append(e);
}
2026-06-16 21:02:54 -03:00
// Keep entries grouped by section (public first) so consecutive rows of the same
// group are contiguous, then mark each group's first row for the QML header.
std::stable_sort(m_entries.begin(), m_entries.end(),
[](const LEZWalletAccountEntry& a, const LEZWalletAccountEntry& b) {
if (a.isPublic != b.isPublic) return a.isPublic;
return a.sectionKey < b.sectionKey;
});
for (int i = 0; i < m_entries.size(); ++i)
m_entries[i].isFirstInGroup = (i == 0) || (m_entries[i].sectionKey != m_entries[i - 1].sectionKey);
endResetModel();
if (oldCount != m_entries.size())
emit countChanged();
}
2026-06-06 01:24:27 -03:00
void LEZWalletAccountModel::setBalanceByAccountId(const QString& accountId, const QString& balance)
2026-02-22 02:03:13 +05:30
{
for (int i = 0; i < m_entries.size(); ++i) {
2026-06-06 01:24:27 -03:00
if (m_entries.at(i).accountId == accountId) {
2026-02-22 02:03:13 +05:30
if (m_entries.at(i).balance != balance) {
m_entries[i].balance = balance;
QModelIndex idx = index(i, 0);
emit dataChanged(idx, idx, { BalanceRole });
}
return;
}
}
}
2026-06-16 21:02:54 -03:00
void LEZWalletAccountModel::setVaultBalanceByAccountId(const QString& accountId, const QString& vaultBalance)
{
for (int i = 0; i < m_entries.size(); ++i) {
if (m_entries.at(i).accountId == accountId) {
if (m_entries.at(i).vaultBalance != vaultBalance) {
m_entries[i].vaultBalance = vaultBalance;
QModelIndex idx = index(i, 0);
emit dataChanged(idx, idx, { VaultBalanceRole });
}
return;
}
}
}
bool LEZWalletAccountModel::isPublicAccount(const QString& accountId, bool defaultValue) const
{
for (const LEZWalletAccountEntry& e : m_entries) {
if (e.accountId == accountId)
return e.isPublic;
}
return defaultValue;
}