Skip to content

Commit

Permalink
allow moving entire folder between workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Jan 3, 2024
1 parent 3f3f2ab commit c72e569
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
17 changes: 10 additions & 7 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ void _BrowserWindow::refreshIntern(bool withRetry)
{
try {
NetworkService::refreshLogin();
NetworkResourceService::invalidateCache();

std::vector<NetworkResourceRawTO> rawTOs;
bool success = NetworkService::getNetworkResources(rawTOs, withRetry);
Expand Down Expand Up @@ -272,7 +271,7 @@ void _BrowserWindow::processToolbar()
ImGui::BeginDisabled(!isOwner(_selectedTreeTO));
WorkspaceId targetWorkspaceId{.resourceType = _currentWorkspace.resourceType, .workspaceType = 2 - _currentWorkspace.workspaceType};
if (AlienImGui::ToolbarButton(ICON_FA_EXCHANGE_ALT)) {
onMoveItem(_selectedTreeTO->getLeaf().rawTO, _currentWorkspace, targetWorkspaceId);
onMoveItem(_selectedTreeTO, _currentWorkspace, targetWorkspaceId);
}
ImGui::EndDisabled();
AlienImGui::Tooltip("Move to " + workspaceTypeToString.at(targetWorkspaceId.workspaceType) + " workspace");
Expand Down Expand Up @@ -1212,17 +1211,21 @@ void _BrowserWindow::onDownloadItem(BrowserLeaf const& leaf)
});
}

void _BrowserWindow::onMoveItem(NetworkResourceRawTO const& rawTO, WorkspaceId const& sourceId, WorkspaceId const& targetId)
void _BrowserWindow::onMoveItem(NetworkResourceTreeTO const& treeTO, WorkspaceId const& sourceId, WorkspaceId const& targetId)
{
auto& source = _workspaces.at(sourceId);
auto findResult = std::ranges::find_if(source.rawTOs, [&](NetworkResourceRawTO const& otherRawTO) { return otherRawTO->id == rawTO->id; });
if (findResult != source.rawTOs.end()) {
source.rawTOs.erase(findResult);
auto rawTOs = NetworkResourceService::getMatchingRawTOs(treeTO, source.rawTOs);

for (auto const& rawTO : rawTOs) {
auto findResult = std::ranges::find_if(source.rawTOs, [&](NetworkResourceRawTO const& otherRawTO) { return otherRawTO->id == rawTO->id; });
if (findResult != source.rawTOs.end()) {
source.rawTOs.erase(findResult);
}
}
createTreeTOs(source);

auto& target = _workspaces.at(targetId);
target.rawTOs.emplace_back(rawTO);
target.rawTOs.insert(target.rawTOs.end(), rawTOs.begin(), rawTOs.end());
createTreeTOs(target);
}

Expand Down
2 changes: 1 addition & 1 deletion source/Gui/BrowserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class _BrowserWindow : public _AlienWindow
void sortUserList();

void onDownloadItem(BrowserLeaf const& leaf);
void onMoveItem(NetworkResourceRawTO const& rawTO, WorkspaceId const& sourceId, WorkspaceId const& targetId);
void onMoveItem(NetworkResourceTreeTO const& treeTO, WorkspaceId const& sourceId, WorkspaceId const& targetId);
void onDeleteItem(BrowserLeaf const& leaf);
void onToggleLike(NetworkResourceTreeTO const& to, int emojiType);
void onExpandFolders();
Expand Down
42 changes: 23 additions & 19 deletions source/Network/NetworkResourceService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ std::vector<NetworkResourceTreeTO> NetworkResourceService::createTreeTOs(
std::vector<NetworkResourceRawTO> const& rawTOs,
std::set<std::vector<std::string>> const& collapsedFolderNames)
{
NetworkResourceService::invalidateCache();

std::list<NetworkResourceTreeTO> treeToList;
for (auto const& rawTO : rawTOs) {

Expand Down Expand Up @@ -240,27 +242,36 @@ std::vector<NetworkResourceRawTO> NetworkResourceService::getMatchingRawTOs(Netw
{
if (treeTO->isLeaf()) {
return {treeTO->getLeaf().rawTO};
}
auto findResult = _treeTOtoRawTOcache.find(treeTO);
if (findResult != _treeTOtoRawTOcache.end()) {
return findResult->second;
}
std::vector<NetworkResourceRawTO> result;
for (auto const& [index, rawTO] : rawTOs | boost::adaptors::indexed(0)) {
auto folderNames = separateFolderNames(rawTO->resourceName);
if (contains(folderNames, treeTO->folderNames)) {
result.emplace_back(rawTO);
} else {
auto findResult = _treeTOtoRawTOcache.find(treeTO);
if (findResult != _treeTOtoRawTOcache.end()) {
return findResult->second;
}
std::vector<NetworkResourceRawTO> result;
for (auto const& [index, rawTO] : rawTOs | boost::adaptors::indexed(0)) {
auto folderNames = getFolderNames(rawTO->resourceName);
if (contains(folderNames, treeTO->folderNames)) {
result.emplace_back(rawTO);
}
}
_treeTOtoRawTOcache.emplace(treeTO, result);
return result;
}
_treeTOtoRawTOcache.emplace(treeTO, result);
return result;
}

void NetworkResourceService::invalidateCache()
{
_treeTOtoRawTOcache.clear();
}

std::vector<std::string> NetworkResourceService::getFolderNames(std::string const& resourceName)
{
std::vector<std::string> result;
boost::split(result, resourceName, boost::is_any_of("/"));
result.pop_back();
return result;
}

std::set<std::vector<std::string>> NetworkResourceService::getFolderNames(std::vector<NetworkResourceRawTO> const& rawTOs, int minNesting)
{
std::set<std::vector<std::string>> result;
Expand All @@ -283,13 +294,6 @@ std::string NetworkResourceService::concatenateFolderName(std::vector<std::strin
return result;
}

std::vector<std::string> NetworkResourceService::separateFolderNames(std::string const& concatenatedFolderName)
{
std::vector<std::string> result;
boost::split(result, concatenatedFolderName, boost::is_any_of("/"));
return result;
}

std::string NetworkResourceService::convertFolderNamesToSettings(std::set<std::vector<std::string>> const& data)
{
std::vector<std::string> parts;
Expand Down
2 changes: 1 addition & 1 deletion source/Network/NetworkResourceService.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class NetworkResourceService
static std::vector<NetworkResourceRawTO> getMatchingRawTOs(NetworkResourceTreeTO const& treeTO, std::vector<NetworkResourceRawTO> const& rawTOs);
static void invalidateCache(); //invalidate cache for getMatchingRawTOs

static std::vector<std::string> getFolderNames(std::string const& resourceName);
static std::set<std::vector<std::string>> getFolderNames(std::vector<NetworkResourceRawTO> const& browserData, int minNesting = 2);

static std::string concatenateFolderName(std::vector<std::string> const& folderNames, bool withSlashAtTheEnd);
static std::vector<std::string> separateFolderNames(std::string const& concatenatedFolderName);

static std::string convertFolderNamesToSettings(std::set<std::vector<std::string>> const& data);
static std::set<std::vector<std::string>> convertSettingsToFolderNames(std::string const& data);
Expand Down

0 comments on commit c72e569

Please sign in to comment.