From 4cf4b6a3689ddfbda73cc627f35c5d74a4835484 Mon Sep 17 00:00:00 2001 From: John Preston <johnprestonmail@gmail.com> Date: Tue, 26 Jul 2022 17:45:41 +0300 Subject: [PATCH] Optimize emoji / stickers update requests. --- Telegram/SourceFiles/api/api_updates.cpp | 6 ++- Telegram/SourceFiles/apiwrap.cpp | 10 ++-- .../SourceFiles/boxes/sticker_set_box.cpp | 5 +- Telegram/SourceFiles/boxes/stickers_box.cpp | 10 ++-- .../chat_helpers/emoji_list_widget.cpp | 2 + .../chat_helpers/stickers_list_footer.cpp | 11 ++-- .../chat_helpers/stickers_list_footer.h | 5 +- .../chat_helpers/stickers_list_widget.cpp | 14 ++--- .../chat_helpers/tabbed_selector.cpp | 4 +- .../data/stickers/data_stickers.cpp | 52 ++++++++++++------- .../SourceFiles/data/stickers/data_stickers.h | 23 ++++---- .../SourceFiles/history/history_widget.cpp | 1 + .../history_view_compose_controls.cpp | 1 + Telegram/SourceFiles/main/main_session.cpp | 4 +- .../mac/touchbar/items/mac_scrubber_item.mm | 7 ++- 15 files changed, 96 insertions(+), 59 deletions(-) diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp index dfb2f0ccb..120c35b84 100644 --- a/Telegram/SourceFiles/api/api_updates.cpp +++ b/Telegram/SourceFiles/api/api_updates.cpp @@ -2336,7 +2336,11 @@ void Updates::feedUpdate(const MTPUpdate &update) { stickers.setsOrderRef() = std::move(result); session().local().writeInstalledStickers(); } - stickers.notifyUpdated(); + stickers.notifyUpdated(isEmoji + ? Data::StickersType::Emoji + : isMasks + ? Data::StickersType::Masks + : Data::StickersType::Stickers); } } break; diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index b9204877a..4f316cfe0 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -1679,7 +1679,7 @@ void ApiWrap::saveStickerSets( if (writeFaved) { storage.writeFavedStickers(); } - _session->data().stickers().notifyUpdated(); + _session->data().stickers().notifyUpdated(type); if (setDisenableRequests.empty()) { stickersSaveOrder(); @@ -2555,7 +2555,7 @@ void ApiWrap::setGroupStickerSet( megagroup->inputChannel, Data::InputStickerSet(set) )).send(); - _session->data().stickers().notifyUpdated(); + _session->data().stickers().notifyUpdated(Data::StickersType::Stickers); } std::vector<not_null<DocumentData*>> *ApiWrap::stickersByEmoji( @@ -2595,7 +2595,8 @@ std::vector<not_null<DocumentData*>> *ApiWrap::stickersByEmoji( } entry.hash = data.vhash().v; entry.received = crl::now(); - _session->data().stickers().notifyUpdated(); + _session->data().stickers().notifyUpdated( + Data::StickersType::Stickers); }).send(); } if (it == _stickersByEmoji.end()) { @@ -2864,7 +2865,8 @@ void ApiWrap::readFeaturedSets() { MTP_vector<MTPlong>(wrappedIds)); request(std::move(requestData)).done([=] { local().writeFeaturedStickers(); - _session->data().stickers().notifyUpdated(); + _session->data().stickers().notifyUpdated( + Data::StickersType::Stickers); }).send(); _session->data().stickers().setFeaturedSetsUnreadCount(count); diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.cpp b/Telegram/SourceFiles/boxes/sticker_set_box.cpp index d97816e27..4cf6c7bf7 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.cpp +++ b/Telegram/SourceFiles/boxes/sticker_set_box.cpp @@ -301,6 +301,7 @@ void StickerSetBox::prepare() { object_ptr<Inner>(this, _controller, _set, _type), st::stickersScroll); _controller->session().data().stickers().updated( + _type ) | rpl::start_with_next([=] { updateButtons(); }, lifetime()); @@ -369,7 +370,7 @@ void StickerSetBox::prepare() { } } - _controller->session().data().stickers().notifyUpdated(); + _controller->session().data().stickers().notifyUpdated(type); closeBox(); }, lifetime()); @@ -785,7 +786,7 @@ void StickerSetBox::Inner::installDone( } else { storage.writeInstalledStickers(); } - stickers.notifyUpdated(); + stickers.notifyUpdated(type); } _setInstalled.fire_copy(_setId); } diff --git a/Telegram/SourceFiles/boxes/stickers_box.cpp b/Telegram/SourceFiles/boxes/stickers_box.cpp index ed8caa6b9..d0766585f 100644 --- a/Telegram/SourceFiles/boxes/stickers_box.cpp +++ b/Telegram/SourceFiles/boxes/stickers_box.cpp @@ -619,9 +619,10 @@ void StickersBox::prepare() { setDimensions(st::boxWideWidth, st::boxMaxListHeight); session().data().stickers().updated( - ) | rpl::start_with_next( - [this] { handleStickersUpdated(); }, - lifetime()); + _isMasks ? Data::StickersType::Masks : Data::StickersType::Stickers + ) | rpl::start_with_next([=] { + handleStickersUpdated(); + }, lifetime()); session().api().updateStickers(); session().api().updateMasks(); @@ -871,7 +872,8 @@ void StickersBox::installSet(uint64 setId) { } } -void StickersBox::installDone(const MTPmessages_StickerSetInstallResult &result) { +void StickersBox::installDone( + const MTPmessages_StickerSetInstallResult &result) { if (result.type() == mtpc_messages_stickerSetInstallResultArchive) { session().data().stickers().applyArchivedResult( result.c_messages_stickerSetInstallResultArchive()); diff --git a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp index 6b7bd48b1..5130fe6e5 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp @@ -393,6 +393,7 @@ EmojiListWidget::EmojiListWidget( }, lifetime()); controller->session().data().stickers().updated( + Data::StickersType::Emoji ) | rpl::start_with_next([=] { refreshCustom(); resizeToWidth(width()); @@ -1372,6 +1373,7 @@ void EmojiListWidget::refreshCustom() { _footer->refreshIcons( fillIcons(), + currentSet(getVisibleTop()), nullptr, ValidateIconAnimations::None); update(); diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_footer.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_footer.cpp index 22682db7e..83e88f6cf 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_footer.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_footer.cpp @@ -643,7 +643,7 @@ void StickersListFooter::resizeEvent(QResizeEvent *e) { if (_searchField) { resizeSearchControls(); } - refreshIconsGeometry(ValidateIconAnimations::None); + refreshIconsGeometry(_activeByScrollId, ValidateIconAnimations::None); } void StickersListFooter::resizeSearchControls() { @@ -929,6 +929,7 @@ auto StickersListFooter::getLottieRenderer() void StickersListFooter::refreshIcons( std::vector<StickerIcon> icons, + uint64 activeSetId, Fn<std::shared_ptr<Lottie::FrameRenderer>()> renderer, ValidateIconAnimations animations) { _renderer = renderer @@ -955,7 +956,7 @@ void StickersListFooter::refreshIcons( } _icons = std::move(icons); - refreshIconsGeometry(animations); + refreshIconsGeometry(activeSetId, animations); } void StickersListFooter::refreshScrollableDimensions() { @@ -969,6 +970,7 @@ void StickersListFooter::refreshScrollableDimensions() { } void StickersListFooter::refreshIconsGeometry( + uint64 activeSetId, ValidateIconAnimations animations) { _selected = _pressed = SpecialOver::None; _iconState.x.finish(); @@ -990,11 +992,14 @@ void StickersListFooter::refreshIconsGeometry( refreshScrollableDimensions(); refreshSubiconsGeometry(); _iconState.selected = _subiconState.selected = -1; - validateSelectedIcon(_activeByScrollId, animations); + validateSelectedIcon(activeSetId, animations); update(); } void StickersListFooter::refreshSubiconsGeometry() { + if (_barSelection) { + return; + } using Section = Ui::Emoji::Section; _subiconState.x.finish(); _subiconState.animationStart = 0; diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_footer.h b/Telegram/SourceFiles/chat_helpers/stickers_list_footer.h index f8a5bc761..2e95b877e 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_footer.h +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_footer.h @@ -92,6 +92,7 @@ public: ValidateIconAnimations animations); void refreshIcons( std::vector<StickerIcon> icons, + uint64 activeSetId, Fn<std::shared_ptr<Lottie::FrameRenderer>()> renderer, ValidateIconAnimations animations); [[nodiscard]] bool hasOnlyFeaturedSets() const; @@ -179,7 +180,9 @@ private: void validateIconWebmAnimation(const StickerIcon &icon); void validateIconAnimation(const StickerIcon &icon); - void refreshIconsGeometry(ValidateIconAnimations animations); + void refreshIconsGeometry( + uint64 activeSetId, + ValidateIconAnimations animations); void refreshSubiconsGeometry(); void refreshScrollableDimensions(); void updateSelected(); diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index a5b937d51..fb1461fc3 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -209,11 +209,8 @@ StickersListWidget::StickersListWidget( }, lifetime()); session().data().stickers().recentUpdated( - ) | rpl::start_with_next([=](Data::Stickers::Recent recent) { - const auto attached = (recent == Data::Stickers::Recent::Attached); - if (attached != _isMasks) { - return; - } + _isMasks ? Data::StickersType::Masks : Data::StickersType::Stickers + ) | rpl::start_with_next([=] { refreshRecent(); }, lifetime()); @@ -2547,6 +2544,7 @@ void StickersListWidget::refreshIcons(ValidateIconAnimations animations) { if (_footer) { _footer->refreshIcons( fillIcons(), + currentSet(getVisibleTop()), [=] { return getLottieRenderer(); }, animations); } @@ -2725,7 +2723,9 @@ object_ptr<Ui::BoxContent> MakeConfirmRemoveSetBox( if (removeIndex >= 0) { orderRef.removeAt(removeIndex); } - if (set->flags & SetFlag::Masks) { + if (set->type() == Data::StickersType::Emoji) { + session->local().writeInstalledCustomEmoji(); + } else if (set->type() == Data::StickersType::Masks) { session->local().writeInstalledMasks(); } else { session->local().writeInstalledStickers(); @@ -2733,7 +2733,7 @@ object_ptr<Ui::BoxContent> MakeConfirmRemoveSetBox( if (writeRecent) { session->saveSettings(); } - session->data().stickers().notifyUpdated(); + session->data().stickers().notifyUpdated(set->type()); } }, .cancelled = [=](Fn<void()> &&close) { diff --git a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp index ae0b4ffc3..023127c06 100644 --- a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp +++ b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp @@ -399,7 +399,9 @@ TabbedSelector::TabbedSelector( rpl::merge( session().premiumPossibleValue() | rpl::to_empty, - session().data().stickers().updated() + session().data().stickers().updated(hasMasksTab() + ? Data::StickersType::Masks + : Data::StickersType::Stickers) ) | rpl::start_with_next([=] { refreshStickers(); }, lifetime()); diff --git a/Telegram/SourceFiles/data/stickers/data_stickers.cpp b/Telegram/SourceFiles/data/stickers/data_stickers.cpp index 467a70765..b9b489d5b 100644 --- a/Telegram/SourceFiles/data/stickers/data_stickers.cpp +++ b/Telegram/SourceFiles/data/stickers/data_stickers.cpp @@ -146,22 +146,32 @@ Main::Session &Stickers::session() const { return _owner->session(); } -void Stickers::notifyUpdated() { - _updated.fire({}); +void Stickers::notifyUpdated(StickersType type) { + _updated.fire_copy(type); } -rpl::producer<> Stickers::updated() const { +rpl::producer<StickersType> Stickers::updated() const { return _updated.events(); } -void Stickers::notifyRecentUpdated(Recent recent) { - _recentUpdated.fire(std::move(recent)); +rpl::producer<> Stickers::updated(StickersType type) const { + using namespace rpl::mappers; + return updated() | rpl::filter(_1 == type) | rpl::to_empty; } -rpl::producer<Stickers::Recent> Stickers::recentUpdated() const { +void Stickers::notifyRecentUpdated(StickersType type) { + _recentUpdated.fire_copy(type); +} + +rpl::producer<StickersType> Stickers::recentUpdated() const { return _recentUpdated.events(); } +rpl::producer<> Stickers::recentUpdated(StickersType type) const { + using namespace rpl::mappers; + return recentUpdated() | rpl::filter(_1 == type) | rpl::to_empty; +} + void Stickers::notifySavedGifsUpdated() { _savedGifsUpdated.fire({}); } @@ -297,7 +307,7 @@ void Stickers::incrementSticker(not_null<DocumentData*> document) { if (writeRecentStickers) { session().local().writeRecentStickers(); } - notifyRecentUpdated(); + notifyRecentUpdated(StickersType::Stickers); } void Stickers::addSavedGif( @@ -392,8 +402,12 @@ void Stickers::applyArchivedResult( //Ui::show( // Box<StickersBox>(archived, &session()), // Ui::LayerOption::KeepOther); - - notifyUpdated(); + if (stickersCount) { + notifyUpdated(StickersType::Stickers); + } + if (masksCount) { + notifyUpdated(StickersType::Masks); + } } void Stickers::installLocally(uint64 setId) { @@ -458,7 +472,7 @@ void Stickers::installLocally(uint64 setId) { } } } - notifyUpdated(); + notifyUpdated(set->type()); } void Stickers::undoInstallLocally(uint64 setId) { @@ -479,7 +493,7 @@ void Stickers::undoInstallLocally(uint64 setId) { } session().local().writeInstalledStickers(); - notifyUpdated(); + notifyUpdated(set->type()); Ui::show( Ui::MakeInformBox(tr::lng_stickers_not_found()), @@ -592,7 +606,7 @@ void Stickers::setIsFaved( return; } session().local().writeFavedStickers(); - notifyUpdated(); + notifyUpdated(StickersType::Stickers); notifyStickerSetInstalled(FavedSetId); } @@ -642,13 +656,13 @@ void Stickers::requestSetToPushFaved( void Stickers::removeFromRecentSet(not_null<DocumentData*> document) { RemoveFromSet(setsRef(), document, CloudRecentSetId); session().local().writeRecentStickers(); - notifyRecentUpdated(); + notifyRecentUpdated(StickersType::Stickers); } void Stickers::setIsNotFaved(not_null<DocumentData*> document) { RemoveFromSet(setsRef(), document, FavedSetId); session().local().writeFavedStickers(); - notifyUpdated(); + notifyUpdated(StickersType::Stickers); } void Stickers::setFaved( @@ -772,7 +786,7 @@ void Stickers::somethingReceived( ).arg(counted)); } - notifyUpdated(); + notifyUpdated(type); } void Stickers::setPackAndEmoji( @@ -925,7 +939,9 @@ void Stickers::specialSetReceived( default: Unexpected("setId in SpecialSetReceived()"); } - notifyUpdated(); + notifyUpdated((setId == CloudRecentAttachedSetId) + ? StickersType::Masks + : StickersType::Stickers); } void Stickers::featuredSetsReceived( @@ -1079,7 +1095,7 @@ void Stickers::featuredReceived( session().local().writeFeaturedStickers(); } - notifyUpdated(); + notifyUpdated(type); } void Stickers::gifsReceived(const QVector<MTPDocument> &items, uint64 hash) { @@ -1501,7 +1517,7 @@ void Stickers::feedSetStickers( session().local().writeArchivedStickers(); } } - notifyUpdated(); + notifyUpdated(set->type()); } void Stickers::feedSetCovers( diff --git a/Telegram/SourceFiles/data/stickers/data_stickers.h b/Telegram/SourceFiles/data/stickers/data_stickers.h index 782d87903..755bc9601 100644 --- a/Telegram/SourceFiles/data/stickers/data_stickers.h +++ b/Telegram/SourceFiles/data/stickers/data_stickers.h @@ -60,15 +60,12 @@ public: // For setting up megagroup sticker set. static constexpr auto MegagroupSetId = 0xFFFFFFFFFFFFFFEFULL; - enum Recent { - Regular, - Attached, - }; - - void notifyUpdated(); - [[nodiscard]] rpl::producer<> updated() const; - void notifyRecentUpdated(Recent recent = Recent::Regular); - [[nodiscard]] rpl::producer<Recent> recentUpdated() const; + void notifyUpdated(StickersType type); + [[nodiscard]] rpl::producer<StickersType> updated() const; + [[nodiscard]] rpl::producer<> updated(StickersType type) const; + void notifyRecentUpdated(StickersType type); + [[nodiscard]] rpl::producer<StickersType> recentUpdated() const; + [[nodiscard]] rpl::producer<> recentUpdated(StickersType type) const; void notifySavedGifsUpdated(); [[nodiscard]] rpl::producer<> savedGifsUpdated() const; void notifyStickerSetInstalled(uint64 setId); @@ -89,7 +86,7 @@ public: } void setLastRecentUpdate(crl::time update) { if (update) { - notifyRecentUpdated(); + notifyRecentUpdated(StickersType::Stickers); } _lastRecentUpdate = update; } @@ -110,7 +107,7 @@ public: } void setLastRecentAttachedUpdate(crl::time update) { if (update) { - notifyRecentUpdated(Recent::Attached); + notifyRecentUpdated(StickersType::Masks); } _lastRecentAttachedUpdate = update; } @@ -293,8 +290,8 @@ private: StickersType type); const not_null<Session*> _owner; - rpl::event_stream<> _updated; - rpl::event_stream<Recent> _recentUpdated; + rpl::event_stream<StickersType> _updated; + rpl::event_stream<StickersType> _recentUpdated; rpl::event_stream<> _savedGifsUpdated; rpl::event_stream<uint64> _stickerSetInstalled; rpl::event_stream<uint64> _emojiSetInstalled; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index fd4a23c29..9cc03b32d 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1335,6 +1335,7 @@ int HistoryWidget::itemTopForHighlight( void HistoryWidget::start() { session().data().stickers().updated( + Data::StickersType::Stickers ) | rpl::start_with_next([=] { updateStickersByEmoji(); }, lifetime()); diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp index cd4f94a06..9f237f7d1 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -1531,6 +1531,7 @@ void ComposeControls::initAutocomplete() { }, _autocomplete->lifetime()); _window->session().data().stickers().updated( + Data::StickersType::Stickers ) | rpl::start_with_next([=] { updateStickersByEmoji(); }, _autocomplete->lifetime()); diff --git a/Telegram/SourceFiles/main/main_session.cpp b/Telegram/SourceFiles/main/main_session.cpp index 02144bc47..438fa6009 100644 --- a/Telegram/SourceFiles/main/main_session.cpp +++ b/Telegram/SourceFiles/main/main_session.cpp @@ -164,7 +164,9 @@ Session::Session( local().readRecentMasks(); local().readFavedStickers(); local().readSavedGifs(); - data().stickers().notifyUpdated(); + data().stickers().notifyUpdated(Data::StickersType::Stickers); + data().stickers().notifyUpdated(Data::StickersType::Masks); + data().stickers().notifyUpdated(Data::StickersType::Emoji); data().stickers().notifySavedGifsUpdated(); }); diff --git a/Telegram/SourceFiles/platform/mac/touchbar/items/mac_scrubber_item.mm b/Telegram/SourceFiles/platform/mac/touchbar/items/mac_scrubber_item.mm index f6cecda06..ba214a64f 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar/items/mac_scrubber_item.mm +++ b/Telegram/SourceFiles/platform/mac/touchbar/items/mac_scrubber_item.mm @@ -577,11 +577,10 @@ void AppendEmojiPacks( rpl::merge( rpl::merge( - _session->data().stickers().updated(), + _session->data().stickers().updated( + Data::StickersType::Stickers), _session->data().stickers().recentUpdated( - ) | rpl::filter([](Data::Stickers::Recent recent) { - return (recent != Data::Stickers::Recent::Attached); - }) | rpl::to_empty + Data::StickersType::Stickers) ) | rpl::map_to(ScrubberItemType::Sticker), rpl::merge( Core::App().settings().recentEmojiUpdated(),