fix history sort

This commit is contained in:
Agel committed 2026-10-30 12:39:30 +01:00
1 parent c25e8ddb90
commit 511472bc73
4 files changed
+298 -35

No files matched your search

+115 -26
View File
@@ -6,7 +6,9 @@
#include <QDateTime>
#include <QFileInfo>
#include <QPointer>
#include <QSet>
#include <QtConcurrent>
#include <algorithm>
namespace {
QString formatForPath(const QString &filePath)
@@ -21,6 +23,19 @@ QString formatForPath(const QString &filePath)
return QStringLiteral("FB2");
return filePath.section('.', -1).toUpper();
}
QDateTime parseDateTime(const QString &str)
{
if (str.isEmpty())
return {};
QDateTime dt = QDateTime::fromString(str, Qt::ISODate);
if (!dt.isValid()) {
const QDate d = QDate::fromString(str, Qt::ISODate);
if (d.isValid())
dt = d.startOfDay();
}
return dt;
}
}
HistoryModel::HistoryModel(FormatRegistry &registry, QObject *parent)
@@ -62,7 +77,7 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const
case ProgressRole:
return entry.progress;
case LastOpenedRole:
return entry.lastOpened;
return entry.lastOpened.isValid() ? entry.lastOpened.toString(Qt::ISODate) : QString();
case CoverImageUrlRole:
return entry.coverImageUrl;
default:
@@ -81,10 +96,18 @@ bool HistoryModel::setData(const QModelIndex &index, const QVariant &value, int
entry.progress = value.toDouble();
emit dataChanged(index, index, {ProgressRole});
return true;
case LastOpenedRole:
entry.lastOpened = value.toString();
emit dataChanged(index, index, {LastOpenedRole});
case LastOpenedRole: {
QDateTime dt;
if (value.canConvert<QDateTime>()) {
dt = value.toDateTime();
} else {
dt = parseDateTime(value.toString());
}
if (!dt.isValid())
return false;
addOrUpdateBook(entry.filePath, entry.progress, dt);
return true;
}
default:
return false;
}
@@ -105,14 +128,46 @@ QHash<int, QByteArray> HistoryModel::roleNames() const
void HistoryModel::load(AppState &state)
{
clear();
for (const QVariantMap &item : state.loadArray(QStringLiteral("history"))) {
const QList<QVariantMap> rawItems = state.loadArray(QStringLiteral("history"));
QList<HistoryEntry> loadedEntries;
loadedEntries.reserve(rawItems.size());
QSet<QString> seenPaths;
for (const QVariantMap &item : rawItems) {
const QString fp = item.value(QStringLiteral("filePath")).toString();
if (fp.isEmpty()) continue;
if (fp.isEmpty() || seenPaths.contains(fp))
continue;
const QString loStr = item.value(QStringLiteral("lastOpened")).toString();
const QDateTime lastOpened = parseDateTime(loStr);
if (!lastOpened.isValid())
continue; // bogus date: immediately dropped
seenPaths.insert(fp);
double progress = item.value(QStringLiteral("progress"), 0.0).toDouble();
progress = qBound(0.0, progress, 1.0);
const QString lo = item.value(QStringLiteral("lastOpened")).toString();
addOrUpdateBook(fp, progress, lo);
HistoryEntry entry;
entry.filePath = fp;
entry.progress = progress;
entry.lastOpened = lastOpened;
entry.format = formatForPath(fp);
loadedEntries.append(entry);
}
std::stable_sort(loadedEntries.begin(), loadedEntries.end(),
[](const HistoryEntry &a, const HistoryEntry &b) {
return a.lastOpened > b.lastOpened;
});
beginResetModel();
m_entries = std::move(loadedEntries);
endResetModel();
emit countChanged();
for (const auto &entry : m_entries) {
requestMetadata(entry.filePath);
}
}
@@ -124,43 +179,78 @@ void HistoryModel::save(AppState &state) const
items.append({
{QStringLiteral("filePath"), entry.filePath},
{QStringLiteral("progress"), entry.progress},
{QStringLiteral("lastOpened"), entry.lastOpened},
{QStringLiteral("lastOpened"), entry.lastOpened.toString(Qt::ISODate)},
});
}
state.saveArray(QStringLiteral("history"), items);
}
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened)
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, const QDateTime &lastOpened)
{
if (filePath.isEmpty() || !lastOpened.isValid())
return; // bogus date: immediately dropped
int existingIndex = -1;
for (int i = 0; i < m_entries.size(); ++i) {
if (m_entries[i].filePath == filePath) {
m_entries[i].progress = progress;
m_entries[i].lastOpened = lastOpened;
if (i != 0) {
beginMoveRows(QModelIndex(), i, i, QModelIndex(), 0);
m_entries.move(i, 0);
endMoveRows();
}
emit dataChanged(createIndex(0, 0), createIndex(0, 0), {ProgressRole, LastOpenedRole});
return;
existingIndex = i;
break;
}
}
if (existingIndex != -1) {
m_entries[existingIndex].progress = progress;
m_entries[existingIndex].lastOpened = lastOpened;
int targetIndex = 0;
for (int i = 0; i < m_entries.size(); ++i) {
if (i == existingIndex)
continue;
if (lastOpened > m_entries[i].lastOpened)
break;
++targetIndex;
}
if (existingIndex != targetIndex) {
if (existingIndex < targetIndex) {
beginMoveRows(QModelIndex(), existingIndex, existingIndex, QModelIndex(), targetIndex + 1);
} else {
beginMoveRows(QModelIndex(), existingIndex, existingIndex, QModelIndex(), targetIndex);
}
m_entries.move(existingIndex, targetIndex);
endMoveRows();
}
emit dataChanged(createIndex(targetIndex, 0), createIndex(targetIndex, 0), {ProgressRole, LastOpenedRole});
return;
}
HistoryEntry entry;
entry.filePath = filePath;
entry.progress = progress;
entry.lastOpened = lastOpened;
entry.format = formatForPath(filePath);
beginInsertRows(QModelIndex(), 0, 0);
m_entries.prepend(entry);
int targetIndex = 0;
while (targetIndex < m_entries.size() && !(lastOpened > m_entries[targetIndex].lastOpened)) {
++targetIndex;
}
beginInsertRows(QModelIndex(), targetIndex, targetIndex);
m_entries.insert(targetIndex, entry);
endInsertRows();
emit countChanged();
requestMetadata(filePath);
}
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened)
{
const QDateTime dt = parseDateTime(lastOpened);
if (!dt.isValid())
return; // bogus date: immediately dropped
addOrUpdateBook(filePath, progress, dt);
}
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
{
double resolvedProgress = progress;
@@ -174,8 +264,7 @@ void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
if (resolvedProgress < 0.0)
resolvedProgress = 0.0;
}
addOrUpdateBook(filePath, resolvedProgress,
QDateTime::currentDateTime().toString(Qt::ISODate));
addOrUpdateBook(filePath, resolvedProgress, QDateTime::currentDateTime());
}
void HistoryModel::removeBook(const QString &filePath)
@@ -214,7 +303,7 @@ double HistoryModel::progress(int index) const
return m_entries.at(index).progress;
}
QString HistoryModel::lastOpened(int index) const
QDateTime HistoryModel::lastOpened(int index) const
{
if (index < 0 || index >= m_entries.size()) return {};
return m_entries.at(index).lastOpened;
+6 -4
View File
@@ -2,6 +2,7 @@
#define HISTORYMODEL_H
#include <QAbstractListModel>
#include <QDateTime>
#include <QFuture>
#include <QHash>
#include <QList>
@@ -13,7 +14,7 @@ class FormatRegistry;
struct HistoryEntry {
QString filePath;
double progress = 0.0;
QString lastOpened;
QDateTime lastOpened;
QString title;
QString author;
QString format;
@@ -50,14 +51,15 @@ public:
void load(AppState &state);
void save(AppState &state) const;
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress, const QDateTime &lastOpened);
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened);
Q_INVOKABLE void addOrUpdateBook(const QString &filePath, double progress);
Q_INVOKABLE void removeBook(const QString &filePath);
Q_INVOKABLE void clear();
QString filePath(int index) const;
double progress(int index) const;
QString lastOpened(int index) const;
QString filePath(int index) const;
double progress(int index) const;
QDateTime lastOpened(int index) const;
signals:
void countChanged();