2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Search stickers by emoji.

This commit is contained in:
John Preston
2023-01-24 18:46:16 +04:00
parent 65b1a0c9a4
commit c48ac28204
11 changed files with 161 additions and 85 deletions

View File

@@ -365,10 +365,8 @@ inline int indexOfInFirstN(const T &v, const U &elem, int last) {
}
FieldAutocomplete::StickerRows FieldAutocomplete::getStickerSuggestions() {
const auto list = _controller->session().data().stickers().getListByEmoji(
_emoji,
_stickersSeed
);
const auto data = &_controller->session().data().stickers();
const auto list = data->getListByEmoji({ _emoji }, _stickersSeed);
auto result = ranges::views::all(
list
) | ranges::views::transform([](not_null<DocumentData*> sticker) {

View File

@@ -532,13 +532,19 @@ void StickersListWidget::sendSearchRequest() {
}).handleAllErrors().send();
}
void StickersListWidget::searchForSets(const QString &query) {
void StickersListWidget::searchForSets(
const QString &query,
std::vector<EmojiPtr> emoji) {
const auto cleaned = query.trimmed();
if (cleaned.isEmpty()) {
cancelSetsSearch();
return;
}
_filteredStickers = session().data().stickers().getListByEmoji(
std::move(emoji),
0,
true);
if (_searchQuery != cleaned) {
_search->setLoading(false);
if (const auto requestId = base::take(_searchRequestId)) {
@@ -562,6 +568,7 @@ void StickersListWidget::cancelSetsSearch() {
}
_searchRequestTimer.cancel();
_searchQuery = _searchNextQuery = QString();
_filteredStickers.clear();
_searchCache.clear();
refreshSearchRows(nullptr);
}
@@ -591,6 +598,7 @@ void StickersListWidget::refreshSearchRows(
}
});
fillFilteredStickersRow();
fillLocalSearchRows(_searchNextQuery);
if (!cloudSets && _searchNextQuery.isEmpty()) {
@@ -657,6 +665,27 @@ void StickersListWidget::fillCloudSearchRows(
}
}
void StickersListWidget::fillFilteredStickersRow() {
if (_filteredStickers.empty()) {
return;
}
auto elements = ranges::views::all(
_filteredStickers
) | ranges::views::transform([](not_null<DocumentData*> document) {
return Sticker{ document };
}) | ranges::to_vector;
_searchSets.emplace_back(
SearchEmojiSectionSetId(),
nullptr,
Data::StickersSetFlag::Special,
QString(), // title
QString(), // shortName
_filteredStickers.size(),
false, // externalLayout
std::move(elements));
}
void StickersListWidget::addSearchRow(not_null<StickersSet*> set) {
const auto skipPremium = !session().premiumPossible();
auto elements = PrepareStickers(
@@ -2528,11 +2557,17 @@ void StickersListWidget::beforeHiding() {
void StickersListWidget::setupSearch() {
const auto session = &_controller->session();
_search = MakeSearch(this, st(), [=](std::vector<QString> &&query) {
searchForSets(ranges::accumulate(query, QString(), [](
auto emoji = query | ranges::views::transform([](const QString &k) {
return Ui::Emoji::Find(k);
}) | ranges::views::filter([](EmojiPtr emoji) {
return (emoji != nullptr);
}) | ranges::to_vector;
auto text = ranges::accumulate(query, QString(), [](
QString a,
QString b) {
return a.isEmpty() ? b : (a + ' ' + b);
}));
});
searchForSets(std::move(text), std::move(emoji));
}, session);
}

View File

@@ -89,7 +89,7 @@ public:
uint64 currentSet(int yOffset) const;
void sendSearchRequest();
void searchForSets(const QString &query);
void searchForSets(const QString &query, std::vector<EmojiPtr> emoji);
std::shared_ptr<Lottie::FrameRenderer> getLottieRenderer();
@@ -319,6 +319,7 @@ private:
void searchResultsDone(const MTPmessages_FoundStickerSets &result);
void refreshSearchRows();
void refreshSearchRows(const std::vector<uint64> *cloudSets);
void fillFilteredStickersRow();
void fillLocalSearchRows(const QString &query);
void fillCloudSearchRows(const std::vector<uint64> &cloudSets);
void addSearchRow(not_null<Data::StickersSet*> set);
@@ -387,6 +388,7 @@ private:
std::unique_ptr<StickerPremiumMark> _premiumMark;
std::vector<not_null<DocumentData*>> _filteredStickers;
std::map<QString, std::vector<uint64>> _searchCache;
std::vector<std::pair<uint64, QStringList>> _searchIndex;
base::Timer _searchRequestTimer;