Added support of api updates for overview in channel earn section.

This commit is contained in:
23rd 2024-06-22 06:28:44 +03:00 committed by John Preston
parent ae1f364730
commit 63c4c5064f
2 changed files with 104 additions and 27 deletions

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "api/api_credits.h" #include "api/api_credits.h"
#include "api/api_earn.h" #include "api/api_earn.h"
#include "api/api_filter_updates.h"
#include "api/api_statistics.h" #include "api/api_statistics.h"
#include "base/unixtime.h" #include "base/unixtime.h"
#include "boxes/peers/edit_peer_color_box.h" // AddLevelBadge. #include "boxes/peers/edit_peer_color_box.h" // AddLevelBadge.
@ -27,11 +28,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "info/statistics/info_statistics_inner_widget.h" // FillLoading. #include "info/statistics/info_statistics_inner_widget.h" // FillLoading.
#include "iv/iv_instance.h" #include "iv/iv_instance.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "main/main_account.h"
#include "main/main_app_config.h" #include "main/main_app_config.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "statistics/chart_widget.h" #include "statistics/chart_widget.h"
#include "ui/basic_click_handlers.h" #include "ui/basic_click_handlers.h"
#include "ui/widgets/label_with_custom_emoji.h"
#include "ui/boxes/boost_box.h" #include "ui/boxes/boost_box.h"
#include "ui/controls/userpic_button.h" #include "ui/controls/userpic_button.h"
#include "ui/effects/animation_value_f.h" #include "ui/effects/animation_value_f.h"
@ -43,6 +44,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/text/text_utilities.h" #include "ui/text/text_utilities.h"
#include "ui/vertical_list.h" #include "ui/vertical_list.h"
#include "ui/widgets/fields/input_field.h" #include "ui/widgets/fields/input_field.h"
#include "ui/widgets/label_with_custom_emoji.h"
#include "ui/widgets/popup_menu.h" #include "ui/widgets/popup_menu.h"
#include "ui/wrap/slide_wrap.h" #include "ui/wrap/slide_wrap.h"
#include "styles/style_boxes.h" #include "styles/style_boxes.h"
@ -258,6 +260,17 @@ InnerWidget::InnerWidget(
} }
void InnerWidget::load() { void InnerWidget::load() {
struct State final {
State(not_null<PeerData*> peer)
: api(peer->asChannel())
, apiCredits(peer) {
}
Api::ChannelEarnStatistics api;
Api::CreditsEarnStatistics apiCredits;
rpl::lifetime apiLifetime;
rpl::lifetime apiCreditsLifetime;
};
const auto state = lifetime().make_state<State>(_peer);
const auto api = lifetime().make_state<Api::ChannelEarnStatistics>( const auto api = lifetime().make_state<Api::ChannelEarnStatistics>(
_peer->asChannel()); _peer->asChannel());
const auto apiCredits = lifetime().make_state<Api::CreditsEarnStatistics>( const auto apiCredits = lifetime().make_state<Api::CreditsEarnStatistics>(
@ -272,22 +285,56 @@ void InnerWidget::load() {
show->showToast(error); show->showToast(error);
}; };
const auto finish = [=] {
_loaded.fire(true);
fill();
state->apiLifetime.destroy();
state->apiCreditsLifetime.destroy();
_peer->session().account().mtpUpdates(
) | rpl::start_with_next([=, peerId = _peer->id](
const MTPUpdates &updates) {
using TLCreditsUpdate = MTPDupdateStarsRevenueStatus;
using TLCurrencyUpdate = MTPDupdateBroadcastRevenueTransactions;
Api::PerformForUpdate<TLCreditsUpdate>(updates, [&](
const TLCreditsUpdate &d) {
if (peerId == peerFromMTP(d.vpeer())) {
apiCredits->request(
) | rpl::start_with_error_done(fail, [=] {
state->apiCreditsLifetime.destroy();
_state.creditsEarn = state->apiCredits.data();
_stateUpdated.fire({});
}, state->apiCreditsLifetime);
}
});
Api::PerformForUpdate<TLCurrencyUpdate>(updates, [&](
const TLCurrencyUpdate &d) {
if (peerId == peerFromMTP(d.vpeer())) {
const auto &data = d.vbalances().data();
auto &e = _state.currencyEarn;
e.currentBalance = data.vcurrent_balance().v;
e.availableBalance = data.vavailable_balance().v;
e.overallRevenue = data.voverall_revenue().v;
_stateUpdated.fire({});
}
});
}, lifetime());
};
_showFinished.events( _showFinished.events(
) | rpl::take(1) | rpl::start_with_next([=] { ) | rpl::take(1) | rpl::start_with_next([=] {
api->request( state->api.request(
) | rpl::start_with_error_done(fail, [=] { ) | rpl::start_with_error_done(fail, [=] {
_state.currencyEarn = api->data(); _state.currencyEarn = state->api.data();
apiCredits->request( state->apiCredits.request(
) | rpl::start_with_error_done([=](const QString &error) { ) | rpl::start_with_error_done([=](const QString &error) {
fail(error); fail(error);
_loaded.fire(true); finish();
fill();
}, [=] { }, [=] {
_state.creditsEarn = apiCredits->data(); _state.creditsEarn = state->apiCredits.data();
_loaded.fire(true); finish();
fill(); }, state->apiCreditsLifetime);
}, lifetime()); }, state->apiLifetime);
}, lifetime());
}, lifetime()); }, lifetime());
} }
@ -296,6 +343,20 @@ void InnerWidget::fill() {
const auto &data = _state.currencyEarn; const auto &data = _state.currencyEarn;
const auto &creditsData = _state.creditsEarn; const auto &creditsData = _state.creditsEarn;
auto currencyStateValue = rpl::single(
data
) | rpl::then(
_stateUpdated.events() | rpl::map([=] {
return _state.currencyEarn;
})
);
auto creditsStateValue = rpl::single(
creditsData
) | rpl::then(
_stateUpdated.events() | rpl::map([=] { return _state.creditsEarn; })
);
constexpr auto kMinus = QChar(0x2212); constexpr auto kMinus = QChar(0x2212);
//constexpr auto kApproximately = QChar(0x2248); //constexpr auto kApproximately = QChar(0x2248);
const auto multiplier = data.usdRate; const auto multiplier = data.usdRate;
@ -584,8 +645,8 @@ void InnerWidget::fill() {
Ui::AddSkip(container, st::channelEarnOverviewTitleSkip); Ui::AddSkip(container, st::channelEarnOverviewTitleSkip);
const auto addOverview = [&]( const auto addOverview = [&](
EarnInt value, rpl::producer<EarnInt> currencyValue,
EarnInt credits, rpl::producer<EarnInt> creditsValue,
const tr::phrase<> &text) { const tr::phrase<> &text) {
const auto line = container->add( const auto line = container->add(
Ui::CreateSkipWidget(container, 0), Ui::CreateSkipWidget(container, 0),
@ -593,37 +654,49 @@ void InnerWidget::fill() {
const auto majorLabel = Ui::CreateChild<Ui::FlatLabel>( const auto majorLabel = Ui::CreateChild<Ui::FlatLabel>(
line, line,
st::channelEarnOverviewMajorLabel); st::channelEarnOverviewMajorLabel);
addEmojiToMajor(majorLabel, rpl::single(value), {}, {}); addEmojiToMajor(majorLabel, rpl::duplicate(currencyValue), {}, {});
const auto minorLabel = Ui::CreateChild<Ui::FlatLabel>( const auto minorLabel = Ui::CreateChild<Ui::FlatLabel>(
line, line,
MinorPart(value), rpl::duplicate(currencyValue) | rpl::map(MinorPart),
st::channelEarnOverviewMinorLabel); st::channelEarnOverviewMinorLabel);
const auto secondMinorLabel = Ui::CreateChild<Ui::FlatLabel>( const auto secondMinorLabel = Ui::CreateChild<Ui::FlatLabel>(
line, line,
value ? ToUsd(value, multiplier) : QString(), std::move(
currencyValue
) | rpl::map([=](EarnInt value) {
return value ? ToUsd(value, multiplier) : QString();
}),
st::channelEarnOverviewSubMinorLabel); st::channelEarnOverviewSubMinorLabel);
const auto creditsLabel = Ui::CreateChild<Ui::FlatLabel>( const auto creditsLabel = Ui::CreateChild<Ui::FlatLabel>(
line, line,
QString::number(credits), rpl::duplicate(creditsValue) | rpl::map([](EarnInt value) {
return QString::number(value);
}),
st::channelEarnOverviewMajorLabel); st::channelEarnOverviewMajorLabel);
const auto icon = Ui::CreateSingleStarWidget( const auto icon = Ui::CreateSingleStarWidget(
line, line,
creditsLabel->height()); creditsLabel->height());
const auto creditsMultiplies = creditsData.usdRate const auto creditsMultiplier = creditsData.usdRate
* Data::kEarnMultiplier; * Data::kEarnMultiplier;
const auto creditsSecondLabel = Ui::CreateChild<Ui::FlatLabel>( const auto creditsSecondLabel = Ui::CreateChild<Ui::FlatLabel>(
line, line,
credits ? ToUsd(credits, creditsMultiplies) : QString(), rpl::duplicate(
creditsValue
) | rpl::map([creditsMultiplier](EarnInt c) {
return c ? ToUsd(c, creditsMultiplier) : QString();
}),
st::channelEarnOverviewSubMinorLabel); st::channelEarnOverviewSubMinorLabel);
rpl::combine( rpl::combine(
line->widthValue(), line->widthValue(),
majorLabel->sizeValue(), majorLabel->sizeValue(),
creditsLabel->sizeValue() creditsLabel->sizeValue(),
std::move(creditsValue)
) | rpl::start_with_next([=]( ) | rpl::start_with_next([=](
int available, int available,
const QSize &size, const QSize &size,
const QSize &creditsSize) { const QSize &creditsSize,
EarnInt credits) {
const auto skip = st::channelEarnOverviewSubMinorLabelPos.x(); const auto skip = st::channelEarnOverviewSubMinorLabelPos.x();
line->resize(line->width(), size.height()); line->resize(line->width(), size.height());
minorLabel->moveToLeft( minorLabel->moveToLeft(
@ -664,21 +737,24 @@ void InnerWidget::fill() {
st::boxRowPadding); st::boxRowPadding);
sub->setTextColorOverride(st::windowSubTextFg->c); sub->setTextColorOverride(st::windowSubTextFg->c);
}; };
auto availValueMap = [](const auto &v) { return v.availableBalance; };
auto currentValueMap = [](const auto &v) { return v.currentBalance; };
auto overallValueMap = [](const auto &v) { return v.overallRevenue; };
addOverview( addOverview(
data.availableBalance, rpl::duplicate(currencyStateValue) | rpl::map(availValueMap),
creditsData.availableBalance, rpl::duplicate(creditsStateValue) | rpl::map(availValueMap),
tr::lng_channel_earn_available); tr::lng_channel_earn_available);
Ui::AddSkip(container); Ui::AddSkip(container);
Ui::AddSkip(container); Ui::AddSkip(container);
addOverview( addOverview(
data.currentBalance, rpl::duplicate(currencyStateValue) | rpl::map(currentValueMap),
creditsData.currentBalance, rpl::duplicate(creditsStateValue) | rpl::map(currentValueMap),
tr::lng_channel_earn_reward); tr::lng_channel_earn_reward);
Ui::AddSkip(container); Ui::AddSkip(container);
Ui::AddSkip(container); Ui::AddSkip(container);
addOverview( addOverview(
data.overallRevenue, rpl::duplicate(currencyStateValue) | rpl::map(overallValueMap),
creditsData.overallRevenue, rpl::duplicate(creditsStateValue) | rpl::map(overallValueMap),
tr::lng_channel_earn_total); tr::lng_channel_earn_total);
Ui::AddSkip(container); Ui::AddSkip(container);
} }

View file

@ -63,6 +63,7 @@ private:
rpl::event_stream<> _showFinished; rpl::event_stream<> _showFinished;
rpl::event_stream<> _focusRequested; rpl::event_stream<> _focusRequested;
rpl::event_stream<bool> _loaded; rpl::event_stream<bool> _loaded;
rpl::event_stream<> _stateUpdated;
}; };