Replaced api for credits rate.

This commit is contained in:
23rd 2024-08-15 16:06:04 +03:00
parent 3a7a485dd0
commit 838d5669ed
2 changed files with 12 additions and 34 deletions

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "api/api_credits.h"
#include "data/data_user.h"
#include "main/main_app_config.h"
#include "main/main_session.h"
namespace Data {
@ -31,26 +32,10 @@ void Credits::apply(const MTPDupdateStarsBalance &data) {
rpl::producer<float64> Credits::rateValue(
not_null<PeerData*> ownedBotOrChannel) {
// Should be replaced in the future.
if (_rate > 0) {
return rpl::single(_rate);
}
return [=](auto consumer) {
auto lifetime = rpl::lifetime();
const auto api = lifetime.make_state<Api::CreditsEarnStatistics>(
ownedBotOrChannel);
api->request(
) | rpl::start_with_done([=] {
_rate = api->data().usdRate;
if (_rate > 0) {
consumer.put_next_copy(_rate);
consumer.put_done();
}
}, lifetime);
return lifetime;
};
return rpl::single(
_session->appConfig().get<float64>(
u"stars_usd_withdraw_rate_x1000"_q,
1200) / 1000.);
}
void Credits::load(bool force) {

View file

@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/boxes/edit_invite_link_session.h"
#include "api/api_credits.h"
#include "data/components/credits.h"
#include "data/data_peer.h"
#include "data/data_session.h"
#include "data/stickers/data_custom_emoji.h"
@ -39,7 +39,7 @@ InviteLinkSubscriptionToggle FillCreateInviteLinkSubscriptionToggle(
not_null<Ui::GenericBox*> box,
not_null<PeerData*> peer) {
struct State final {
float64 usdRate = 0;
rpl::variable<float64> usdRate = 0;
};
const auto state = box->lifetime().make_state<State>();
const auto currency = u"USD"_q;
@ -101,7 +101,7 @@ InviteLinkSubscriptionToggle FillCreateInviteLinkSubscriptionToggle(
});
priceOverlay->paintRequest(
) | rpl::start_with_next([=, right = st::boxRowPadding.right()] {
if (state->usdRate <= 0) {
if (state->usdRate.current() <= 0) {
return;
}
const auto amount = input->getLastText().toDouble();
@ -111,7 +111,9 @@ InviteLinkSubscriptionToggle FillCreateInviteLinkSubscriptionToggle(
const auto text = tr::lng_group_invite_subscription_price(
tr::now,
lt_cost,
Ui::FillAmountAndCurrency(amount * state->usdRate, currency));
Ui::FillAmountAndCurrency(
amount * state->usdRate.current(),
currency));
auto p = QPainter(priceOverlay);
p.setFont(st.placeholderFont);
p.setPen(st.placeholderFg);
@ -120,16 +122,7 @@ InviteLinkSubscriptionToggle FillCreateInviteLinkSubscriptionToggle(
p.drawText(priceOverlay->rect() - m, text, style::al_right);
}, priceOverlay->lifetime());
{
auto &lifetime = priceOverlay->lifetime();
const auto api = lifetime.make_state<Api::CreditsEarnStatistics>(
peer);
api->request(
) | rpl::start_with_done([=] {
state->usdRate = api->data().usdRate;
priceOverlay->update();
}, priceOverlay->lifetime());
}
state->usdRate = peer->session().credits().rateValue(peer);
const auto arrow = Ui::Text::SingleCustomEmoji(
peer->owner().customEmojiManager().registerInternalEmoji(