Allowed to save not loaded photos from bulk download menu item.

This commit is contained in:
23rd 2023-09-06 13:31:14 +03:00
parent ed345e0823
commit 1b5b9f46d2

View file

@ -35,7 +35,7 @@ namespace Menu {
namespace { namespace {
using Documents = std::vector<std::pair<not_null<DocumentData*>, FullMsgId>>; using Documents = std::vector<std::pair<not_null<DocumentData*>, FullMsgId>>;
using Photos = std::vector<std::shared_ptr<Data::PhotoMedia>>; using Photos = std::vector<std::pair<not_null<PhotoData*>, FullMsgId>>;
[[nodiscard]] bool Added( [[nodiscard]] bool Added(
HistoryItem *item, HistoryItem *item,
@ -44,12 +44,8 @@ using Photos = std::vector<std::shared_ptr<Data::PhotoMedia>>;
if (item && !item->forbidsForward()) { if (item && !item->forbidsForward()) {
if (const auto media = item->media()) { if (const auto media = item->media()) {
if (const auto photo = media->photo()) { if (const auto photo = media->photo()) {
if (const auto view = photo->activeMediaView()) { photos.emplace_back(photo, item->fullId());
if (view->loaded()) {
photos.push_back(view);
return true; return true;
}
}
} else if (const auto document = media->document()) { } else if (const auto document = media->document()) {
documents.emplace_back(document, item->fullId()); documents.emplace_back(document, item->fullId());
return true; return true;
@ -71,7 +67,7 @@ void AddAction(
const auto icon = documents.empty() const auto icon = documents.empty()
? &st::menuIconSaveImage ? &st::menuIconSaveImage
: &st::menuIconDownload; : &st::menuIconDownload;
const auto showToast = documents.empty(); const auto shouldShowToast = documents.empty();
const auto weak = base::make_weak(controller); const auto weak = base::make_weak(controller);
const auto saveImages = [=](const QString &folderPath) { const auto saveImages = [=](const QString &folderPath) {
@ -91,21 +87,11 @@ void AddAction(
if (path.isEmpty()) { if (path.isEmpty()) {
return; return;
} }
QDir().mkpath(path); QDir().mkpath(path);
const auto fullPath = [&](int i) {
return filedialogDefaultName(
u"photo_"_q + QString::number(i),
u".jpg"_q,
path);
};
auto lastPath = QString();
for (auto i = 0; i < photos.size(); i++) {
lastPath = fullPath(i + 1);
photos[i]->saveToFile(lastPath);
}
if (showToast) { const auto showToast = !shouldShowToast
? Fn<void(const QString &)>(nullptr)
: [=](const QString &lastPath) {
const auto filter = [lastPath](const auto ...) { const auto filter = [lastPath](const auto ...) {
File::ShowInFolder(lastPath); File::ShowInFolder(lastPath);
return false; return false;
@ -123,6 +109,53 @@ void AddAction(
.st = &st::defaultToast, .st = &st::defaultToast,
.filter = filter, .filter = filter,
}); });
};
auto views = std::vector<std::shared_ptr<Data::PhotoMedia>>();
for (const auto &[photo, fullId] : photos) {
if (const auto view = photo->createMediaView()) {
view->wanted(Data::PhotoSize::Large, fullId);
views.push_back(view);
}
}
const auto finalCheck = [=] {
for (const auto &[photo, _] : photos) {
if (photo->loading()) {
return false;
}
}
return true;
};
const auto saveToFiles = [=] {
const auto fullPath = [&](int i) {
return filedialogDefaultName(
u"photo_"_q + QString::number(i),
u".jpg"_q,
path);
};
auto lastPath = QString();
for (auto i = 0; i < views.size(); i++) {
lastPath = fullPath(i + 1);
views[i]->saveToFile(lastPath);
}
if (showToast) {
showToast(lastPath);
}
};
if (finalCheck()) {
saveToFiles();
} else {
auto lifetime = std::make_shared<rpl::lifetime>();
session->downloaderTaskFinished(
) | rpl::start_with_next([=]() mutable {
if (finalCheck()) {
saveToFiles();
base::take(lifetime)->destroy();
}
}, *lifetime);
} }
}; };
const auto saveDocuments = [=](const QString &folderPath) { const auto saveDocuments = [=](const QString &folderPath) {