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 ae552bf commit 115d3d0
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 156 deletions.
33 changes: 18 additions & 15 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ 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 GetExcludedPublishersNumber(const base::ListValue* args);

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

void OnGetRecurringTips(
std::unique_ptr<brave_rewards::ContentSiteList> list);
Expand Down Expand Up @@ -160,7 +162,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;

void OnRecurringTipSaved(brave_rewards::RewardsService* rewards_service,
Expand Down Expand Up @@ -286,8 +288,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 @@ -997,32 +1000,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
Original file line number Diff line number Diff line change
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>)>;
using GetRecurringTipsCallback = base::OnceCallback<void(
Expand Down Expand Up @@ -189,8 +190,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;

virtual void RefreshPublisher(
const std::string& publisher_key,
Expand Down
75 changes: 17 additions & 58 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1518,40 +1518,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 @@ -1568,51 +1534,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
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ class RewardsServiceImpl : public RewardsService,
const GetRewardsMainEnabledCallback& callback) const override;

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

void GetOneTimeTipsUI(GetOneTimeTipsCallback callback) override;
void RefreshPublisher(
Expand Down Expand Up @@ -364,8 +363,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 @@ -448,8 +447,8 @@ class RewardsServiceImpl : public RewardsService,
void ShowNotificationTipsPaid(bool ac_enabled);

// Mojo Proxy methods
void OnGetConfirmationsHistory(
brave_rewards::ConfirmationsHistoryCallback callback,
void OnGetTransactionHistoryForThisCycle(
GetTransactionHistoryForThisCycleCallback callback,
const std::string& transactions);
void OnGetAllBalanceReports(
const GetAllBalanceReportsCallback& callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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) {}
virtual void OnRecurringTipSaved(
brave_rewards::RewardsService* rewards_service,
Expand Down
Original file line number Diff line number Diff line change
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
12 changes: 6 additions & 6 deletions components/brave_rewards/resources/ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +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 transactionHistoryForThisCycleChanged () {
getActions().onTransactionHistoryForThisCycleChanged()
}

function recurringTipSaved (success: boolean) {
Expand Down Expand Up @@ -193,8 +193,8 @@ window.cr.define('brave_rewards', function () {
onPendingContributionSaved,
rewardsEnabled,
addressesForPaymentId,
confirmationsHistory,
confirmationsHistoryChanged,
transactionHistoryForThisCycle,
transactionHistoryForThisCycleChanged,
recurringTipSaved,
recurringTipRemoved,
onContributionSaved
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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_REWARDS_MAIN_ENABLED = '@@rewards/GET_REWARDS_MAIN_ENABLED',
ON_RECURRING_TIP_SAVED = '@@rewards/ON_RECURRING_TIP_SAVED',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,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
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,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
Original file line number Diff line number Diff line change
Expand Up @@ -398,25 +398,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
Loading

0 comments on commit 115d3d0

Please sign in to comment.