import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
-import com.google.android.material.snackbar.Snackbar;
-
import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
public int getMovementFlags(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder) {
int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
- // the top item is always there (date and description)
- if (viewHolder.getAdapterPosition() > 0) {
+ // the top (date and description) and the bottom (padding) items are always there
+ final int adapterPosition = viewHolder.getAdapterPosition();
+ if ((adapterPosition > 0) && (adapterPosition < adapter.getItemCount() - 1)) {
flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
- ItemTouchHelper.UP | ItemTouchHelper.DOWN);
-
- if (viewModel.getAccountCount() > 2) {
- flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
- ItemTouchHelper.START | ItemTouchHelper.END);
- }
+ ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
+ makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
+ ItemTouchHelper.START | ItemTouchHelper.END);
}
return flags;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
- if (viewModel.getAccountCount() == 2)
- Snackbar.make(recyclerView, R.string.msg_at_least_two_accounts_are_required,
- Snackbar.LENGTH_LONG)
- .setAction("Action", null)
- .show();
- else {
- int pos = viewHolder.getAdapterPosition();
- viewModel.removeItem(pos - 1);
- notifyItemRemoved(pos);
- viewModel.sendCountNotifications(); // needed after items re-arrangement
- viewModel.checkTransactionSubmittable(adapter);
- }
+ int pos = viewHolder.getAdapterPosition();
+ viewModel.removeItem(pos - 1);
+ notifyItemRemoved(pos);
+ viewModel.sendCountNotifications(); // needed after items re-arrangement
+ viewModel.checkTransactionSubmittable(adapter);
}
});
}
mProfile = profile;
}
int addRow() {
- final int newAccountCount = model.addAccount(new LedgerTransactionAccount(""));
+ return addRow(null);
+ }
+ int addRow(String commodity) {
+ final int newAccountCount = model.addAccount(new LedgerTransactionAccount("", commodity));
Logger.debug("new-transaction",
String.format(Locale.US, "invoking notifyItemInserted(%d)", newAccountCount));
// the header is at position 0
tr = profile.loadTransaction(transactionId);
ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
NewTransactionModel.Item firstNegative = null;
- boolean singleNegative = false;
+ NewTransactionModel.Item firstPositive = null;
+ int singleNegativeIndex = -1;
+ int singlePositiveIndex = -1;
int negativeCount = 0;
for (int i = 0; i < accounts.size(); i++) {
LedgerTransactionAccount acc = accounts.get(i);
if (acc.getAmount() < 0) {
if (firstNegative == null) {
firstNegative = item;
- singleNegative = true;
+ singleNegativeIndex = i;
}
else
- singleNegative = false;
+ singleNegativeIndex = -1;
+ }
+ else {
+ if (firstPositive == null) {
+ firstPositive = item;
+ singlePositiveIndex = i;
+ }
+ else
+ singlePositiveIndex = -1;
}
}
else
notifyItemChanged(i + 1);
}
- if (singleNegative) {
+ if (singleNegativeIndex != -1) {
firstNegative.getAccount()
.resetAmount();
+ model.moveItemLast(singleNegativeIndex);
+ }
+ else if (singlePositiveIndex != -1) {
+ firstPositive.getAccount()
+ .resetAmount();
+ model.moveItemLast(singlePositiveIndex);
}
}
model.checkTransactionSubmittable(this);
public void noteFocusIsOnComment(int position) {
model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
}
+ public void toggleComment(int position) {
+ model.toggleComment(position);
+ }
}