Skip to content

Commit

Permalink
dev: Add ProjectPersistence.
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmin42 committed Jan 7, 2024
1 parent 650088c commit 91c9a4d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
53 changes: 53 additions & 0 deletions PB/include/pb/persistence/ProjectPersistence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#include <pb/persistence/Persistence.h>

namespace PB {
class ProjectPersistenceListener {
public:
virtual ~ProjectPersistenceListener() = default;
virtual void onMetadataUpdated() = 0;
virtual void onProjectRead() = 0;
virtual void onPersistenceError(PBDev::Error) = 0;
};

class ProjectPersistence final : public PersistenceMetadataListener,
public PersistenceProjectListener {
public:
ProjectPersistence() = default;
~ProjectPersistence() = default;

void configure(Path localStatePath);
void configure(ProjectPersistenceListener *listener);

std::shared_ptr<Project> currentProject();

void recallProject(boost::uuids::uuid const &uuid);

void recallProject(std::string name);

void recallMetadata();

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

bool hasUUID(std::string name) const;

std::vector<std::string> projectsNames() const;

void onProjectRead(std::shared_ptr<Project> project) override;
void onProjectPersistenceError(PBDev::Error) override;

void onMetadataRead(
boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata) override;
void onMetadataPersistenceError(PBDev::Error) override;

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

ProjectPersistenceListener *mListener = nullptr;
Path mLocalStatePath;
Persistence mPersistence;
std::shared_ptr<Project> mProject = nullptr;

boost::bimaps::bimap<boost::uuids::uuid, std::string> mMetadata;
};
} // namespace PB
78 changes: 78 additions & 0 deletions PB/src/ProjectPersistence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <pb/persistence/ProjectPersistence.h>

namespace PB {
void ProjectPersistence::configure(Path localStatePath)
{
mLocalStatePath = localStatePath;
}

void ProjectPersistence::configure(ProjectPersistenceListener *listener)
{
mListener = listener;
}

std::shared_ptr<Project> ProjectPersistence::currentProject()
{
return mProject;
}

void ProjectPersistence::recallProject(boost::uuids::uuid const &uuid)
{
mPersistence.recallProject(name(uuid));
}

void ProjectPersistence::recallProject(std::string name)
{
mPersistence.recallProject(name);
}

void ProjectPersistence::recallMetadata() { mPersistence.recallMetadata(); }

boost::uuids::uuid ProjectPersistence::uuid(std::string name)
{
return mMetadata.right.at(name);
}

bool ProjectPersistence::hasUUID(std::string name) const
{
return mMetadata.right.find(name) != mMetadata.right.end();
}

std::vector<std::string> ProjectPersistence::projectsNames() const
{
std::vector<std::string> projects;
for (auto &it : mMetadata) {
projects.push_back(it.right);
}
return projects;
}

std::string ProjectPersistence::name(boost::uuids::uuid uuid)
{
return mMetadata.left.at(uuid);
}

void ProjectPersistence::onProjectRead(std::shared_ptr<Project> project)
{
mProject = project;
mListener->onProjectRead();
}

void ProjectPersistence::onProjectPersistenceError(PBDev::Error error)
{
mListener->onPersistenceError(error);
}

void ProjectPersistence::onMetadataRead(
boost::bimaps::bimap<boost::uuids::uuid, std::string> metadata)
{
mMetadata = metadata;
mListener->onMetadataUpdated();
}

void ProjectPersistence::onMetadataPersistenceError(PBDev::Error error)
{
mListener->onPersistenceError(error);
}

} // namespace PB

0 comments on commit 91c9a4d

Please sign in to comment.