Skip to content

Commit

Permalink
make edit resources asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 22, 2024
1 parent 1908bf2 commit d741f96
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 20 deletions.
28 changes: 9 additions & 19 deletions source/Gui/EditSimulationDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "HelpStrings.h"
#include "StyleRepository.h"
#include "GenericMessageDialog.h"
#include "NetworkTransferController.h"
#include "OverlayController.h"

void EditSimulationDialog::openForLeaf(NetworkResourceTreeTO const& treeTO)
Expand Down Expand Up @@ -72,13 +73,8 @@ void EditSimulationDialog::processForLeaf()
ImGui::BeginDisabled(_newName.empty());
if (AlienImGui::Button("OK")) {
if (ValidationService::isStringValidForDatabase(_newName) && ValidationService::isStringValidForDatabase(_newDescription)) {
delayedExecution([rawTO = rawTO, resourceTypeString = resourceTypeString, this] {
if (!NetworkService::get().editResource(rawTO->id, _newName, _newDescription)) {
showMessage("Error", "Failed to edit " + resourceTypeString + ".");
}
BrowserWindow::get().onRefresh();
});
printOverlayMessage("Applying changes ...");
NetworkTransferController::get().onEdit(
EditNetworkResourceRequestData{.resourceId = rawTO->id, .newName = _newName, .newDescription = _newDescription});
close();
} else {
showMessage("Error", Const::NotAllowedCharacters);
Expand All @@ -105,18 +101,12 @@ void EditSimulationDialog::processForFolder()
ImGui::BeginDisabled(_newName.empty());
if (AlienImGui::Button("OK")) {
if (ValidationService::isStringValidForDatabase(_newName)) {
delayedExecution([this] {
for (auto const& rawTO : _rawTOs) {
auto nameWithoutOldFolder = rawTO->resourceName.substr(_origFolderName.size() + 1);
auto newName = NetworkResourceService::get().concatenateFolderName({_newName, nameWithoutOldFolder}, false);
if (!NetworkService::get().editResource(rawTO->id, newName, rawTO->description)) {
showMessage("Error", "Failed to change folder name.");
break;
}
}
BrowserWindow::get().onRefresh();
});
printOverlayMessage("Applying changes ...");
for (auto const& rawTO : _rawTOs) {
auto nameWithoutOldFolder = rawTO->resourceName.substr(_origFolderName.size() + 1);
auto newName = NetworkResourceService::get().concatenateFolderName({_newName, nameWithoutOldFolder}, false);
NetworkTransferController::get().onEdit(
EditNetworkResourceRequestData{.resourceId = rawTO->id, .newName = newName, .newDescription = rawTO->description});
}
close();
} else {
showMessage("Error", Const::NotAllowedCharacters);
Expand Down
2 changes: 2 additions & 0 deletions source/Gui/MainLoopController.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "MainLoopController.h"

#include <thread>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

Expand Down
18 changes: 18 additions & 0 deletions source/Gui/NetworkTransferController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void NetworkTransferController::init(SimulationFacade simulationFacade, Persiste
_uploadProcessor = _TaskProcessor::createTaskProcessor(_persisterFacade);
_replaceProcessor = _TaskProcessor::createTaskProcessor(_persisterFacade);
_deleteProcessor = _TaskProcessor::createTaskProcessor(_persisterFacade);
_editProcessor = _TaskProcessor::createTaskProcessor(_persisterFacade);
}

void NetworkTransferController::onDownload(DownloadNetworkResourceRequestData const& requestData)
Expand Down Expand Up @@ -134,10 +135,27 @@ void NetworkTransferController::onDelete(DeleteNetworkResourceRequestData const&
[](auto const& errors) { GenericMessageDialog::get().information("Error", errors); });
}

void NetworkTransferController::onEdit(EditNetworkResourceRequestData const& requestData)
{
printOverlayMessage("Applying changes ...");

_editProcessor->executeTask(
[&](auto const& senderId) {
return _persisterFacade->scheduleEditNetworkResource(
SenderInfo{.senderId = senderId, .wishResultData = true, .wishErrorInfo = true}, requestData);
},
[&](auto const& requestId) {
_persisterFacade->fetchEditNetworkResourcesData(requestId);
BrowserWindow::get().onRefresh();
},
[](auto const& errors) { GenericMessageDialog::get().information("Error", errors); });
}

void NetworkTransferController::process()
{
_downloadProcessor->process();
_uploadProcessor->process();
_replaceProcessor->process();
_deleteProcessor->process();
_editProcessor->process();
}
2 changes: 2 additions & 0 deletions source/Gui/NetworkTransferController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class NetworkTransferController : public MainLoopEntity<SimulationFacade, Persis
void onUpload(UploadNetworkResourceRequestData const& requestData);
void onReplace(ReplaceNetworkResourceRequestData const& requestData);
void onDelete(DeleteNetworkResourceRequestData const& requestData);
void onEdit(EditNetworkResourceRequestData const& requestData);

private:
void init(SimulationFacade simulationFacade, PersisterFacade persisterFacade) override;
Expand All @@ -37,4 +38,5 @@ class NetworkTransferController : public MainLoopEntity<SimulationFacade, Persis
TaskProcessor _uploadProcessor;
TaskProcessor _replaceProcessor;
TaskProcessor _deleteProcessor;
TaskProcessor _editProcessor;
};
17 changes: 17 additions & 0 deletions source/PersisterImpl/PersisterWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ void _PersisterWorker::processRequests(std::unique_lock<std::mutex>& lock)
if (auto const& concreteRequest = std::dynamic_pointer_cast<_DeleteNetworkResourceRequest>(request)) {
processingResult = processRequest(lock, concreteRequest);
}
if (auto const& concreteRequest = std::dynamic_pointer_cast<_EditNetworkResourceRequest>(request)) {
processingResult = processRequest(lock, concreteRequest);
}
auto inProgressJobsIter = std::ranges::find_if(
_inProgressRequests, [&](PersisterRequest const& otherRequest) { return otherRequest->getRequestId() == request->getRequestId(); });
_inProgressRequests.erase(inProgressJobsIter);
Expand Down Expand Up @@ -554,3 +557,17 @@ _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest

return std::make_shared<_DeleteNetworkResourceRequestResult>(request->getRequestId(), DeleteNetworkResourceResultData{});
}

_PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest(std::unique_lock<std::mutex>& lock, EditNetworkResourceRequest const& request)
{
UnlockGuard unlockGuard(lock);

auto const& requestData = request->getData();

if (!NetworkService::get().editResource(requestData.resourceId, requestData.newName, requestData.newDescription)) {
return std::make_shared<_PersisterRequestError>(
request->getRequestId(), request->getSenderInfo().senderId, PersisterErrorInfo{"Failed to edit item. Please try again later."});
}

return std::make_shared<_EditNetworkResourceRequestResult>(request->getRequestId(), EditNetworkResourceResultData{});
}
1 change: 1 addition & 0 deletions source/PersisterImpl/PersisterWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class _PersisterWorker
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, ReplaceNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, GetUserNamesForEmojiRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, DeleteNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, EditNetworkResourceRequest const& request);

SimulationFacade _simulationFacade;

Expand Down
7 changes: 6 additions & 1 deletion source/PersisterInterface/EditNetworkResourceRequestData.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#pragma once
#include <string>

struct EditNetworkResourceRequestData
{};
{
std::string resourceId;
std::string newName;
std::string newDescription;
};

0 comments on commit d741f96

Please sign in to comment.