Skip to content

Commit

Permalink
Dialogs that have volatile members.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jan 1, 2025
1 parent 80e0036 commit c93d6ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import org.gnucash.android.R;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ public class BulkMoveDialogFragment extends DialogFragment {

public static final String TAG = "bulk_move_transactions";

/**
* Spinner for selecting the account to move the transactions to
*/
private Spinner mDestinationAccountSpinner;

/**
* Record IDs of the transactions to be moved
*/
private long[] mTransactionIds = null;

/**
* GUID of account from which to move the transactions
*/
private String mOriginAccountUID = null;

/**
* Create new instance of the bulk move dialog
*
Expand All @@ -84,10 +69,27 @@ public static BulkMoveDialogFragment newInstance(long[] transactionIds, String o
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
DialogBulkMoveBinding binding = DialogBulkMoveBinding.inflate(getLayoutInflater());
Context context = binding.getRoot().getContext();
mDestinationAccountSpinner = binding.accountsListSpinner;
final Context context = binding.getRoot().getContext();
final Spinner accountSpinner = binding.accountsListSpinner;

Bundle args = getArguments();
final long[] selectedTransactionIds = args.getLongArray(UxArgument.SELECTED_TRANSACTION_IDS);
final long[] transactionIds = (selectedTransactionIds != null) ? selectedTransactionIds : new long[0];
final String originAccountUID = args.getString(UxArgument.ORIGIN_ACCOUNT_UID);

AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance();
String conditions = "(" + DatabaseSchema.AccountEntry.COLUMN_UID + " != ? AND "
+ DatabaseSchema.AccountEntry.COLUMN_CURRENCY + " = ? AND "
+ DatabaseSchema.AccountEntry.COLUMN_HIDDEN + " = 0 AND "
+ DatabaseSchema.AccountEntry.COLUMN_PLACEHOLDER + " = 0"
+ ")";
Cursor cursor = accountsDbAdapter.fetchAccountsOrderedByFullName(conditions,
new String[]{originAccountUID, accountsDbAdapter.getCurrencyCode(originAccountUID)});

SimpleCursorAdapter adapter = new QualifiedAccountNameCursorAdapter(context, cursor);
accountSpinner.setAdapter(adapter);

String title = context.getString(R.string.title_move_transactions, mTransactionIds.length);
String title = context.getString(R.string.title_move_transactions, transactionIds.length);

return new AlertDialog.Builder(context, getTheme())
.setTitle(title)
Expand All @@ -101,49 +103,25 @@ public void onClick(DialogInterface dialog, int which) {
.setPositiveButton(R.string.btn_move, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
moveTransaction();
moveTransaction(context, transactionIds, originAccountUID, accountSpinner.getSelectedItemId());
}
})
.create();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

Bundle args = getArguments();
mTransactionIds = args.getLongArray(UxArgument.SELECTED_TRANSACTION_IDS);
mOriginAccountUID = args.getString(UxArgument.ORIGIN_ACCOUNT_UID);

AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance();
String conditions = "(" + DatabaseSchema.AccountEntry.COLUMN_UID + " != ? AND "
+ DatabaseSchema.AccountEntry.COLUMN_CURRENCY + " = ? AND "
+ DatabaseSchema.AccountEntry.COLUMN_HIDDEN + " = 0 AND "
+ DatabaseSchema.AccountEntry.COLUMN_PLACEHOLDER + " = 0"
+ ")";
Cursor cursor = accountsDbAdapter.fetchAccountsOrderedByFullName(conditions,
new String[]{mOriginAccountUID, accountsDbAdapter.getCurrencyCode(mOriginAccountUID)});

SimpleCursorAdapter adapter = new QualifiedAccountNameCursorAdapter(getActivity(), cursor);
mDestinationAccountSpinner.setAdapter(adapter);
}

private void moveTransaction() {
if (mTransactionIds == null) {
dismiss();
private void moveTransaction(@NonNull Context context, @Nullable long[] transactionIds, String srcAccountUID, long dstAccountId) {
if (transactionIds == null) {
return;
}
Context context = requireContext();

String srcAccountUID = mOriginAccountUID;
long dstAccountId = mDestinationAccountSpinner.getSelectedItemId();
String dstAccountUID = AccountsDbAdapter.getInstance().getUID(dstAccountId);
TransactionsDbAdapter trxnAdapter = TransactionsDbAdapter.getInstance();
if (!trxnAdapter.getAccountCurrencyCode(dstAccountUID).equals(trxnAdapter.getAccountCurrencyCode(srcAccountUID))) {
Toast.makeText(context, R.string.toast_incompatible_currency, Toast.LENGTH_LONG).show();
return;
}

for (long trxnId : mTransactionIds) {
for (long trxnId : transactionIds) {
trxnAdapter.moveTransaction(trxnAdapter.getUID(trxnId), srcAccountUID, dstAccountUID);
}

Expand All @@ -152,7 +130,5 @@ private void moveTransaction() {
result.putBoolean(Refreshable.EXTRA_REFRESH, true);
result.putString(UxArgument.SELECTED_ACCOUNT_UID, dstAccountUID);
getParentFragmentManager().setFragmentResult(TAG, result);

dismiss();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ private DialogTransferFundsBinding onCreateBinding(LayoutInflater inflater) {
colorBalanceZero = binding.amountToConvert.getCurrentTextColor();

displayBalance(binding.amountToConvert, mOriginAmount, colorBalanceZero);
Commodity fromCommodity = mOriginAmount.getCommodity();
Commodity targetCommodity = mTargetCommodity;
final Commodity fromCommodity = mOriginAmount.getCommodity();
final Commodity targetCommodity = mTargetCommodity;
String fromCurrencyCode = fromCommodity.getCurrencyCode();
String targetCurrencyCode = targetCommodity.getCurrencyCode();
binding.fromCurrency.setText(fromCurrencyCode);
Expand Down Expand Up @@ -180,7 +180,7 @@ public void onClick(DialogInterface dialog, int which) {
.setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
transferFunds();
transferFunds(mOriginAmount.getCommodity(), mTargetCommodity);
}
})
.create();
Expand All @@ -189,9 +189,7 @@ public void onClick(DialogInterface dialog, int which) {
/**
* Converts the currency amount with the given exchange rate and saves the price to the db
*/
private void transferFunds() {
Commodity originCommodity = mOriginAmount.getCommodity();
Commodity targetCommodity = mTargetCommodity;
private void transferFunds(Commodity originCommodity, Commodity targetCommodity) {
Money convertedAmount = null;

Price price = null;
Expand All @@ -214,7 +212,7 @@ private void transferFunds() {
binding.convertedAmountTextInputLayout.setError(getString(R.string.error_invalid_amount));
return;
}
convertedAmount = new Money(amount, mTargetCommodity);
convertedAmount = new Money(amount, targetCommodity);

price = new Price(originCommodity, targetCommodity);
// fractions cannot be exactly represented by BigDecimal.
Expand Down

0 comments on commit c93d6ef

Please sign in to comment.