Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Adds delays for suggestion api (uplift to 1.3) #4441

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void OnWalletProperties(int error_code) {}
public void OnPublisherInfo(int tabId) {}

@Override
public void OnGetCurrentBalanceReport(String[] report) {}
public void OnGetCurrentBalanceReport(double[] report) {}

@Override
public void OnNotificationAdded(String id, int type, long timestamp, String[] args) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public void OnIsWalletCreated(boolean created) {
}

@CalledByNative
public void OnGetCurrentBalanceReport(String[] report) {
public void OnGetCurrentBalanceReport(double[] report) {
for(BraveRewardsObserver observer : observers_) {
observer.OnGetCurrentBalanceReport(report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface BraveRewardsObserver {
public void OnWalletInitialized(int error_code);
public void OnWalletProperties(int error_code);
public void OnPublisherInfo(int tabId);
public void OnGetCurrentBalanceReport(String[] report);
public void OnGetCurrentBalanceReport(double[] report);
public void OnNotificationAdded(String id, int type, long timestamp,
String[] args);
public void OnNotificationsCount(int count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ public void OnPublisherInfo(int tabId) {
}

@Override
public void OnGetCurrentBalanceReport(String[] report) {
public void OnGetCurrentBalanceReport(double[] report) {
boolean no_activity = true;
for (int i = 0; i < report.length; i++) {
TextView tvTitle = null;
Expand All @@ -1201,7 +1201,7 @@ public void OnGetCurrentBalanceReport(String[] report) {
String text = "";
String textUSD = "";

double probiDouble = BraveRewardsHelper.probiToDouble(report[i]);
double probiDouble = report[i];
boolean hideControls = (probiDouble == 0);
String value = Double.isNaN(probiDouble) ? ERROR_CONVERT_PROBI : String.format(Locale.getDefault(), "%.1f", probiDouble);

Expand Down Expand Up @@ -1261,7 +1261,7 @@ public void OnGetCurrentBalanceReport(String[] report) {
tv.setText(toInsert);
tvUSD.setText(textUSD);
}
if (!report[i].equals("0")) {
if (report[i] != 0) {
no_activity = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void OnWalletProperties(int error_code){
public void OnPublisherInfo(int tabId){}

@Override
public void OnGetCurrentBalanceReport(String[] report){}
public void OnGetCurrentBalanceReport(double[] report){}

@Override
public void OnNotificationAdded(String id, int type, long timestamp, String[] args) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void OnWalletInitialized(int error_code) {
public void OnPublisherInfo(int tabId){};

@Override
public void OnGetCurrentBalanceReport(String[] report){};
public void OnGetCurrentBalanceReport(double[] report){};

@Override
public void OnNotificationAdded(String id, int type, long timestamp, String[] args){};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void OnWalletProperties(int error_code) {}
public void OnPublisherInfo(int tabId) {}

@Override
public void OnGetCurrentBalanceReport(String[] report) {}
public void OnGetCurrentBalanceReport(double[] report) {}

@Override
public void OnNotificationAdded(String id, int type, long timestamp, String[] args) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public void OnWalletProperties(int error_code) {}
public void OnPublisherInfo(int tabId) {}

@Override
public void OnGetCurrentBalanceReport(String[] report) {}
public void OnGetCurrentBalanceReport(double[] report) {}

@Override
public void OnNotificationAdded(String id, int type, long timestamp,
Expand Down
19 changes: 13 additions & 6 deletions browser/brave_rewards/android/brave_rewards_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include <utility>

#include "base/time/time.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/jni_array.h"
Expand Down Expand Up @@ -304,25 +305,31 @@ void BraveRewardsNativeWorker::OnIsWalletCreated(bool created) {
void BraveRewardsNativeWorker::GetCurrentBalanceReport(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj) {
if (brave_rewards_service_) {
return brave_rewards_service_->GetCurrentBalanceReport();
auto now = base::Time::Now();
base::Time::Exploded exploded;
now.LocalExplode(&exploded);

brave_rewards_service_->GetBalanceReport(
exploded.month, exploded.year,
base::BindOnce(&BraveRewardsNativeWorker::OnGetCurrentBalanceReport,
base::Unretained(this), brave_rewards_service_));
}
}

void BraveRewardsNativeWorker::OnGetCurrentBalanceReport(
brave_rewards::RewardsService* rewards_service,
const int32_t result,
const brave_rewards::BalanceReport& balance_report) {
std::vector<std::string> values;
values.push_back(balance_report.deposits);
std::vector<double> values;
values.push_back(balance_report.grants);
values.push_back(balance_report.earning_from_ads);
values.push_back(balance_report.auto_contribute);
values.push_back(balance_report.recurring_donation);
values.push_back(balance_report.one_time_donation);
values.push_back(balance_report.total);

JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobjectArray> java_array =
base::android::ToJavaArrayOfStrings(env, values);
base::android::ScopedJavaLocalRef<jdoubleArray> java_array =
base::android::ToJavaDoubleArray(env, values);

Java_BraveRewardsNativeWorker_OnGetCurrentBalanceReport(env,
weak_java_brave_rewards_native_worker_.get(env), java_array);
Expand Down
3 changes: 2 additions & 1 deletion browser/brave_rewards/android/brave_rewards_native_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ class BraveRewardsNativeWorker : public brave_rewards::RewardsServiceObserver,

void OnGetCurrentBalanceReport(
brave_rewards::RewardsService* rewards_service,
const brave_rewards::BalanceReport& balance_report) override;
const int32_t result,
const brave_rewards::BalanceReport& balance_report);

void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down
79 changes: 29 additions & 50 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,16 +418,39 @@ BraveRewardsGetWalletPropertiesFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsGetCurrentReportFunction::~BraveRewardsGetCurrentReportFunction() {
}
BraveRewardsGetBalanceReportFunction::
~BraveRewardsGetBalanceReportFunction() = default;

ExtensionFunction::ResponseAction BraveRewardsGetCurrentReportFunction::Run() {
ExtensionFunction::ResponseAction BraveRewardsGetBalanceReportFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
rewards_service->GetCurrentBalanceReport();
if (!rewards_service) {
auto data = std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
return RespondNow(OneArgument(std::move(data)));
}
return RespondNow(NoArguments());

std::unique_ptr<brave_rewards::GetBalanceReport::Params> params(
brave_rewards::GetBalanceReport::Params::Create(*args_));

rewards_service->GetBalanceReport(
params->month,
params->year,
base::BindOnce(
&BraveRewardsGetBalanceReportFunction::OnBalanceReport,
this));
return RespondLater();
}

void BraveRewardsGetBalanceReportFunction::OnBalanceReport(
const int32_t result,
const ::brave_rewards::BalanceReport& report) {
auto data = std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
data->SetDoubleKey("ads", report.earning_from_ads);
data->SetDoubleKey("contribute", report.auto_contribute);
data->SetDoubleKey("grant", report.grants);
data->SetDoubleKey("tips", report.one_time_donation);
data->SetDoubleKey("donation", report.recurring_donation);
Respond(OneArgument(std::move(data)));
}

BraveRewardsFetchPromotionsFunction::
Expand Down Expand Up @@ -1062,50 +1085,6 @@ void BraveRewardsGetAdsEstimatedEarningsFunction::OnAdsEstimatedEarnings(
std::make_unique<base::Value>(estimated_pending_rewards)));
}

BraveRewardsGetBalanceReportsFunction::
~BraveRewardsGetBalanceReportsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetBalanceReportsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service_ = RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service_) {
return RespondNow(Error("Rewards service is not not initialized"));
}

rewards_service_->GetAllBalanceReports(base::Bind(
&BraveRewardsGetBalanceReportsFunction::OnGetBalanceReports,
this));

return RespondLater();
}

void BraveRewardsGetBalanceReportsFunction::OnGetBalanceReports(
const std::map<std::string,
::brave_rewards::BalanceReport>& reports) {
std::unique_ptr<base::DictionaryValue> data(
new base::DictionaryValue());

if (!reports.empty()) {
for (auto const& report : reports) {
const ::brave_rewards::BalanceReport old_report = report.second;
auto new_report = std::make_unique<base::DictionaryValue>();
new_report->SetString("grant", old_report.grants);
new_report->SetString("deposit", old_report.deposits);
new_report->SetString("ads", old_report.earning_from_ads);
new_report->SetString("contribute", old_report.auto_contribute);
new_report->SetString("donation", old_report.recurring_donation);
new_report->SetString("tips", old_report.one_time_donation);
new_report->SetString("total", old_report.total);
data->SetDictionary(report.first, std::move(new_report));
}
}

Respond(OneArgument(std::move(data)));
}

BraveRewardsGetWalletExistsFunction::
~BraveRewardsGetWalletExistsFunction() {
}
Expand Down
27 changes: 8 additions & 19 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ class BraveRewardsGetWalletPropertiesFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetCurrentReportFunction : public ExtensionFunction {
class BraveRewardsGetBalanceReportFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getCurrentReport", UNKNOWN)
DECLARE_EXTENSION_FUNCTION("braveRewards.getBalanceReport", UNKNOWN)

protected:
~BraveRewardsGetCurrentReportFunction() override;
~BraveRewardsGetBalanceReportFunction() override;

ResponseAction Run() override;

private:
void OnBalanceReport(
const int32_t result,
const brave_rewards::BalanceReport& report);
};

class BraveRewardsIncludeInAutoContributionFunction : public ExtensionFunction {
Expand Down Expand Up @@ -415,22 +420,6 @@ class BraveRewardsGetAdsEstimatedEarningsFunction
const uint64_t ad_notifications_received_this_month);
};

class BraveRewardsGetBalanceReportsFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getBalanceReports", UNKNOWN)

protected:
~BraveRewardsGetBalanceReportsFunction() override;

ResponseAction Run() override;

private:
void OnGetBalanceReports(
const std::map<std::string,
::brave_rewards::BalanceReport>& reports);
};

class BraveRewardsGetWalletExistsFunction
: public ExtensionFunction {
public:
Expand Down
Loading