use of org.thoughtcrime.securesms.payments.MoneyView in project Signal-Android by signalapp.
the class PaymentsHomeFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = view.findViewById(R.id.payments_home_fragment_toolbar);
RecyclerView recycler = view.findViewById(R.id.payments_home_fragment_recycler);
View header = view.findViewById(R.id.payments_home_fragment_header);
MoneyView balance = view.findViewById(R.id.payments_home_fragment_header_balance);
TextView exchange = view.findViewById(R.id.payments_home_fragment_header_exchange);
View addMoney = view.findViewById(R.id.button_start_frame);
View sendMoney = view.findViewById(R.id.button_end_frame);
View refresh = view.findViewById(R.id.payments_home_fragment_header_refresh);
LottieAnimationView refreshAnimation = view.findViewById(R.id.payments_home_fragment_header_refresh_animation);
toolbar.setNavigationOnClickListener(v -> {
viewModel.markAllPaymentsSeen();
requireActivity().finish();
});
toolbar.setOnMenuItemClickListener(this::onMenuItemSelected);
addMoney.setOnClickListener(v -> {
if (SignalStore.paymentsValues().getPaymentsAvailability().isSendAllowed()) {
SafeNavigation.safeNavigate(Navigation.findNavController(v), PaymentsHomeFragmentDirections.actionPaymentsHomeToPaymentsAddMoney());
} else {
showPaymentsDisabledDialog();
}
});
sendMoney.setOnClickListener(v -> {
if (SignalStore.paymentsValues().getPaymentsAvailability().isSendAllowed()) {
SafeNavigation.safeNavigate(Navigation.findNavController(v), PaymentsHomeFragmentDirections.actionPaymentsHomeToPaymentRecipientSelectionFragment());
} else {
showPaymentsDisabledDialog();
}
});
PaymentsHomeAdapter adapter = new PaymentsHomeAdapter(new HomeCallbacks());
recycler.setAdapter(adapter);
viewModel = ViewModelProviders.of(this, new PaymentsHomeViewModel.Factory()).get(PaymentsHomeViewModel.class);
viewModel.getList().observe(getViewLifecycleOwner(), list -> {
boolean hadPaymentItems = Stream.of(adapter.getCurrentList()).anyMatch(model -> model instanceof PaymentItem);
if (!hadPaymentItems) {
adapter.submitList(list, () -> recycler.scrollToPosition(0));
} else {
adapter.submitList(list);
}
});
viewModel.getPaymentsEnabled().observe(getViewLifecycleOwner(), enabled -> {
if (enabled) {
toolbar.inflateMenu(R.menu.payments_home_fragment_menu);
} else {
toolbar.getMenu().clear();
}
header.setVisibility(enabled ? View.VISIBLE : View.GONE);
});
viewModel.getBalance().observe(getViewLifecycleOwner(), balance::setMoney);
viewModel.getExchange().observe(getViewLifecycleOwner(), amount -> {
if (amount != null) {
exchange.setText(FiatMoneyUtil.format(getResources(), amount));
} else {
exchange.setText(R.string.PaymentsHomeFragment__unknown_amount);
}
});
refresh.setOnClickListener(v -> viewModel.refreshExchangeRates(true));
exchange.setOnClickListener(v -> viewModel.refreshExchangeRates(true));
viewModel.getExchangeLoadState().observe(getViewLifecycleOwner(), loadState -> {
switch(loadState) {
case INITIAL:
case LOADED:
refresh.setVisibility(View.VISIBLE);
refreshAnimation.cancelAnimation();
refreshAnimation.setVisibility(View.GONE);
break;
case LOADING:
refresh.setVisibility(View.INVISIBLE);
refreshAnimation.playAnimation();
refreshAnimation.setVisibility(View.VISIBLE);
break;
case ERROR:
refresh.setVisibility(View.VISIBLE);
refreshAnimation.cancelAnimation();
refreshAnimation.setVisibility(View.GONE);
exchange.setText(R.string.PaymentsHomeFragment__currency_conversion_not_available);
Toast.makeText(view.getContext(), R.string.PaymentsHomeFragment__cant_display_currency_conversion, Toast.LENGTH_SHORT).show();
break;
}
});
viewModel.getPaymentStateEvents().observe(getViewLifecycleOwner(), paymentStateEvent -> {
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
builder.setTitle(R.string.PaymentsHomeFragment__deactivate_payments_question);
builder.setMessage(R.string.PaymentsHomeFragment__you_will_not_be_able_to_send);
builder.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss());
switch(paymentStateEvent) {
case NO_BALANCE:
Toast.makeText(requireContext(), R.string.PaymentsHomeFragment__balance_is_not_currently_available, Toast.LENGTH_SHORT).show();
return;
case DEACTIVATED:
Snackbar.make(requireView(), R.string.PaymentsHomeFragment__payments_deactivated, Snackbar.LENGTH_SHORT).setTextColor(Color.WHITE).show();
return;
case DEACTIVATE_WITHOUT_BALANCE:
builder.setPositiveButton(SpanUtil.color(ContextCompat.getColor(requireContext(), R.color.signal_alert_primary), getString(R.string.PaymentsHomeFragment__deactivate)), (dialog, which) -> {
viewModel.confirmDeactivatePayments();
dialog.dismiss();
});
break;
case DEACTIVATE_WITH_BALANCE:
builder.setPositiveButton(getString(R.string.PaymentsHomeFragment__continue), (dialog, which) -> {
dialog.dismiss();
SafeNavigation.safeNavigate(NavHostFragment.findNavController(this), R.id.deactivateWallet);
});
break;
case ACTIVATED:
return;
default:
throw new IllegalStateException("Unsupported event type: " + paymentStateEvent.name());
}
builder.show();
});
viewModel.getErrorEnablingPayments().observe(getViewLifecycleOwner(), errorEnabling -> {
switch(errorEnabling) {
case REGION:
Toast.makeText(view.getContext(), R.string.PaymentsHomeFragment__payments_is_not_available_in_your_region, Toast.LENGTH_LONG).show();
break;
case NETWORK:
Toast.makeText(view.getContext(), R.string.PaymentsHomeFragment__could_not_enable_payments, Toast.LENGTH_SHORT).show();
break;
default:
throw new AssertionError();
}
});
requireActivity().getOnBackPressedDispatcher().addCallback(onBackPressed);
}
Aggregations