Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made books save and load Zoom and Y Offset #115

Open
wants to merge 5 commits into
base: dev/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/adapters/DTOs/book_dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct ADAPTERS_EXPORT BookDto
Q_PROPERTY(QString lastOpened MEMBER lastOpened)
Q_PROPERTY(QString coverPath MEMBER coverPath)
Q_PROPERTY(bool downloaded MEMBER downloaded)
Q_PROPERTY(float zoom MEMBER zoom)
Q_PROPERTY(float yOffset MEMBER yOffset)

public:
QString uuid;
Expand All @@ -53,6 +55,8 @@ struct ADAPTERS_EXPORT BookDto
QString lastOpened;
QString coverPath;
bool downloaded;
float zoom;
float yOffset;
QList<TagDto> tags;
};

Expand Down
8 changes: 8 additions & 0 deletions src/adapters/controllers/library_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ int LibraryController::updateBook(const QString& uuid,
case MetaProperty::LastModified:
updatedBook.setLastOpened(QDateTime::fromString(value.toString()));
break;
case MetaProperty::Zoom:
updatedBook.setZoom(static_cast<float>(value.toDouble()));
break;
case MetaProperty::YOffset:
updatedBook.setYOffset(static_cast<float>(value.toDouble()));
break;
case MetaProperty::Invalid:
return static_cast<int>(BookOperationStatus::PropertyDoesNotExist);
break;
Expand Down Expand Up @@ -358,6 +364,8 @@ void LibraryController::addBookMetaDataToDto(const Book& book, BookDto& bookDto)
bookDto.bookReadingProgress = book.getBookReadingProgress();
bookDto.coverPath = pathWithScheme;
bookDto.downloaded = book.isDownloaded();
bookDto.zoom = book.getZoom();
bookDto.yOffset = book.getYOffset();

bookDto.addedToLibrary = book.getAddedToLibrary().toLocalTime().toString(
Book::dateTimeStringFormat);
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/interfaces/controllers/i_library_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class ADAPTERS_EXPORT ILibraryController : public QObject
CurrentPage,
AddedToLibrary,
LastModified,
Zoom,
YOffset,
Invalid
};
Q_ENUM(MetaProperty);
Expand Down
6 changes: 4 additions & 2 deletions src/application/services/book_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ void BookService::setCurrentPage(int newCurrentPage)

float BookService::getZoom() const
{
return m_zoom;
auto book = getBook();
return book->getZoom();
}

void BookService::setZoom(float newZoom)
{
m_zoom = newZoom;
auto book = getBook();
book->setZoom(newZoom);
}

QString BookService::getColorTheme()
Expand Down
1 change: 0 additions & 1 deletion src/application/services/book_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class BookService : public IBookService
ILibraryService* m_libraryService;
std::unique_ptr<mupdf::FzDocument> m_fzDocument = nullptr;
std::unique_ptr<core::utils::BookSearcher> m_bookSearcher = nullptr;
float m_zoom = 1;
QUuid m_uuid;

std::unique_ptr<core::TOCModel> m_TOCModel;
Expand Down
28 changes: 28 additions & 0 deletions src/domain/entities/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,26 @@ void Book::setColorTheme(const QString& newColorTheme)
m_metaData.colorTheme = newColorTheme;
}

float Book::getZoom() const
{
return m_metaData.zoom;
}

void Book::setZoom(float newZoom)
{
m_metaData.zoom = newZoom;
}

float Book::getYOffset() const
{
return m_metaData.yOffset;
}

void Book::setYOffset(float newYOffset)
{
m_metaData.yOffset = newYOffset;
}

QString Book::getFileHash() const
{
return m_metaData.fileHash;
Expand Down Expand Up @@ -521,6 +541,10 @@ void Book::update(const Book& other)
m_metaData.colorTheme = other.getColorTheme();
if(m_metaData.fileHash != other.getFileHash())
m_metaData.fileHash = other.getFileHash();
if(m_metaData.zoom != other.getZoom())
m_metaData.zoom = other.getZoom();
if(m_metaData.yOffset != other.getYOffset())
m_metaData.yOffset = other.getYOffset();

if(!tagsAreTheSame(other.getTags()))
m_tags = other.getTags();
Expand Down Expand Up @@ -633,6 +657,8 @@ QByteArray Book::toJson() const
{ "coverPath", getCoverPath() },
{ "colorTheme", getColorTheme() },
{ "fileHash", getFileHash() },
{ "zoom", getZoom() },
{ "yOffset", getYOffset() },
{ "existsOnlyOnClient", existsOnlyOnClient() },
{ "tags", serializeTags() },
{ "highlights", serializeHighlights() },
Expand Down Expand Up @@ -729,6 +755,8 @@ BookMetaData Book::getBookMetaDataFromJson(const QJsonObject& jsonBook)
.coverPath = jsonBook["coverPath"].toString(),
.colorTheme = jsonBook["colorTheme"].toString(),
.fileHash = jsonBook["fileHash"].toString(),
.zoom = static_cast<float>(jsonBook["zoom"].toDouble()),
.yOffset = static_cast<float>(jsonBook["yOffset"].toDouble()),
};

// Specify that the dates are UTC, else Qt thinks its local time
Expand Down
6 changes: 6 additions & 0 deletions src/domain/entities/book.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class DOMAIN_EXPORT Book
QString getColorTheme() const;
void setColorTheme(const QString& newColorTheme);

float getZoom() const;
void setZoom(float newZoom);

float getYOffset() const;
void setYOffset(float newYOffset);

QString getFileHash() const;
void setFileHash(const QString& newFileHash);

Expand Down
5 changes: 4 additions & 1 deletion src/domain/value_objects/book_meta_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct DOMAIN_EXPORT BookMetaData
QString coverPath;
QString colorTheme;
QString fileHash;
float zoom = 1.0;
float yOffset = 0.0;

bool operator==(const BookMetaData& rhs) const
{
Expand All @@ -48,7 +50,8 @@ struct DOMAIN_EXPORT BookMetaData
coverLastModified.toSecsSinceEpoch() ==
rhs.coverLastModified.toSecsSinceEpoch() &&
hasCover == rhs.hasCover && coverPath == coverPath &&
colorTheme == rhs.colorTheme && fileHash == rhs.fileHash;
colorTheme == rhs.colorTheme && fileHash == rhs.fileHash &&
zoom == rhs.zoom && yOffset == rhs.yOffset;
}
};

Expand Down
10 changes: 5 additions & 5 deletions src/presentation/readingPage/MDocumentView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Pane {
}

Component.onCompleted: {
BookController.zoom = SettingsController.appearanceSettings.DefaultZoom / 100
}

Component.onDestruction: {
BookController.zoom = 1
if (BookController.zoom === 0) {
zoom(SettingsController.appearanceSettings.DefaultZoom / 100)
} else {
zoom(BookController.zoom)
}
}

Connections {
Expand Down
8 changes: 5 additions & 3 deletions src/presentation/readingPage/MReadingPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Page {
}

Component.onCompleted: root.forceActiveFocus()
Component.onDestruction: internal.saveCurrentPage()
Component.onDestruction: internal.saveCurrentBookState()

// Save the current page every 5s automatically
Timer {
Expand All @@ -27,7 +27,7 @@ Page {
repeat: true
running: true

onTriggered: internal.saveCurrentPage()
onTriggered: internal.saveCurrentBookState()
}

Shortcut {
Expand Down Expand Up @@ -363,13 +363,15 @@ Page {
documentView.setPage(BookController.pageCount - 1)
}

function saveCurrentPage() {
function saveCurrentBookState() {
let currentPage = BookController.currentPage
if (currentPage === internal.prevCurrentPage)
return

var operationsMap = {}
operationsMap[LibraryController.MetaProperty.CurrentPage] = currentPage
operationsMap[LibraryController.MetaProperty.Zoom] = BookController.zoom
operationsMap[LibraryController.MetaProperty.YOffset] = documentView.getYOffset()
LibraryController.updateBook(Globals.selectedBook.uuid,
operationsMap)
internal.prevCurrentPage = currentPage
Expand Down
Loading