Skip to content

Commit

Permalink
Re-enable Rewards (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
kekkyojin authored Dec 5, 2021
1 parent 042fc4d commit f423a3f
Show file tree
Hide file tree
Showing 11 changed files with 544 additions and 227 deletions.
93 changes: 79 additions & 14 deletions app/src/main/java/com/odysee/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
import com.odysee.app.model.lbryinc.Reward;
import com.odysee.app.model.lbryinc.RewardVerified;
import com.odysee.app.model.lbryinc.Subscription;
import com.odysee.app.supplier.FetchRewardsSupplier;
import com.odysee.app.supplier.GetLocalNotificationsSupplier;
import com.odysee.app.supplier.NotificationListSupplier;
import com.odysee.app.supplier.NotificationUpdateSupplier;
Expand All @@ -203,7 +204,6 @@
import com.odysee.app.tasks.claim.ClaimListTask;
import com.odysee.app.tasks.lbryinc.AndroidPurchaseTask;
import com.odysee.app.tasks.lbryinc.ClaimRewardTask;
import com.odysee.app.tasks.lbryinc.FetchRewardsTask;
import com.odysee.app.tasks.MergeSubscriptionsTask;
import com.odysee.app.tasks.claim.ResolveTask;
import com.odysee.app.tasks.lbryinc.NotificationDeleteTask;
Expand Down Expand Up @@ -744,9 +744,8 @@ public void onClick(View view) {
@Override
public void onClick(View view) {
closeButton.performClick();
// hideNotifications();
// openFragment(RewardsFragment.class, true, null);
startActivity(new Intent(view.getContext(), ComingSoon.class));
hideNotifications();
openFragment(RewardsFragment.class, true, null);
}
});
signUserButton.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -1957,7 +1956,6 @@ public void checkAndClaimNewAndroidReward() {
Reward.TYPE_NEW_ANDROID,
null,
null,
this,
new ClaimRewardTask.ClaimRewardHandler() {
@Override
public void onSuccess(double amountClaimed, String message) {
Expand Down Expand Up @@ -3306,21 +3304,88 @@ protected void onPostExecute(Boolean startupSuccessful) {
}

private void fetchRewards() {
FetchRewardsTask task = new FetchRewardsTask(null, new FetchRewardsTask.FetchRewardsHandler() {
@Override
public void onSuccess(List<Reward> rewards) {
String authToken;
AccountManager am = AccountManager.get(this);
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
} else {
authToken = "";
}

Map<String, String> options = new HashMap<>();
options.put("multiple_rewards_per_type", "true");
if (odyseeAccount != null && authToken != null) {
options.put("auth_token", authToken);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<List<Reward>> supplier = new FetchRewardsSupplier(options);
CompletableFuture<List<Reward>> cf = CompletableFuture.supplyAsync(supplier, executorService);
cf.exceptionally(e -> {
runOnUiThread(new Runnable() {
@Override
public void run() {
showError(e.getMessage());
}
});
return null;
}).thenAccept(rewards -> {
Lbryio.updateRewardsLists(rewards);

if (Lbryio.totalUnclaimedRewardAmount > 0) {
updateRewardsUsdValue();
}
}
});
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Callable<List<Reward>> callable = new Callable<List<Reward>>() {
@Override
public List<Reward> call() {
List<Reward> rewards = null;
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showError(ex.getMessage());
}
});
}

@Override
public void onError(Exception error) {
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return rewards;
}
};

Future<List<Reward>> future = executorService.submit(callable);

try {
List<Reward> rewards = future.get();

if (rewards != null) {
Lbryio.updateRewardsLists(rewards);

if (Lbryio.totalUnclaimedRewardAmount > 0) {
updateRewardsUsdValue();
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
t.start();
}
}

public void updateRewardsUsdValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.odysee.app.R;
import com.odysee.app.model.lbryinc.Reward;
import com.odysee.app.utils.Helper;
import com.odysee.app.utils.Lbryio;
import com.odysee.app.views.CreditsBalanceView;

import lombok.Getter;
Expand Down Expand Up @@ -84,6 +83,7 @@ private void addCustomReward() {
custom.setRewardTitle(context.getString(R.string.custom_reward_title));
custom.setRewardDescription(context.getString(R.string.custom_reward_description));
items.add(custom);
notifyItemInserted(items.indexOf(custom));
}

public static class ViewHolder extends RecyclerView.ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package com.odysee.app.callable;

import android.accounts.AccountManager;
import android.content.Context;

import com.odysee.app.exceptions.ApiCallException;
import com.odysee.app.utils.Lbry;

import java.util.concurrent.Callable;

public class WalletGetUnusedAddress implements Callable<String> {
Context ctx;
String token;

public WalletGetUnusedAddress(Context ctx) {
this.ctx = ctx;
public WalletGetUnusedAddress(String token) {
this.token = token;
}

@Override
public String call() throws Exception {
String address = null;
try {
AccountManager am = AccountManager.get(ctx);
address = (String) Lbry.directApiCall(Lbry.METHOD_ADDRESS_UNUSED, am.peekAuthToken(am.getAccounts()[0], "auth_token_type"));
address = (String) Lbry.directApiCall(Lbry.METHOD_ADDRESS_UNUSED, token);
} catch (ApiCallException | ClassCastException ex) {
ex.printStackTrace();
}
Expand Down
87 changes: 87 additions & 0 deletions app/src/main/java/com/odysee/app/supplier/ClaimRewardSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.odysee.app.supplier;

import com.odysee.app.exceptions.ApiCallException;
import com.odysee.app.exceptions.LbryioRequestException;
import com.odysee.app.exceptions.LbryioResponseException;
import com.odysee.app.model.Claim;
import com.odysee.app.model.lbryinc.Reward;
import com.odysee.app.utils.Helper;
import com.odysee.app.utils.Lbry;
import com.odysee.app.utils.Lbryio;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import lombok.SneakyThrows;

public class ClaimRewardSupplier implements Supplier<JSONObject> {
private final String type;
private final String rewardCode;
private final String token;

public ClaimRewardSupplier(String type, String rewardCode, String token) {
this.type = type;
this.rewardCode = rewardCode;
this.token = token;
}

@SneakyThrows
@Override
public JSONObject get() {
return claimReward();
}

private JSONObject claimReward() throws ApiCallException, LbryioResponseException {
try {
String txid = null;
if (Reward.TYPE_FIRST_CHANNEL.equalsIgnoreCase(type)) {
// fetch a channel
txid = fetchSingleClaimTxid(Claim.TYPE_CHANNEL);
} else if (Reward.TYPE_FIRST_PUBLISH.equalsIgnoreCase(type)) {
// fetch a publish
txid = fetchSingleClaimTxid(Claim.TYPE_STREAM);
}

Map<String, String> options = new HashMap<>();
options.put("reward_type", type);
options.put("wallet_address", (String) Lbry.genericApiCall(Lbry.METHOD_ADDRESS_UNUSED, token));

if (!Helper.isNullOrEmpty(rewardCode)) {
options.put("code", rewardCode);
}
if (!Helper.isNullOrEmpty(txid)) {
options.put("transaction_id", txid);
}
if (token != null) {
options.put("auth_token", token);
}

return (JSONObject) Lbryio.parseResponse(Lbryio.call("reward", "claim", options, Helper.METHOD_POST, null));
} catch (JSONException | LbryioRequestException ex) {
ex.printStackTrace();
return null;
}
}

private String fetchSingleClaimTxid(String claimType) throws ApiCallException, JSONException {
Map<String, Object> options = new HashMap<>();
options.put("claim_type", claimType);
options.put("page", 1);
options.put("page_size", 1);
options.put("resolve", true);

JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_CLAIM_LIST, options);
JSONArray items = result.getJSONArray("items");
if (items.length() > 0) {
Claim claim = Claim.fromJSONObject(items.getJSONObject(0));
return claim.getTxid();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.odysee.app.supplier;

import com.odysee.app.exceptions.LbryioRequestException;
import com.odysee.app.exceptions.LbryioResponseException;
import com.odysee.app.model.lbryinc.Reward;
import com.odysee.app.utils.Lbryio;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class FetchRewardsSupplier implements Supplier<List<Reward>> {
Map<String, String> options;

public FetchRewardsSupplier(Map<String, String> options) {
this.options = options;
}

@Override
public List<Reward> get() {
List<Reward> rewards = new ArrayList<>();
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (LbryioResponseException | JSONException | LbryioRequestException e) {
e.printStackTrace();
}
return rewards;
}
}
30 changes: 11 additions & 19 deletions app/src/main/java/com/odysee/app/tasks/lbryinc/ClaimRewardTask.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.odysee.app.tasks.lbryinc;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import com.odysee.app.R;
import com.odysee.app.exceptions.ApiCallException;
import com.odysee.app.exceptions.LbryioRequestException;
import com.odysee.app.exceptions.LbryioResponseException;
Expand All @@ -24,24 +20,21 @@

public class ClaimRewardTask extends AsyncTask<Void, Void, String> {

private final Context context;
private final String type;
private final String rewardCode;
private final View progressView;
private double amountClaimed;
private final String authToken;
private final ClaimRewardHandler handler;
private Exception error;

public ClaimRewardTask(String type, String rewardCode, View progressView, Context context, ClaimRewardHandler handler) {
public ClaimRewardTask(String type, String rewardCode, String authToken, ClaimRewardHandler handler) {
this.type = type;
this.rewardCode = rewardCode;
this.progressView = progressView;
this.context = context;
this.authToken = authToken;
this.handler = handler;
}

protected void onPreExecute() {
Helper.setViewVisibility(progressView, View.VISIBLE);
}

public String doInBackground(Void... params) {
Expand All @@ -57,7 +50,7 @@ public String doInBackground(Void... params) {
}

// Get a new wallet address for the reward
String address = (String) Lbry.genericApiCall(Lbry.METHOD_ADDRESS_UNUSED);
String address = (String) Lbry.genericApiCall(Lbry.METHOD_ADDRESS_UNUSED, authToken);
Map<String, String> options = new HashMap<>();
options.put("reward_type", type);
options.put("wallet_address", address);
Expand All @@ -67,16 +60,16 @@ public String doInBackground(Void... params) {
if (!Helper.isNullOrEmpty(txid)) {
options.put("transaction_id", txid);
}
if (authToken != null) {
options.put("auth_token", authToken);
}

JSONObject reward = (JSONObject) Lbryio.parseResponse(
Lbryio.call("reward", "claim", options, Helper.METHOD_POST, null));
amountClaimed = Helper.getJSONDouble("reward_amount", 0, reward);
String defaultMessage = context != null ?
context.getResources().getQuantityString(
R.plurals.claim_reward_message,
amountClaimed == 1 ? 1 : 2,
new DecimalFormat(Helper.LBC_CURRENCY_FORMAT_PATTERN).format(amountClaimed)) : "";
message = Helper.getJSONString("reward_notification", defaultMessage, reward);
if (reward != null) {
amountClaimed = Helper.getJSONDouble("reward_amount", 0, reward);
message = Helper.getJSONString("reward_notification", "", reward);
}
} catch (ApiCallException | JSONException | LbryioRequestException | LbryioResponseException ex) {
error = ex;
}
Expand All @@ -85,7 +78,6 @@ public String doInBackground(Void... params) {
}

protected void onPostExecute(String message) {
Helper.setViewVisibility(progressView, View.INVISIBLE);
if (handler != null) {
if (message != null) {
handler.onSuccess(amountClaimed, message);
Expand Down
Loading

0 comments on commit f423a3f

Please sign in to comment.