Skip to content

Commit

Permalink
dev: ProjectPersistence refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmin42 committed Jan 9, 2024
1 parent 91c9a4d commit 5c3be57
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 127 deletions.
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

0 comments on commit 5c3be57

Please sign in to comment.