File indexing completed on 2024-05-12 05:04:09

0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QtQml>
0007 
0008 /// Fetches server announcements
0009 class AnnouncementModel : public QAbstractListModel
0010 {
0011     Q_OBJECT
0012     QML_ELEMENT
0013 
0014     Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
0015 
0016 public:
0017     /// Custom roles for this model
0018     enum CustomRoles {
0019         IdRole = Qt::UserRole, ///< ID of the announcement
0020         ContentRole, ///< Content of the announcement, given in rich HTML
0021         PublishedAt ///< The date and time the announcement was published
0022     };
0023 
0024     explicit AnnouncementModel(QObject *parent = nullptr);
0025 
0026     bool loading() const;
0027     void setLoading(bool loading);
0028 
0029     QVariant data(const QModelIndex &index, int role) const override;
0030     int rowCount(const QModelIndex &parent) const override;
0031     QHash<int, QByteArray> roleNames() const override;
0032 
0033     void fillTimeline();
0034 
0035 Q_SIGNALS:
0036     void loadingChanged();
0037 
0038 private:
0039     struct Announcement {
0040         QString id;
0041         QString content;
0042         QDateTime publishedAt;
0043     };
0044 
0045     QList<Announcement> m_announcements;
0046     bool m_loading = false;
0047     Announcement fromSourceData(const QJsonObject &object) const;
0048 };