Fix folder chats limit checking.

This commit is contained in:
John Preston 2022-05-06 20:06:15 +04:00
parent 23caae689b
commit d11f1c22be
2 changed files with 53 additions and 19 deletions

View file

@ -9,17 +9,20 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history.h" #include "history/history.h"
#include "window/window_session_controller.h" #include "window/window_session_controller.h"
#include "boxes/premium_limits_box.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "ui/widgets/labels.h" #include "ui/widgets/labels.h"
#include "ui/wrap/vertical_layout.h" #include "ui/wrap/vertical_layout.h"
#include "main/main_app_config.h"
#include "main/main_account.h"
#include "main/main_session.h"
#include "base/object_ptr.h" #include "base/object_ptr.h"
#include "data/data_user.h"
#include "styles/style_window.h" #include "styles/style_window.h"
#include "styles/style_boxes.h" #include "styles/style_boxes.h"
namespace { namespace {
constexpr auto kMaxExceptions = 100;
using Flag = Data::ChatFilter::Flag; using Flag = Data::ChatFilter::Flag;
using Flags = Data::ChatFilter::Flags; using Flags = Data::ChatFilter::Flags;
@ -93,6 +96,22 @@ private:
return PeerId(FakeChatId(static_cast<BareId>(flag))).value; return PeerId(FakeChatId(static_cast<BareId>(flag))).value;
} }
[[nodiscard]] int Limit(
not_null<Main::Session*> session,
const QString &key,
double fallback) {
return session->account().appConfig().get<double>(key, fallback);
}
[[nodiscard]] int Limit(not_null<Main::Session*> session) {
const auto premium = session->user()->isPremium();
return Limit(session,
(premium
? "dialog_filters_chats_limit_premium"
: "dialog_filters_chats_limit_default"),
premium ? 200 : 100);
}
TypeRow::TypeRow(Flag flag) : PeerListRow(TypeId(flag)) { TypeRow::TypeRow(Flag flag) : PeerListRow(TypeId(flag)) {
} }
@ -302,18 +321,34 @@ EditFilterChatsListController::EditFilterChatsListController(
, _title(std::move(title)) , _title(std::move(title))
, _peers(peers) , _peers(peers)
, _options(options) , _options(options)
, _selected(selected) { , _selected(selected)
, _limit(Limit(session)) {
} }
Main::Session &EditFilterChatsListController::session() const { Main::Session &EditFilterChatsListController::session() const {
return *_session; return *_session;
} }
int EditFilterChatsListController::selectedTypesCount() const {
Expects(_typesDelegate != nullptr);
auto result = 0;
for (auto i = 0; i != _typesDelegate->peerListFullRowsCount(); ++i) {
if (_typesDelegate->peerListRowAt(i)->checked()) {
++result;
}
}
return result;
}
void EditFilterChatsListController::rowClicked(not_null<PeerListRow*> row) { void EditFilterChatsListController::rowClicked(not_null<PeerListRow*> row) {
const auto count = delegate()->peerListSelectedRowsCount(); const auto count = delegate()->peerListSelectedRowsCount()
if (count < kMaxExceptions || row->checked()) { - selectedTypesCount();
if (count < _limit || row->checked()) {
delegate()->peerListSetRowChecked(row, !row->checked()); delegate()->peerListSetRowChecked(row, !row->checked());
updateTitle(); updateTitle();
} else {
delegate()->peerListShowBox(Box(FilterChatsLimitBox, _session));
} }
} }
@ -363,7 +398,7 @@ object_ptr<Ui::RpWidget> EditFilterChatsListController::prepareTypesList() {
container->add(object_ptr<Ui::FixedHeightWidget>( container->add(object_ptr<Ui::FixedHeightWidget>(
container, container,
st::membersMarginTop)); st::membersMarginTop));
const auto delegate = container->lifetime().make_state< _typesDelegate = container->lifetime().make_state<
PeerListContentDelegateSimple PeerListContentDelegateSimple
>(); >();
const auto controller = container->lifetime().make_state<TypeController>( const auto controller = container->lifetime().make_state<TypeController>(
@ -374,11 +409,11 @@ object_ptr<Ui::RpWidget> EditFilterChatsListController::prepareTypesList() {
const auto content = result->add(object_ptr<PeerListContent>( const auto content = result->add(object_ptr<PeerListContent>(
container, container,
controller)); controller));
delegate->setContent(content); _typesDelegate->setContent(content);
controller->setDelegate(delegate); controller->setDelegate(_typesDelegate);
for (const auto flag : kAllTypes) { for (const auto flag : kAllTypes) {
if (_selected & flag) { if (_selected & flag) {
if (const auto row = delegate->peerListFindRow(TypeId(flag))) { if (const auto row = _typesDelegate->peerListFindRow(TypeId(flag))) {
content->changeCheckState(row, true, anim::type::instant); content->changeCheckState(row, true, anim::type::instant);
this->delegate()->peerListSetForeignRowChecked( this->delegate()->peerListSetForeignRowChecked(
row, row,
@ -408,8 +443,8 @@ object_ptr<Ui::RpWidget> EditFilterChatsListController::prepareTypesList() {
}, _lifetime); }, _lifetime);
_deselectOption = [=](PeerListRowId itemId) { _deselectOption = [=](PeerListRowId itemId) {
if (const auto row = delegate->peerListFindRow(itemId)) { if (const auto row = _typesDelegate->peerListFindRow(itemId)) {
delegate->peerListSetRowChecked(row, false); _typesDelegate->peerListSetRowChecked(row, false);
} }
}; };
@ -424,13 +459,8 @@ auto EditFilterChatsListController::createRow(not_null<History*> history)
} }
void EditFilterChatsListController::updateTitle() { void EditFilterChatsListController::updateTitle() {
auto types = 0; const auto count = delegate()->peerListSelectedRowsCount()
for (const auto flag : kAllTypes) { - selectedTypesCount();
if (_selected & flag) { const auto additional = qsl("%1 / %2").arg(count).arg(_limit);
++types;
}
}
const auto count = delegate()->peerListSelectedRowsCount() - types;
const auto additional = qsl("%1 / %2").arg(count).arg(kMaxExceptions);
delegate()->peerListSetAdditionalTitle(rpl::single(additional)); delegate()->peerListSetAdditionalTitle(rpl::single(additional));
} }

View file

@ -62,6 +62,7 @@ public:
bool handleDeselectForeignRow(PeerListRowId itemId) override; bool handleDeselectForeignRow(PeerListRowId itemId) override;
private: private:
int selectedTypesCount() const;
void prepareViewHook() override; void prepareViewHook() override;
std::unique_ptr<Row> createRow(not_null<History*> history) override; std::unique_ptr<Row> createRow(not_null<History*> history) override;
[[nodiscard]] object_ptr<Ui::RpWidget> prepareTypesList(); [[nodiscard]] object_ptr<Ui::RpWidget> prepareTypesList();
@ -73,9 +74,12 @@ private:
base::flat_set<not_null<History*>> _peers; base::flat_set<not_null<History*>> _peers;
Flags _options; Flags _options;
Flags _selected; Flags _selected;
int _limit = 0;
Fn<void(PeerListRowId)> _deselectOption; Fn<void(PeerListRowId)> _deselectOption;
PeerListContentDelegate *_typesDelegate = nullptr;
rpl::lifetime _lifetime; rpl::lifetime _lifetime;
}; };