diff --git a/app/src/main/java/com/odysee/app/dialog/CreateSupportDialogFragment.java b/app/src/main/java/com/odysee/app/dialog/CreateSupportDialogFragment.java index 9beecc10..e3150a35 100644 --- a/app/src/main/java/com/odysee/app/dialog/CreateSupportDialogFragment.java +++ b/app/src/main/java/com/odysee/app/dialog/CreateSupportDialogFragment.java @@ -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; @@ -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; @@ -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 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 task = new SupportCreateSupplier(options, authToken); + CompletableFuture 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 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 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(); + } } }); diff --git a/app/src/main/java/com/odysee/app/supplier/SupportCreateSupplier.java b/app/src/main/java/com/odysee/app/supplier/SupportCreateSupplier.java new file mode 100644 index 00000000..18f5dff9 --- /dev/null +++ b/app/src/main/java/com/odysee/app/supplier/SupportCreateSupplier.java @@ -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 { + private final Map options; + private final String authToken; + + public SupportCreateSupplier(Map 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; + } +} diff --git a/app/src/main/java/com/odysee/app/tasks/wallet/SupportCreateTask.java b/app/src/main/java/com/odysee/app/tasks/wallet/SupportCreateTask.java deleted file mode 100644 index cbf4d8a0..00000000 --- a/app/src/main/java/com/odysee/app/tasks/wallet/SupportCreateTask.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.odysee.app.tasks.wallet; - -import android.os.AsyncTask; -import android.view.View; - -import java.math.BigDecimal; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import com.odysee.app.exceptions.ApiCallException; -import com.odysee.app.tasks.GenericTaskHandler; -import com.odysee.app.utils.Helper; -import com.odysee.app.utils.Lbry; - -public class SupportCreateTask extends AsyncTask { - private final String claimId; - private final String channelId; - private final BigDecimal amount; - private final boolean tip; - private final View progressView; - private final GenericTaskHandler handler; - private Exception error; - - public SupportCreateTask(String claimId, String channelId, BigDecimal amount, boolean tip, View progressView, GenericTaskHandler handler) { - this.claimId = claimId; - this.channelId = channelId; - this.amount = amount; - this.tip = tip; - this.progressView = progressView; - this.handler = handler; - } - - protected void onPreExecute() { - if (handler != null) { - handler.beforeStart(); - } - Helper.setViewVisibility(progressView, View.VISIBLE); - } - protected Boolean doInBackground(Void... params) { - try { - Map options = new HashMap<>(); - options.put("blocking", true); - options.put("claim_id", claimId); - options.put("amount", new DecimalFormat(Helper.SDK_AMOUNT_FORMAT, new DecimalFormatSymbols(Locale.US)).format(amount.doubleValue())); - options.put("tip", tip); - if (!Helper.isNullOrEmpty(channelId)) { - options.put("channel_id", channelId); - } - Lbry.genericApiCall(Lbry.METHOD_SUPPORT_CREATE, options); - } catch (ApiCallException ex) { - error = ex; - return false; - } - - return true; - } - - protected void onPostExecute(Boolean result) { - Helper.setViewVisibility(progressView, View.GONE); - if (handler != null) { - if (result) { - handler.onSuccess(); - } else { - handler.onError(error); - } - } - } -} diff --git a/app/src/main/java/com/odysee/app/utils/Lbry.java b/app/src/main/java/com/odysee/app/utils/Lbry.java index 3fc3c889..fcf1906b 100644 --- a/app/src/main/java/com/odysee/app/utils/Lbry.java +++ b/app/src/main/java/com/odysee/app/utils/Lbry.java @@ -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 p, String authToken) throws ApiCallException { p.put("auth_token", authToken); Object response = null;