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;
}
Aggregations