Skip to content

Commit

Permalink
Adds update function for pending totals
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jan 11, 2019
1 parent f269e5c commit 84ba860
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 8 deletions.
13 changes: 13 additions & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnCurrentTips(brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList) override;

void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) override;

// RewardsNotificationsServiceObserver implementation
void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down Expand Up @@ -864,6 +868,15 @@ void RewardsDOMHandler::OnGetPendingContributionsTotal(double amount) {
}
}

void RewardsDOMHandler::OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.onPendingContributionSaved", base::Value(result));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
11 changes: 11 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@
"type": "boolean"
}
]
},
{
"name": "onPendingContributionSaved",
"type": "function",
"description": "",
"parameters": [
{
"name": "result",
"type": "integer"
}
]
}
],
"functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,22 @@ void ExtensionRewardsServiceObserver::OnRewardsMainEnabled(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnPendingContributionSaved(
RewardsService* rewards_service,
int result) {
extensions::EventRouter* event_router = extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnPendingContributionSaved::Create(result)
.release());
std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnPendingContributionSaved::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnRewardsMainEnabled(RewardsService* rewards_service,
bool rewards_main_enabled) override;

void OnPendingContributionSaved(RewardsService* rewards_service,
int result) override;

private:
Profile* profile_;

Expand Down
13 changes: 8 additions & 5 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2191,17 +2191,20 @@ void RewardsServiceImpl::SetShortRetries(bool short_retries) {
bat_ledger_service_->SetShortRetries(short_retries);
}

bool SavePendingContributionOnFileTaskRunner(PublisherInfoDatabase* backend,
ledger::Result SavePendingContributionOnFileTaskRunner(PublisherInfoDatabase* backend,
const ledger::PendingContributionList& list) {
if (!backend) {
return false;
return ledger::Result::LEDGER_ERROR;
}

return backend->InsertPendingContribution(list);
bool result = backend->InsertPendingContribution(list);

return result ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR;
}

void RewardsServiceImpl::OnSavePendingContribution(bool result) {
// TODO(nejczdovc) add callback with db result
void RewardsServiceImpl::OnSavePendingContribution(ledger::Result result) {
for (auto& observer : observers_)
observer.OnPendingContributionSaved(this, result);
}

void RewardsServiceImpl::SavePendingContribution(
Expand Down
2 changes: 1 addition & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class RewardsServiceImpl : public RewardsService,
void SavePendingContribution(
const ledger::PendingContributionList& list) override;

void OnSavePendingContribution(bool result);
void OnSavePendingContribution(ledger::Result result);

// URLFetcherDelegate impl
void OnURLFetchComplete(const net::URLFetcher* source) override;
Expand Down
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class RewardsServiceObserver : public base::CheckedObserver {
const brave_rewards::PublisherBanner banner) {};
virtual void OnRewardsMainEnabled(brave_rewards::RewardsService* rewards_service,
bool rewards_main_enabled) {};
virtual void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) {};
// DO NOT ADD ANY MORE METHODS HERE UNLESS IT IS A BROADCAST NOTIFICATION
// RewardsServiceObserver should not be used to return responses to the caller.
// Method calls on RewardsService should use callbacks to return responses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ chrome.rewardsNotifications.onNotificationDeleted.addListener((id: string, type:
chrome.braveRewards.onEnabledMain.addListener((enabledMain: boolean) => {
rewardsPanelActions.onEnabledMain(enabledMain)
})

chrome.braveRewards.onPendingContributionSaved.addListener((result: number) => {
if (result === 0) {
chrome.braveRewards.getPendingContributionsTotal(((amount: number) => {
rewardsPanelActions.OnPendingContributionsTotal(amount)
}))
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const enum types {
ON_GRANT_RESET = '@@rewards_panel/ON_GRANT_RESET',
ON_GRANT_DELETE = '@@rewards_panel/ON_GRANT_DELETE',
ON_GRANT_FINISH = '@@rewards_panel/ON_GRANT_FINISH',
GET_PENDING_CONTRIBUTIONS_TOTAL = '@@rewards_panel/GET_PENDING_CONTRIBUTIONS_TOTAL',
ON_PENDING_CONTRIBUTIONS_TOTAL = '@@rewards_panel/ON_PENDING_CONTRIBUTIONS_TOTAL',
ON_ENABLED_MAIN = '@@rewards_panel/ON_ENABLED_MAIN'
}
Expand Down
9 changes: 8 additions & 1 deletion components/brave_rewards/resources/ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ window.cr.define('brave_rewards', function () {
getActions().onPendingContributionTotal(amount)
}

function onPendingContributionSaved (result: number) {
if (result === 0) {
getActions().getPendingContributionsTotal()
}
}

return {
initialize,
walletCreated,
Expand All @@ -155,7 +161,8 @@ window.cr.define('brave_rewards', function () {
initAutoContributeSettings,
imported,
adsData,
pendingContributionTotal
pendingContributionTotal,
onPendingContributionSaved
}
})

Expand Down
3 changes: 3 additions & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ declare namespace chrome.braveRewards {
}
const getRewardsMainEnabled: (callback: (enabled: boolean) => void) => {}
const saveAdsSetting: (key: string, value: string) => {}
const onPendingContributionSaved: {
addListener: (callback: (result: number) => void) => void
}
}

declare namespace chrome.rewardsNotifications {
Expand Down

0 comments on commit 84ba860

Please sign in to comment.