Added ability to cancel and renew subscription from settings section.

This commit is contained in:
23rd 2024-08-10 01:22:32 +03:00 committed by John Preston
parent 32e8ed93e2
commit 4f7364b798
3 changed files with 71 additions and 4 deletions

View file

@ -342,7 +342,8 @@ void ConfirmSubscriptionBox(
void CheckChatInvite(
not_null<Window::SessionController*> controller,
const QString &hash,
ChannelData *invitePeekChannel) {
ChannelData *invitePeekChannel,
Fn<void()> loaded) {
const auto session = &controller->session();
const auto weak = base::make_weak(controller);
session->api().checkChatInvite(hash, [=](const MTPChatInvite &result) {
@ -350,6 +351,9 @@ void CheckChatInvite(
if (!strong) {
return;
}
if (loaded) {
loaded();
}
Core::App().hideMediaView();
const auto show = [&](not_null<PeerData*> chat) {
const auto way = Window::SectionShow::Way::Forward;

View file

@ -38,7 +38,8 @@ namespace Api {
void CheckChatInvite(
not_null<Window::SessionController*> controller,
const QString &hash,
ChannelData *invitePeekChannel = nullptr);
ChannelData *invitePeekChannel = nullptr,
Fn<void()> loaded = nullptr);
} // namespace Api

View file

@ -7,8 +7,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "settings/settings_credits_graphics.h"
#include "api/api_chat_invite.h"
#include "api/api_credits.h"
#include "api/api_earn.h"
#include "apiwrap.h"
#include "base/timer_rpl.h"
#include "base/unixtime.h"
#include "boxes/gift_premium_box.h"
@ -29,6 +31,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history.h"
#include "history/history_item.h"
#include "history/history_item_components.h" // HistoryServicePaymentRefund.
#include "info/channel_statistics/boosts/giveaway/boost_badge.h" // InfiniteRadialAnimationWidget.
#include "info/settings/info_settings_widget.h" // SectionCustomTopBarData.
#include "info/statistics/info_statistics_list_controllers.h"
#include "lang/lang_keys.h"
@ -783,9 +786,68 @@ void ReceiptCreditsBox(
}, widget->lifetime());
}
const auto button = box->addButton(tr::lng_box_ok(), [=] {
box->closeBox();
const auto toRenew = (s.cancelled || s.expired)
&& !s.inviteHash.isEmpty();
const auto toCancel = !toRenew && s;
struct State final {
rpl::variable<bool> confirmButtonBusy;
};
const auto state = box->lifetime().make_state<State>();
auto confirmText = rpl::conditional(
state->confirmButtonBusy.value(),
rpl::single(QString()),
toRenew
? tr::lng_credits_subscription_off_button()
: toCancel
? tr::lng_credits_subscription_on_button()
: tr::lng_box_ok());
using Flag = MTPpayments_ChangeStarsSubscription::Flag;
const auto send = [=, weak = Ui::MakeWeak(box)] {
if (toRenew && s.expired) {
Api::CheckChatInvite(controller, s.inviteHash, nullptr, [=] {
if (const auto strong = weak.get()) {
strong->closeBox();
}
});
} else {
session->api().request(
MTPpayments_ChangeStarsSubscription(
MTP_flags(Flag::f_canceled),
MTP_inputPeerSelf(),
MTP_string(s.id),
MTP_bool(toCancel)
)).done([=] {
state->confirmButtonBusy = false;
if (const auto strong = weak.get()) {
strong->closeBox();
}
}).fail([=, show = box->uiShow()](const MTP::Error &error) {
state->confirmButtonBusy = false;
show->showToast(error.type());
}).send();
}
};
const auto button = box->addButton(std::move(confirmText), [=] {
if (state->confirmButtonBusy.current()) {
return;
}
state->confirmButtonBusy = true;
if ((toRenew || toCancel) && peer) {
send();
} else {
box->closeBox();
}
});
{
using namespace Info::Statistics;
const auto loadingAnimation = InfiniteRadialAnimationWidget(
button,
button->height() / 2);
AddChildToWidgetCenter(button, loadingAnimation);
loadingAnimation->showOn(state->confirmButtonBusy.value());
}
const auto buttonWidth = st::boxWidth
- rect::m::sum::h(st::giveawayGiftCodeBox.buttonPadding);
button->widthValue() | rpl::filter([=] {