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

Send auth-token when making a tipping request #52

Merged
merged 1 commit into from
Nov 15, 2021
Merged
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
@@ -1,9 +1,12 @@
package com.odysee.app.dialog;

import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
Expand All @@ -25,19 +28,31 @@
import com.google.android.material.textfield.TextInputEditText;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Supplier;

import com.odysee.app.MainActivity;
import com.odysee.app.R;
import com.odysee.app.adapter.InlineChannelSpinnerAdapter;
import com.odysee.app.exceptions.ApiCallException;
import com.odysee.app.listener.WalletBalanceListener;
import com.odysee.app.model.Claim;
import com.odysee.app.model.WalletBalance;
import com.odysee.app.tasks.GenericTaskHandler;
import com.odysee.app.supplier.SupportCreateSupplier;
import com.odysee.app.tasks.claim.ClaimListResultHandler;
import com.odysee.app.tasks.claim.ClaimListTask;
import com.odysee.app.tasks.wallet.SupportCreateTask;
import com.odysee.app.utils.Helper;
import com.odysee.app.utils.Lbry;

Expand Down Expand Up @@ -182,30 +197,89 @@ public void onClick(View view) {
Claim selectedChannel = (Claim) channelSpinner.getSelectedItem();
String channelId = !fetchingChannels && selectedChannel != null ? selectedChannel.getClaimId() : null;
boolean isTip = switchTip.isChecked();
SupportCreateTask task = new SupportCreateTask(
claim.getClaimId(), channelId, amount, isTip, sendProgress, new GenericTaskHandler() {
@Override
public void beforeStart() {
disableControls();
}

@Override
public void onSuccess() {
enableControls();
if (listener != null) {
listener.onSupportCreated(amount, isTip);
}

dismiss();
}
disableControls();
AccountManager am = AccountManager.get(getContext());
String authToken = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
Map<String, Object> options = new HashMap<>();
options.put("blocking", true);
options.put("claim_id", claim.getClaimId());
options.put("amount", new DecimalFormat(Helper.SDK_AMOUNT_FORMAT, new DecimalFormatSymbols(Locale.US)).format(amount.doubleValue()));
options.put("tip", isTip);
if (!Helper.isNullOrEmpty(channelId)) {
options.put("channel_id", channelId);
}

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<String> task = new SupportCreateSupplier(options, authToken);
CompletableFuture<String> cf = CompletableFuture.supplyAsync(task);
cf.thenAccept(result -> {
Activity activity = getActivity();
if (result == null) {
if (listener != null) {
listener.onSupportCreated(amount, isTip);
}
dismiss();
} else {
showError(result);
}

@Override
public void onError(Exception error) {
showError(error.getMessage());
enableControls();
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if (activity != null) {
enableControls();
}
});
} else {
Thread supportingThread = new Thread(new Runnable() {
@Override
public void run() {
Callable<Boolean> callable = () -> {
try {
Lbry.authenticatedGenericApiCall(Lbry.METHOD_SUPPORT_CREATE, options, authToken);
} catch (ApiCallException ex) {
ex.printStackTrace();
showError(ex.getMessage());
return false;
}
return true;
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Boolean> future = executorService.submit(callable);

try {
boolean result = future.get();

Activity activity = getActivity();

if (result) {
if (listener != null) {
listener.onSupportCreated(amount, isTip);
}

if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
dismiss();
}
});
}
}

if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
enableControls();
}
});
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
supportingThread.start();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.odysee.app.supplier;

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

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

public class SupportCreateSupplier implements Supplier<String> {
private final Map<String, Object> options;
private final String authToken;

public SupportCreateSupplier(Map<String, Object> options, String authToken) {
this.options = options;
this.authToken = authToken;
}

@Override
public String get() {
String error = null;
try {
Lbry.authenticatedGenericApiCall(Lbry.METHOD_SUPPORT_CREATE, options, authToken);
} catch (ApiCallException ex) {
ex.printStackTrace();
return ex.getMessage();
}
return error;
}
}

This file was deleted.

10 changes: 10 additions & 0 deletions app/src/main/java/com/odysee/app/utils/Lbry.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,16 @@ public static Object directApiCall(String method, String authToken) throws ApiCa
}
return response;
}

/**
* @deprecated Use authenticatedGenericApiCall(String, Map, String) instead
* @param method
* @param p
* @param authToken
* @return
* @throws ApiCallException
*/
@Deprecated
public static Object directApiCall(String method, Map<String, Object> p, String authToken) throws ApiCallException {
p.put("auth_token", authToken);
Object response = null;
Expand Down