change snapshot project saving

https://github.com/JaneaSystems/PowerToys-DevProjects/issues/14
This commit is contained in:
seraphima
2024-07-04 15:57:25 +02:00
parent d0775dac28
commit 6516af8a7a
4 changed files with 20 additions and 42 deletions

View File

@@ -42,4 +42,16 @@ namespace ProjectsJsonUtils
Logger::error("Error writing projects file. {}", ex.what());
}
}
inline void Write(const std::wstring& fileName, const Project& project)
{
try
{
json::to_file(fileName, JsonUtils::ProjectJSON::ToJson(project));
}
catch (std::exception ex)
{
Logger::error("Error writing projects file. {}", ex.what());
}
}
}

View File

@@ -1,35 +0,0 @@
#pragma once
#include <projects-common/Data.h>
#include <common/utils/resources.h>
#include "Generated Files/resource.h"
namespace ProjectNameUtils
{
inline std::wstring CreateProjectName(const std::vector<Project>& projects)
{
std::wstring defaultNamePrefix = GET_RESOURCE_STRING(IDS_DEFAULTPROJECTNAMEPREFIX);
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

@@ -7,7 +7,6 @@
#include <projects-common/MonitorUtils.h>
#include <JsonUtils.h>
#include <NameUtils.h>
#include <SnapshotUtils.h>
#include <common/utils/gpo.h>
@@ -45,12 +44,9 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cm
fileName = fileNameParam;
}
// read previously saved projects
std::vector<Project> projects = ProjectsJsonUtils::Read(fileName);
// create new project
time_t creationTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
Project project{ .id = CreateGuidString(), .name = ProjectNameUtils::CreateProjectName(projects), .creationTime = creationTime };
Project project{ .id = CreateGuidString(), .creationTime = creationTime };
Logger::trace(L"Creating project {}:{}", project.name, project.id);
project.monitors = MonitorUtils::IdentifyMonitors();
@@ -69,8 +65,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cm
return monitorNumber;
});
projects.push_back(project);
ProjectsJsonUtils::Write(fileName, projects);
ProjectsJsonUtils::Write(JsonUtils::TempProjectsFile(), project);
Logger::trace(L"Project {}:{} created", project.name, project.id);
return 0;

View File

@@ -81,6 +81,12 @@ namespace JsonUtils
return std::wstring(settingsFolderPath) + L"\\projects.json";
}
inline std::wstring TempProjectsFile()
{
std::wstring settingsFolderPath = PTSettingsHelper::get_module_save_folder_location(NonLocalizable::ModuleKey);
return std::wstring(settingsFolderPath) + L"\\temp-project.json";
}
namespace ProjectJSON
{
namespace ApplicationJSON