X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionItemsAdapter.java;h=ef5947afd37f8ecf2b9babe3cef7bc63fe2c4ae6;hb=ee38e21aa7318a51f9f3e62788d920e13b7be620;hp=1585f1d8d48bde6c1cbb85f0ce04b93f3fb9373b;hpb=865334093695e52f99d93ad9a255252b9d575053;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java index 1585f1d8..ef5947af 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java @@ -21,9 +21,9 @@ import android.database.Cursor; import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.LinearLayout; -import android.widget.TableRow; import androidx.annotation.NonNull; +import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.App; @@ -44,6 +44,8 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 0) && (adapterPosition < adapter.getItemCount() - 1)) { + flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, + ItemTouchHelper.UP | ItemTouchHelper.DOWN) | + makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE, + ItemTouchHelper.START | ItemTouchHelper.END); + } + + return flags; + } + @Override + public boolean onMove(@NonNull RecyclerView recyclerView, + @NonNull RecyclerView.ViewHolder viewHolder, + @NonNull RecyclerView.ViewHolder target) { + + model.swapItems(viewHolder.getAdapterPosition(), target.getAdapterPosition()); + notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); + return true; + } + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + int pos = viewHolder.getAdapterPosition(); + viewModel.removeItem(pos - 1); + notifyItemRemoved(pos); + viewModel.sendCountNotifications(); // needed after items re-arrangement + viewModel.checkTransactionSubmittable(adapter); + } + }); } public void setProfile(MobileLedgerProfile profile) { 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 notifyItemInserted(newAccountCount); + model.sendCountNotifications(); // needed after holders' positions have changed return newAccountCount; } @NonNull @@ -72,34 +133,50 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter accounts = tr.getAccounts(); - TableRow firstNegative = null; + NewTransactionModel.Item firstNegative = null; + 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); NewTransactionModel.Item item; - if (model.getAccountCount() < i) { + if (model.getAccountCount() < i + 1) { model.addAccount(acc); notifyItemInserted(i + 1); } @@ -151,22 +234,53 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 2) notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone } - public void removeItem(int pos) { - model.removeItem(pos - 1, this); - notifyItemRemoved(pos); + public void updateFocusedItem(int position) { + model.updateFocusedItem(position); + } + public void noteFocusIsOnAccount(int position) { + model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Account); + } + public void noteFocusIsOnAmount(int position) { + model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Amount); + } + public void noteFocusIsOnComment(int position) { + model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment); + } + public void toggleComment(int position) { + model.toggleComment(position); } }