diff --git a/Telegram/SourceFiles/data/data_chat_filters.cpp b/Telegram/SourceFiles/data/data_chat_filters.cpp index 4ed74938d7..d77b47c02e 100644 --- a/Telegram/SourceFiles/data/data_chat_filters.cpp +++ b/Telegram/SourceFiles/data/data_chat_filters.cpp @@ -534,6 +534,28 @@ const std::vector &ChatFilters::list() const { return _list; } +FilterId ChatFilters::defaultId() const { + return lookupId(0); +} + +FilterId ChatFilters::lookupId(int index) const { + Expects(index >= 0 && index < _list.size()); + + if (_owner->session().user()->isPremium() || !_list.front().id()) { + return _list[index].id(); + } + const auto i = ranges::find(_list, FilterId(0), &ChatFilter::id); + return !index + ? FilterId() + : (index <= int(i - begin(_list))) + ? _list[index - 1].id() + : _list[index].id(); +} + +bool ChatFilters::loaded() const { + return _loaded; +} + bool ChatFilters::has() const { return _list.size() > 1; } diff --git a/Telegram/SourceFiles/data/data_chat_filters.h b/Telegram/SourceFiles/data/data_chat_filters.h index 45b7d12511..b4637e0c2d 100644 --- a/Telegram/SourceFiles/data/data_chat_filters.h +++ b/Telegram/SourceFiles/data/data_chat_filters.h @@ -102,8 +102,12 @@ public: void moveAllToFront(); [[nodiscard]] const std::vector &list() const; [[nodiscard]] rpl::producer<> changed() const; + [[nodiscard]] bool loaded() const; [[nodiscard]] bool has() const; + [[nodiscard]] FilterId defaultId() const; + [[nodiscard]] FilterId lookupId(int index) const; + bool loadNextExceptions(bool chatsListLoaded); void refreshHistory(not_null history); diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp index 3f4af37037..d7aa1fc0e2 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp @@ -3205,20 +3205,19 @@ void InnerWidget::setupShortcuts() { }); if (session().data().chatsFilters().has()) { - const auto filters = &session().data().chatsFilters().list(); - const auto filtersCount = int(filters->size()); + const auto filters = &session().data().chatsFilters(); + const auto filtersCount = int(filters->list().size()); auto &&folders = ranges::views::zip( Shortcuts::kShowFolder, ranges::views::ints(0, ranges::unreachable)); for (const auto [command, index] : folders) { const auto select = (command == Command::ShowFolderLast) - ? filtersCount - : std::clamp(index, 0, int(filtersCount)); + ? (filtersCount - 1) + : std::clamp(index, 0, filtersCount - 1); request->check(command) && request->handle([=] { if (select <= filtersCount) { - _controller->setActiveChatsFilter((select > 0) - ? (*filters)[select - 1].id() - : 0); + _controller->setActiveChatsFilter( + filters->lookupId(select)); } return true; }); diff --git a/Telegram/SourceFiles/window/window_filters_menu.cpp b/Telegram/SourceFiles/window/window_filters_menu.cpp index db77188352..f20a18df94 100644 --- a/Telegram/SourceFiles/window/window_filters_menu.cpp +++ b/Telegram/SourceFiles/window/window_filters_menu.cpp @@ -121,8 +121,12 @@ void FiltersMenu::setup() { _activeFilterId = _session->activeChatsFilterCurrent(); _session->activeChatsFilter( ) | rpl::filter([=](FilterId id) { - return id != _activeFilterId; + return (id != _activeFilterId); }) | rpl::start_with_next([=](FilterId id) { + if (!_list) { + _activeFilterId = id; + return; + } const auto i = _filters.find(_activeFilterId); if (i != end(_filters)) { i->second->setActive(false); diff --git a/Telegram/SourceFiles/window/window_session_controller.cpp b/Telegram/SourceFiles/window/window_session_controller.cpp index d70348298b..bc3a659fe0 100644 --- a/Telegram/SourceFiles/window/window_session_controller.cpp +++ b/Telegram/SourceFiles/window/window_session_controller.cpp @@ -600,6 +600,7 @@ SessionController::SessionController( _window->widget(), this)) , _invitePeekTimer([=] { checkInvitePeek(); }) +, _activeChatsFilter(session->data().chatsFilters().defaultId()) , _defaultChatTheme(std::make_shared()) , _chatStyle(std::make_unique()) , _cachedReactionIconFactory(std::make_unique()) { @@ -657,6 +658,10 @@ SessionController::SessionController( }, _lifetime); session->addWindow(this); + + crl::on_main(this, [=] { + activateFirstChatsFilter(); + }); } void SessionController::suggestArchiveAndMute() { @@ -772,6 +777,7 @@ rpl::producer<> SessionController::filtersMenuChanged() const { } void SessionController::checkOpenedFilter() { + activateFirstChatsFilter(); if (const auto filterId = activeChatsFilterCurrent()) { const auto &list = session().data().chatsFilters().list(); const auto i = ranges::find(list, filterId, &Data::ChatFilter::id); @@ -781,6 +787,14 @@ void SessionController::checkOpenedFilter() { } } +void SessionController::activateFirstChatsFilter() { + if (_filtersActivated || !session().data().chatsFilters().loaded()) { + return; + } + _filtersActivated = true; + setActiveChatsFilter(session().data().chatsFilters().defaultId()); +} + bool SessionController::uniqueChatsInSearchResults() const { return session().supportMode() && !session().settings().supportAllSearchResults() diff --git a/Telegram/SourceFiles/window/window_session_controller.h b/Telegram/SourceFiles/window/window_session_controller.h index 907f1c9a4e..f038d14ff5 100644 --- a/Telegram/SourceFiles/window/window_session_controller.h +++ b/Telegram/SourceFiles/window/window_session_controller.h @@ -513,6 +513,7 @@ private: void refreshFiltersMenu(); void checkOpenedFilter(); void suggestArchiveAndMute(); + void activateFirstChatsFilter(); int minimalThreeColumnWidth() const; int countDialogsWidthFromRatio(int bodyWidth) const; @@ -562,6 +563,7 @@ private: base::Variable _dialogsListDisplayForced = { false }; std::deque _chatEntryHistory; int _chatEntryHistoryPosition = -1; + bool _filtersActivated = false; bool _selectingPeer = false; base::Timer _invitePeekTimer;