Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add price if commodities not in database. #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,18 @@ public String getCurrencyCode(@NonNull String guid) {
cursor.close();
}
}

@Nullable
public Commodity loadCommodity(@NonNull Commodity commodity) {
if (commodity.id != 0) {
return commodity;
}
try {
commodity = getRecord(commodity.getUID());
} catch (Exception e) {
// Commodity not found.
commodity = getCommodity(commodity.getCurrencyCode());
}
return commodity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.SQLException;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -50,21 +51,26 @@
import java.text.NumberFormat;
import java.text.ParseException;

import timber.log.Timber;

/**
* Dialog fragment for handling currency conversions when inputting transactions.
* <p>This is used whenever a multi-currency transaction is being created.</p>
*/
public class TransferFundsDialogFragment extends VolatileDialogFragment {
// FIXME these fields must be persisted for when dialog is changed, e.g. rotated.
private Money mOriginAmount;
// FIXME these fields must be persisted for when dialog is changed, e.g. rotated.
private Commodity mTargetCommodity;

// FIXME these fields must be persisted for when dialog is changed, e.g. rotated.
private OnTransferFundsListener mOnTransferFundsListener;

private DialogTransferFundsBinding binding;
@ColorInt
private int colorBalanceZero;
private final PricesDbAdapter pricesDbAdapter = PricesDbAdapter.getInstance();
private final CommoditiesDbAdapter commoditiesDbAdapter = CommoditiesDbAdapter.getInstance();

public static TransferFundsDialogFragment getInstance(
@NonNull Money transactionAmount,
Expand All @@ -84,7 +90,6 @@ public static TransferFundsDialogFragment getInstance(
@Nullable OnTransferFundsListener transferFundsListener
) {
TransferFundsDialogFragment fragment = new TransferFundsDialogFragment();
// FIXME these fields must be persisted for when dialog is changed, e.g. rotated.
fragment.mOriginAmount = transactionAmount;
fragment.mTargetCommodity = targetCurrency;
fragment.mOnTransferFundsListener = transferFundsListener;
Expand Down Expand Up @@ -198,9 +203,19 @@ public void onClick(DialogInterface dialog, int which) {
private void transferFunds(Commodity originCommodity, Commodity targetCommodity) {
Money convertedAmount = null;

final Price price = new Price(originCommodity, targetCommodity);
Commodity commodityFrom = commoditiesDbAdapter.loadCommodity(originCommodity);
if (commodityFrom == null) {
Timber.e("Origin commodity not found in db!");
return;
}
Commodity commodityTo = commoditiesDbAdapter.loadCommodity(targetCommodity);
if (commodityTo == null) {
Timber.e("Target commodity not found in db!");
return;
}
final Price price = new Price(commodityFrom, commodityTo);
if (binding.radioExchangeRate.isChecked()) {
BigDecimal rate;
final BigDecimal rate;
try {
rate = AmountParser.parse(binding.inputExchangeRate.getText().toString());
} catch (ParseException e) {
Expand All @@ -211,7 +226,7 @@ private void transferFunds(Commodity originCommodity, Commodity targetCommodity)

price.setExchangeRate(rate);
} else if (binding.radioConvertedAmount.isChecked()) {
BigDecimal amount;
final BigDecimal amount;
try {
amount = AmountParser.parse(binding.inputConvertedAmount.getText().toString());
} catch (ParseException e) {
Expand All @@ -225,10 +240,14 @@ private void transferFunds(Commodity originCommodity, Commodity targetCommodity)
price.setValueDenom(mOriginAmount.getNumerator() * convertedAmount.getDenominator());
}
price.setSource(Price.SOURCE_USER);
pricesDbAdapter.addRecord(price, DatabaseAdapter.UpdateMethod.insert);
try {
pricesDbAdapter.addRecord(price, DatabaseAdapter.UpdateMethod.insert);

if (mOnTransferFundsListener != null && convertedAmount != null) {
mOnTransferFundsListener.transferComplete(mOriginAmount, convertedAmount);
if (mOnTransferFundsListener != null && convertedAmount != null) {
mOnTransferFundsListener.transferComplete(mOriginAmount, convertedAmount);
}
} catch (SQLException e) {
Timber.e(e);
}
}

Expand Down
Loading