Page MenuHomePhorge

matrix-sticker-pack-list.cpp
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

matrix-sticker-pack-list.cpp

/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <immer/config.hpp>
#include <lager/lenses/optional.hpp>
#include <lager/lenses/at.hpp>
#include "matrix-sticker-pack.hpp"
#include "matrix-sticker-pack-list.hpp"
using namespace Kazv;
static const std::string accountDataEventType = "im.ponies.user_emotes";
lager::reader<immer::flex_vector<MatrixStickerPackSource>> getEventsFromClient(Client client)
{
lager::reader<Event> userEmotes = client.accountData()[accountDataEventType][lager::lenses::or_default];
return userEmotes.map([](Event e) {
return immer::flex_vector<MatrixStickerPackSource>{
{MatrixStickerPackSource::AccountData, accountDataEventType, e},
};
});
}
MatrixStickerPackList::MatrixStickerPackList(Client client, QObject *parent)
: QAbstractListModel(parent)
, m_client(client)
, m_events(getEventsFromClient(m_client))
, m_internalCount(0)
, LAGER_QT(count)(m_events.map([](const auto &events) {
return static_cast<int>(events.size());
}))
{
m_internalCount = count();
connect(this, &MatrixStickerPackList::countChanged, this, &MatrixStickerPackList::updateInternalCount);
}
MatrixStickerPackList::~MatrixStickerPackList() = default;
MatrixStickerPack *MatrixStickerPackList::at(int index) const
{
return new MatrixStickerPack(m_events.map([index](const auto &events) {
if (events.size() > std::size_t(index)) {
return events[index];
} else {
return MatrixStickerPackSource{
MatrixStickerPackSource::AccountData,
accountDataEventType,
Event(),
};
}
}));
}
QVariant MatrixStickerPackList::data(const QModelIndex &/* index */, int /* role */) const
{
return QVariant();
}
int MatrixStickerPackList::rowCount(const QModelIndex &parent = QModelIndex()) const
{
if (parent.isValid()) {
return 0;
} else {
return count();
}
}
void MatrixStickerPackList::updateInternalCount()
{
auto curCount = count();
auto oldCount = m_internalCount;
auto diff = std::abs(curCount - oldCount);
if (curCount > oldCount) {
beginInsertRows(QModelIndex(), 0, diff - 1);
m_internalCount = curCount;
endInsertRows();
} else if (curCount < oldCount) {
beginRemoveRows(QModelIndex(), 0, diff - 1);
m_internalCount = curCount;
endRemoveRows();
}
}

File Metadata

Mime Type
text/x-c
Expires
Wed, May 14, 7:14 AM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
87149
Default Alt Text
matrix-sticker-pack-list.cpp (2 KB)

Event Timeline