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

dev: ProjectPersistence refactoring. #23

Merged
merged 28 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5c3be57
dev: ProjectPersistence refactoring.
cosmin42 Jan 9, 2024
53e645c
dev: Replace the usage of old Persistence.(1)
cosmin42 Jan 10, 2024
55dd445
dev: Replace the usage of old Persistence.(2)
cosmin42 Jan 10, 2024
f0ffa04
dev: Replace the usage of old Persistence.(3)
cosmin42 Jan 10, 2024
14e1e88
dev: Replace the usage of old Persistence.(34)
cosmin42 Jan 10, 2024
2c5961d
dev: Replace the usage of old Persistence.(5)
cosmin42 Jan 11, 2024
77b5b71
tests: Fix tests build.
cosmin42 Jan 11, 2024
a8bd4fd
dev: Test TestProjectPersistence.
cosmin42 Jan 11, 2024
920741a
fix: Fix missing persistence path configuration.
cosmin42 Jan 12, 2024
001b401
dev: Move newProject to ProjectPersistence.
cosmin42 Jan 12, 2024
68d6061
dev: Remove name member of ProjectSnapshot.
cosmin42 Jan 12, 2024
e8da81e
dev: Remove uuid from ProjectSnapshot.
cosmin42 Jan 12, 2024
b8d6af1
dev: Remove Project default constructor.
cosmin42 Jan 12, 2024
4a0fb1f
fix: Fix newProject save operation.
cosmin42 Jan 12, 2024
d1eebef
dev: Fixes to accomodate the tests with the new changes.
cosmin42 Jan 13, 2024
dd901c5
tests: Update tests to use the new interfaces.
cosmin42 Jan 13, 2024
28f1365
tests: Fix the console app.
cosmin42 Jan 13, 2024
f6f3351
dev: Make sleep cross platform.
cosmin42 Jan 13, 2024
1f33e42
build: Fix python data generation path.
cosmin42 Jan 13, 2024
75f6dc0
fix: Fix naming function stopping condition.
cosmin42 Jan 13, 2024
54c204d
build: Update NugetPackages.
cosmin42 Jan 13, 2024
7880265
dev: Github Actions Test.
cosmin42 Jan 13, 2024
161e341
dev: Github Actions Test.(2)
cosmin42 Jan 13, 2024
2b40034
dev: Github Actions Test.(3)
cosmin42 Jan 13, 2024
9d7eed6
build: Attempt to fix the github actions.
cosmin42 Jan 13, 2024
b715cae
dev: Github Actions Test.(4)
cosmin42 Jan 13, 2024
e168fe3
dev: Github Actions Test.(5)
cosmin42 Jan 13, 2024
061ddb7
build: Remove useless changes.
cosmin42 Jan 13, 2024
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
2 changes: 2 additions & 0 deletions PB/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ add_library(pblib STATIC
include/pb/persistence/Persistence.h
include/pb/persistence/SerializationStrategy.h
include/pb/persistence/SQLPersistence.h
include/pb/persistence/ProjectPersistence.h
include/pb/PhotoBook.h
include/pb/PhotobookListener.h
include/pb/Platform.h
Expand Down Expand Up @@ -139,6 +140,7 @@ add_library(pblib STATIC
src/Project.cpp
src/ProjectSnapshot.cpp
src/ProjectMetadata.cpp
src/ProjectPersistence.cpp
src/RegularImage.cpp
src/ThumbnailsProcessor.cpp
src/SerializationStrategy.cpp
Expand Down
30 changes: 15 additions & 15 deletions PB/include/pb/PhotoBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
#include <pb/export/Jpg.h>
#include <pb/export/Pdf.h>
#include <pb/persistence/Persistence.h>
#include <pb/persistence/ProjectPersistence.h>
#include <pb/project/Project.h>
#include <pb/tasks/FileMapper.h>
#include <pb/util/Util.h>

namespace PB {

class Photobook final : public PBDev::Observer,
public PersistenceMetadataListener,
public PersistenceProjectListener,
public ProjectPersistenceListener,
public ImportFoldersLogicListener,
public ThreadScheduler {
public:
Expand All @@ -39,23 +39,23 @@ class Photobook final : public PBDev::Observer,
void configure(DashboardListener *listener);
void configure(std::shared_ptr<Project> project);

void renameProject(std::string uuid, std::string oldName, std::string newName);
void renameProject(std::string uuid, std::string oldName,
std::string newName);

void recallMetadata();
void recallProject(Path path);
void recallProject(std::string name);

void newProject(std::string name);
void deleteProject(std::string id);

void saveProject();
// todo: rename to renameProject
void saveProject(std::string name);
void loadProject();
void unloadProject();
bool isSaved() const;
bool isSaved();

ImageViews &imageViews();
ProjectSnapshot &activeProject();
ImageViews &imageViews();
ProjectPersistence &project();

void addImportFolder(Path importPath);
void removeImportFolder(Path path);
Expand All @@ -67,11 +67,12 @@ class Photobook final : public PBDev::Observer,
void onError(PBDev::Error error);

void update(PBDev::ObservableSubject &subject) override;
void onProjectRead(std::shared_ptr<Project> project) override;
void onMetadataRead(ProjectMetadata projectMetadata) override;
void onMetadataRead(std::vector<ProjectMetadata> projectMetadata) override;
void onMetadataPersistenceError(PBDev::Error) override;
void onProjectPersistenceError(PBDev::Error) override;

void onProjectRead() override;

void onMetadataUpdated() override;

void onPersistenceError(PBDev::Error) override;

void onMappingStarted(Path path) override;
void onMappingFinished(Path, std::vector<Path> newFolders) override;
Expand All @@ -90,8 +91,7 @@ class Photobook final : public PBDev::Observer,
PhotobookListener *mParent = nullptr;
DashboardListener *mDashboardListener = nullptr;
std::shared_ptr<PlatformInfo> mPlatformInfo = nullptr;
Persistence mPersistence;
std::shared_ptr<Project> mProject = nullptr;
ProjectPersistence mProjectPersistence;
ImportFoldersLogic mImportLogic;
ImageViews mImageViews;
std::vector<std::shared_ptr<Exportable>> mExporters;
Expand Down
4 changes: 4 additions & 0 deletions PB/include/pb/PhotobookListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class PhotobookListener {
public:
virtual ~PhotobookListener() = default;

virtual void onProjectRead() = 0;
virtual void onMetadataUpdated() = 0;
virtual void onPersistenceError(PBDev::Error) = 0;

virtual void onProgressUpdate(int, int) = 0;
virtual void onExportProgressUpdate(int, int) = 0;
virtual void onExportFinished() = 0;
Expand Down
23 changes: 9 additions & 14 deletions PB/include/pb/persistence/Persistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,26 @@
namespace PB {
class PersistenceProjectListener {
public:
virtual ~PersistenceProjectListener() = default;
virtual void onProjectRead(std::shared_ptr<Project> project) = 0;
virtual void onProjectPersistenceError(PBDev::Error) = 0;
};

class PersistenceMetadataListener {
public:
virtual void onMetadataRead(ProjectMetadata projectMetadata) = 0;
virtual void onMetadataRead(std::vector<ProjectMetadata> projectMetadata) = 0;
virtual ~PersistenceMetadataListener() = default;
virtual void onMetadataRead(
boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata) = 0;
virtual void onMetadataPersistenceError(PBDev::Error) = 0;
};

class Persistence final {
public:
static std::optional<PBDev::Error> createSupportDirectory(Path path);

explicit Persistence(
Path applicationLocalStatePath,
PersistenceProjectListener *persistenceProjectListener,
PersistenceMetadataListener *persistenceMetadataListener);

~Persistence() = default;

void setPersistenceListener(
PersistenceProjectListener *persistenceProjectListener,
PersistenceMetadataListener *persistenceMetadataListener);
void configure(Path localStatePath);
void configure(PersistenceProjectListener *);
void configure(PersistenceMetadataListener *);

void persistProject(std::string name, ProjectSnapshot project);
void persistMetadata(ProjectMetadata projectMetadata);
Expand All @@ -47,8 +42,8 @@ class Persistence final {
private:
void persistProject(Path filePath, ProjectSnapshot project);

PersistenceProjectListener *mPersistenceProjectListener;
PersistenceMetadataListener *mPersistenceMetadataListener;
PersistenceProjectListener *mPersistenceProjectListener = nullptr;
PersistenceMetadataListener *mPersistenceMetadataListener = nullptr;
SQLitePersistence mCentral;
Json mProjectCache;
Path mLocalStatePath;
Expand Down
7 changes: 6 additions & 1 deletion PB/include/pb/persistence/ProjectPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProjectPersistenceListener {
class ProjectPersistence final : public PersistenceMetadataListener,
public PersistenceProjectListener {
public:
ProjectPersistence() = default;
ProjectPersistence();
~ProjectPersistence() = default;

void configure(Path localStatePath);
Expand All @@ -40,6 +40,11 @@ class ProjectPersistence final : public PersistenceMetadataListener,
boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata) override;
void onMetadataPersistenceError(PBDev::Error) override;

void remove(boost::uuids::uuid id);
void remove(Path path);

void clear();

private:
std::string name(boost::uuids::uuid uuid);

Expand Down
4 changes: 1 addition & 3 deletions PB/include/pb/persistence/SQLPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class SQLitePersistence final {
public:
static constexpr const char *DATABASE_NAME = "database.db";

explicit SQLitePersistence(Path path);

~SQLitePersistence() = default;
void configure(Path localStatePath);

std::optional<PBDev::Error> connect();

Expand Down
41 changes: 18 additions & 23 deletions PB/src/Persistence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@
#include <unordered_map>

namespace PB {
Persistence::Persistence(
Path applicationLocalStatePath,
PersistenceProjectListener *persistenceProjectListener,
PersistenceMetadataListener *persistenceMetadataListener)
: mPersistenceProjectListener(persistenceProjectListener),
mPersistenceMetadataListener(persistenceMetadataListener),
mCentral(applicationLocalStatePath),
mLocalStatePath(applicationLocalStatePath)
{
PBDev::basicAssert(persistenceProjectListener != nullptr);
PBDev::basicAssert(persistenceMetadataListener != nullptr);

void Persistence::configure(Path localStatePath)
{
mLocalStatePath = localStatePath;
mCentral.configure(localStatePath);
auto maybeError = mCentral.connect();
if (maybeError && mPersistenceMetadataListener) {
mPersistenceMetadataListener->onMetadataPersistenceError(
maybeError.value());
}
PBDev::basicAssert(!maybeError.has_value());
}

void Persistence::setPersistenceListener(
PersistenceProjectListener *persistenceProjectListener,
PersistenceMetadataListener *persistenceMetadataListener)
void Persistence::configure(PersistenceProjectListener *listener)
{
mPersistenceProjectListener = persistenceProjectListener;
mPersistenceMetadataListener = persistenceMetadataListener;
mPersistenceProjectListener = listener;
}

void Persistence::configure(PersistenceMetadataListener *listener)
{
mPersistenceMetadataListener = listener;
}

void Persistence::persistProject(Path filePath, ProjectSnapshot projectDetails)
Expand Down Expand Up @@ -98,12 +90,15 @@ void Persistence::recallMetadata()
else {
auto &map =
std::get<std::unordered_map<std::string, std::string>>(mapOrError);
std::vector<ProjectMetadata> projectsMetadata;

boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata;
for (auto &[key, value] : map) {
projectsMetadata.push_back(ProjectMetadata(key, value));
auto generator = boost::uuids::string_generator();
boost::uuids::uuid parsedUuid = generator(key);
metadata.insert({parsedUuid, value});
}
if (mPersistenceMetadataListener) {
mPersistenceMetadataListener->onMetadataRead(projectsMetadata);
mPersistenceMetadataListener->onMetadataRead(metadata);
}
}
});
Expand Down
Loading
Loading