Search in sources :

Example 1 with SupportCreateSupplier

use of com.odysee.app.supplier.SupportCreateSupplier in project odysee-android by OdyseeTeam.

the class CreateSupportDialogFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_create_support, container, false);
    inputAmount = view.findViewById(R.id.create_support_input_amount);
    inlineBalanceContainer = view.findViewById(R.id.create_support_inline_balance_container);
    inlineBalanceValue = view.findViewById(R.id.create_support_inline_balance_value);
    sendProgress = view.findViewById(R.id.create_support_progress);
    cancelLink = view.findViewById(R.id.create_support_cancel_link);
    sendButton = view.findViewById(R.id.create_support_send);
    channelSpinner = view.findViewById(R.id.create_support_channel_spinner);
    switchTip = view.findViewById(R.id.create_support_make_tip_switch);
    progressLoadingChannels = view.findViewById(R.id.create_support_channel_progress);
    inputAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            inputAmount.setHint(hasFocus ? getString(R.string.zero) : "");
            inlineBalanceContainer.setVisibility(hasFocus ? View.VISIBLE : View.INVISIBLE);
        }
    });
    inputAmount.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            updateSendButtonText();
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    updateInfoText();
    updateSendButtonText();
    String channel = null;
    if (Claim.TYPE_CHANNEL.equalsIgnoreCase(claim.getValueType())) {
        channel = claim.getTitleOrName();
    } else if (claim.getSigningChannel() != null) {
        channel = claim.getPublisherTitle();
    }
    TextView titleView = view.findViewById(R.id.create_support_title);
    String tipTitleText = Helper.isNullOrEmpty(channel) ? getString(R.string.send_a_tip) : getString(R.string.send_a_tip_to, channel);
    titleView.setText(tipTitleText);
    switchTip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
            if (checked) {
                // show tip info
                titleView.setText(tipTitleText);
                updateSendButtonText();
            } else {
                // show support info
                titleView.setText(R.string.support_this_content);
                sendButton.setText(R.string.send_revocable_support);
            }
            updateInfoText();
        }
    });
    sendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            String amountString = Helper.getValue(inputAmount.getText());
            if (Helper.isNullOrEmpty(amountString)) {
                showError(getString(R.string.invalid_amount));
                return;
            }
            BigDecimal amount = new BigDecimal(amountString);
            if (amount.doubleValue() > Lbry.getAvailableBalance()) {
                showError(getString(R.string.insufficient_balance));
                return;
            }
            if (amount.doubleValue() < Helper.MIN_SPEND) {
                showError(getString(R.string.min_spend_required));
                return;
            }
            Claim selectedChannel = (Claim) channelSpinner.getSelectedItem();
            String channelId = !fetchingChannels && selectedChannel != null ? selectedChannel.getClaimId() : null;
            boolean isTip = switchTip.isChecked();
            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);
                    }
                    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();
            }
        }
    });
    cancelLink.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            dismiss();
        }
    });
    onWalletBalanceUpdated(Lbry.walletBalance);
    updateInfoText();
    inputAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                Context context = getContext();
                if (context != null) {
                    ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                }
            }
        }
    });
    inputAmount.requestFocus();
    return view;
}
Also used : Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) DecimalFormatSymbols(java.text.DecimalFormatSymbols) ClaimListTask(com.odysee.app.tasks.claim.ClaimListTask) LinkMovementMethod(android.text.method.LinkMovementMethod) WalletBalance(com.odysee.app.model.WalletBalance) AppCompatSpinner(androidx.appcompat.widget.AppCompatSpinner) BigDecimal(java.math.BigDecimal) Future(java.util.concurrent.Future) Locale(java.util.Locale) Map(java.util.Map) View(android.view.View) AccountManager(android.accounts.AccountManager) AsyncTask(android.os.AsyncTask) Helper(com.odysee.app.utils.Helper) HtmlCompat(androidx.core.text.HtmlCompat) Claim(com.odysee.app.model.Claim) ViewGroup(android.view.ViewGroup) Executors(java.util.concurrent.Executors) List(java.util.List) TextView(android.widget.TextView) WalletBalanceListener(com.odysee.app.listener.WalletBalanceListener) Snackbar(com.google.android.material.snackbar.Snackbar) TextWatcher(android.text.TextWatcher) Context(android.content.Context) TextInputEditText(com.google.android.material.textfield.TextInputEditText) ClaimListResultHandler(com.odysee.app.tasks.claim.ClaimListResultHandler) Lbry(com.odysee.app.utils.Lbry) Dialog(android.app.Dialog) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) Editable(android.text.Editable) InputMethodManager(android.view.inputmethod.InputMethodManager) ArrayList(java.util.ArrayList) MaterialButton(com.google.android.material.button.MaterialButton) MainActivity(com.odysee.app.MainActivity) SwitchMaterial(com.google.android.material.switchmaterial.SwitchMaterial) Build(android.os.Build) ExecutorService(java.util.concurrent.ExecutorService) InlineChannelSpinnerAdapter(com.odysee.app.adapter.InlineChannelSpinnerAdapter) CompoundButton(android.widget.CompoundButton) ApiCallException(com.odysee.app.exceptions.ApiCallException) SupportCreateSupplier(com.odysee.app.supplier.SupportCreateSupplier) LayoutInflater(android.view.LayoutInflater) DecimalFormat(java.text.DecimalFormat) Color(android.graphics.Color) ExecutionException(java.util.concurrent.ExecutionException) BottomSheetDialogFragment(com.google.android.material.bottomsheet.BottomSheetDialogFragment) Activity(android.app.Activity) R(com.odysee.app.R) DecimalFormat(java.text.DecimalFormat) MainActivity(com.odysee.app.MainActivity) Activity(android.app.Activity) CompletableFuture(java.util.concurrent.CompletableFuture) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) Supplier(java.util.function.Supplier) SupportCreateSupplier(com.odysee.app.supplier.SupportCreateSupplier) ExecutionException(java.util.concurrent.ExecutionException) Context(android.content.Context) SupportCreateSupplier(com.odysee.app.supplier.SupportCreateSupplier) DecimalFormatSymbols(java.text.DecimalFormatSymbols) ApiCallException(com.odysee.app.exceptions.ApiCallException) View(android.view.View) TextView(android.widget.TextView) BigDecimal(java.math.BigDecimal) ExecutorService(java.util.concurrent.ExecutorService) AccountManager(android.accounts.AccountManager) Map(java.util.Map) HashMap(java.util.HashMap) CompoundButton(android.widget.CompoundButton) Claim(com.odysee.app.model.Claim)

Aggregations

AccountManager (android.accounts.AccountManager)1 Activity (android.app.Activity)1 Dialog (android.app.Dialog)1 Context (android.content.Context)1 Color (android.graphics.Color)1 AsyncTask (android.os.AsyncTask)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 LinkMovementMethod (android.text.method.LinkMovementMethod)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 CompoundButton (android.widget.CompoundButton)1 ProgressBar (android.widget.ProgressBar)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 AppCompatSpinner (androidx.appcompat.widget.AppCompatSpinner)1