From 554eb3a342f67144fb7fe738e2f9cbc83929fad6 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 5 Nov 2017 21:07:27 +0400 Subject: [PATCH] Move stickers state variables to AuthSessionData. Also allow to click on the selected set when choosing megagroup sticker set and allow to paste a t.me link to the set there. --- Telegram/SourceFiles/apiwrap.cpp | 99 +++--- Telegram/SourceFiles/apiwrap.h | 8 +- Telegram/SourceFiles/app.cpp | 25 +- Telegram/SourceFiles/auth_session.h | 110 ++++++- .../SourceFiles/boxes/sticker_set_box.cpp | 23 +- Telegram/SourceFiles/boxes/sticker_set_box.h | 6 +- Telegram/SourceFiles/boxes/stickers_box.cpp | 284 ++++++++++++------ Telegram/SourceFiles/boxes/stickers_box.h | 35 ++- .../chat_helpers/emoji_list_widget.cpp | 4 +- .../chat_helpers/emoji_list_widget.h | 2 +- .../chat_helpers/field_autocomplete.cpp | 8 +- .../chat_helpers/field_autocomplete.h | 9 +- .../chat_helpers/gifs_list_widget.cpp | 14 +- .../chat_helpers/gifs_list_widget.h | 6 +- .../SourceFiles/chat_helpers/stickers.cpp | 161 +++------- Telegram/SourceFiles/chat_helpers/stickers.h | 59 ++-- .../chat_helpers/stickers_list_widget.cpp | 40 +-- .../chat_helpers/stickers_list_widget.h | 9 +- .../chat_helpers/tabbed_selector.cpp | 5 +- .../chat_helpers/tabbed_selector.h | 2 +- Telegram/SourceFiles/data/data_document.cpp | 6 +- Telegram/SourceFiles/facades.h | 51 ---- .../SourceFiles/history/history_widget.cpp | 13 +- .../inline_bot_layout_internal.cpp | 6 +- Telegram/SourceFiles/mainwidget.cpp | 30 +- Telegram/SourceFiles/settings.cpp | 3 - Telegram/SourceFiles/settings.h | 7 - Telegram/SourceFiles/storage/localstorage.cpp | 51 ++-- .../storage/serialize_document.cpp | 1 + .../SourceFiles/ui/widgets/input_fields.cpp | 34 ++- .../SourceFiles/ui/widgets/input_fields.h | 41 ++- 31 files changed, 673 insertions(+), 479 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index e17c91912..3017dbf6e 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -49,11 +49,11 @@ constexpr auto kReloadChannelMembersTimeout = 1000; // 1 second wait before relo constexpr auto kSaveCloudDraftTimeout = 1000; // save draft to the cloud with 1 sec extra delay constexpr auto kSaveDraftBeforeQuitTimeout = 1500; // give the app 1.5 secs to save drafts to cloud when quitting constexpr auto kSmallDelayMs = 5; -constexpr auto kStickersUpdateTimeout = 3600000; // update not more than once in an hour constexpr auto kUnreadMentionsPreloadIfLess = 5; constexpr auto kUnreadMentionsFirstRequestLimit = 10; constexpr auto kUnreadMentionsNextRequestLimit = 100; constexpr auto kSharedMediaLimit = 100; +constexpr auto kReadFeaturedSetsTimeout = TimeMs(1000); } // namespace @@ -61,7 +61,8 @@ ApiWrap::ApiWrap(not_null session) : _session(session) , _messageDataResolveDelayed([this] { resolveMessageDatas(); }) , _webPagesTimer([this] { resolveWebPages(); }) -, _draftsSaveTimer([this] { saveDraftsToCloud(); }) { +, _draftsSaveTimer([this] { saveDraftsToCloud(); }) +, _featuredSetsReadTimer([this] { readFeaturedSets(); }) { } void ApiWrap::start() { @@ -838,7 +839,7 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers: auto writeInstalled = true, writeRecent = false, writeCloudRecent = false, writeFaved = false, writeArchived = false; auto &recent = cGetRecentStickers(); - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); _stickersOrder = localOrder; for_const (auto removedSetId, localRemoved) { @@ -883,8 +884,8 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers: _stickerSetDisenableRequests.insert(requestId); - int removeIndex = Global::StickerSetsOrder().indexOf(it->id); - if (removeIndex >= 0) Global::RefStickerSetsOrder().removeAt(removeIndex); + int removeIndex = Auth().data().stickerSetsOrder().indexOf(it->id); + if (removeIndex >= 0) Auth().data().stickerSetsOrderRef().removeAt(removeIndex); if (!(it->flags & MTPDstickerSet_ClientFlag::f_featured) && !(it->flags & MTPDstickerSet_ClientFlag::f_special)) { sets.erase(it); } else { @@ -904,7 +905,7 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers: } } - auto &order(Global::RefStickerSetsOrder()); + auto &order = Auth().data().stickerSetsOrderRef(); order.clear(); for_const (auto setId, _stickersOrder) { auto it = sets.find(setId); @@ -943,9 +944,9 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers: if (writeArchived) Local::writeArchivedStickers(); if (writeCloudRecent) Local::writeRecentStickers(); if (writeFaved) Local::writeFavedStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); - if (_stickerSetDisenableRequests.isEmpty()) { + if (_stickerSetDisenableRequests.empty()) { stickersSaveOrder(); } else { requestSendDelayed(); @@ -954,7 +955,7 @@ void ApiWrap::saveStickerSets(const Stickers::Order &localOrder, const Stickers: void ApiWrap::stickerSetDisenabled(mtpRequestId requestId) { _stickerSetDisenableRequests.remove(requestId); - if (_stickerSetDisenableRequests.isEmpty()) { + if (_stickerSetDisenableRequests.empty()) { stickersSaveOrder(); } }; @@ -1465,7 +1466,7 @@ void ApiWrap::stickersSaveOrder() { _stickersReorderRequestId = 0; }).fail([this](const RPCError &error) { _stickersReorderRequestId = 0; - Global::SetLastStickersUpdate(0); + Auth().data().setLastStickersUpdate(0); updateStickers(); }).send(); } @@ -1484,18 +1485,16 @@ void ApiWrap::setGroupStickerSet(not_null megagroup, const MTPInpu Expects(megagroup->mgInfo != nullptr); megagroup->mgInfo->stickerSet = set; request(MTPchannels_SetStickers(megagroup->inputChannel, set)).send(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } void ApiWrap::requestStickers(TimeId now) { - if (Global::LastStickersUpdate() != 0 && now < Global::LastStickersUpdate() + kStickersUpdateTimeout) { - return; - } - if (_stickersUpdateRequest) { + if (!Auth().data().stickersUpdateNeeded(now) + || _stickersUpdateRequest) { return; } auto onDone = [this](const MTPmessages_AllStickers &result) { - Global::SetLastStickersUpdate(getms(true)); + Auth().data().setLastStickersUpdate(getms(true)); _stickersUpdateRequest = 0; switch (result.type()) { @@ -1514,14 +1513,12 @@ void ApiWrap::requestStickers(TimeId now) { } void ApiWrap::requestRecentStickers(TimeId now) { - if (Global::LastRecentStickersUpdate() != 0 && now < Global::LastRecentStickersUpdate() + kStickersUpdateTimeout) { - return; - } - if (_recentStickersUpdateRequest) { + if (!Auth().data().recentStickersUpdateNeeded(now) + || _recentStickersUpdateRequest) { return; } auto onDone = [this](const MTPmessages_RecentStickers &result) { - Global::SetLastRecentStickersUpdate(getms(true)); + Auth().data().setLastRecentStickersUpdate(getms(true)); _recentStickersUpdateRequest = 0; switch (result.type()) { @@ -1540,14 +1537,12 @@ void ApiWrap::requestRecentStickers(TimeId now) { } void ApiWrap::requestFavedStickers(TimeId now) { - if (Global::LastFavedStickersUpdate() != 0 && now < Global::LastFavedStickersUpdate() + kStickersUpdateTimeout) { - return; - } - if (_favedStickersUpdateRequest) { + if (!Auth().data().favedStickersUpdateNeeded(now) + || _favedStickersUpdateRequest) { return; } auto onDone = [this](const MTPmessages_FavedStickers &result) { - Global::SetLastFavedStickersUpdate(getms(true)); + Auth().data().setLastFavedStickersUpdate(getms(true)); _favedStickersUpdateRequest = 0; switch (result.type()) { @@ -1566,14 +1561,12 @@ void ApiWrap::requestFavedStickers(TimeId now) { } void ApiWrap::requestFeaturedStickers(TimeId now) { - if (Global::LastFeaturedStickersUpdate() != 0 && now < Global::LastFeaturedStickersUpdate() + kStickersUpdateTimeout) { - return; - } - if (_featuredStickersUpdateRequest) { + if (!Auth().data().featuredStickersUpdateNeeded(now) + || _featuredStickersUpdateRequest) { return; } auto onDone = [this](const MTPmessages_FeaturedStickers &result) { - Global::SetLastFeaturedStickersUpdate(getms(true)); + Auth().data().setLastFeaturedStickersUpdate(getms(true)); _featuredStickersUpdateRequest = 0; switch (result.type()) { @@ -1592,14 +1585,12 @@ void ApiWrap::requestFeaturedStickers(TimeId now) { } void ApiWrap::requestSavedGifs(TimeId now) { - if (cLastSavedGifsUpdate() != 0 && now < cLastSavedGifsUpdate() + kStickersUpdateTimeout) { - return; - } - if (_savedGifsUpdateRequest) { + if (!Auth().data().savedGifsUpdateNeeded(now) + || _savedGifsUpdateRequest) { return; } auto onDone = [this](const MTPmessages_SavedGifs &result) { - cSetLastSavedGifsUpdate(getms(true)); + Auth().data().setLastSavedGifsUpdate(getms(true)); _savedGifsUpdateRequest = 0; switch (result.type()) { @@ -1617,6 +1608,42 @@ void ApiWrap::requestSavedGifs(TimeId now) { }).send(); } +void ApiWrap::readFeaturedSetDelayed(uint64 setId) { + if (!_featuredSetsRead.contains(setId)) { + _featuredSetsRead.insert(setId); + _featuredSetsReadTimer.callOnce(kReadFeaturedSetsTimeout); + } +} + +void ApiWrap::readFeaturedSets() { + auto &sets = Auth().data().stickerSetsRef(); + auto count = Auth().data().featuredStickerSetsUnreadCount(); + QVector wrappedIds; + wrappedIds.reserve(_featuredSetsRead.size()); + for (auto setId : _featuredSetsRead) { + auto it = sets.find(setId); + if (it != sets.cend()) { + it->flags &= ~MTPDstickerSet_ClientFlag::f_unread; + wrappedIds.append(MTP_long(setId)); + if (count) { + --count; + } + } + } + _featuredSetsRead.clear(); + + if (!wrappedIds.empty()) { + auto requestData = MTPmessages_ReadFeaturedStickers( + MTP_vector(wrappedIds)); + request(std::move(requestData)).done([](const MTPBool &result) { + Local::writeFeaturedStickers(); + Auth().data().markStickersUpdated(); + }).send(); + + Auth().data().setFeaturedStickerSetsUnreadCount(count); + } +} + void ApiWrap::applyUpdatesNoPtsCheck(const MTPUpdates &updates) { switch (updates.type()) { case mtpc_updateShortMessage: { diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 1ab158c61..2bc283ed6 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -26,6 +26,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "mtproto/sender.h" #include "base/flat_map.h" #include "base/flat_set.h" +#include "chat_helpers/stickers.h" class AuthSession; @@ -135,6 +136,7 @@ public: auto stickerSetInstalled() const { return _stickerSetInstalled.events(); } + void readFeaturedSetDelayed(uint64 setId); ~ApiWrap(); @@ -177,6 +179,7 @@ private: void requestFavedStickers(TimeId now); void requestFeaturedStickers(TimeId now); void requestSavedGifs(TimeId now); + void readFeaturedSets(); void cancelEditChatAdmins(not_null chat); void saveChatAdmins(not_null chat); @@ -229,7 +232,7 @@ private: QMap _draftsSaveRequestIds; base::Timer _draftsSaveTimer; - OrderedSet _stickerSetDisenableRequests; + base::flat_set _stickerSetDisenableRequests; Stickers::Order _stickersOrder; mtpRequestId _stickersReorderRequestId = 0; mtpRequestId _stickersClearRecentRequestId = 0; @@ -240,6 +243,9 @@ private: mtpRequestId _featuredStickersUpdateRequest = 0; mtpRequestId _savedGifsUpdateRequest = 0; + base::Timer _featuredSetsReadTimer; + base::flat_set _featuredSetsRead; + QMap _privacySaveRequests; mtpRequestId _contactsStatusesRequestId = 0; diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 9b925e34c..8eae15204 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -1079,7 +1079,7 @@ namespace { } void addSavedGif(DocumentData *doc) { - SavedGifs &saved(cRefSavedGifs()); + auto &saved = Auth().data().savedGifsRef(); int32 index = saved.indexOf(doc); if (index) { if (index > 0) saved.remove(index); @@ -1087,8 +1087,8 @@ namespace { if (saved.size() > Global::SavedGifsLimit()) saved.pop_back(); Local::writeSavedGifs(); - Auth().data().savedGifsUpdated().notify(); - cSetLastSavedGifsUpdate(0); + Auth().data().markSavedGifsUpdated(); + Auth().data().setLastSavedGifsUpdate(0); Auth().api().updateStickers(); } } @@ -1636,7 +1636,7 @@ namespace { } } - if (cSavedGifs().indexOf(convert) >= 0) { // id changed + if (Auth().data().savedGifs().indexOf(convert) >= 0) { // id changed Local::writeSavedGifs(); } } @@ -1682,8 +1682,8 @@ namespace { } if (versionChanged) { if (result->sticker() && result->sticker()->set.type() == mtpc_inputStickerSetID) { - auto it = Global::StickerSets().constFind(result->sticker()->set.c_inputStickerSetID().vid.v); - if (it != Global::StickerSets().cend()) { + auto it = Auth().data().stickerSets().constFind(result->sticker()->set.c_inputStickerSetID().vid.v); + if (it != Auth().data().stickerSets().cend()) { if (it->id == Stickers::CloudRecentSetId) { Local::writeRecentStickers(); } else if (it->id == Stickers::FavedSetId) { @@ -2035,19 +2035,6 @@ namespace { Auth().api().clearWebPageRequests(); } cSetRecentStickers(RecentStickerPack()); - Global::SetStickerSets(Stickers::Sets()); - Global::SetStickerSetsOrder(Stickers::Order()); - Global::SetLastStickersUpdate(0); - Global::SetLastRecentStickersUpdate(0); - Global::SetFeaturedStickerSetsOrder(Stickers::Order()); - if (Global::FeaturedStickerSetsUnreadCount() != 0) { - Global::SetFeaturedStickerSetsUnreadCount(0); - Global::RefFeaturedStickerSetsUnreadCountChanged().notify(); - } - Global::SetLastFeaturedStickersUpdate(0); - Global::SetArchivedStickerSetsOrder(Stickers::Order()); - cSetSavedGifs(SavedGifs()); - cSetLastSavedGifsUpdate(0); cSetReportSpamStatuses(ReportSpamStatuses()); cSetAutoDownloadPhoto(0); cSetAutoDownloadAudio(0); diff --git a/Telegram/SourceFiles/auth_session.h b/Telegram/SourceFiles/auth_session.h index 76043ac36..5f3ec219b 100644 --- a/Telegram/SourceFiles/auth_session.h +++ b/Telegram/SourceFiles/auth_session.h @@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include #include #include "base/timer.h" +#include "chat_helpers/stickers.h" namespace Storage { class Downloader; @@ -59,12 +60,6 @@ public: base::Observable &moreChatsLoaded() { return _moreChatsLoaded; } - base::Observable &stickersUpdated() { - return _stickersUpdated; - } - base::Observable &savedGifsUpdated() { - return _savedGifsUpdated; - } base::Observable &pendingHistoryResize() { return _pendingHistoryResize; } @@ -230,6 +225,18 @@ public: return _variables.dialogsWidthRatio.changes(); } + void markStickersUpdated() { + _stickersUpdated.fire({}); + } + rpl::producer<> stickersUpdated() const { + return _stickersUpdated.events(); + } + void markSavedGifsUpdated() { + _savedGifsUpdated.fire({}); + } + rpl::producer<> savedGifsUpdated() const { + return _savedGifsUpdated.events(); + } void setGroupStickersSectionHidden(PeerId peerId) { _variables.groupStickersSectionHidden.insert(peerId); } @@ -239,6 +246,75 @@ public: void removeGroupStickersSectionHidden(PeerId peerId) { _variables.groupStickersSectionHidden.remove(peerId); } + bool stickersUpdateNeeded(TimeMs now) const { + return stickersUpdateNeeded(_lastStickersUpdate, now); + } + void setLastStickersUpdate(TimeMs update) { + _lastStickersUpdate = update; + } + bool recentStickersUpdateNeeded(TimeMs now) const { + return stickersUpdateNeeded(_lastRecentStickersUpdate, now); + } + void setLastRecentStickersUpdate(TimeMs update) { + _lastRecentStickersUpdate = update; + } + bool favedStickersUpdateNeeded(TimeMs now) const { + return stickersUpdateNeeded(_lastFavedStickersUpdate, now); + } + void setLastFavedStickersUpdate(TimeMs update) { + _lastFavedStickersUpdate = update; + } + bool featuredStickersUpdateNeeded(TimeMs now) const { + return stickersUpdateNeeded(_lastFeaturedStickersUpdate, now); + } + void setLastFeaturedStickersUpdate(TimeMs update) { + _lastFeaturedStickersUpdate = update; + } + bool savedGifsUpdateNeeded(TimeMs now) const { + return stickersUpdateNeeded(_lastSavedGifsUpdate, now); + } + void setLastSavedGifsUpdate(TimeMs update) { + _lastSavedGifsUpdate = update; + } + int featuredStickerSetsUnreadCount() const { + return _featuredStickerSetsUnreadCount.current(); + } + void setFeaturedStickerSetsUnreadCount(int count) { + _featuredStickerSetsUnreadCount = count; + } + rpl::producer featuredStickerSetsUnreadCountValue() const { + return _featuredStickerSetsUnreadCount.value(); + } + const Stickers::Sets &stickerSets() const { + return _stickerSets; + } + Stickers::Sets &stickerSetsRef() { + return _stickerSets; + } + const Stickers::Order &stickerSetsOrder() const { + return _stickerSetsOrder; + } + Stickers::Order &stickerSetsOrderRef() { + return _stickerSetsOrder; + } + const Stickers::Order &featuredStickerSetsOrder() const { + return _featuredStickerSetsOrder; + } + Stickers::Order &featuredStickerSetsOrderRef() { + return _featuredStickerSetsOrder; + } + const Stickers::Order &archivedStickerSetsOrder() const { + return _archivedStickerSetsOrder; + } + Stickers::Order &archivedStickerSetsOrderRef() { + return _archivedStickerSetsOrder; + } + const Stickers::SavedGifs &savedGifs() const { + return _savedGifs; + } + Stickers::SavedGifs &savedGifsRef() { + return _savedGifs; + } private: struct Variables { @@ -259,11 +335,15 @@ private: rpl::variable dialogsWidthRatio = kDefaultDialogsWidthRatio; // per-window }; + bool stickersUpdateNeeded(TimeMs lastUpdate, TimeMs now) const { + constexpr auto kStickersUpdateTimeout = TimeMs(3600'000); + return (lastUpdate == 0) + || (now >= lastUpdate + kStickersUpdateTimeout); + } + base::Variable _contactsLoaded = { false }; base::Variable _allChatsLoaded = { false }; base::Observable _moreChatsLoaded; - base::Observable _stickersUpdated; - base::Observable _savedGifsUpdated; base::Observable _pendingHistoryResize; base::Observable _queryItemVisibility; rpl::event_stream> _itemLayoutChanged; @@ -274,6 +354,20 @@ private: rpl::event_stream _megagroupParticipantRemoved; rpl::event_stream _megagroupParticipantAdded; + rpl::event_stream<> _stickersUpdated; + rpl::event_stream<> _savedGifsUpdated; + TimeMs _lastStickersUpdate = 0; + TimeMs _lastRecentStickersUpdate = 0; + TimeMs _lastFavedStickersUpdate = 0; + TimeMs _lastFeaturedStickersUpdate = 0; + TimeMs _lastSavedGifsUpdate = 0; + rpl::variable _featuredStickerSetsUnreadCount = 0; + Stickers::Sets _stickerSets; + Stickers::Order _stickerSetsOrder; + Stickers::Order _featuredStickerSetsOrder; + Stickers::Order _archivedStickerSetsOrder; + Stickers::SavedGifs _savedGifs; + rpl::event_stream _thirdSectionInfoEnabledValue; bool _tabbedReplacedWithInfo = false; rpl::event_stream _tabbedReplacedWithInfoValue; diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.cpp b/Telegram/SourceFiles/boxes/sticker_set_box.cpp index 722ac7b40..3e3178552 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.cpp +++ b/Telegram/SourceFiles/boxes/sticker_set_box.cpp @@ -50,7 +50,10 @@ void StickerSetBox::prepare() { setTitle(langFactory(lng_contacts_loading)); _inner = setInnerWidget(object_ptr(this, _set), st::stickersScroll); - subscribe(Auth().data().stickersUpdated(), [this] { updateButtons(); }); + Auth().data().stickersUpdated() + | rpl::start_with_next( + [this] { updateButtons(); }, + lifetime()); setDimensions(st::boxWideWidth, st::stickersMaxHeight); @@ -145,7 +148,7 @@ void StickerSetBox::Inner::gotSet(const MTPmessages_StickerSet &set) { emoji = emoji->original(); auto &stickers = pack.vdocuments.v; - StickerPack p; + Stickers::Pack p; p.reserve(stickers.size()); for (auto j = 0, c = stickers.size(); j != c; ++j) { auto doc = App::document(stickers[j].v); @@ -165,7 +168,7 @@ void StickerSetBox::Inner::gotSet(const MTPmessages_StickerSet &set) { _setCount = s.vcount.v; _setHash = s.vhash.v; _setFlags = s.vflags.v; - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(_setId); if (it != sets.cend()) { auto clientFlags = it->flags & (MTPDstickerSet_ClientFlag::f_featured | MTPDstickerSet_ClientFlag::f_not_loaded | MTPDstickerSet_ClientFlag::f_unread | MTPDstickerSet_ClientFlag::f_special); @@ -201,13 +204,13 @@ bool StickerSetBox::Inner::failedSet(const RPCError &error) { } void StickerSetBox::Inner::installDone(const MTPmessages_StickerSetInstallResult &result) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); bool wasArchived = (_setFlags & MTPDstickerSet::Flag::f_archived); if (wasArchived) { - auto index = Global::RefArchivedStickerSetsOrder().indexOf(_setId); + auto index = Auth().data().archivedStickerSetsOrderRef().indexOf(_setId); if (index >= 0) { - Global::RefArchivedStickerSetsOrder().removeAt(index); + Auth().data().archivedStickerSetsOrderRef().removeAt(index); } } _setFlags &= ~MTPDstickerSet::Flag::f_archived; @@ -221,7 +224,7 @@ void StickerSetBox::Inner::installDone(const MTPmessages_StickerSetInstallResult it->stickers = _pack; it->emoji = _emoji; - auto &order = Global::RefStickerSetsOrder(); + auto &order = Auth().data().stickerSetsOrderRef(); int insertAtIndex = 0, currentIndex = order.indexOf(_setId); if (currentIndex != insertAtIndex) { if (currentIndex > 0) { @@ -248,7 +251,7 @@ void StickerSetBox::Inner::installDone(const MTPmessages_StickerSetInstallResult Local::writeArchivedStickers(); } Local::writeInstalledStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } _setInstalled.fire_copy(_setId); } @@ -407,8 +410,8 @@ bool StickerSetBox::Inner::loaded() const { int32 StickerSetBox::Inner::notInstalled() const { if (!_loaded) return 0; - auto it = Global::StickerSets().constFind(_setId); - if (it == Global::StickerSets().cend() || !(it->flags & MTPDstickerSet::Flag::f_installed) || (it->flags & MTPDstickerSet::Flag::f_archived)) return _pack.size(); + auto it = Auth().data().stickerSets().constFind(_setId); + if (it == Auth().data().stickerSets().cend() || !(it->flags & MTPDstickerSet::Flag::f_installed) || (it->flags & MTPDstickerSet::Flag::f_archived)) return _pack.size(); return 0; } diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.h b/Telegram/SourceFiles/boxes/sticker_set_box.h index 16f378500..12ed08a0a 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.h +++ b/Telegram/SourceFiles/boxes/sticker_set_box.h @@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #pragma once #include "boxes/abstract_box.h" -#include +#include "chat_helpers/stickers.h" class ConfirmBox; @@ -105,8 +105,8 @@ private: } std::vector _packOvers; - StickerPack _pack; - StickersByEmojiMap _emoji; + Stickers::Pack _pack; + Stickers::ByEmojiMap _emoji; bool _loaded = false; uint64 _setId = 0; uint64 _setAccess = 0; diff --git a/Telegram/SourceFiles/boxes/stickers_box.cpp b/Telegram/SourceFiles/boxes/stickers_box.cpp index bd91fbcfb..1552ca877 100644 --- a/Telegram/SourceFiles/boxes/stickers_box.cpp +++ b/Telegram/SourceFiles/boxes/stickers_box.cpp @@ -52,8 +52,8 @@ constexpr auto kHandleMegagroupSetAddressChangeTimeout = TimeMs(1000); int stickerPacksCount(bool includeArchivedOfficial) { auto result = 0; - auto &order = Global::StickerSetsOrder(); - auto &sets = Global::StickerSets(); + auto &order = Auth().data().stickerSetsOrder(); + auto &sets = Auth().data().stickerSets(); for (auto i = 0, l = order.size(); i < l; ++i) { auto it = sets.constFind(order.at(i)); if (it != sets.cend()) { @@ -65,24 +65,23 @@ int stickerPacksCount(bool includeArchivedOfficial) { return result; } -class StickersBox::CounterWidget : public TWidget, private base::Subscriber { +class StickersBox::CounterWidget : public Ui::RpWidget { public: CounterWidget(QWidget *parent); - void setCounter(int counter); - protected: void paintEvent(QPaintEvent *e) override; private: - void updateCounter(); + void setCounter(int counter); QString _text; Dialogs::Layout::UnreadBadgeStyle _st; }; -StickersBox::CounterWidget::CounterWidget(QWidget *parent) : TWidget(parent) { +StickersBox::CounterWidget::CounterWidget(QWidget *parent) +: RpWidget(parent) { setAttribute(Qt::WA_TransparentForMouseEvents); _st.sizeId = Dialogs::Layout::UnreadBadgeInStickersBox; @@ -91,8 +90,11 @@ StickersBox::CounterWidget::CounterWidget(QWidget *parent) : TWidget(parent) { _st.padding = st::stickersFeaturedBadgePadding; _st.font = st::stickersFeaturedBadgeFont; - subscribe(Global::RefFeaturedStickerSetsUnreadCountChanged(), [this] { updateCounter(); }); - updateCounter(); + Auth().data().featuredStickerSetsUnreadCountValue() + | rpl::start_with_next([this](int count) { + setCounter(count); + update(); + }, lifetime()); } void StickersBox::CounterWidget::setCounter(int counter) { @@ -116,11 +118,6 @@ void StickersBox::CounterWidget::paintEvent(QPaintEvent *e) { } } -void StickersBox::CounterWidget::updateCounter() { - setCounter(Global::FeaturedStickerSetsUnreadCount()); - update(); -} - template StickersBox::Tab::Tab(int index, Args&&... args) : _index(index) @@ -166,7 +163,7 @@ void StickersBox::getArchivedDone(uint64 offsetId, const MTPmessages_ArchivedSti } auto &stickers = result.c_messages_archivedStickers(); - auto &archived = Global::RefArchivedStickerSetsOrder(); + auto &archived = Auth().data().archivedStickerSetsOrderRef(); if (offsetId) { auto index = archived.indexOf(offsetId); if (index >= 0) { @@ -241,7 +238,7 @@ void StickersBox::prepare() { requestArchivedSets(); } if (_tabs) { - if (Global::ArchivedStickerSetsOrder().isEmpty()) { + if (Auth().data().archivedStickerSetsOrder().isEmpty()) { preloadArchivedSets(); } setNoContentMargin(true); @@ -280,7 +277,10 @@ void StickersBox::prepare() { setInnerWidget(_tab->takeWidget(), getTopSkip()); setDimensions(st::boxWideWidth, st::boxMaxListHeight); - subscribe(Auth().data().stickersUpdated(), [this] { handleStickersUpdated(); }); + Auth().data().stickersUpdated() + | rpl::start_with_next( + [this] { handleStickersUpdated(); }, + lifetime()); Auth().api().updateStickers(); if (_installed.widget()) { @@ -304,11 +304,11 @@ void StickersBox::refreshTabs() { auto sections = QStringList(); sections.push_back(lang(lng_stickers_installed_tab).toUpper()); _tabIndices.push_back(Section::Installed); - if (!Global::FeaturedStickerSetsOrder().isEmpty()) { + if (!Auth().data().featuredStickerSetsOrder().isEmpty()) { sections.push_back(lang(lng_stickers_featured_tab).toUpper()); _tabIndices.push_back(Section::Featured); } - if (!Global::ArchivedStickerSetsOrder().isEmpty()) { + if (!Auth().data().archivedStickerSetsOrder().isEmpty()) { sections.push_back(lang(lng_stickers_archived_tab).toUpper()); _tabIndices.push_back(Section::Archived); } @@ -330,10 +330,10 @@ void StickersBox::loadMoreArchived() { } uint64 lastId = 0; - for (auto setIt = Global::ArchivedStickerSetsOrder().cend(), e = Global::ArchivedStickerSetsOrder().cbegin(); setIt != e;) { + for (auto setIt = Auth().data().archivedStickerSetsOrder().cend(), e = Auth().data().archivedStickerSetsOrder().cbegin(); setIt != e;) { --setIt; - auto it = Global::StickerSets().constFind(*setIt); - if (it != Global::StickerSets().cend()) { + auto it = Auth().data().stickerSets().constFind(*setIt); + if (it != Auth().data().stickerSets().cend()) { if (it->flags & MTPDstickerSet::Flag::f_archived) { lastId = it->id; break; @@ -445,7 +445,7 @@ QPixmap StickersBox::grabContentCache() { } void StickersBox::installSet(uint64 setId) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(setId); if (it == sets.cend()) { rebuildList(); @@ -474,7 +474,7 @@ void StickersBox::installDone(const MTPmessages_StickerSetInstallResult &result) bool StickersBox::installFail(uint64 setId, const RPCError &error) { if (MTP::isDefaultHandledError(error)) return false; - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(setId); if (it == sets.cend()) { rebuildList(); @@ -498,8 +498,8 @@ void StickersBox::requestArchivedSets() { preloadArchivedSets(); } - auto &sets = Global::StickerSets(); - for_const (auto setId, Global::ArchivedStickerSetsOrder()) { + auto &sets = Auth().data().stickerSets(); + for_const (auto setId, Auth().data().archivedStickerSetsOrder()) { auto it = sets.constFind(setId); if (it != sets.cend()) { if (it->stickers.isEmpty() && (it->flags & MTPDstickerSet_ClientFlag::f_not_loaded)) { @@ -530,7 +530,7 @@ void StickersBox::handleStickersUpdated() { } else { _tab->widget()->updateRows(); } - if (Global::ArchivedStickerSetsOrder().isEmpty()) { + if (Auth().data().archivedStickerSetsOrder().isEmpty()) { preloadArchivedSets(); } else { refreshTabs(); @@ -612,8 +612,20 @@ StickersBox::Inner::Inner(QWidget *parent, not_null megagroup) : T _megagroupSetField->setLinkPlaceholder(Messenger::Instance().createInternalLink(qsl("addstickers/"))); _megagroupSetField->setPlaceholderHidden(false); _megagroupSetAddressChangedTimer.setCallback([this] { handleMegagroupSetAddressChange(); }); - connect(_megagroupSetField, &Ui::MaskedInputField::changed, this, [this] { _megagroupSetAddressChangedTimer.callOnce(kHandleMegagroupSetAddressChangeTimeout); }); - connect(_megagroupSetField, &Ui::MaskedInputField::submitted, this, [this] { _megagroupSetAddressChangedTimer.cancel(); handleMegagroupSetAddressChange(); }); + connect( + _megagroupSetField, + &Ui::MaskedInputField::changed, + [this] { + _megagroupSetAddressChangedTimer.callOnce( + kHandleMegagroupSetAddressChangeTimeout); + }); + connect( + _megagroupSetField, + &Ui::MaskedInputField::submitted, + [this] { + _megagroupSetAddressChangedTimer.cancel(); + handleMegagroupSetAddressChange(); + }); setup(); } @@ -717,7 +729,13 @@ void StickersBox::Inner::paintRow(Painter &p, Row *set, int index, TimeMs ms) { if (xadd || yadd) p.translate(xadd, yadd); if (_megagroupSet) { - if (index >= 0 && index == _selected) { + auto selectedIndex = [&] { + if (auto index = base::get_if(&_selected)) { + return *index; + } + return -1; + }(); + if (index >= 0 && index == selectedIndex) { p.fillRect(0, 0, width(), _rowHeight, st::contactsBgOver); if (set->ripple) { set->ripple->paint(p, 0, 0, width(), ms); @@ -854,9 +872,11 @@ void StickersBox::Inner::mousePressEvent(QMouseEvent *e) { if (_actionSel >= 0) { setActionDown(_actionSel); update(0, _itemsTop + _actionSel * _rowHeight, width(), _rowHeight); - } else if (_selected >= 0 && _section == Section::Installed && !_rows.at(_selected)->isRecentSet() && _inDragArea) { - _above = _dragging = _started = _selected; - _dragStart = mapFromGlobal(_mouse); + } else if (auto selectedIndex = base::get_if(&_selected)) { + if (_section == Section::Installed && !_rows[*selectedIndex]->isRecentSet() && _inDragArea) { + _above = _dragging = _started = *selectedIndex; + _dragStart = mapFromGlobal(_mouse); + } } } @@ -900,41 +920,65 @@ void StickersBox::Inner::setActionDown(int newActionDown) { } } -void StickersBox::Inner::setSelected(int selected) { +void StickersBox::Inner::setSelected(SelectedRow selected) { if (_selected == selected) { return; } - if (_megagroupSet && _selected >= 0 && _selected < _rows.size()) { - update(0, _itemsTop + _selected * _rowHeight, width(), _rowHeight); + if ((_megagroupSet || _section != Section::Installed) + && ((_selected.has_value() || _pressed.has_value()) != (selected.has_value() || _pressed.has_value()))) { + if (!_inDragArea) { + setCursor((selected.has_value() || _pressed.has_value()) + ? style::cur_pointer + : style::cur_default); + } + } + auto countSelectedIndex = [&] { + if (auto index = base::get_if(&_selected)) { + return *index; + } + return -1; + }; + auto selectedIndex = countSelectedIndex(); + if (_megagroupSet && selectedIndex >= 0 && selectedIndex < _rows.size()) { + update(0, _itemsTop + selectedIndex * _rowHeight, width(), _rowHeight); } _selected = selected; - if (_megagroupSet && _selected >= 0 && _selected < _rows.size()) { - update(0, _itemsTop + _selected * _rowHeight, width(), _rowHeight); + selectedIndex = countSelectedIndex(); + if (_megagroupSet && selectedIndex >= 0 && selectedIndex < _rows.size()) { + update(0, _itemsTop + selectedIndex * _rowHeight, width(), _rowHeight); } } -void StickersBox::Inner::setPressed(int pressed) { +void StickersBox::Inner::setPressed(SelectedRow pressed) { if (_pressed == pressed) { return; } - if (_megagroupSet && _pressed >= 0 && _pressed < _rows.size()) { - update(0, _itemsTop + _pressed * _rowHeight, width(), _rowHeight); - auto &set = _rows[_pressed]; + auto countPressedIndex = [&] { + if (auto index = base::get_if(&_pressed)) { + return *index; + } + return -1; + }; + auto pressedIndex = countPressedIndex(); + if (_megagroupSet && pressedIndex >= 0 && pressedIndex < _rows.size()) { + update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight); + auto &set = _rows[pressedIndex]; if (set->ripple) { set->ripple->lastStop(); } } _pressed = pressed; - if (_megagroupSet && _pressed >= 0 && _pressed < _rows.size()) { - update(0, _itemsTop + _pressed * _rowHeight, width(), _rowHeight); - auto &set = _rows[_pressed]; + pressedIndex = countPressedIndex(); + if (_megagroupSet && pressedIndex >= 0 && pressedIndex < _rows.size()) { + update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight); + auto &set = _rows[pressedIndex]; auto rippleMask = Ui::RippleAnimation::rectMask(QSize(width(), _rowHeight)); - if (!_rows[_pressed]->ripple) { - _rows[_pressed]->ripple = std::make_unique(st::contactsRipple, std::move(rippleMask), [this, index = _pressed] { - update(0, _itemsTop + index * _rowHeight, width(), _rowHeight); + if (!_rows[pressedIndex]->ripple) { + _rows[pressedIndex]->ripple = std::make_unique(st::contactsRipple, std::move(rippleMask), [this, pressedIndex] { + update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight); }); } - _rows[_pressed]->ripple->add(mapFromGlobal(QCursor::pos()) - QPoint(0, _itemsTop + _pressed * _rowHeight)); + _rows[pressedIndex]->ripple->add(mapFromGlobal(QCursor::pos()) - QPoint(0, _itemsTop + pressedIndex * _rowHeight)); } } @@ -997,17 +1041,18 @@ void StickersBox::Inner::onUpdateSelected() { emit draggingScrollDelta(countDraggingScrollDelta()); } else { bool in = rect().marginsRemoved(QMargins(0, _itemsTop, 0, st::membersMarginBottom)).contains(local); - auto selected = -1; + auto selected = SelectedRow(); auto actionSel = -1; auto inDragArea = false; if (in && !_rows.empty()) { - selected = floorclamp(local.y() - _itemsTop, _rowHeight, 0, _rows.size() - 1); - local.setY(local.y() - _itemsTop - selected * _rowHeight); - auto &set = _rows[selected]; + auto selectedIndex = floorclamp(local.y() - _itemsTop, _rowHeight, 0, _rows.size() - 1); + selected = selectedIndex; + local.setY(local.y() - _itemsTop - selectedIndex * _rowHeight); + auto &set = _rows[selectedIndex]; if (!_megagroupSet && (_section == Section::Installed || !set->installed || set->archived || set->removed)) { auto removeButton = (_section == Section::Installed && !set->removed); auto rect = myrtlrect(relativeButtonRect(removeButton)); - actionSel = rect.contains(local) ? selected : -1; + actionSel = rect.contains(local) ? selectedIndex : -1; } else { actionSel = -1; } @@ -1016,20 +1061,20 @@ void StickersBox::Inner::onUpdateSelected() { auto dragArea = myrtlrect(0, 0, dragAreaWidth, _rowHeight); inDragArea = dragArea.contains(local); } - } else { - selected = -1; - } - if (_selected != selected) { - if ((_megagroupSet || _section != Section::Installed) && ((_selected >= 0 || _pressed >= 0) != (selected >= 0 || _pressed >= 0))) { - if (!inDragArea) { - setCursor((selected >= 0 || _pressed >= 0) ? style::cur_pointer : style::cur_default); - } + } else if (_megagroupSelectedSet) { + auto setTop = _megagroupDivider->y() - _rowHeight; + if (QRect(0, setTop, width(), _rowHeight).contains(local)) { + selected = MegagroupSet(); } - setSelected(selected); } + setSelected(selected); if (_inDragArea != inDragArea) { _inDragArea = inDragArea; - setCursor(_inDragArea ? style::cur_sizeall : (_selected >= 0 || _pressed >= 0) ? style::cur_pointer : style::cur_default); + setCursor(_inDragArea + ? style::cur_sizeall + : ((_selected.has_value() || _pressed.has_value()) + ? style::cur_pointer + : style::cur_default)); } setActionSel(actionSel); emit draggingScrollDelta(0); @@ -1045,9 +1090,9 @@ float64 StickersBox::Inner::aboveShadowOpacity() const { } void StickersBox::Inner::mouseReleaseEvent(QMouseEvent *e) { - auto pressed = std::exchange(_pressed, -1); + auto pressed = std::exchange(_pressed, SelectedRow()); - if (_section != Section::Installed && _selected < 0 && pressed >= 0) { + if (_section != Section::Installed && !_selected.has_value() && pressed.has_value()) { setCursor(style::cur_default); } @@ -1070,22 +1115,43 @@ void StickersBox::Inner::mouseReleaseEvent(QMouseEvent *e) { _dragging = _started = -1; } else if (pressed == _selected && _actionSel < 0 && _actionDown < 0) { - if (_selected >= 0 && !_inDragArea) { - auto &sets = Global::RefStickerSets(); - auto &row = _rows[pressed]; - if (!row->isRecentSet()) { - auto it = sets.find(row->id); + auto selectedIndex = [&] { + if (auto index = base::get_if(&_selected)) { + return *index; + } + return -1; + }(); + auto getSetByRow = [&](const Row &row) -> const Stickers::Set* { + auto &sets = Auth().data().stickerSetsRef(); + if (!row.isRecentSet()) { + auto it = sets.find(row.id); if (it != sets.cend()) { - if (_megagroupSet) { - setMegagroupSelectedSet(MTP_inputStickerSetID(MTP_long(it->id), MTP_long(it->access))); - } else { - setSelected(-1); - Ui::show( - Box(Stickers::inputSetId(*it)), - LayerOption::KeepOther); - } + return &*it; } } + return nullptr; + }; + auto showSetByRow = [&](const Row &row) { + if (auto set = getSetByRow(row)) { + setSelected(SelectedRow()); + Ui::show( + Box(Stickers::inputSetId(*set)), + LayerOption::KeepOther); + } + }; + if (selectedIndex >= 0 && !_inDragArea) { + auto &row = *_rows[selectedIndex]; + if (_megagroupSet) { + if (auto set = getSetByRow(row)) { + setMegagroupSelectedSet(MTP_inputStickerSetID( + MTP_long(set->id), + MTP_long(set->access))); + } + } else { + showSetByRow(row); + } + } else if (_megagroupSelectedSet && _selected.is()) { + showSetByRow(*_megagroupSelectedSet); } } setActionDown(-1); @@ -1177,8 +1243,8 @@ void StickersBox::Inner::clear() { _aboveShadowFadeOpacity = anim::value(); _a_shifting.stop(); _above = _dragging = _started = -1; - setSelected(-1); - setPressed(-1); + setSelected(SelectedRow()); + setPressed(SelectedRow()); setActionSel(-1); setActionDown(-1); update(); @@ -1195,12 +1261,39 @@ void StickersBox::Inner::setActionSel(int32 actionSel) { } } +void StickersBox::Inner::AddressField::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { + auto newText = now; + auto newCursor = nowCursor; + auto removeFromBeginning = { + qstr("http://"), + qstr("https://"), + qstr("www.t.me/"), + qstr("www.telegram.me/"), + qstr("www.telegram.dog/"), + qstr("t.me/"), + qstr("telegram.me/"), + qstr("telegram.dog/"), + qstr("addstickers/"), + }; + for (auto &removePhrase : removeFromBeginning) { + if (newText.startsWith(removePhrase)) { + newText = newText.mid(removePhrase.size()); + newCursor = newText.size(); + } + } + setCorrectedText(now, nowCursor, newText, newCursor); +} + void StickersBox::Inner::handleMegagroupSetAddressChange() { auto text = _megagroupSetField->getLastText().trimmed(); if (text.isEmpty()) { if (_megagroupSelectedSet) { - auto it = Global::StickerSets().constFind(_megagroupSelectedSet->id); - if (it != Global::StickerSets().end() && !it->shortName.isEmpty()) { + auto it = Auth().data().stickerSets().constFind(_megagroupSelectedSet->id); + if (it != Auth().data().stickerSets().cend() && !it->shortName.isEmpty()) { setMegagroupSelectedSet(MTP_inputStickerSetEmpty()); } } @@ -1232,7 +1325,7 @@ void StickersBox::Inner::rebuildMegagroupSet() { } auto &set = _megagroupSetInput.c_inputStickerSetID(); auto setId = set.vid.v; - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.find(setId); if (it == sets.cend() || (it->flags & MTPDstickerSet_ClientFlag::f_not_loaded)) { Auth().api().scheduleStickerSetRequest(set.vid.v, set.vaccess_hash.v); @@ -1279,16 +1372,16 @@ void StickersBox::Inner::rebuild() { clear(); auto &order = ([this]() -> const Stickers::Order & { if (_section == Section::Installed) { - return Global::StickerSetsOrder(); + return Auth().data().stickerSetsOrder(); } else if (_section == Section::Featured) { - return Global::FeaturedStickerSetsOrder(); + return Auth().data().featuredStickerSetsOrder(); } - return Global::ArchivedStickerSetsOrder(); + return Auth().data().archivedStickerSetsOrder(); })(); _rows.reserve(order.size() + 1); _animStartTimes.reserve(order.size() + 1); - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); if (!_megagroupSet && _section == Section::Installed) { auto cloudIt = sets.constFind(Stickers::CloudRecentSetId); if (cloudIt != sets.cend() && !cloudIt->stickers.isEmpty()) { @@ -1331,7 +1424,7 @@ void StickersBox::Inner::updateSize(int newWidth) { void StickersBox::Inner::updateRows() { int maxNameWidth = countMaxNameWidth(); - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); for_const (auto &row, _rows) { auto it = sets.constFind(row->id); if (it != sets.cend()) { @@ -1376,9 +1469,14 @@ bool StickersBox::Inner::appendSet(const Stickers::Set &set) { int StickersBox::Inner::countMaxNameWidth() const { int namex = st::contactsPadding.left() + st::contactsPhotoSize + st::contactsPadding.left(); + if (!_megagroupSet && _section == Section::Installed) { + namex += st::stickersReorderIcon.width() + st::stickersReorderSkip; + } int namew = st::boxWideWidth - namex - st::contactsPadding.right() - st::contactsCheckPosition.x(); if (_section == Section::Installed) { - namew -= _undoWidth - st::stickersUndoRemove.width; + if (!_megagroupSet) { + namew -= _undoWidth - st::stickersUndoRemove.width; + } } else { namew -= _addWidth - st::stickersTrendingAdd.width; if (_section == Section::Featured) { @@ -1438,8 +1536,8 @@ void StickersBox::Inner::fillSetCover(const Stickers::Set &set, DocumentData **o int StickersBox::Inner::fillSetCount(const Stickers::Set &set) const { int result = set.stickers.isEmpty() ? set.count : set.stickers.size(), added = 0; if (set.id == Stickers::CloudRecentSetId) { - auto customIt = Global::StickerSets().constFind(Stickers::CustomSetId); - if (customIt != Global::StickerSets().cend()) { + auto customIt = Auth().data().stickerSets().constFind(Stickers::CustomSetId); + if (customIt != Auth().data().stickerSets().cend()) { added = customIt->stickers.size(); for_const (auto &sticker, cGetRecentStickers()) { if (customIt->stickers.indexOf(sticker.first) < 0) { @@ -1572,7 +1670,7 @@ void StickersBox::Inner::readVisibleSets() { continue; } if (!_rows[i]->sticker || _rows[i]->sticker->thumb->loaded() || _rows[i]->sticker->loaded()) { - Stickers::MarkFeaturedAsRead(_rows[i]->id); + Auth().api().readFeaturedSetDelayed(_rows[i]->id); } } } diff --git a/Telegram/SourceFiles/boxes/stickers_box.h b/Telegram/SourceFiles/boxes/stickers_box.h index 31685deea..a8db9bc70 100644 --- a/Telegram/SourceFiles/boxes/stickers_box.h +++ b/Telegram/SourceFiles/boxes/stickers_box.h @@ -23,6 +23,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "boxes/abstract_box.h" #include "base/timer.h" #include "mtproto/sender.h" +#include "chat_helpers/stickers.h" +#include "ui/widgets/input_fields.h" class ConfirmBox; @@ -35,7 +37,6 @@ class PlainShadow; class RippleAnimation; class SettingsSlider; class SlideAnimation; -class UsernameInput; class CrossButton; } // namespace Ui @@ -223,6 +224,27 @@ private: anim::value yadd; std::unique_ptr ripple; }; + struct MegagroupSet { + inline bool operator==(const MegagroupSet &other) const { + return true; + } + inline bool operator!=(const MegagroupSet &other) const { + return false; + } + }; + using SelectedRow = base::optional_variant; + class AddressField : public Ui::UsernameInput { + public: + using UsernameInput::UsernameInput; + + protected: + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; + + }; template Stickers::Order collectSets(Check check) const; @@ -232,9 +254,9 @@ private: int getRowIndex(uint64 setId) const; void setRowRemoved(int index, bool removed); - void setSelected(int selected); + void setSelected(SelectedRow selected); void setActionDown(int newActionDown); - void setPressed(int pressed); + void setPressed(SelectedRow pressed); void setup(); QRect relativeButtonRect(bool removeButton) const; void ensureRipple(const style::RippleAnimation &st, QImage mask, bool removeButton); @@ -255,6 +277,7 @@ private: QString fillSetTitle(const Stickers::Set &set, int maxNameWidth, int *outTitleWidth) const; void fillSetFlags(const Stickers::Set &set, bool *outInstalled, bool *outOfficial, bool *outUnread, bool *outArchived); void rebuildMegagroupSet(); + void fixupMegagroupSetAddress(); void handleMegagroupSetAddressChange(); void setMegagroupSelectedSet(const MTPInputStickerSet &set); @@ -289,8 +312,8 @@ private: QPoint _mouse; bool _inDragArea = false; - int _selected = -1; - int _pressed = -1; + SelectedRow _selected; + SelectedRow _pressed; QPoint _dragStart; int _started = -1; int _dragging = -1; @@ -302,7 +325,7 @@ private: ChannelData *_megagroupSet = nullptr; MTPInputStickerSet _megagroupSetInput = MTP_inputStickerSetEmpty(); std::unique_ptr _megagroupSelectedSet; - object_ptr _megagroupSetField = { nullptr }; + object_ptr _megagroupSetField = { nullptr }; object_ptr _megagroupSelectedShadow = { nullptr }; object_ptr _megagroupSelectedRemove = { nullptr }; object_ptr _megagroupDivider = { nullptr }; diff --git a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp index 2ad4e20d6..05c96ef11 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp @@ -651,14 +651,14 @@ void EmojiListWidget::refreshRecent() { updateSize(); } -bool EmojiListWidget::event(QEvent *e) { +bool EmojiListWidget::eventHook(QEvent *e) { if (e->type() == QEvent::ParentChange) { if (_picker->parentWidget() != parentWidget()) { _picker->setParent(parentWidget()); } _picker->raise(); } - return Inner::event(e); + return Inner::eventHook(e); } void EmojiListWidget::updateSelected() { diff --git a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.h b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.h index 9caedb39d..f6e97c4d3 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.h +++ b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.h @@ -122,7 +122,7 @@ protected: void leaveEventHook(QEvent *e) override; void leaveToChildEvent(QEvent *e, QWidget *child) override; void enterFromChildEvent(QEvent *e, QWidget *child) override; - bool event(QEvent *e) override; + bool eventHook(QEvent *e) override; TabbedSelector::InnerFooter *getFooter() const override; void processHideFinished() override; diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index c660da359..975e1e139 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -113,7 +113,7 @@ void FieldAutocomplete::showStickers(EmojiPtr emoji) { _emoji = emoji; _type = Type::Stickers; if (!emoji) { - rowsUpdated(_mrows, _hrows, _brows, StickerPack(), false); + rowsUpdated(_mrows, _hrows, _brows, Stickers::Pack(), false); return; } @@ -147,7 +147,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { internal::MentionRows mrows; internal::HashtagRows hrows; internal::BotCommandRows brows; - StickerPack srows; + Stickers::Pack srows; if (_emoji) { srows = Stickers::GetListByEmoji(_emoji); } else if (_type == Type::Mentions) { @@ -330,7 +330,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { _inner->setRecentInlineBotsInRows(recentInlineBots); } -void FieldAutocomplete::rowsUpdated(const internal::MentionRows &mrows, const internal::HashtagRows &hrows, const internal::BotCommandRows &brows, const StickerPack &srows, bool resetScroll) { +void FieldAutocomplete::rowsUpdated(const internal::MentionRows &mrows, const internal::HashtagRows &hrows, const internal::BotCommandRows &brows, const Stickers::Pack &srows, bool resetScroll) { if (mrows.isEmpty() && hrows.isEmpty() && brows.isEmpty() && srows.isEmpty()) { if (!isHidden()) { hideAnimated(); @@ -508,7 +508,7 @@ FieldAutocomplete::~FieldAutocomplete() { namespace internal { -FieldAutocompleteInner::FieldAutocompleteInner(FieldAutocomplete *parent, MentionRows *mrows, HashtagRows *hrows, BotCommandRows *brows, StickerPack *srows) +FieldAutocompleteInner::FieldAutocompleteInner(FieldAutocomplete *parent, MentionRows *mrows, HashtagRows *hrows, BotCommandRows *brows, Stickers::Pack *srows) : _parent(parent) , _mrows(mrows) , _hrows(hrows) diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h index 6db13b67e..8f0a5df68 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h @@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #pragma once #include "ui/twidget.h" +#include "chat_helpers/stickers.h" namespace Ui { class ScrollArea; @@ -104,9 +105,9 @@ private: internal::MentionRows _mrows; internal::HashtagRows _hrows; internal::BotCommandRows _brows; - StickerPack _srows; + Stickers::Pack _srows; - void rowsUpdated(const internal::MentionRows &mrows, const internal::HashtagRows &hrows, const internal::BotCommandRows &brows, const StickerPack &srows, bool resetScroll); + void rowsUpdated(const internal::MentionRows &mrows, const internal::HashtagRows &hrows, const internal::BotCommandRows &brows, const Stickers::Pack &srows, bool resetScroll); object_ptr _scroll; QPointer _inner; @@ -141,7 +142,7 @@ class FieldAutocompleteInner final : public TWidget, private base::Subscriber { Q_OBJECT public: - FieldAutocompleteInner(FieldAutocomplete *parent, MentionRows *mrows, HashtagRows *hrows, BotCommandRows *brows, StickerPack *srows); + FieldAutocompleteInner(FieldAutocomplete *parent, MentionRows *mrows, HashtagRows *hrows, BotCommandRows *brows, Stickers::Pack *srows); void clearSel(bool hidden = false); bool moveSel(int key); @@ -179,7 +180,7 @@ private: MentionRows *_mrows; HashtagRows *_hrows; BotCommandRows *_brows; - StickerPack *_srows; + Stickers::Pack *_srows; int32 _stickersPerRow, _recentInlineBotsInRows; int32 _sel, _down; bool _mouseSel; diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp index a68bfa3ab..1d1ae45ff 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp @@ -122,7 +122,10 @@ void GifsListWidget::Footer::processPanelHideFinished() { //_field->setText(QString()); } -GifsListWidget::GifsListWidget(QWidget *parent, not_null controller) : Inner(parent, controller) +GifsListWidget::GifsListWidget( + QWidget *parent, + not_null controller) +: Inner(parent, controller) , _section(Section::Gifs) { updateSize(); @@ -138,9 +141,10 @@ GifsListWidget::GifsListWidget(QWidget *parent, not_null co _inlineRequestTimer.setSingleShot(true); connect(&_inlineRequestTimer, &QTimer::timeout, this, [this] { sendInlineRequest(); }); - subscribe(Auth().data().savedGifsUpdated(), [this] { - refreshSavedGifs(); - }); + Auth().data().savedGifsUpdated() + | rpl::start_with_next([this] { + refreshSavedGifs(); + }, lifetime()); subscribe(Auth().downloaderTaskFinished(), [this] { update(); }); @@ -459,7 +463,7 @@ void GifsListWidget::refreshSavedGifs() { if (_section == Section::Gifs) { clearInlineRows(false); - auto &saved = cSavedGifs(); + auto &saved = Auth().data().savedGifs(); if (!saved.isEmpty()) { _rows.reserve(saved.size()); auto row = Row(); diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h index 169f4bc97..47b507137 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h @@ -40,7 +40,11 @@ class Controller; namespace ChatHelpers { -class GifsListWidget : public TabbedSelector::Inner, public InlineBots::Layout::Context, private base::Subscriber, private MTP::Sender { +class GifsListWidget + : public TabbedSelector::Inner + , public InlineBots::Layout::Context + , private base::Subscriber + , private MTP::Sender { Q_OBJECT public: diff --git a/Telegram/SourceFiles/chat_helpers/stickers.cpp b/Telegram/SourceFiles/chat_helpers/stickers.cpp index 3fead9c56..ad9f28981 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers.cpp @@ -32,16 +32,10 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "styles/style_chat_helpers.h" namespace Stickers { -namespace { - -constexpr int kReadFeaturedSetsTimeoutMs = 1000; -QPointer FeaturedReaderInstance; - -} // namespace void ApplyArchivedResult(const MTPDmessages_stickerSetInstallResultArchive &d) { auto &v = d.vsets.v; - auto &order = Global::RefStickerSetsOrder(); + auto &order = Auth().data().stickerSetsOrderRef(); Order archived; archived.reserve(v.size()); QMap setsToRequest; @@ -89,13 +83,13 @@ void ApplyArchivedResult(const MTPDmessages_stickerSetInstallResultArchive &d) { Ui::Toast::Show(toast); // Ui::show(Box(archived), LayerOption::KeepOther); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } // For testing: Just apply random subset or your sticker sets as archived. bool ApplyArchivedResultFake() { auto sets = QVector(); - for (auto &set : Global::RefStickerSets()) { + for (auto &set : Auth().data().stickerSetsRef()) { if ((set.flags & MTPDstickerSet::Flag::f_installed) && !(set.flags & MTPDstickerSet_ClientFlag::f_special)) { if (rand_value() % 128 < 64) { auto data = MTP_stickerSet(MTP_flags(set.flags | MTPDstickerSet::Flag::f_archived), MTP_long(set.id), MTP_long(set.access), MTP_string(set.title), MTP_string(set.shortName), MTP_int(set.count), MTP_int(set.hash)); @@ -110,7 +104,7 @@ bool ApplyArchivedResultFake() { } void InstallLocally(uint64 setId) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(setId); if (it == sets.end()) { return; @@ -121,7 +115,7 @@ void InstallLocally(uint64 setId) { it->flags |= MTPDstickerSet::Flag::f_installed; auto changedFlags = flags ^ it->flags; - auto &order = Global::RefStickerSetsOrder(); + auto &order = Auth().data().stickerSetsOrderRef(); int insertAtIndex = 0, currentIndex = order.indexOf(setId); if (currentIndex != insertAtIndex) { if (currentIndex > 0) { @@ -143,17 +137,17 @@ void InstallLocally(uint64 setId) { Local::writeInstalledStickers(); if (changedFlags & MTPDstickerSet_ClientFlag::f_unread) Local::writeFeaturedStickers(); if (changedFlags & MTPDstickerSet::Flag::f_archived) { - auto index = Global::RefArchivedStickerSetsOrder().indexOf(setId); + auto index = Auth().data().archivedStickerSetsOrderRef().indexOf(setId); if (index >= 0) { - Global::RefArchivedStickerSetsOrder().removeAt(index); + Auth().data().archivedStickerSetsOrderRef().removeAt(index); Local::writeArchivedStickers(); } } - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } void UndoInstallLocally(uint64 setId) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(setId); if (it == sets.end()) { return; @@ -161,34 +155,23 @@ void UndoInstallLocally(uint64 setId) { it->flags &= ~MTPDstickerSet::Flag::f_installed; - auto &order = Global::RefStickerSetsOrder(); + auto &order = Auth().data().stickerSetsOrderRef(); int currentIndex = order.indexOf(setId); if (currentIndex >= 0) { order.removeAt(currentIndex); } Local::writeInstalledStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); Ui::show( Box(lang(lng_stickers_not_found)), LayerOption::KeepOther); } -void MarkFeaturedAsRead(uint64 setId) { - if (!FeaturedReaderInstance) { - if (auto main = App::main()) { - FeaturedReaderInstance = object_ptr(main); - } else { - return; - } - } - FeaturedReaderInstance->scheduleRead(setId); -} - bool IsFaved(not_null document) { - auto it = Global::StickerSets().constFind(FavedSetId); - return (it != Global::StickerSets().cend()) && it->stickers.contains(document); + auto it = Auth().data().stickerSets().constFind(FavedSetId); + return (it != Auth().data().stickerSets().cend()) && it->stickers.contains(document); } void CheckFavedLimit(Set &set) { @@ -242,7 +225,7 @@ void MoveFavedToFront(Set &set, int index) { void RequestSetToPushFaved(not_null document); void SetIsFaved(not_null document, base::optional>> emojiList = base::none) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(FavedSetId); if (it == sets.end()) { it = sets.insert(FavedSetId, Set(FavedSetId, 0, Lang::Hard::FavedSetTitle(), QString(), 0, 0, MTPDstickerSet_ClientFlag::f_special | 0)); @@ -262,7 +245,7 @@ void SetIsFaved(not_null document, base::optional document) { } void SetIsNotFaved(not_null document) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(FavedSetId); if (it == sets.end()) { return; @@ -330,7 +313,7 @@ void SetIsNotFaved(not_null document) { sets.erase(it); } Local::writeFavedStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } void SetFaved(not_null document, bool faved) { @@ -342,10 +325,10 @@ void SetFaved(not_null document, bool faved) { } void SetsReceived(const QVector &data, int32 hash) { - auto &setsOrder = Global::RefStickerSetsOrder(); + auto &setsOrder = Auth().data().stickerSetsOrderRef(); setsOrder.clear(); - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); QMap setsToRequest; for (auto &set : sets) { if (!(set.flags & MTPDstickerSet::Flag::f_archived)) { @@ -402,10 +385,10 @@ void SetsReceived(const QVector &data, int32 hash) { LOG(("API Error: received stickers hash %1 while counted hash is %2").arg(hash).arg(Local::countStickersHash())); } - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } -void SetPackAndEmoji(Set &set, StickerPack &&pack, const QVector &packs) { +void SetPackAndEmoji(Set &set, Pack &&pack, const QVector &packs) { set.stickers = std::move(pack); set.emoji.clear(); for_const (auto &mtpPack, packs) { @@ -415,7 +398,7 @@ void SetPackAndEmoji(Set &set, StickerPack &&pack, const QVector emoji = emoji->original(); auto &stickers = pack.vdocuments.v; - auto p = StickerPack(); + auto p = Pack(); p.reserve(stickers.size()); for (auto j = 0, c = stickers.size(); j != c; ++j) { auto document = App::document(stickers[j].v); @@ -429,7 +412,7 @@ void SetPackAndEmoji(Set &set, StickerPack &&pack, const QVector } void SpecialSetReceived(uint64 setId, const QString &setTitle, const QVector &items, int32 hash, const QVector &packs) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(setId); auto &d_docs = items; @@ -446,7 +429,7 @@ void SpecialSetReceived(uint64 setId, const QString &setTitle, const QVectorhash = hash; auto custom = sets.find(CustomSetId); - auto pack = StickerPack(); + auto pack = Pack(); pack.reserve(d_docs.size()); for_const (auto &mtpDocument, d_docs) { auto document = App::feedDocument(mtpDocument); @@ -503,7 +486,7 @@ void SpecialSetReceived(uint64 setId, const QString &setTitle, const QVector &data, const QVector &unread, int32 hash) { @@ -512,10 +495,10 @@ void FeaturedSetsReceived(const QVector &data, const QVect unreadMap.insert(unreadSetId.v); } - auto &setsOrder = Global::RefFeaturedStickerSetsOrder(); + auto &setsOrder = Auth().data().featuredStickerSetsOrderRef(); setsOrder.clear(); - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); QMap setsToRequest; for (auto &set : sets) { set.flags &= ~MTPDstickerSet_ClientFlag::f_featured; // mark for removing @@ -587,10 +570,7 @@ void FeaturedSetsReceived(const QVector &data, const QVect it = sets.erase(it); } } - if (Global::FeaturedStickerSetsUnreadCount() != unreadCount) { - Global::SetFeaturedStickerSetsUnreadCount(unreadCount); - Global::RefFeaturedStickerSetsUnreadCountChanged().notify(); - } + Auth().data().setFeaturedStickerSetsUnreadCount(unreadCount); if (Local::countFeaturedStickersHash() != hash) { LOG(("API Error: received featured stickers hash %1 while counted hash is %2").arg(hash).arg(Local::countFeaturedStickersHash())); @@ -606,11 +586,11 @@ void FeaturedSetsReceived(const QVector &data, const QVect Local::writeFeaturedStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } void GifsReceived(const QVector &items, int32 hash) { - auto &saved = cRefSavedGifs(); + auto &saved = Auth().data().savedGifsRef(); saved.clear(); saved.reserve(items.size()); @@ -629,16 +609,16 @@ void GifsReceived(const QVector &items, int32 hash) { Local::writeSavedGifs(); - Auth().data().savedGifsUpdated().notify(); + Auth().data().markSavedGifsUpdated(); } -StickerPack GetListByEmoji(not_null emoji) { +Pack GetListByEmoji(not_null emoji) { auto original = emoji->original(); - auto result = StickerPack(); + auto result = Pack(); auto setsToRequest = QMap(); - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); - auto faved = StickerPack(); + auto faved = Pack(); auto favedIt = sets.find(Stickers::FavedSetId); if (favedIt != sets.cend()) { auto i = favedIt->emoji.constFind(original); @@ -647,7 +627,7 @@ StickerPack GetListByEmoji(not_null emoji) { result = faved; } } - auto &order = Global::StickerSetsOrder(); + auto &order = Auth().data().stickerSetsOrder(); for (auto i = 0, l = order.size(); i != l; ++i) { auto it = sets.find(order[i]); if (it != sets.cend()) { @@ -683,7 +663,7 @@ base::optional>> GetEmojiListFromSet( if (inputSet.type() != mtpc_inputStickerSetID) { return base::none; } - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(inputSet.c_inputStickerSetID().vid.v); if (it == sets.cend()) { return base::none; @@ -703,7 +683,7 @@ base::optional>> GetEmojiListFromSet( } Set *FeedSet(const MTPDstickerSet &set) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(set.vid.v); auto title = GetSetTitle(set); auto flags = MTPDstickerSet::Flags(0); @@ -724,13 +704,13 @@ Set *FeedSet(const MTPDstickerSet &set) { } auto changedFlags = (flags ^ it->flags); if (changedFlags & MTPDstickerSet::Flag::f_archived) { - auto index = Global::ArchivedStickerSetsOrder().indexOf(it->id); + auto index = Auth().data().archivedStickerSetsOrder().indexOf(it->id); if (it->flags & MTPDstickerSet::Flag::f_archived) { if (index < 0) { - Global::RefArchivedStickerSetsOrder().push_front(it->id); + Auth().data().archivedStickerSetsOrderRef().push_front(it->id); } } else if (index >= 0) { - Global::RefArchivedStickerSetsOrder().removeAt(index); + Auth().data().archivedStickerSetsOrderRef().removeAt(index); } } return &it.value(); @@ -744,11 +724,11 @@ Set *FeedSetFull(const MTPmessages_StickerSet &data) { set->flags &= ~MTPDstickerSet_ClientFlag::f_not_loaded; - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto &d_docs = d.vdocuments.v; auto custom = sets.find(Stickers::CustomSetId); - auto pack = StickerPack(); + auto pack = Pack(); pack.reserve(d_docs.size()); for (auto i = 0, l = d_docs.size(); i != l; ++i) { auto doc = App::feedDocument(d_docs.at(i)); @@ -779,8 +759,8 @@ Set *FeedSetFull(const MTPmessages_StickerSet &data) { } if (pack.isEmpty()) { - int removeIndex = Global::StickerSetsOrder().indexOf(set->id); - if (removeIndex >= 0) Global::RefStickerSetsOrder().removeAt(removeIndex); + int removeIndex = Auth().data().stickerSetsOrder().indexOf(set->id); + if (removeIndex >= 0) Auth().data().stickerSetsOrderRef().removeAt(removeIndex); sets.remove(set->id); set = nullptr; } else { @@ -795,7 +775,7 @@ Set *FeedSetFull(const MTPmessages_StickerSet &data) { emoji = emoji->original(); auto &stickers = pack.vdocuments.v; - StickerPack p; + Pack p; p.reserve(stickers.size()); for (auto j = 0, c = stickers.size(); j != c; ++j) { auto doc = App::document(stickers[j].v); @@ -823,7 +803,7 @@ Set *FeedSetFull(const MTPmessages_StickerSet &data) { } } - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); return set; } @@ -836,51 +816,4 @@ QString GetSetTitle(const MTPDstickerSet &s) { return title; } -namespace internal { - -FeaturedReader::FeaturedReader(QObject *parent) : QObject(parent) -, _timer(this) { - _timer->setTimeoutHandler([this] { readSets(); }); -} - -void FeaturedReader::scheduleRead(uint64 setId) { - if (!_setIds.contains(setId)) { - _setIds.insert(setId); - _timer->start(kReadFeaturedSetsTimeoutMs); - } -} - -void FeaturedReader::readSets() { - auto &sets = Global::RefStickerSets(); - auto count = Global::FeaturedStickerSetsUnreadCount(); - QVector wrappedIds; - wrappedIds.reserve(_setIds.size()); - for_const (auto setId, _setIds) { - auto it = sets.find(setId); - if (it != sets.cend()) { - it->flags &= ~MTPDstickerSet_ClientFlag::f_unread; - wrappedIds.append(MTP_long(setId)); - if (count) { - --count; - } - } - } - _setIds.clear(); - - if (!wrappedIds.empty()) { - request(MTPmessages_ReadFeaturedStickers(MTP_vector(wrappedIds))).done([](const MTPBool &result) { - Local::writeFeaturedStickers(); - if (AuthSession::Exists()) { - Auth().data().stickersUpdated().notify(true); - } - }).send(); - - if (Global::FeaturedStickerSetsUnreadCount() != count) { - Global::SetFeaturedStickerSetsUnreadCount(count); - Global::RefFeaturedStickerSetsUnreadCountChanged().notify(); - } - } -} - -} // namespace internal } // namespace Stickers diff --git a/Telegram/SourceFiles/chat_helpers/stickers.h b/Telegram/SourceFiles/chat_helpers/stickers.h index 4d9b737c0..7fd33e75f 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers.h +++ b/Telegram/SourceFiles/chat_helpers/stickers.h @@ -24,13 +24,52 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org namespace Stickers { +constexpr auto DefaultSetId = 0; // for backward compatibility +constexpr auto CustomSetId = 0xFFFFFFFFFFFFFFFFULL; +constexpr auto RecentSetId = 0xFFFFFFFFFFFFFFFEULL; // for emoji/stickers panel, should not appear in Sets +constexpr auto NoneSetId = 0xFFFFFFFFFFFFFFFDULL; // for emoji/stickers panel, should not appear in Sets +constexpr auto CloudRecentSetId = 0xFFFFFFFFFFFFFFFCULL; // for cloud-stored recent stickers +constexpr auto FeaturedSetId = 0xFFFFFFFFFFFFFFFBULL; // for emoji/stickers panel, should not appear in Sets +constexpr auto FavedSetId = 0xFFFFFFFFFFFFFFFAULL; // for cloud-stored faved stickers +constexpr auto MegagroupSetId = 0xFFFFFFFFFFFFFFEFULL; // for setting up megagroup sticker set + +using Order = QList; +using SavedGifs = QVector; +using Pack = QVector; +using ByEmojiMap = QMap; + +struct Set { + Set(uint64 id, uint64 access, const QString &title, const QString &shortName, int32 count, int32 hash, MTPDstickerSet::Flags flags) + : id(id) + , access(access) + , title(title) + , shortName(shortName) + , count(count) + , hash(hash) + , flags(flags) { + } + uint64 id, access; + QString title, shortName; + int32 count, hash; + MTPDstickerSet::Flags flags; + Pack stickers; + ByEmojiMap emoji; +}; +using Sets = QMap; + +inline MTPInputStickerSet inputSetId(const Set &set) { + if (set.id && set.access) { + return MTP_inputStickerSetID(MTP_long(set.id), MTP_long(set.access)); + } + return MTP_inputStickerSetShortName(MTP_string(set.shortName)); +} + constexpr auto kPanelPerRow = 5; void ApplyArchivedResult(const MTPDmessages_stickerSetInstallResultArchive &d); bool ApplyArchivedResultFake(); // For testing. void InstallLocally(uint64 setId); void UndoInstallLocally(uint64 setId); -void MarkFeaturedAsRead(uint64 setId); bool IsFaved(not_null document); void SetFaved(not_null document, bool faved); @@ -39,7 +78,7 @@ void SpecialSetReceived(uint64 setId, const QString &setTitle, const QVector &data, const QVector &unread, int32 hash); void GifsReceived(const QVector &items, int32 hash); -StickerPack GetListByEmoji(not_null emoji); +Pack GetListByEmoji(not_null emoji); base::optional>> GetEmojiListFromSet( not_null document); @@ -48,20 +87,4 @@ Set *FeedSetFull(const MTPmessages_StickerSet &data); QString GetSetTitle(const MTPDstickerSet &s); -namespace internal { - -class FeaturedReader : public QObject, private MTP::Sender { -public: - FeaturedReader(QObject *parent); - void scheduleRead(uint64 setId); - -private: - void readSets(); - - object_ptr _timer; - OrderedSet _setIds; - -}; - -} // namespace internal } // namespace Stickers diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index 671ef4b2f..2653fa2b3 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -398,7 +398,7 @@ void StickersListWidget::Footer::paintStickerSettingsIcon(Painter &p) const { } void StickersListWidget::Footer::paintFeaturedStickerSetsBadge(Painter &p, int iconLeft) const { - if (auto unread = Global::FeaturedStickerSetsUnreadCount()) { + if (auto unread = Auth().data().featuredStickerSetsUnreadCount()) { Dialogs::Layout::UnreadBadgeStyle unreadSt; unreadSt.sizeId = Dialogs::Layout::UnreadBadgeInStickersPanel; unreadSt.size = st::stickersSettingsUnreadSize; @@ -502,7 +502,7 @@ void StickersListWidget::readVisibleSets() { } } if (loaded == count) { - Stickers::MarkFeaturedAsRead(set.id); + Auth().api().readFeaturedSetDelayed(set.id); } } } @@ -1049,7 +1049,7 @@ void StickersListWidget::removeRecentSticker(int section, int index) { break; } } - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(Stickers::CustomSetId); if (it != sets.cend()) { for (int i = 0, l = it->stickers.size(); i < l; ++i) { @@ -1135,20 +1135,20 @@ void StickersListWidget::refreshStickers() { _mySets.clear(); _favedStickersMap.clear(); - _mySets.reserve(Global::StickerSetsOrder().size() + 3); + _mySets.reserve(Auth().data().stickerSetsOrder().size() + 3); refreshFavedStickers(); refreshRecentStickers(false); refreshMegagroupStickers(GroupStickersPlace::Visible); - for_const (auto setId, Global::StickerSetsOrder()) { + for_const (auto setId, Auth().data().stickerSetsOrder()) { appendSet(_mySets, setId, AppendSkip::Archived); } refreshMegagroupStickers(GroupStickersPlace::Hidden); _featuredSets.clear(); - _featuredSets.reserve(Global::FeaturedStickerSetsOrder().size()); + _featuredSets.reserve(Auth().data().featuredStickerSetsOrder().size()); - for_const (auto setId, Global::FeaturedStickerSetsOrder()) { + for_const (auto setId, Auth().data().featuredStickerSetsOrder()) { appendSet(_featuredSets, setId, AppendSkip::Installed); } @@ -1203,7 +1203,7 @@ uint64 StickersListWidget::currentSet(int yOffset) const { } void StickersListWidget::appendSet(Sets &to, uint64 setId, AppendSkip skip) { - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(setId); if (it == sets.cend() || it->stickers.isEmpty()) return; if ((skip == AppendSkip::Archived) && (it->flags & MTPDstickerSet::Flag::f_archived)) return; @@ -1228,12 +1228,12 @@ void StickersListWidget::refreshRecent() { void StickersListWidget::refreshRecentStickers(bool performResize) { _custom.clear(); clearSelection(); - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto &recent = cGetRecentStickers(); auto customIt = sets.constFind(Stickers::CustomSetId); auto cloudIt = sets.constFind(Stickers::CloudRecentSetId); - StickerPack recentPack; + Stickers::Pack recentPack; int customCnt = (customIt == sets.cend()) ? 0 : customIt->stickers.size(); int cloudCnt = (cloudIt == sets.cend()) ? 0 : cloudIt->stickers.size(); recentPack.reserve(cloudCnt + recent.size() + customCnt); @@ -1285,7 +1285,7 @@ void StickersListWidget::refreshRecentStickers(bool performResize) { void StickersListWidget::refreshFavedStickers() { clearSelection(); - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(Stickers::FavedSetId); if (it == sets.cend() || it->stickers.isEmpty()) { return; @@ -1324,7 +1324,7 @@ void StickersListWidget::refreshMegagroupStickers(GroupStickersPlace place) { } if (_megagroupSet->mgInfo->stickerSet.type() == mtpc_inputStickerSetID) { auto &set = _megagroupSet->mgInfo->stickerSet.c_inputStickerSetID(); - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(set.vid.v); if (it != sets.cend()) { auto isInstalled = (it->flags & MTPDstickerSet::Flag::f_installed) @@ -1350,7 +1350,7 @@ void StickersListWidget::refreshMegagroupStickers(GroupStickersPlace place) { void StickersListWidget::fillIcons(QList &icons) { icons.clear(); icons.reserve(_mySets.size() + 1); - if (Global::FeaturedStickerSetsUnreadCount() && !_featuredSets.isEmpty()) { + if (Auth().data().featuredStickerSetsUnreadCount() && !_featuredSets.isEmpty()) { icons.push_back(StickerIcon(Stickers::FeaturedSetId)); } @@ -1384,7 +1384,7 @@ void StickersListWidget::fillIcons(QList &icons) { icons.push_back(StickerIcon(_mySets[i].id, s, pixw, pixh)); } - if (!Global::FeaturedStickerSetsUnreadCount() && !_featuredSets.isEmpty()) { + if (!Auth().data().featuredStickerSetsUnreadCount() && !_featuredSets.isEmpty()) { icons.push_back(StickerIcon(Stickers::FeaturedSetId)); } } @@ -1623,7 +1623,7 @@ void StickersListWidget::displaySet(uint64 setId) { return; } } - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(setId); if (it != sets.cend()) { _displayingSetId = setId; @@ -1638,7 +1638,7 @@ void StickersListWidget::displaySet(uint64 setId) { } void StickersListWidget::installSet(uint64 setId) { - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(setId); if (it != sets.cend()) { request(MTPmessages_InstallStickerSet(Stickers::inputSetId(*it), MTP_bool(false))).done([this](const MTPmessages_StickerSetInstallResult &result) { @@ -1678,14 +1678,14 @@ void StickersListWidget::removeMegagroupSet(bool locally) { } void StickersListWidget::removeSet(uint64 setId) { - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(setId); if (it != sets.cend()) { _removingSetId = it->id; auto text = lng_stickers_remove_pack(lt_sticker_pack, it->title); Ui::show(Box(text, lang(lng_box_remove), base::lambda_guarded(this, [this] { Ui::hideLayer(); - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(_removingSetId); if (it != sets.cend()) { if (it->id && it->access) { @@ -1707,8 +1707,8 @@ void StickersListWidget::removeSet(uint64 setId) { if (!(it->flags & MTPDstickerSet_ClientFlag::f_featured) && !(it->flags & MTPDstickerSet_ClientFlag::f_special)) { sets.erase(it); } - int removeIndex = Global::StickerSetsOrder().indexOf(_removingSetId); - if (removeIndex >= 0) Global::RefStickerSetsOrder().removeAt(removeIndex); + int removeIndex = Auth().data().stickerSetsOrder().indexOf(_removingSetId); + if (removeIndex >= 0) Auth().data().stickerSetsOrderRef().removeAt(removeIndex); refreshStickers(); Local::writeInstalledStickers(); if (writeRecent) Local::writeUserSettings(); diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h index 7113689a5..60c33909c 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h @@ -35,7 +35,10 @@ namespace ChatHelpers { struct StickerIcon; -class StickersListWidget : public TabbedSelector::Inner, private base::Subscriber, private MTP::Sender { +class StickersListWidget + : public TabbedSelector::Inner + , private base::Subscriber + , private MTP::Sender { Q_OBJECT public: @@ -135,12 +138,12 @@ private: }; struct Set { - Set(uint64 id, MTPDstickerSet::Flags flags, const QString &title, int32 hoversSize, const StickerPack &pack = StickerPack()) : id(id), flags(flags), title(title), pack(pack) { + Set(uint64 id, MTPDstickerSet::Flags flags, const QString &title, int32 hoversSize, const Stickers::Pack &pack = Stickers::Pack()) : id(id), flags(flags), title(title), pack(pack) { } uint64 id; MTPDstickerSet::Flags flags; QString title; - StickerPack pack; + Stickers::Pack pack; QSharedPointer ripple; }; using Sets = QList; diff --git a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp index 34a91c139..c999e1b15 100644 --- a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp +++ b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp @@ -698,7 +698,10 @@ void TabbedSelector::scrollToY(int y) { _topShadow->update(); } -TabbedSelector::Inner::Inner(QWidget *parent, not_null controller) : TWidget(parent) +TabbedSelector::Inner::Inner( + QWidget *parent, + not_null controller) +: RpWidget(parent) , _controller(controller) { } diff --git a/Telegram/SourceFiles/chat_helpers/tabbed_selector.h b/Telegram/SourceFiles/chat_helpers/tabbed_selector.h index 434e1aa3c..c09c0b490 100644 --- a/Telegram/SourceFiles/chat_helpers/tabbed_selector.h +++ b/Telegram/SourceFiles/chat_helpers/tabbed_selector.h @@ -206,7 +206,7 @@ private: }; -class TabbedSelector::Inner : public TWidget { +class TabbedSelector::Inner : public Ui::RpWidget { Q_OBJECT public: diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index fdd97db38..1d83bf9cf 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -137,12 +137,12 @@ QString saveFileName(const QString &title, const QString &filter, const QString bool StickerData::setInstalled() const { switch (set.type()) { case mtpc_inputStickerSetID: { - auto it = Global::StickerSets().constFind(set.c_inputStickerSetID().vid.v); - return (it != Global::StickerSets().cend()) && !(it->flags & MTPDstickerSet::Flag::f_archived) && (it->flags & MTPDstickerSet::Flag::f_installed); + auto it = Auth().data().stickerSets().constFind(set.c_inputStickerSetID().vid.v); + return (it != Auth().data().stickerSets().cend()) && !(it->flags & MTPDstickerSet::Flag::f_archived) && (it->flags & MTPDstickerSet::Flag::f_installed); } break; case mtpc_inputStickerSetShortName: { auto name = qs(set.c_inputStickerSetShortName().vshort_name).toLower(); - for (auto it = Global::StickerSets().cbegin(), e = Global::StickerSets().cend(); it != e; ++it) { + for (auto it = Auth().data().stickerSets().cbegin(), e = Auth().data().stickerSets().cend(); it != e; ++it) { if (it->shortName.toLower() == name) { return !(it->flags & MTPDstickerSet::Flag::f_archived) && (it->flags & MTPDstickerSet::Flag::f_installed); } diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index 98ddf121d..abe936159 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -309,46 +309,6 @@ enum Flags { }; } // namespace DebugLogging -namespace Stickers { - -constexpr auto DefaultSetId = 0; // for backward compatibility -constexpr auto CustomSetId = 0xFFFFFFFFFFFFFFFFULL; -constexpr auto RecentSetId = 0xFFFFFFFFFFFFFFFEULL; // for emoji/stickers panel, should not appear in Sets -constexpr auto NoneSetId = 0xFFFFFFFFFFFFFFFDULL; // for emoji/stickers panel, should not appear in Sets -constexpr auto CloudRecentSetId = 0xFFFFFFFFFFFFFFFCULL; // for cloud-stored recent stickers -constexpr auto FeaturedSetId = 0xFFFFFFFFFFFFFFFBULL; // for emoji/stickers panel, should not appear in Sets -constexpr auto FavedSetId = 0xFFFFFFFFFFFFFFFAULL; // for cloud-stored faved stickers -constexpr auto MegagroupSetId = 0xFFFFFFFFFFFFFFEFULL; // for setting up megagroup sticker set - -struct Set { - Set(uint64 id, uint64 access, const QString &title, const QString &shortName, int32 count, int32 hash, MTPDstickerSet::Flags flags) - : id(id) - , access(access) - , title(title) - , shortName(shortName) - , count(count) - , hash(hash) - , flags(flags) { - } - uint64 id, access; - QString title, shortName; - int32 count, hash; - MTPDstickerSet::Flags flags; - StickerPack stickers; - StickersByEmojiMap emoji; -}; -using Sets = QMap; -using Order = QList; - -inline MTPInputStickerSet inputSetId(const Set &set) { - if (set.id && set.access) { - return MTP_inputStickerSetID(MTP_long(set.id), MTP_long(set.access)); - } - return MTP_inputStickerSetShortName(MTP_string(set.shortName)); -} - -} // namespace Stickers - namespace Global { bool started(); @@ -414,17 +374,6 @@ DeclareVar(HiddenPinnedMessagesMap, HiddenPinnedMessages); typedef OrderedSet PendingItemsMap; DeclareRefVar(PendingItemsMap, PendingRepaintItems); -DeclareVar(Stickers::Sets, StickerSets); -DeclareVar(Stickers::Order, StickerSetsOrder); -DeclareVar(TimeMs, LastStickersUpdate); -DeclareVar(TimeMs, LastRecentStickersUpdate); -DeclareVar(TimeMs, LastFavedStickersUpdate); -DeclareVar(Stickers::Order, FeaturedStickerSetsOrder); -DeclareVar(int, FeaturedStickerSetsUnreadCount); -DeclareRefVar(base::Observable, FeaturedStickerSetsUnreadCountChanged); -DeclareVar(TimeMs, LastFeaturedStickersUpdate); -DeclareVar(Stickers::Order, ArchivedStickerSetsOrder); - typedef QMap CircleMasksMap; DeclareRefVar(CircleMasksMap, CircleMasks); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 04f9364bb..bb17b433a 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -971,12 +971,13 @@ int HistoryWidget::itemTopForHighlight(not_null item) const { } void HistoryWidget::start() { - subscribe(Auth().data().stickersUpdated(), [this] { - _tabbedSelector->refreshStickers(); - updateStickersByEmoji(); - }); + Auth().data().stickersUpdated() + | rpl::start_with_next([this] { + _tabbedSelector->refreshStickers(); + updateStickersByEmoji(); + }, lifetime()); updateRecentStickers(); - Auth().data().savedGifsUpdated().notify(); + Auth().data().markSavedGifsUpdated(); subscribe(Auth().api().fullPeerUpdated(), [this](PeerData *peer) { fullPeerUpdated(peer); }); @@ -1473,7 +1474,7 @@ bool HistoryWidget::cmd_previous_chat() { } void HistoryWidget::saveGif(DocumentData *doc) { - if (doc->isGifv() && cSavedGifs().indexOf(doc) != 0) { + if (doc->isGifv() && Auth().data().savedGifs().indexOf(doc) != 0) { MTPInputDocument mtpInput = doc->mtpInput(); if (mtpInput.type() != mtpc_inputDocumentEmpty) { MTP::send(MTPmessages_SaveGif(mtpInput, MTP_bool(false)), rpcDone(&HistoryWidget::saveGifDone, doc)); diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp index dc75a7f23..41f0379da 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp @@ -124,14 +124,14 @@ void Gif::setPosition(int32 position) { } void DeleteSavedGifClickHandler::onClickImpl() const { - auto index = cSavedGifs().indexOf(_data); + auto index = Auth().data().savedGifs().indexOf(_data); if (index >= 0) { - cRefSavedGifs().remove(index); + Auth().data().savedGifsRef().remove(index); Local::writeSavedGifs(); MTP::send(MTPmessages_SaveGif(_data->mtpInput(), MTP_bool(true))); } - Auth().data().savedGifsUpdated().notify(); + Auth().data().markSavedGifsUpdated(); } void Gif::paint(Painter &p, const QRect &clip, const PaintContext *context) const { diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 0b08276c4..3c63e521f 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -4714,7 +4714,7 @@ void MainWidget::incrementSticker(DocumentData *sticker) { if (sticker->sticker()->set.type() == mtpc_inputStickerSetEmpty) return; bool writeRecentStickers = false; - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(Stickers::CloudRecentSetId); if (it == sets.cend()) { if (it == sets.cend()) { @@ -5703,7 +5703,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { if (set.vset.type() == mtpc_stickerSet) { auto &s = set.vset.c_stickerSet(); if (!s.is_masks()) { - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); auto it = sets.find(s.vid.v); if (it == sets.cend()) { it = sets.insert(s.vid.v, Stickers::Set(s.vid.v, s.vaccess_hash.v, Stickers::GetSetTitle(s), qs(s.vshort_name), s.vcount.v, s.vhash.v, s.vflags.v | MTPDstickerSet::Flag::f_installed)); @@ -5736,7 +5736,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { emoji = emoji->original(); auto &stickers = pack.vdocuments.v; - StickerPack p; + Stickers::Pack p; p.reserve(stickers.size()); for (auto j = 0, c = stickers.size(); j != c; ++j) { auto doc = App::document(stickers[j].v); @@ -5748,7 +5748,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } } - auto &order(Global::RefStickerSetsOrder()); + auto &order = Auth().data().stickerSetsOrderRef(); int32 insertAtIndex = 0, currentIndex = order.indexOf(s.vid.v); if (currentIndex != insertAtIndex) { if (currentIndex > 0) { @@ -5769,7 +5769,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } Local::writeInstalledStickers(); if (writeArchived) Local::writeArchivedStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } } } @@ -5779,7 +5779,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { auto &d = update.c_updateStickerSetsOrder(); if (!d.is_masks()) { auto &order = d.vorder.v; - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); Stickers::Order result; for (int i = 0, l = order.size(); i < l; ++i) { if (sets.constFind(order.at(i).v) == sets.cend()) { @@ -5787,29 +5787,29 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } result.push_back(order.at(i).v); } - if (result.size() != Global::StickerSetsOrder().size() || result.size() != order.size()) { - Global::SetLastStickersUpdate(0); + if (result.size() != Auth().data().stickerSetsOrder().size() || result.size() != order.size()) { + Auth().data().setLastStickersUpdate(0); Auth().api().updateStickers(); } else { - Global::SetStickerSetsOrder(result); + Auth().data().stickerSetsOrderRef() = std::move(result); Local::writeInstalledStickers(); - Auth().data().stickersUpdated().notify(true); + Auth().data().markStickersUpdated(); } } } break; case mtpc_updateStickerSets: { - Global::SetLastStickersUpdate(0); + Auth().data().setLastStickersUpdate(0); Auth().api().updateStickers(); } break; case mtpc_updateRecentStickers: { - Global::SetLastRecentStickersUpdate(0); + Auth().data().setLastRecentStickersUpdate(0); Auth().api().updateStickers(); } break; case mtpc_updateFavedStickers: { - Global::SetLastFavedStickersUpdate(0); + Auth().data().setLastFavedStickersUpdate(0); Auth().api().updateStickers(); } break; @@ -5817,13 +5817,13 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { // We read some of the featured stickers, perhaps not all of them. // Here we don't know what featured sticker sets were read, so we // request all of them once again. - Global::SetLastFeaturedStickersUpdate(0); + Auth().data().setLastFeaturedStickersUpdate(0); Auth().api().updateStickers(); } break; ////// Cloud saved GIFs case mtpc_updateSavedGifs: { - cSetLastSavedGifsUpdate(0); + Auth().data().setLastSavedGifsUpdate(0); Auth().api().updateStickers(); } break; diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp index 446c331d9..a8de70ed9 100644 --- a/Telegram/SourceFiles/settings.cpp +++ b/Telegram/SourceFiles/settings.cpp @@ -78,9 +78,6 @@ EmojiColorVariants gEmojiVariants; RecentStickerPreload gRecentStickersPreload; RecentStickerPack gRecentStickers; -SavedGifs gSavedGifs; -TimeMs gLastSavedGifsUpdate = 0; - RecentHashtagPack gRecentWriteHashtags, gRecentSearchHashtags; RecentInlineBots gRecentInlineBots; diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index e2de77ff3..51dab5e1b 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -168,7 +168,6 @@ DeclareSetting(RecentEmojiPreload, RecentEmojiPreload); DeclareRefSetting(EmojiColorVariants, EmojiVariants); class DocumentData; -typedef QVector StickerPack; typedef QList > RecentStickerPackOld; typedef QVector > RecentStickerPreload; @@ -178,12 +177,6 @@ DeclareRefSetting(RecentStickerPack, RecentStickers); RecentStickerPack &cGetRecentStickers(); -typedef QMap StickersByEmojiMap; - -typedef QVector SavedGifs; -DeclareRefSetting(SavedGifs, SavedGifs); -DeclareSetting(TimeMs, LastSavedGifsUpdate); - typedef QList > RecentHashtagPack; DeclareRefSetting(RecentHashtagPack, RecentWriteHashtags); DeclareSetting(RecentHashtagPack, RecentSearchHashtags); diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index 51b545849..6aaf0d9ae 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -3198,7 +3198,7 @@ void _writeStickerSet(QDataStream &stream, const Stickers::Set &set) { } stream << quint64(set.id) << quint64(set.access) << set.title << set.shortName << qint32(set.stickers.size()) << qint32(set.hash) << qint32(set.flags); - for (StickerPack::const_iterator j = set.stickers.cbegin(), e = set.stickers.cend(); j != e; ++j) { + for (auto j = set.stickers.cbegin(), e = set.stickers.cend(); j != e; ++j) { Serialize::Document::writeToStream(stream, *j); } @@ -3226,7 +3226,7 @@ template void _writeStickerSets(FileKey &stickersKey, CheckSet checkSet, const Stickers::Order &order) { if (!_working()) return; - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); if (sets.isEmpty()) { if (stickersKey) { clearKey(stickersKey); @@ -3304,7 +3304,7 @@ void _readStickerSets(FileKey &stickersKey, Stickers::Order *outOrder = nullptr, bool readingInstalled = (readingFlags == MTPDstickerSet::Flag::f_installed); - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); if (outOrder) outOrder->clear(); quint32 cnt; @@ -3406,7 +3406,7 @@ void _readStickerSets(FileKey &stickersKey, Stickers::Order *outOrder = nullptr, QString emojiString; qint32 stickersCount; stickers.stream >> emojiString >> stickersCount; - StickerPack pack; + Stickers::Pack pack; pack.reserve(stickersCount); for (int32 k = 0; k < stickersCount; ++k) { quint64 id; @@ -3460,7 +3460,7 @@ void writeInstalledStickers() { return StickerSetCheckResult::Skip; } return StickerSetCheckResult::Write; - }, Global::StickerSetsOrder()); + }, Auth().data().stickerSetsOrder()); } void writeFeaturedStickers() { @@ -3479,7 +3479,7 @@ void writeFeaturedStickers() { return StickerSetCheckResult::Skip; } return StickerSetCheckResult::Write; - }, Global::FeaturedStickerSetsOrder()); + }, Auth().data().featuredStickerSetsOrder()); } void writeRecentStickers() { @@ -3512,7 +3512,7 @@ void writeArchivedStickers() { return StickerSetCheckResult::Skip; } return StickerSetCheckResult::Write; - }, Global::ArchivedStickerSetsOrder()); + }, Auth().data().archivedStickerSetsOrder()); } void importOldRecentStickers() { @@ -3526,10 +3526,10 @@ void importOldRecentStickers() { return; } - auto &sets = Global::RefStickerSets(); + auto &sets = Auth().data().stickerSetsRef(); sets.clear(); - auto &order = Global::RefStickerSetsOrder(); + auto &order = Auth().data().stickerSetsOrderRef(); order.clear(); auto &recent = cRefRecentStickers(); @@ -3596,25 +3596,22 @@ void readInstalledStickers() { return importOldRecentStickers(); } - Global::RefStickerSets().clear(); - _readStickerSets(_installedStickersKey, &Global::RefStickerSetsOrder(), MTPDstickerSet::Flag::f_installed); + Auth().data().stickerSetsRef().clear(); + _readStickerSets(_installedStickersKey, &Auth().data().stickerSetsOrderRef(), MTPDstickerSet::Flag::f_installed); } void readFeaturedStickers() { - _readStickerSets(_featuredStickersKey, &Global::RefFeaturedStickerSetsOrder(), MTPDstickerSet::Flags() | MTPDstickerSet_ClientFlag::f_featured); + _readStickerSets(_featuredStickersKey, &Auth().data().featuredStickerSetsOrderRef(), MTPDstickerSet::Flags() | MTPDstickerSet_ClientFlag::f_featured); - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); int unreadCount = 0; - for_const (auto setId, Global::FeaturedStickerSetsOrder()) { + for_const (auto setId, Auth().data().featuredStickerSetsOrder()) { auto it = sets.constFind(setId); if (it != sets.cend() && (it->flags & MTPDstickerSet_ClientFlag::f_unread)) { ++unreadCount; } } - if (Global::FeaturedStickerSetsUnreadCount() != unreadCount) { - Global::SetFeaturedStickerSetsUnreadCount(unreadCount); - Global::RefFeaturedStickerSetsUnreadCountChanged().notify(); - } + Auth().data().setFeaturedStickerSetsUnreadCount(unreadCount); } void readRecentStickers() { @@ -3628,7 +3625,7 @@ void readFavedStickers() { void readArchivedStickers() { static bool archivedStickersRead = false; if (!archivedStickersRead) { - _readStickerSets(_archivedStickersKey, &Global::RefArchivedStickerSetsOrder()); + _readStickerSets(_archivedStickersKey, &Auth().data().archivedStickerSetsOrderRef()); archivedStickersRead = true; } } @@ -3644,7 +3641,7 @@ int32 countDocumentVectorHash(const QVector vector) { } int32 countSpecialStickerSetHash(uint64 setId) { - auto &sets = Global::StickerSets(); + auto &sets = Auth().data().stickerSets(); auto it = sets.constFind(setId); if (it != sets.cend()) { return countDocumentVectorHash(it->stickers); @@ -3655,8 +3652,8 @@ int32 countSpecialStickerSetHash(uint64 setId) { int32 countStickersHash(bool checkOutdatedInfo) { uint32 acc = 0; bool foundOutdated = false; - auto &sets = Global::StickerSets(); - auto &order = Global::StickerSetsOrder(); + auto &sets = Auth().data().stickerSets(); + auto &order = Auth().data().stickerSetsOrder(); for (auto i = order.cbegin(), e = order.cend(); i != e; ++i) { auto j = sets.constFind(*i); if (j != sets.cend()) { @@ -3681,8 +3678,8 @@ int32 countFavedStickersHash() { int32 countFeaturedStickersHash() { uint32 acc = 0; - auto &sets = Global::StickerSets(); - auto &featured = Global::FeaturedStickerSetsOrder(); + auto &sets = Auth().data().stickerSets(); + auto &featured = Auth().data().featuredStickerSetsOrder(); for_const (auto setId, featured) { acc = (acc * 20261) + uint32(setId >> 32); acc = (acc * 20261) + uint32(setId & 0xFFFFFFFF); @@ -3696,13 +3693,13 @@ int32 countFeaturedStickersHash() { } int32 countSavedGifsHash() { - return countDocumentVectorHash(cSavedGifs()); + return countDocumentVectorHash(Auth().data().savedGifs()); } void writeSavedGifs() { if (!_working()) return; - auto &saved = cSavedGifs(); + auto &saved = Auth().data().savedGifs(); if (saved.isEmpty()) { if (_savedGifsKey) { clearKey(_savedGifsKey); @@ -3742,7 +3739,7 @@ void readSavedGifs() { return; } - SavedGifs &saved(cRefSavedGifs()); + auto &saved = Auth().data().savedGifsRef(); saved.clear(); quint32 cnt; diff --git a/Telegram/SourceFiles/storage/serialize_document.cpp b/Telegram/SourceFiles/storage/serialize_document.cpp index ca70d0230..6e91322bd 100644 --- a/Telegram/SourceFiles/storage/serialize_document.cpp +++ b/Telegram/SourceFiles/storage/serialize_document.cpp @@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "storage/serialize_document.h" #include "storage/serialize_common.h" +#include "chat_helpers/stickers.h" namespace { diff --git a/Telegram/SourceFiles/ui/widgets/input_fields.cpp b/Telegram/SourceFiles/ui/widgets/input_fields.cpp index 6b6c41d12..39c95db12 100644 --- a/Telegram/SourceFiles/ui/widgets/input_fields.cpp +++ b/Telegram/SourceFiles/ui/widgets/input_fields.cpp @@ -3632,8 +3632,8 @@ void MaskedInputField::keyPressEvent(QKeyEvent *e) { QLineEdit::keyPressEvent(e); } - QString newText(text()); - int32 newCursor(cursorPosition()); + auto newText = text(); + auto newCursor = cursorPosition(); if (wasText == newText && wasCursor == newCursor) { // call correct manually correctValue(wasText, wasCursor, newText, newCursor); _oldtext = newText; @@ -3700,7 +3700,11 @@ void CountryCodeInput::codeSelected(const QString &code) { emit changed(); } -void CountryCodeInput::correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { +void CountryCodeInput::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { QString newText, addToNumber; int oldPos(nowCursor), newPos(-1), oldLen(now.length()), start = 0, digits = 5; newText.reserve(oldLen + 1); @@ -3767,7 +3771,11 @@ void PhonePartInput::keyPressEvent(QKeyEvent *e) { } } -void PhonePartInput::correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { +void PhonePartInput::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { QString newText; int oldPos(nowCursor), newPos(-1), oldLen(now.length()), digitCount = 0; for (int i = 0; i < oldLen; ++i) { @@ -3875,7 +3883,11 @@ PortInput::PortInput(QWidget *parent, const style::InputField &st, base::lambda< } } -void PortInput::correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { +void PortInput::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { QString newText; newText.reserve(now.size()); auto newPos = nowCursor; @@ -3916,7 +3928,11 @@ void UsernameInput::paintAdditionalPlaceholder(Painter &p, TimeMs ms) { } } -void UsernameInput::correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { +void UsernameInput::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { auto newPos = nowCursor; auto from = 0, len = now.size(); for (; from < len; ++from) { @@ -3982,7 +3998,11 @@ void PhoneInput::paintAdditionalPlaceholder(Painter &p, TimeMs ms) { } } -void PhoneInput::correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { +void PhoneInput::correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { auto digits = now; digits.replace(QRegularExpression(qsl("[^\\d]")), QString()); _pattern = phoneNumberParse(digits); diff --git a/Telegram/SourceFiles/ui/widgets/input_fields.h b/Telegram/SourceFiles/ui/widgets/input_fields.h index f7a6317ce..cfb77f7e0 100644 --- a/Telegram/SourceFiles/ui/widgets/input_fields.h +++ b/Telegram/SourceFiles/ui/widgets/input_fields.h @@ -142,7 +142,10 @@ protected: void dropEvent(QDropEvent *e) override; void contextMenuEvent(QContextMenuEvent *e) override; - virtual void correctValue(const QString &was, QString &now, TagList &nowTags) { + virtual void correctValue( + const QString &was, + QString &now, + TagList &nowTags) { } void insertEmoji(EmojiPtr emoji, QTextCursor c); @@ -755,7 +758,11 @@ protected: void contextMenuEvent(QContextMenuEvent *e) override; void inputMethodEvent(QInputMethodEvent *e) override; - virtual void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) { + virtual void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) { } void setCorrectedText(QString &now, int &nowCursor, const QString &newText, int newPos); @@ -832,7 +839,11 @@ signals: void addedToNumber(const QString &added); protected: - void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override; + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; private: bool _nosignal; @@ -855,7 +866,11 @@ signals: protected: void keyPressEvent(QKeyEvent *e) override; - void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override; + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; void paintAdditionalPlaceholder(Painter &p, TimeMs ms) override; private: @@ -875,7 +890,11 @@ public: PortInput(QWidget *parent, const style::InputField &st, base::lambda placeholderFactory, const QString &val); protected: - void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override; + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; }; @@ -886,7 +905,11 @@ public: void setLinkPlaceholder(const QString &placeholder); protected: - void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override; + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; void paintAdditionalPlaceholder(Painter &p, TimeMs ms) override; private: @@ -903,7 +926,11 @@ public: protected: void focusInEvent(QFocusEvent *e) override; - void correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) override; + void correctValue( + const QString &was, + int wasCursor, + QString &now, + int &nowCursor) override; void paintAdditionalPlaceholder(Painter &p, TimeMs ms) override; private: