Search in sources :

Example 1 with FiatMoney

use of org.signal.core.util.money.FiatMoney in project Signal-Android by WhisperSystems.

the class CreatePaymentViewModel method updateFiatAmount.

@NonNull
private InputState updateFiatAmount(@NonNull Context context, @NonNull InputState inputState, @NonNull AmountKeyboardGlyph glyph, @NonNull Currency currency) {
    String newFiatAmount = updateAmountString(context, inputState.getFiatAmount(), glyph, currency.getDefaultFractionDigits());
    FiatMoney newFiat = stringToFiatValueOrZero(newFiatAmount, currency);
    Money newMoney = OptionalUtil.flatMap(inputState.getExchangeRate(), e -> e.exchange(newFiat)).get();
    String newMoneyAmount;
    if (newFiatAmount.equals("0")) {
        newMoneyAmount = "0";
    } else {
        newMoneyAmount = newMoney.toString(FormatterOptions.builder().withoutUnit().build());
    }
    if (!withinMobileCoinBounds(newMoney.requireMobileCoin())) {
        return inputState;
    }
    return inputState.updateAmount(newMoneyAmount, newFiatAmount, newMoney, Optional.of(newFiat));
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) Context(android.content.Context) PayeeParcelable(org.thoughtcrime.securesms.payments.preferences.model.PayeeParcelable) CreatePaymentDetails(org.thoughtcrime.securesms.payments.CreatePaymentDetails) NonNull(androidx.annotation.NonNull) ViewModel(androidx.lifecycle.ViewModel) FiatMoney(org.signal.core.util.money.FiatMoney) Currency(java.util.Currency) Store(org.thoughtcrime.securesms.util.livedata.Store) FiatMoneyUtil(org.thoughtcrime.securesms.payments.FiatMoneyUtil) BigDecimal(java.math.BigDecimal) ProfileUtil(org.thoughtcrime.securesms.util.ProfileUtil) FormatterOptions(org.whispersystems.signalservice.api.payments.FormatterOptions) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Transformations(androidx.lifecycle.Transformations) LiveData(androidx.lifecycle.LiveData) ViewModelProvider(androidx.lifecycle.ViewModelProvider) CurrencyExchange(org.thoughtcrime.securesms.payments.currency.CurrencyExchange) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) IOException(java.io.IOException) OptionalUtil(org.whispersystems.signalservice.api.util.OptionalUtil) Optional(org.whispersystems.libsignal.util.guava.Optional) Objects(java.util.Objects) Log(org.signal.core.util.logging.Log) Nullable(androidx.annotation.Nullable) DefaultValueLiveData(org.thoughtcrime.securesms.util.DefaultValueLiveData) Balance(org.thoughtcrime.securesms.payments.Balance) LiveDataUtil(org.thoughtcrime.securesms.util.livedata.LiveDataUtil) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney) NonNull(androidx.annotation.NonNull)

Example 2 with FiatMoney

use of org.signal.core.util.money.FiatMoney in project Signal-Android by signalapp.

the class CreatePaymentViewModel method clearAmount.

void clearAmount() {
    inputState.update(s -> {
        final Money money = Money.MobileCoin.ZERO;
        final Optional<FiatMoney> fiat = OptionalUtil.flatMap(s.getExchangeRate(), r -> r.exchange(money));
        return s.updateAmount("0", "0", Money.MobileCoin.ZERO, fiat);
    });
}
Also used : FiatMoney(org.signal.core.util.money.FiatMoney) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney)

Example 3 with FiatMoney

use of org.signal.core.util.money.FiatMoney in project Signal-Android by signalapp.

the class CreatePaymentViewModel method updateFiatAmount.

@NonNull
private InputState updateFiatAmount(@NonNull Context context, @NonNull InputState inputState, @NonNull AmountKeyboardGlyph glyph, @NonNull Currency currency) {
    String newFiatAmount = updateAmountString(context, inputState.getFiatAmount(), glyph, currency.getDefaultFractionDigits());
    FiatMoney newFiat = stringToFiatValueOrZero(newFiatAmount, currency);
    Money newMoney = OptionalUtil.flatMap(inputState.getExchangeRate(), e -> e.exchange(newFiat)).get();
    String newMoneyAmount;
    if (newFiatAmount.equals("0")) {
        newMoneyAmount = "0";
    } else {
        newMoneyAmount = newMoney.toString(FormatterOptions.builder().withoutUnit().build());
    }
    if (!withinMobileCoinBounds(newMoney.requireMobileCoin())) {
        return inputState;
    }
    return inputState.updateAmount(newMoneyAmount, newFiatAmount, newMoney, Optional.of(newFiat));
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) Context(android.content.Context) PayeeParcelable(org.thoughtcrime.securesms.payments.preferences.model.PayeeParcelable) CreatePaymentDetails(org.thoughtcrime.securesms.payments.CreatePaymentDetails) NonNull(androidx.annotation.NonNull) ViewModel(androidx.lifecycle.ViewModel) FiatMoney(org.signal.core.util.money.FiatMoney) Currency(java.util.Currency) Store(org.thoughtcrime.securesms.util.livedata.Store) FiatMoneyUtil(org.thoughtcrime.securesms.payments.FiatMoneyUtil) BigDecimal(java.math.BigDecimal) ProfileUtil(org.thoughtcrime.securesms.util.ProfileUtil) FormatterOptions(org.whispersystems.signalservice.api.payments.FormatterOptions) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Transformations(androidx.lifecycle.Transformations) LiveData(androidx.lifecycle.LiveData) ViewModelProvider(androidx.lifecycle.ViewModelProvider) CurrencyExchange(org.thoughtcrime.securesms.payments.currency.CurrencyExchange) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) IOException(java.io.IOException) OptionalUtil(org.whispersystems.signalservice.api.util.OptionalUtil) Optional(org.whispersystems.libsignal.util.guava.Optional) Objects(java.util.Objects) Log(org.signal.core.util.logging.Log) Nullable(androidx.annotation.Nullable) DefaultValueLiveData(org.thoughtcrime.securesms.util.DefaultValueLiveData) Balance(org.thoughtcrime.securesms.payments.Balance) LiveDataUtil(org.thoughtcrime.securesms.util.livedata.LiveDataUtil) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney) NonNull(androidx.annotation.NonNull)

Example 4 with FiatMoney

use of org.signal.core.util.money.FiatMoney in project Signal-Android by WhisperSystems.

the class CreatePaymentViewModel method clearAmount.

void clearAmount() {
    inputState.update(s -> {
        final Money money = Money.MobileCoin.ZERO;
        final Optional<FiatMoney> fiat = OptionalUtil.flatMap(s.getExchangeRate(), r -> r.exchange(money));
        return s.updateAmount("0", "0", Money.MobileCoin.ZERO, fiat);
    });
}
Also used : FiatMoney(org.signal.core.util.money.FiatMoney) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney)

Example 5 with FiatMoney

use of org.signal.core.util.money.FiatMoney in project Signal-Android by WhisperSystems.

the class MainActivity method requestPayment.

private void requestPayment() {
    donateButton.setClickable(false);
    payApi.requestPayment(new FiatMoney(BigDecimal.valueOf(4.00), Currency.getInstance(Locale.getDefault())), "Test Purchase", 1);
}
Also used : FiatMoney(org.signal.core.util.money.FiatMoney)

Aggregations

FiatMoney (org.signal.core.util.money.FiatMoney)8 Money (org.whispersystems.signalservice.api.payments.Money)4 Context (android.content.Context)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 LiveData (androidx.lifecycle.LiveData)2 MutableLiveData (androidx.lifecycle.MutableLiveData)2 Transformations (androidx.lifecycle.Transformations)2 ViewModel (androidx.lifecycle.ViewModel)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Currency (java.util.Currency)2 Objects (java.util.Objects)2 Test (org.junit.Test)2 Log (org.signal.core.util.logging.Log)2 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)2 SignalStore (org.thoughtcrime.securesms.keyvalue.SignalStore)2 Balance (org.thoughtcrime.securesms.payments.Balance)2 CreatePaymentDetails (org.thoughtcrime.securesms.payments.CreatePaymentDetails)2