Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -0,0 +1,143 @@
|
||||
#include "AppCoordinator.h"
|
||||
|
||||
#include "AppState.h"
|
||||
#include "HistoryModel.h"
|
||||
#include "IImageProvider.h"
|
||||
#include "ReadingModel.h"
|
||||
#include "book/Book.h"
|
||||
#include "formats/FormatRegistry.h"
|
||||
#include "state/ReadingPreferences.h"
|
||||
#include "state/UiState.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QSettings>
|
||||
|
||||
AppCoordinator::AppCoordinator(Book &book,
|
||||
ReadingModel &readingModel,
|
||||
HistoryModel &historyModel,
|
||||
UiState &uiState,
|
||||
ReadingPreferences &readingPreferences,
|
||||
AppState &appState,
|
||||
QSettings &settings,
|
||||
FormatRegistry &formatRegistry,
|
||||
QQmlApplicationEngine &engine,
|
||||
QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_book(book)
|
||||
, m_readingModel(readingModel)
|
||||
, m_historyModel(historyModel)
|
||||
, m_uiState(uiState)
|
||||
, m_readingPreferences(readingPreferences)
|
||||
, m_appState(appState)
|
||||
, m_settings(settings)
|
||||
, m_formatRegistry(formatRegistry)
|
||||
, m_engine(engine)
|
||||
{
|
||||
// Typography changes → re-push font params into ReadingModel
|
||||
connect(&m_readingPreferences, &ReadingPreferences::fontSizeChanged,
|
||||
this, &AppCoordinator::onFontPrefsChanged);
|
||||
connect(&m_readingPreferences, &ReadingPreferences::lineSpacingChanged,
|
||||
this, &AppCoordinator::onFontPrefsChanged);
|
||||
|
||||
connect(&m_book, &Book::willOpenNewBook, this, &AppCoordinator::onWillOpenNewBook);
|
||||
connect(&m_book, &Book::positionSaved, this, &AppCoordinator::onPositionSaved);
|
||||
connect(&m_book, &Book::bookChanged, this, &AppCoordinator::onBookChanged);
|
||||
connect(&m_book, &Book::contentWidthChanged, this, &AppCoordinator::onContentWidthChanged);
|
||||
|
||||
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
|
||||
this, &AppCoordinator::onAboutToQuit);
|
||||
}
|
||||
|
||||
void AppCoordinator::onFontPrefsChanged()
|
||||
{
|
||||
m_readingModel.setFontParams(m_readingPreferences.fontSize(),
|
||||
m_readingPreferences.lineSpacing());
|
||||
}
|
||||
|
||||
// Before a new book open: snapshot the outgoing book's progress into the library.
|
||||
// Scroll position is handled by PageView's scroll tracker via book.saveCurrentPosition().
|
||||
void AppCoordinator::onWillOpenNewBook()
|
||||
{
|
||||
if (m_book.hasBook())
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
}
|
||||
|
||||
// Real-time history update: fired by PageView's scrollTrackTimer on every scroll.
|
||||
void AppCoordinator::onPositionSaved()
|
||||
{
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
}
|
||||
|
||||
// After the new reader is installed: wire up image provider + ReadingModel
|
||||
void AppCoordinator::onBookChanged()
|
||||
{
|
||||
m_currentImageProvider = nullptr;
|
||||
|
||||
if (!m_book.hasBook()) {
|
||||
m_readingModel.setReader(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
FormatReader *reader = m_book.reader();
|
||||
const QString filePath = reader->filePath();
|
||||
|
||||
IImageProvider *provider = m_formatRegistry.createImageProvider(filePath);
|
||||
if (provider) {
|
||||
m_engine.removeImageProvider(provider->scheme());
|
||||
m_engine.addImageProvider(provider->scheme(), provider);
|
||||
provider->setFilePath(filePath);
|
||||
provider->setDisplayWidth(m_contentWidth);
|
||||
m_currentImageProvider = provider;
|
||||
|
||||
if (!reader->coverImagePath().isEmpty())
|
||||
m_book.setCoverImageUrl(provider->coverImageUrl(reader->coverImagePath()));
|
||||
}
|
||||
|
||||
m_readingModel.setFontParams(m_readingPreferences.fontSize(),
|
||||
m_readingPreferences.lineSpacing());
|
||||
m_readingModel.setImageLoader(m_formatRegistry.createImageLoader(filePath));
|
||||
m_readingModel.setCoverImageUrl(m_book.coverImageUrl());
|
||||
m_readingModel.setReader(reader);
|
||||
m_readingModel.measure(true);
|
||||
|
||||
m_uiState.setLastFilePath(filePath);
|
||||
m_historyModel.addOrUpdateBook(filePath, -1.0);
|
||||
}
|
||||
|
||||
// Propagate content-width changes to image provider + reading model.
|
||||
// Called from Book::setContentWidth which is triggered by QML's PageView.
|
||||
void AppCoordinator::onContentWidthChanged(int width)
|
||||
{
|
||||
m_contentWidth = width;
|
||||
if (m_currentImageProvider)
|
||||
m_currentImageProvider->setDisplayWidth(width);
|
||||
m_readingModel.setContentWidth(width);
|
||||
}
|
||||
|
||||
// Quit: flush everything to persistent storage.
|
||||
// Scroll position was last saved by PageView's scroll tracker; we save library progress here.
|
||||
void AppCoordinator::onAboutToQuit()
|
||||
{
|
||||
if (m_book.hasBook()) {
|
||||
m_historyModel.addOrUpdateBook(m_book.filePath(), m_readingModel.progress());
|
||||
m_uiState.setLastFilePath(m_book.filePath());
|
||||
} else {
|
||||
m_uiState.setLastFilePath(QString());
|
||||
}
|
||||
|
||||
m_readingPreferences.save();
|
||||
m_uiState.save();
|
||||
|
||||
QList<QVariantMap> items;
|
||||
for (int i = 0; i < m_historyModel.rowCount(); ++i) {
|
||||
items.append({
|
||||
{QStringLiteral("filePath"), m_historyModel.filePath(i)},
|
||||
{QStringLiteral("progress"), m_historyModel.progress(i)},
|
||||
{QStringLiteral("lastOpened"), m_historyModel.lastOpened(i)},
|
||||
});
|
||||
}
|
||||
m_appState.saveArray(QStringLiteral("history"), items);
|
||||
|
||||
qInfo() << "Settings saved to:" << m_settings.fileName();
|
||||
}
|
||||
Reference in new issue
Block a user