refactoring: added utils

This commit is contained in:
seraphima
2024-07-02 14:26:33 +02:00
parent 10a3f1162f
commit db8935dca3
5 changed files with 97 additions and 40 deletions

View File

@@ -0,0 +1,45 @@
#pragma once
#include <vector>
#include <projects-common/Data.h>
#include <common/logger/logger.h>
namespace ProjectsJsonUtils
{
inline std::vector<Project> Read(const std::wstring& fileName)
{
std::vector<Project> projects{};
try
{
auto savedProjectsJson = json::from_file(fileName);
if (savedProjectsJson.has_value())
{
auto savedProjects = JsonUtils::ProjectsListJSON::FromJson(savedProjectsJson.value());
if (savedProjects.has_value())
{
projects = savedProjects.value();
}
}
}
catch (std::exception ex)
{
Logger::error("Error reading projects file. {}", ex.what());
}
return projects;
}
inline void Write(const std::wstring& fileName, const std::vector<Project>& projects)
{
try
{
json::to_file(fileName, JsonUtils::ProjectsListJSON::ToJson(projects));
}
catch (std::exception ex)
{
Logger::error("Error writing projects file. {}", ex.what());
}
}
}

View File

@@ -0,0 +1,36 @@
#pragma once
#include <string>
#include <vector>
#include <projects-common/Data.h>
namespace ProjectNameUtils
{
inline std::wstring CreateProjectName(const std::vector<Project>& projects)
{
// new project name
std::wstring defaultNamePrefix = L"Project"; // TODO: localizable
int nextProjectIndex = 0;
for (const auto& proj : projects)
{
const std::wstring& name = proj.name;
if (name.starts_with(defaultNamePrefix))
{
try
{
int index = std::stoi(name.substr(defaultNamePrefix.length() + 1));
if (nextProjectIndex < index)
{
nextProjectIndex = index;
}
}
catch (std::exception)
{
}
}
}
return defaultNamePrefix + L" " + std::to_wstring(nextProjectIndex + 1);
}
}

View File

@@ -129,6 +129,8 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="JsonUtils.h" />
<ClInclude Include="NameUtils.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="resource.h" />
</ItemGroup>

View File

@@ -21,6 +21,12 @@
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="NameUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="JsonUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">

View File

@@ -10,6 +10,9 @@
#include <projects-common/WindowEnumerator.h>
#include <projects-common/WindowFilter.h>
#include <JsonUtils.h>
#include <NameUtils.h>
#include <common/utils/gpo.h>
#include <common/utils/logger_helper.h>
#include <common/utils/UnhandledExceptionHandler.h>
@@ -46,47 +49,11 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cm
}
// read previously saved projects
std::vector<Project> projects;
try
{
auto savedProjectsJson = json::from_file(fileName);
if (savedProjectsJson.has_value())
{
auto savedProjects = JsonUtils::ProjectsListJSON::FromJson(savedProjectsJson.value());
if (savedProjects.has_value())
{
projects = savedProjects.value();
}
}
}
catch (std::exception ex)
{
Logger::error("Error reading projects file. {}", ex.what());
}
std::vector<Project> projects = ProjectsJsonUtils::Read(fileName);
// new project name
std::wstring defaultNamePrefix = L"Project"; // TODO: localizable
int nextProjectIndex = 0;
for (const auto& proj : projects)
{
const std::wstring& name = proj.name;
if (name.starts_with(defaultNamePrefix))
{
try
{
int index = std::stoi(name.substr(defaultNamePrefix.length() + 1));
if (nextProjectIndex < index)
{
nextProjectIndex = index;
}
}
catch (std::exception) {}
}
}
std::wstring projectName = defaultNamePrefix + L" " + std::to_wstring(nextProjectIndex + 1);
// create new project
time_t creationTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
Project project{ .id = CreateGuidString(), .name = projectName, .creationTime = creationTime };
Project project{ .id = CreateGuidString(), .name = ProjectNameUtils::CreateProjectName(projects), .creationTime = creationTime };
Logger::trace(L"Creating project {}:{}", project.name, project.id);
// save monitor configuration
@@ -159,7 +126,8 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cm
}
projects.push_back(project);
json::to_file(fileName, JsonUtils::ProjectsListJSON::ToJson(projects));
ProjectsJsonUtils::Write(fileName, projects);
Logger::trace(L"Project {}:{} created", project.name, project.id);
return 0;
}