Improved processing of premium accounts limits.

This commit is contained in:
23rd 2022-06-02 11:27:00 +03:00
parent 6c48abb562
commit 1d7e901b7a
5 changed files with 79 additions and 43 deletions

View File

@ -1699,8 +1699,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_premium_summary_button" = "Subscribe for {cost} per month";
"lng_accounts_limit_title" = "Limit Reached";
"lng_accounts_limit1#one" = "You have reached the limit of **{count}** connected accounts. You can free one space by subscribing to **Telegram Premium** with on of these connected accounts:";
"lng_accounts_limit1#other" = "You have reached the limit of **{count}** connected accounts. You can free one space by subscribing to **Telegram Premium** with on of these connected accounts:";
"lng_accounts_limit1#one" = "You have reached the limit of **{count}** connected accounts.";
"lng_accounts_limit1#other" = "You have reached the limit of **{count}** connected accounts.";
"lng_accounts_limit2" = "You can free one space by subscribing to **Telegram Premium** with on of these connected accounts:";
"lng_group_about_header" = "You have created a group.";
"lng_group_about_text" = "Groups can have:";

View File

@ -887,18 +887,25 @@ void FileSizeLimitBox(
void AccountsLimitBox(
not_null<Ui::GenericBox*> box,
not_null<Main::Session*> session) {
const auto premium = session->premium();
const auto defaultLimit = Main::Domain::kMaxAccounts;
const auto premiumLimit = Main::Domain::kPremiumMaxAccounts;
const auto accounts = session->domain().orderedAccounts();
const auto current = int(accounts.size());
auto text = tr::lng_accounts_limit1(
lt_count,
rpl::single<float64>(premiumLimit),
Ui::Text::RichLangValue);
auto text = rpl::combine(
tr::lng_accounts_limit1(
lt_count,
rpl::single<float64>(current),
Ui::Text::RichLangValue),
((current > premiumLimit)
? rpl::single(TextWithEntities())
: tr::lng_accounts_limit2(Ui::Text::RichLangValue))
) | rpl::map([](TextWithEntities &&a, TextWithEntities &&b) {
return b.text.isEmpty()
? a
: a.append(QChar(' ')).append(std::move(b));
});
box->setWidth(st::boxWideWidth);
@ -911,7 +918,7 @@ void AccountsLimitBox(
BoxShowFinishes(box),
0,
current,
(current == premiumLimit) ? premiumLimit : (current * 2),
(current > defaultLimit) ? current : (defaultLimit * 2),
std::nullopt,
&st::premiumIconAccounts);
Settings::AddSkip(top, st::premiumLineTextSkip);
@ -930,37 +937,38 @@ void AccountsLimitBox(
padding);
if (premium) {
if (current > premiumLimit) {
// Probably an unreachable state.
box->addButton(tr::lng_box_ok(), [=] {
box->closeBox();
});
} else {
auto switchingLifetime = std::make_shared<rpl::lifetime>();
box->addButton(tr::lng_continue(), [=]() mutable {
const auto ref = QString();
const auto wasAccount = &session->account();
const auto nowAccount = accounts[group->value()];
if (wasAccount == nowAccount) {
Settings::ShowPremium(session, ref);
return;
}
if (*switchingLifetime) {
return;
}
*switchingLifetime = session->domain().activeSessionChanges(
) | rpl::start_with_next([=](Main::Session *session) mutable {
if (session) {
Settings::ShowPremium(session, ref);
}
if (switchingLifetime) {
base::take(switchingLifetime)->destroy();
}
});
session->domain().activate(nowAccount);
});
return;
}
auto switchingLifetime = std::make_shared<rpl::lifetime>();
box->addButton(tr::lng_continue(), [=]() mutable {
const auto ref = QString();
const auto wasAccount = &session->account();
const auto nowAccount = accounts[group->value()];
if (wasAccount == nowAccount) {
Settings::ShowPremium(session, ref);
return;
}
if (*switchingLifetime) {
return;
}
*switchingLifetime = session->domain().activeSessionChanges(
) | rpl::start_with_next([=](Main::Session *session) mutable {
if (session) {
Settings::ShowPremium(session, ref);
}
if (switchingLifetime) {
base::take(switchingLifetime)->destroy();
}
});
session->domain().activate(nowAccount);
});
box->addButton(tr::lng_cancel(), [=] {
box->closeBox();
@ -970,10 +978,12 @@ void AccountsLimitBox(
auto &&entries = ranges::views::all(
accounts
) | ranges::views::transform([&](not_null<Main::Account*> account) {
) | ranges::views::filter([&](not_null<Main::Account*> account) {
return account->sessionExists() && !account->session().premium();
}) | ranges::views::transform([&](not_null<Main::Account*> account) {
const auto user = account->session().user();
return Args::Entry{ user->name, PaintUserpicCallback(user, false) };
});
}) | ranges::views::take(defaultLimit);
auto args = Args{
.group = group,

View File

@ -85,8 +85,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/qthelp_regex.h"
#include "base/qthelp_url.h"
#include "boxes/connection_box.h"
#include "boxes/premium_limits_box.h"
#include "ui/boxes/confirm_box.h"
#include "boxes/share_box.h"
#include <QtCore/QMimeDatabase>
#include <QtGui/QGuiApplication>
@ -285,6 +285,31 @@ void Application::run() {
_primaryWindow->showAccount(account);
}, _primaryWindow->widget()->lifetime());
(
_domain->activeValue(
) | rpl::to_empty | rpl::filter([=] {
return _domain->started();
}) | rpl::take(1)
) | rpl::then(
_domain->accountsChanges()
) | rpl::map([=] {
return (_domain->accounts().size() > Main::Domain::kMaxAccounts)
? _domain->activeChanges()
: rpl::never<not_null<Main::Account*>>();
}) | rpl::flatten_latest(
) | rpl::start_with_next([=](not_null<Main::Account*> account) {
const auto ordered = _domain->orderedAccounts();
const auto it = ranges::find(ordered, account);
if (it != end(ordered)) {
const auto index = std::distance(begin(ordered), it);
if ((index + 1) > _domain->maxAccounts()) {
_primaryWindow->show(Box(
AccountsLimitBox,
&account->session()));
}
}
}, _primaryWindow->widget()->lifetime());
QCoreApplication::instance()->installEventFilter(this);
appDeactivatedValue(

View File

@ -467,11 +467,11 @@ void Domain::scheduleWriteAccounts() {
}
int Domain::maxAccounts() const {
const auto isAnyPreimium = ranges::any_of(accounts(), [](
const auto premiumCount = ranges::count_if(accounts(), [](
const Main::Domain::AccountWithIndex &d) {
return d.account->session().premium();
return d.account->sessionExists() && d.account->session().premium();
});
return isAnyPreimium ? kPremiumMaxAccounts : kMaxAccounts;
return std::min(int(premiumCount) + kMaxAccounts, kPremiumMaxAccounts);
}
rpl::producer<int> Domain::maxAccountsChanges() const {

View File

@ -31,7 +31,7 @@ public:
};
static constexpr auto kMaxAccounts = 3;
static constexpr auto kPremiumMaxAccounts = 4;
static constexpr auto kPremiumMaxAccounts = 6;
explicit Domain(const QString &dataName);
~Domain();