Skip to content

Commit

Permalink
Fix the Ad Rewards settings panel
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Apr 18, 2019
1 parent 6ce8091 commit f3fb38b
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 161 deletions.
33 changes: 18 additions & 15 deletions browser/ui/webui/brave_rewards_ui.cc
Expand Up @@ -105,15 +105,17 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnContentSiteUpdated(
brave_rewards::RewardsService* rewards_service) override;
void GetAddressesForPaymentId(const base::ListValue* args);
void GetConfirmationsHistory(const base::ListValue* args);
void GetTransactionHistoryForThisCycle(const base::ListValue* args);
void GetRewardsMainEnabled(const base::ListValue* args);
void OnGetRewardsMainEnabled(bool enabled);
void OnAdsIsSupportedRegion(bool is_supported);

void GetExcludedPublishersNumber(const base::ListValue* args);
void AdsIsSupportedRegion(const base::ListValue* args);

void OnConfirmationsHistory(int total_viewed, double estimated_earnings);
void OnTransactionHistoryForThisCycle(
int ads_notifications_received,
double estimated_earnings);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
Expand Down Expand Up @@ -161,7 +163,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList list) override;

void OnConfirmationsHistoryChanged(
void OnTransactionHistoryForThisCycleChanged(
brave_rewards::RewardsService* rewards_service) override;

// RewardsNotificationsServiceObserver implementation
Expand Down Expand Up @@ -276,8 +278,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getAddressesForPaymentId",
base::BindRepeating(&RewardsDOMHandler::GetAddressesForPaymentId,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getConfirmationsHistory",
base::BindRepeating(&RewardsDOMHandler::GetConfirmationsHistory,
web_ui()->RegisterMessageCallback(
"brave_rewards.getTransactionHistoryForThisCycle",
base::BindRepeating(&RewardsDOMHandler::GetTransactionHistoryForThisCycle,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getRewardsMainEnabled",
base::BindRepeating(&RewardsDOMHandler::GetRewardsMainEnabled,
Expand Down Expand Up @@ -977,32 +980,32 @@ void RewardsDOMHandler::GetAddressesForPaymentId(
}
}

void RewardsDOMHandler::GetConfirmationsHistory(
void RewardsDOMHandler::GetTransactionHistoryForThisCycle(
const base::ListValue* args) {
rewards_service_->GetConfirmationsHistory(base::Bind(
&RewardsDOMHandler::OnConfirmationsHistory,
weak_factory_.GetWeakPtr()));
rewards_service_->GetTransactionHistoryForThisCycle(base::Bind(
&RewardsDOMHandler::OnTransactionHistoryForThisCycle,
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnConfirmationsHistory(
int total_viewed,
void RewardsDOMHandler::OnTransactionHistoryForThisCycle(
int ads_notifications_received,
double estimated_earnings) {
if (web_ui()->CanCallJavascript()) {
base::DictionaryValue history;

history.SetInteger("adsTotalPages", total_viewed);
history.SetInteger("adsNotificationsReceived", ads_notifications_received);
history.SetDouble("adsEstimatedEarnings", estimated_earnings);

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.confirmationsHistory", history);
"brave_rewards.transactionHistoryForThisCycle", history);
}
}

void RewardsDOMHandler::OnConfirmationsHistoryChanged(
void RewardsDOMHandler::OnTransactionHistoryForThisCycleChanged(
brave_rewards::RewardsService* rewards_service) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.confirmationsHistoryChanged");
"brave_rewards.transactionHistoryForThisCycleChanged");
}
}

Expand Down
7 changes: 4 additions & 3 deletions components/brave_rewards/browser/rewards_service.h
Expand Up @@ -68,7 +68,8 @@ using GetReconcileStampCallback = base::Callback<void(uint64_t)>;
using IsWalletCreatedCallback = base::Callback<void(bool)>;
using GetPendingContributionsTotalCallback = base::Callback<void(double)>;
using GetRewardsMainEnabledCallback = base::Callback<void(bool)>;
using ConfirmationsHistoryCallback = base::Callback<void(int, double)>;
using GetTransactionHistoryForThisCycleCallback =
base::Callback<void(int, double)>;
using GetRewardsInternalsInfoCallback = base::OnceCallback<void(
std::unique_ptr<brave_rewards::RewardsInternalsInfo>)>;

Expand Down Expand Up @@ -180,8 +181,8 @@ class RewardsService : public KeyedService {

virtual void GetAddressesForPaymentId(
const GetAddressesCallback& callback) = 0;
virtual void GetConfirmationsHistory(
brave_rewards::ConfirmationsHistoryCallback callback) = 0;
virtual void GetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback) = 0;

void AddObserver(RewardsServiceObserver* observer);
void RemoveObserver(RewardsServiceObserver* observer);
Expand Down
75 changes: 17 additions & 58 deletions components/brave_rewards/browser/rewards_service_impl.cc
Expand Up @@ -1485,40 +1485,6 @@ void RewardsServiceImpl::SetCatalogIssuers(const std::string& json) {
bat_ledger_->SetCatalogIssuers(json);
}

std::pair<uint64_t, uint64_t> RewardsServiceImpl::GetEarningsRange() {
auto now = base::Time::Now();
base::Time::Exploded exploded;
now.LocalExplode(&exploded);

if (exploded.day_of_month < 5) {
exploded.month--;
if (exploded.month < 1) {
exploded.month = 12;

exploded.year--;
}
}

exploded.day_of_month = 1;

exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;

base::Time from_timestamp;
auto success = base::Time::FromLocalExploded(exploded, &from_timestamp);
DCHECK(success);

uint64_t from_timestamp_in_seconds =
(from_timestamp - base::Time()).InSeconds();

uint64_t to_timestamp_in_seconds =
(now - base::Time()).InSeconds();

return std::make_pair(from_timestamp_in_seconds, to_timestamp_in_seconds);
}

void RewardsServiceImpl::ConfirmAd(const std::string& json) {
if (!Connected()) {
return;
Expand All @@ -1535,51 +1501,44 @@ void RewardsServiceImpl::SetConfirmationsIsReady(const bool is_ready) {

void RewardsServiceImpl::ConfirmationsTransactionHistoryDidChange() {
for (auto& observer : observers_)
observer.OnConfirmationsHistoryChanged(this);
observer.OnTransactionHistoryForThisCycleChanged(this);
}

void RewardsServiceImpl::GetConfirmationsHistory(
brave_rewards::ConfirmationsHistoryCallback callback) {
void RewardsServiceImpl::GetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback) {
if (!Connected()) {
return;
}

auto earnings_range = GetEarningsRange();

bat_ledger_->GetConfirmationsHistory(
earnings_range.first,
earnings_range.second,
base::BindOnce(&RewardsServiceImpl::OnGetConfirmationsHistory,
AsWeakPtr(),
std::move(callback)));
bat_ledger_->GetTransactionHistoryForThisCycle(
base::BindOnce(&RewardsServiceImpl::OnGetTransactionHistoryForThisCycle,
AsWeakPtr(), std::move(callback)));
}

void RewardsServiceImpl::OnGetConfirmationsHistory(
brave_rewards::ConfirmationsHistoryCallback callback,
void RewardsServiceImpl::OnGetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback,
const std::string& transactions) {
std::unique_ptr<ledger::TransactionsInfo> info;
if (!transactions.empty()) {
info.reset(new ledger::TransactionsInfo());
info->FromJson(transactions);
}

if (!info) {
if (transactions.empty()) {
callback.Run(0, 0.0);
return;
}

ledger::TransactionsInfo info;
info.FromJson(transactions);

int ads_notifications_received = 0;
double estimated_earnings = 0.0;
int total_viewed = 0;

for (const auto& transaction : info->transactions) {
for (const auto& transaction : info.transactions) {
if (transaction.estimated_redemption_value == 0.0) {
continue;
}

ads_notifications_received++;
estimated_earnings += transaction.estimated_redemption_value;
total_viewed++;
}

callback.Run(total_viewed, estimated_earnings);
callback.Run(ads_notifications_received, estimated_earnings);
}

void RewardsServiceImpl::SaveState(const std::string& name,
Expand Down
9 changes: 4 additions & 5 deletions components/brave_rewards/browser/rewards_service_impl.h
Expand Up @@ -192,7 +192,6 @@ class RewardsServiceImpl : public RewardsService,
const GetRewardsMainEnabledCallback& callback) const override;

void GetAddressesForPaymentId(const GetAddressesCallback& callback) override;
std::pair<uint64_t, uint64_t> GetEarningsRange();

// Testing methods
void SetLedgerEnvForTesting();
Expand Down Expand Up @@ -337,8 +336,8 @@ class RewardsServiceImpl : public RewardsService,
void SetCatalogIssuers(const std::string& json) override;
void ConfirmAd(const std::string& json) override;
void SetConfirmationsIsReady(const bool is_ready) override;
void GetConfirmationsHistory(
brave_rewards::ConfirmationsHistoryCallback callback) override;
void GetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback) override;
void ConfirmationsTransactionHistoryDidChange() override;

void OnExcludedSitesChanged(const std::string& publisher_id,
Expand Down Expand Up @@ -419,8 +418,8 @@ class RewardsServiceImpl : public RewardsService,

// Mojo Proxy methods
void OnPublisherBannerMojoProxy(const std::string& banner);
void OnGetConfirmationsHistory(
brave_rewards::ConfirmationsHistoryCallback callback,
void OnGetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback,
const std::string& transactions);
void OnGetAllBalanceReports(
const GetAllBalanceReportsCallback& callback,
Expand Down
Expand Up @@ -79,7 +79,7 @@ class RewardsServiceObserver : public base::CheckedObserver {
virtual void OnPublisherListNormalized(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {}
virtual void OnConfirmationsHistoryChanged(
virtual void OnTransactionHistoryForThisCycleChanged(
brave_rewards::RewardsService* rewards_service) {}
// DO NOT ADD ANY MORE METHODS HERE UNLESS IT IS A BROADCAST NOTIFICATION
// RewardsServiceObserver should not be used to return responses to the
Expand Down
Expand Up @@ -175,14 +175,14 @@ export const onAddressesForPaymentId = (addresses: Record<Rewards.AddressesType,
addresses
})

export const onConfirmationsHistory = (data: {adsTotalPages: number, adsEstimatedEarnings: number}) =>
action(types.ON_CONFIRMATIONS_HISTORY, {
export const onTransactionHistoryForThisCycle = (data: {adsNotificationsReceived: number, adsEstimatedEarnings: number}) =>
action(types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE, {
data
})

export const getConfirmationsHistory = () => action(types.GET_CONFIRMATIONS_HISTORY)
export const getTransactionHistoryForThisCycle = () => action(types.GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE)

export const onConfirmationsHistoryChanged = () => action(types.ON_CONFIRMATIONS_HISTORY_CHANGED)
export const onTransactionHistoryForThisCycleChanged = () => action(types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED)

export const getExcludedPublishersNumber = () => action(types.GET_EXCLUDED_PUBLISHERS_NUMBER)

Expand Down
17 changes: 6 additions & 11 deletions components/brave_rewards/resources/ui/brave_rewards.tsx
Expand Up @@ -147,16 +147,12 @@ window.cr.define('brave_rewards', function () {
getActions().onAddressesForPaymentId(addresses)
}

function confirmationsHistory (totalPages: number, estimatedEarnings: number) {
getActions().onConfirmationsHistory(totalPages, estimatedEarnings)
function transactionHistoryForThisCycle (totalPages: number, estimatedEarnings: number) {
getActions().onTransactionHistoryForThisCycle(totalPages, estimatedEarnings)
}

function confirmationsHistoryChanged () {
getActions().onConfirmationsHistoryChanged()
}

function adsIsSupportedRegion (supported: boolean) {
getActions().onAdsIsSupportedRegion(supported)
function transactionHistoryForThisCycleChanged () {
getActions().onTransactionHistoryForThisCycleChanged()
}

return {
Expand Down Expand Up @@ -185,9 +181,8 @@ window.cr.define('brave_rewards', function () {
onPendingContributionSaved,
rewardsEnabled,
addressesForPaymentId,
confirmationsHistory,
confirmationsHistoryChanged,
adsIsSupportedRegion
transactionHistoryForThisCycle,
transactionHistoryForThisCycleChanged
}
})

Expand Down
Expand Up @@ -38,7 +38,7 @@ class SettingsPage extends React.Component<Props, {}> {
this.actions.getContributeList()
this.actions.getPendingContributionsTotal()
this.actions.getReconcileStamp()
this.actions.getConfirmationsHistory()
this.actions.getTransactionHistoryForThisCycle()
this.actions.getExcludedPublishersNumber()
this.actions.getAdsData()
}
Expand Down
Expand Up @@ -54,9 +54,9 @@ export const enum types {
ON_REWARDS_ENABLED = '@@rewards/ON_REWARDS_ENABLED',
GET_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/GET_ADDRESSES_FOR_PAYMENT_ID',
ON_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/ON_ADDRESSES_FOR_PAYMENT_ID',
GET_CONFIRMATIONS_HISTORY = '@@rewards/GET_CONFIRMATIONS_HISTORY',
ON_CONFIRMATIONS_HISTORY = '@@rewards/ON_CONFIRMATIONS_HISTORY',
ON_CONFIRMATIONS_HISTORY_CHANGED = '@@rewards/ON_CONFIRMATIONS_HISTORY_CHANGED',
GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE = '@@rewards/GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE',
ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE = '@@rewards/ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE',
ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED = '@@rewards/ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED',
GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER',
GET_ADS_IS_SUPPORTED_REGION = '@@rewards/GET_ADS_IS_SUPPORTED_REGION',
ON_ADS_IS_SUPPORTED_REGION = '@@rewards/ON_ADS_IS_SUPPORTED_REGION'
Expand Down
Expand Up @@ -138,19 +138,19 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
state.enabledMain = action.payload.enabled
break
}
case types.GET_CONFIRMATIONS_HISTORY:
case types.ON_CONFIRMATIONS_HISTORY_CHANGED: {
chrome.send('brave_rewards.getConfirmationsHistory', [])
case types.GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE:
case types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED: {
chrome.send('brave_rewards.getTransactionHistoryForThisCycle', [])
break
}
case types.ON_CONFIRMATIONS_HISTORY: {
case types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE: {
if (!action.payload.data) {
break
}

state = { ...state }
const data = action.payload.data
state.adsData.adsNotificationsReceived = data.adsTotalPages
state.adsData.adsNotificationsReceived = data.adsNotificationsReceived
state.adsData.adsEstimatedEarnings = data.adsEstimatedEarnings
break
}
Expand Down
2 changes: 1 addition & 1 deletion components/resources/brave_components_strings.grd
Expand Up @@ -196,7 +196,7 @@
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_DESC" desc="">Earn tokens by viewing ads in Brave. Ads presented are based on your interests, as inferred from your browsing behavior. No personal data or browsing history ever leaves your browser.</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_CONTR_TITLE" desc="">Auto-Contribute</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_CONTR_DESC" desc="">An automatic way to support publishers and content creators. Set a monthly payment and browse normally. The sites you visit receive your contributions automatically, based on your attention as measured by Brave.</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_CURRENT_EARNINGS" desc="">Current earnings this month (Estimated)</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_CURRENT_EARNINGS" desc="">Estimated earnings this cycle</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_PAYMENT_DATE" desc="">Payment date</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_NOTIFICATIONS_RECEIVED" desc="">Ad notifications received</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_NOT_SUPPORTED" desc="">Sorry! Ads are not yet available in your region.</message>
Expand Down
18 changes: 8 additions & 10 deletions components/services/bat_ledger/bat_ledger_impl.cc
Expand Up @@ -396,25 +396,23 @@ void BatLedgerImpl::ConfirmAd(const std::string& info) {
}

// static
void BatLedgerImpl::OnGetConfirmationsHistory(
CallbackHolder<GetConfirmationsHistoryCallback>* holder,
void BatLedgerImpl::OnGetTransactionHistoryForThisCycle(
CallbackHolder<GetTransactionHistoryForThisCycleCallback>* holder,
std::unique_ptr<ledger::TransactionsInfo> history) {
std::string json_transactions = history.get() ? history->ToJson() : "";
if (holder->is_valid())
std::move(holder->get()).Run(json_transactions);
delete holder;
}

void BatLedgerImpl::GetConfirmationsHistory(
const uint64_t from_timestamp_seconds,
const uint64_t to_timestamp_seconds,
GetConfirmationsHistoryCallback callback) {
auto* holder = new CallbackHolder<GetConfirmationsHistoryCallback>(
void BatLedgerImpl::GetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback) {
auto* holder = new CallbackHolder<GetTransactionHistoryForThisCycleCallback>(
AsWeakPtr(), std::move(callback));

ledger_->GetConfirmationsHistory(from_timestamp_seconds,
to_timestamp_seconds, std::bind(
BatLedgerImpl::OnGetConfirmationsHistory, holder, _1));
ledger_->GetTransactionHistoryForThisCycle(
std::bind(BatLedgerImpl::OnGetTransactionHistoryForThisCycle,
holder, _1));
}

void BatLedgerImpl::GetRewardsInternalsInfo(
Expand Down

0 comments on commit f3fb38b

Please sign in to comment.