Search in sources :

Example 1 with ChannelCreateUpdate

use of com.odysee.app.callable.ChannelCreateUpdate in project odysee-android by OdyseeTeam.

the class ChannelCreateDialogFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.channel_form_bottom_sheet, container, false);
    View balanceView = v.findViewById(R.id.channel_form_balanceview);
    TextInputEditText inputDeposit = v.findViewById(R.id.inline_channel_form_input_deposit);
    inputDeposit.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            Helper.setViewVisibility(balanceView, hasFocus ? View.VISIBLE : View.INVISIBLE);
        }
    });
    TextInputEditText inputChannelName = v.findViewById(R.id.inline_channel_form_input_name);
    TextView linkCancel = v.findViewById(R.id.inline_channel_form_cancel_link);
    linkCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            inputChannelName.setText("");
            inputDeposit.setText(R.string.min_deposit);
            dismiss();
        }
    });
    View progressView = v.findViewById(R.id.inline_channel_form_create_progress);
    Button createButton = v.findViewById(R.id.inline_channel_form_create_button);
    createButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // validate deposit and channel name
            String channelNameString = Helper.normalizeChannelName(Helper.getValue(inputChannelName.getText()));
            Claim claimToSave = new Claim();
            claimToSave.setName(channelNameString);
            String channelName = claimToSave.getName().startsWith("@") ? claimToSave.getName().substring(1) : claimToSave.getName();
            String depositString = Helper.getValue(inputDeposit.getText());
            if ("@".equals(channelName) || Helper.isNullOrEmpty(channelName)) {
                showError(getString(R.string.please_enter_channel_name));
                return;
            }
            if (!LbryUri.isNameValid(channelName)) {
                showError(getString(R.string.channel_name_invalid_characters));
                return;
            }
            if (Helper.channelExists(channelName)) {
                showError(getString(R.string.channel_name_already_created));
                return;
            }
            double depositAmount;
            try {
                depositAmount = Double.parseDouble(depositString);
            } catch (NumberFormatException ex) {
                // pass
                showError(getString(R.string.please_enter_valid_deposit));
                return;
            }
            if (depositAmount == 0) {
                String error = getResources().getQuantityString(R.plurals.min_deposit_required, depositAmount == 1 ? 1 : 2, String.valueOf(Helper.MIN_DEPOSIT));
                showError(error);
                return;
            }
            if (Lbry.walletBalance == null || Lbry.getAvailableBalance() < depositAmount) {
                showError(getString(R.string.deposit_more_than_balance));
                return;
            }
            AccountManager am = AccountManager.get(getContext());
            String authToken = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
            MainActivity activity = (MainActivity) getActivity();
            enableViews(inputChannelName, inputDeposit, createButton, linkCancel, false);
            Helper.setViewVisibility(progressView, View.VISIBLE);
            Callable<Claim> c = new ChannelCreateUpdate(claimToSave, new BigDecimal(depositString), false, authToken);
            ExecutorService executor = Executors.newSingleThreadExecutor();
            Future<Claim> future = executor.submit(c);
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Claim result = future.get();
                        if (result != null) {
                            if (!BuildConfig.DEBUG) {
                                LogPublishTask logPublishTask = new LogPublishTask(result);
                                logPublishTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                            }
                            // channel created
                            Bundle bundle = new Bundle();
                            bundle.putString("claim_id", result.getClaimId());
                            bundle.putString("claim_name", result.getName());
                            LbryAnalytics.logEvent(LbryAnalytics.EVENT_CHANNEL_CREATE, bundle);
                            if (activity != null) {
                                activity.runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        if (listener != null) {
                                            listener.onChannelCreated(result);
                                        }
                                    }
                                });
                            }
                        }
                    } catch (ExecutionException ex) {
                        Throwable cause = ex.getCause();
                        if (cause instanceof ApiCallException) {
                            showError(cause.getMessage());
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    } finally {
                        if (activity != null) {
                            activity.runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    Helper.setViewVisibility(progressView, View.GONE);
                                    enableViews(inputChannelName, inputDeposit, createButton, linkCancel, true);
                                    dismiss();
                                }
                            });
                        }
                    }
                }
            });
            t.start();
        }
    });
    Helper.setViewText((TextView) balanceView, Helper.shortCurrencyFormat(Lbry.getAvailableBalance()));
    return v;
}
Also used : MainActivity(com.odysee.app.MainActivity) Callable(java.util.concurrent.Callable) Button(android.widget.Button) LogPublishTask(com.odysee.app.tasks.lbryinc.LogPublishTask) TextView(android.widget.TextView) ExecutionException(java.util.concurrent.ExecutionException) ChannelCreateUpdate(com.odysee.app.callable.ChannelCreateUpdate) ApiCallException(com.odysee.app.exceptions.ApiCallException) Bundle(android.os.Bundle) View(android.view.View) TextView(android.widget.TextView) BigDecimal(java.math.BigDecimal) ApiCallException(com.odysee.app.exceptions.ApiCallException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) TextInputEditText(com.google.android.material.textfield.TextInputEditText) Future(java.util.concurrent.Future) AccountManager(android.accounts.AccountManager) Claim(com.odysee.app.model.Claim)

Aggregations

AccountManager (android.accounts.AccountManager)1 Bundle (android.os.Bundle)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 TextInputEditText (com.google.android.material.textfield.TextInputEditText)1 MainActivity (com.odysee.app.MainActivity)1 ChannelCreateUpdate (com.odysee.app.callable.ChannelCreateUpdate)1 ApiCallException (com.odysee.app.exceptions.ApiCallException)1 Claim (com.odysee.app.model.Claim)1 LogPublishTask (com.odysee.app.tasks.lbryinc.LogPublishTask)1 BigDecimal (java.math.BigDecimal)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1