mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-16 06:07:06 +02:00
Create/Update confirm box for the starref.
This commit is contained in:
parent
400df0f980
commit
6539d14852
5 changed files with 128 additions and 43 deletions
|
@ -1683,7 +1683,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
"lng_star_ref_transparent_about" = "Track your commissions from referred users in real time.";
|
||||
"lng_star_ref_simple_title" = "Simple";
|
||||
"lng_star_ref_simple_about" = "Choose a mini app below, get your referral link, and start earning Stars.";
|
||||
"lng_star_ref_duration_forever" = "Lifetime";
|
||||
"lng_star_ref_duration_forever" = "Forever";
|
||||
"lng_star_ref_one_about" = "{app} will share {amount} of the revenue from each user you refer to it {duration}.";
|
||||
"lng_star_ref_one_about_for_forever" = "for **lifetime**";
|
||||
"lng_star_ref_one_about_for_months#one" = "for **{count} month**";
|
||||
|
|
|
@ -17,17 +17,20 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
#include "lang/lang_keys.h"
|
||||
#include "main/main_session.h"
|
||||
#include "settings/settings_common.h"
|
||||
#include "ui/boxes/confirm_box.h"
|
||||
#include "ui/controls/userpic_button.h"
|
||||
#include "ui/layers/generic_box.h"
|
||||
#include "ui/widgets/buttons.h"
|
||||
#include "ui/widgets/labels.h"
|
||||
#include "ui/wrap/padding_wrap.h"
|
||||
#include "ui/wrap/table_layout.h"
|
||||
#include "ui/wrap/vertical_layout.h"
|
||||
#include "ui/text/text_utilities.h"
|
||||
#include "ui/painter.h"
|
||||
#include "ui/vertical_list.h"
|
||||
#include "styles/style_chat.h"
|
||||
#include "styles/style_dialogs.h"
|
||||
#include "styles/style_giveaway.h"
|
||||
#include "styles/style_layers.h"
|
||||
#include "styles/style_premium.h"
|
||||
#include "styles/style_settings.h"
|
||||
|
@ -157,18 +160,26 @@ QString FormatCommission(ushort commission) {
|
|||
return QString::number(commission / 10.) + '%';
|
||||
}
|
||||
|
||||
rpl::producer<TextWithEntities> FormatProgramDuration(
|
||||
StarRefProgram program) {
|
||||
return !program.durationMonths
|
||||
QString FormatProgramDuration(int durationMonths) {
|
||||
return !durationMonths
|
||||
? tr::lng_star_ref_duration_forever(tr::now)
|
||||
: (durationMonths < 12)
|
||||
? tr::lng_months(tr::now, lt_count, durationMonths)
|
||||
: tr::lng_years(tr::now, lt_count, durationMonths / 12);
|
||||
}
|
||||
|
||||
rpl::producer<TextWithEntities> FormatForProgramDuration(
|
||||
int durationMonths) {
|
||||
return !durationMonths
|
||||
? tr::lng_star_ref_one_about_for_forever(Ui::Text::RichLangValue)
|
||||
: (program.durationMonths < 12)
|
||||
: (durationMonths < 12)
|
||||
? tr::lng_star_ref_one_about_for_months(
|
||||
lt_count,
|
||||
rpl::single(program.durationMonths * 1.),
|
||||
rpl::single(durationMonths * 1.),
|
||||
Ui::Text::RichLangValue)
|
||||
: tr::lng_star_ref_one_about_for_years(
|
||||
lt_count,
|
||||
rpl::single((program.durationMonths / 12) * 1.),
|
||||
rpl::single((durationMonths / 12) * 1.),
|
||||
Ui::Text::RichLangValue);
|
||||
}
|
||||
|
||||
|
@ -386,7 +397,7 @@ object_ptr<Ui::BoxContent> StarRefLinkBox(
|
|||
lt_app,
|
||||
rpl::single(Ui::Text::Bold(bot->name())),
|
||||
lt_duration,
|
||||
FormatProgramDuration(program),
|
||||
FormatForProgramDuration(program.durationMonths),
|
||||
Ui::Text::WithEntities),
|
||||
st::starrefCenteredText),
|
||||
st::boxRowPadding);
|
||||
|
@ -483,7 +494,7 @@ object_ptr<Ui::BoxContent> StarRefLinkBox(
|
|||
rpl::single(Ui::Text::Bold(
|
||||
FormatCommission(program.commission))),
|
||||
lt_duration,
|
||||
FormatProgramDuration(program),
|
||||
FormatForProgramDuration(program.durationMonths),
|
||||
Ui::Text::WithEntities),
|
||||
st::starrefCenteredText),
|
||||
st::boxRowPadding);
|
||||
|
@ -658,11 +669,71 @@ std::unique_ptr<Ui::AbstractButton> MakePeerBubbleButton(
|
|||
return result;
|
||||
}
|
||||
|
||||
void ConfirmUpdate(
|
||||
std::shared_ptr<Ui::Show> show,
|
||||
not_null<UserData*> bot,
|
||||
const StarRefProgram &program,
|
||||
bool exists,
|
||||
Fn<void(Fn<void(bool)>)> update) {
|
||||
show->show(Box([=](not_null<Ui::GenericBox*> box) {
|
||||
const auto sent = std::make_shared<bool>();
|
||||
Ui::ConfirmBox(box, {
|
||||
.text = (exists
|
||||
? tr::lng_star_ref_warning_change
|
||||
: tr::lng_star_ref_warning_text)(Ui::Text::RichLangValue),
|
||||
.confirmed = [=](Fn<void()> close) {
|
||||
if (*sent) {
|
||||
return;
|
||||
}
|
||||
*sent = true;
|
||||
update([=](bool success) {
|
||||
*sent = false;
|
||||
if (success) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
},
|
||||
.confirmText = (exists
|
||||
? tr::lng_star_ref_warning_update
|
||||
: tr::lng_star_ref_warning_start)(),
|
||||
.title = tr::lng_star_ref_warning_title(),
|
||||
});
|
||||
|
||||
auto table = box->addRow(
|
||||
object_ptr<Ui::TableLayout>(
|
||||
box,
|
||||
st::giveawayGiftCodeTable),
|
||||
st::giveawayGiftCodeTableMargin);
|
||||
const auto addRow = [&](
|
||||
rpl::producer<QString> label,
|
||||
const QString &value) {
|
||||
table->addRow(
|
||||
object_ptr<Ui::FlatLabel>(
|
||||
table,
|
||||
std::move(label),
|
||||
st::giveawayGiftCodeLabel),
|
||||
object_ptr<Ui::FlatLabel>(
|
||||
table,
|
||||
value,
|
||||
st::giveawayGiftCodeValue,
|
||||
st::defaultPopupMenu),
|
||||
st::giveawayGiftCodeLabelMargin,
|
||||
st::giveawayGiftCodeValueMargin);
|
||||
};
|
||||
addRow(
|
||||
tr::lng_star_ref_commission_title(),
|
||||
FormatCommission(program.commission));
|
||||
addRow(
|
||||
tr::lng_star_ref_duration_title(),
|
||||
FormatProgramDuration(program.durationMonths));
|
||||
}));
|
||||
}
|
||||
|
||||
void UpdateProgram(
|
||||
std::shared_ptr<Ui::Show> show,
|
||||
not_null<UserData*> bot,
|
||||
const StarRefProgram &program,
|
||||
Fn<void()> finished) {
|
||||
Fn<void(bool)> done) {
|
||||
using Flag = MTPbots_UpdateStarRefProgram::Flag;
|
||||
bot->session().api().request(MTPbots_UpdateStarRefProgram(
|
||||
MTP_flags((program.commission > 0 && program.durationMonths > 0)
|
||||
|
@ -673,17 +744,18 @@ void UpdateProgram(
|
|||
MTP_int(program.durationMonths)
|
||||
)).done([=](const MTPStarRefProgram &result) {
|
||||
bot->setStarRefProgram(Data::ParseStarRefProgram(&result));
|
||||
finished();
|
||||
done(true);
|
||||
}).fail([=](const MTP::Error &error) {
|
||||
show->showToast(u"Failed: "_q + error.type());
|
||||
done(false);
|
||||
}).send();
|
||||
}
|
||||
|
||||
void FinishProgram(
|
||||
std::shared_ptr<Ui::Show> show,
|
||||
not_null<UserData*> bot,
|
||||
Fn<void()> finished) {
|
||||
UpdateProgram(std::move(show), bot, {}, std::move(finished));
|
||||
Fn<void(bool)> done) {
|
||||
UpdateProgram(std::move(show), bot, {}, std::move(done));
|
||||
}
|
||||
|
||||
ConnectedBots Parse(
|
||||
|
|
|
@ -40,8 +40,9 @@ struct ConnectedBot {
|
|||
using ConnectedBots = std::vector<ConnectedBot>;
|
||||
|
||||
[[nodiscard]] QString FormatCommission(ushort commission);
|
||||
[[nodiscard]] rpl::producer<TextWithEntities> FormatProgramDuration(
|
||||
StarRefProgram program);
|
||||
[[nodiscard]] QString FormatProgramDuration(int durationMonths);
|
||||
[[nodiscard]] rpl::producer<TextWithEntities> FormatForProgramDuration(
|
||||
int durationMonths);
|
||||
|
||||
[[nodiscard]] not_null<Ui::AbstractButton*> AddViewListButton(
|
||||
not_null<Ui::VerticalLayout*> parent,
|
||||
|
@ -73,15 +74,21 @@ std::unique_ptr<Ui::AbstractButton> MakePeerBubbleButton(
|
|||
not_null<PeerData*> peer,
|
||||
Ui::RpWidget *right = nullptr);
|
||||
|
||||
void ConfirmUpdate(
|
||||
std::shared_ptr<Ui::Show> show,
|
||||
not_null<UserData*> bot,
|
||||
const StarRefProgram &program,
|
||||
bool exists,
|
||||
Fn<void(Fn<void(bool)> done)> update);
|
||||
void UpdateProgram(
|
||||
std::shared_ptr<Ui::Show> show,
|
||||
not_null<UserData*> bot,
|
||||
const StarRefProgram &program,
|
||||
Fn<void()> finished);
|
||||
Fn<void(bool)> done);
|
||||
void FinishProgram(
|
||||
std::shared_ptr<Ui::Show> show,
|
||||
not_null<UserData*> bot,
|
||||
Fn<void()> removed);
|
||||
Fn<void(bool)> done);
|
||||
|
||||
[[nodiscard]] ConnectedBots Parse(
|
||||
not_null<Main::Session*> session,
|
||||
|
|
|
@ -149,17 +149,12 @@ std::unique_ptr<PeerListRow> ListController::createRow(ConnectedBot bot) {
|
|||
_states.emplace(bot.bot, bot.state);
|
||||
auto result = std::make_unique<PeerListRow>(bot.bot);
|
||||
const auto program = bot.state.program;
|
||||
const auto duration = !program.durationMonths
|
||||
? tr::lng_star_ref_duration_forever(tr::now)
|
||||
: (program.durationMonths < 12)
|
||||
? tr::lng_months(tr::now, lt_count, program.durationMonths)
|
||||
: tr::lng_years(tr::now, lt_count, program.durationMonths / 12);
|
||||
if (bot.state.revoked) {
|
||||
result->setCustomStatus(u"Revoked"_q);
|
||||
} else {
|
||||
result->setCustomStatus(u"+%1, %2"_q.arg(
|
||||
FormatCommission(program.commission),
|
||||
duration));
|
||||
FormatProgramDuration(program.durationMonths)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -740,9 +740,18 @@ void InnerWidget::setupEnd() {
|
|||
end->setClickedCallback([=] {
|
||||
const auto weak = Ui::MakeWeak(this);
|
||||
const auto window = _controller->parentController();
|
||||
const auto sent = std::make_shared<bool>();
|
||||
window->show(ConfirmEndBox([=] {
|
||||
FinishProgram(_controller->uiShow(), _state.user, [=] {
|
||||
if (const auto strong = weak.data()) {
|
||||
if (*sent) {
|
||||
return;
|
||||
}
|
||||
*sent = true;
|
||||
const auto show = _controller->uiShow();
|
||||
FinishProgram(show, _state.user, [=](bool success) {
|
||||
*sent = false;
|
||||
if (!success) {
|
||||
return;
|
||||
} else if (const auto strong = weak.data()) {
|
||||
_controller->showBackFromStack();
|
||||
window->showToast({
|
||||
.title = tr::lng_star_ref_ended_title(tr::now),
|
||||
|
@ -1004,28 +1013,30 @@ std::unique_ptr<Ui::RpWidget> Widget::setupBottom() {
|
|||
st::boxDividerLabel),
|
||||
QMargins(margins.left(), 0, margins.right(), 0));
|
||||
save->setClickedCallback([=] {
|
||||
const auto weak = Ui::MakeWeak(this);
|
||||
const auto user = _state->user;
|
||||
const auto program = StarRefProgram{
|
||||
.commission = _state->program.commission,
|
||||
.durationMonths = _state->program.durationMonths,
|
||||
};
|
||||
|
||||
const auto program = _state->program;
|
||||
const auto show = controller()->uiShow();
|
||||
const auto exists = _state->exists;
|
||||
UpdateProgram(show, user, program, crl::guard(this, [=] {
|
||||
controller()->showBackFromStack();
|
||||
show->showToast({
|
||||
.title = (exists
|
||||
? tr::lng_star_ref_updated_title
|
||||
: tr::lng_star_ref_created_title)(tr::now),
|
||||
.text = (exists
|
||||
? tr::lng_star_ref_updated_text
|
||||
: tr::lng_star_ref_created_text)(
|
||||
tr::now,
|
||||
Ui::Text::RichLangValue),
|
||||
.duration = Ui::Toast::kDefaultDuration * 3,
|
||||
ConfirmUpdate(show, user, program, exists, [=](Fn<void(bool)> done) {
|
||||
UpdateProgram(show, user, program, [=](bool success) {
|
||||
done(success);
|
||||
if (weak) {
|
||||
controller()->showBackFromStack();
|
||||
}
|
||||
show->showToast({
|
||||
.title = (exists
|
||||
? tr::lng_star_ref_updated_title
|
||||
: tr::lng_star_ref_created_title)(tr::now),
|
||||
.text = (exists
|
||||
? tr::lng_star_ref_updated_text
|
||||
: tr::lng_star_ref_created_text)(
|
||||
tr::now,
|
||||
Ui::Text::RichLangValue),
|
||||
.duration = Ui::Toast::kDefaultDuration * 3,
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
widthValue() | rpl::start_with_next([=](int width) {
|
||||
|
|
Loading…
Add table
Reference in a new issue