]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
rewrap
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionActivity.java
index cef99101f8eae72776103b72c46d477cb0a31265..6a6fb4194c5139e0e0187abee725e8fc3d09469d 100644 (file)
@@ -78,9 +78,11 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         fab = findViewById(R.id.fab);
         fab.setOnClickListener(v -> saveTransaction());
 
-        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
+        Objects.requireNonNull(getSupportActionBar())
+               .setDisplayHomeAsUpEnabled(true);
         list = findViewById(R.id.new_transaction_accounts);
-        viewModel = ViewModelProviders.of(this).get(NewTransactionModel.class);
+        viewModel = ViewModelProviders.of(this)
+                                      .get(NewTransactionModel.class);
         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
         list.setAdapter(listAdapter);
         list.setLayoutManager(new LinearLayoutManager(this));
@@ -91,9 +93,14 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
             public int getMovementFlags(@NonNull RecyclerView recyclerView,
                                         @NonNull RecyclerView.ViewHolder viewHolder) {
                 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
-                if (viewModel.getAccountCount() > 2) flags |=
-                        makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
+                // the top item is always there (date and description)
+                if (viewHolder.getAdapterPosition() > 0) {
+                    if (viewModel.getAccountCount() > 2) {
+                        flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
                                 ItemTouchHelper.START | ItemTouchHelper.END);
+                    }
+                }
+
                 return flags;
             }
             @Override
@@ -106,32 +113,36 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                 if (viewModel.getAccountCount() == 2)
                     Snackbar.make(list, R.string.msg_at_least_two_accounts_are_required,
-                            Snackbar.LENGTH_LONG).setAction("Action", null).show();
+                            Snackbar.LENGTH_LONG)
+                            .setAction("Action", null)
+                            .show();
                 else {
                     int pos = viewHolder.getAdapterPosition();
-                    listAdapter.removeItem(pos);
-                    // FIXME hook next/prev links somehow
-                    throw new RuntimeException("TODO");
+                    viewModel.removeItem(pos - 1);
+                    listAdapter.notifyItemRemoved(pos);
+                    viewModel.sendCountNotifications(); // needed after items re-arrangement
+                    viewModel.checkTransactionSubmittable(listAdapter);
                 }
             }
         }).attachToRecyclerView(list);
 
-        viewModel.isSubmittable().observe(this, new Observer<Boolean>() {
-            @Override
-            public void onChanged(Boolean isSubmittable) {
-                if (isSubmittable) {
-                    if (fab != null) {
-                        fab.show();
-                        fab.setEnabled(true);
-                    }
-                }
-                else {
-                    if (fab != null) {
-                        fab.hide();
-                    }
-                }
-            }
-        });
+        viewModel.isSubmittable()
+                 .observe(this, new Observer<Boolean>() {
+                     @Override
+                     public void onChanged(Boolean isSubmittable) {
+                         if (isSubmittable) {
+                             if (fab != null) {
+                                 fab.show();
+                                 fab.setEnabled(true);
+                             }
+                         }
+                         else {
+                             if (fab != null) {
+                                 fab.hide();
+                             }
+                         }
+                     }
+                 });
         viewModel.checkTransactionSubmittable(listAdapter);
     }
     @Override
@@ -141,6 +152,7 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         if (profileUUID != null) {
             mProfile = Data.getProfile(profileUUID);
             if (mProfile == null) finish();
+            Data.setCurrentProfile(mProfile);
         }
         else super.initProfile();
     }
@@ -173,14 +185,15 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
 
             Date date = viewModel.getDate();
             LedgerTransaction tr =
-                    new LedgerTransaction(null, date, viewModel.getDescription(),
-                            mProfile);
+                    new LedgerTransaction(null, date, viewModel.getDescription(), mProfile);
 
             LedgerTransactionAccount emptyAmountAccount = null;
             float emptyAmountAccountBalance = 0;
             for (int i = 0; i < viewModel.getAccountCount(); i++) {
                 LedgerTransactionAccount acc = viewModel.getAccount(i);
-                if (acc.getAccountName().trim().isEmpty()) continue;
+                if (acc.getAccountName()
+                       .trim()
+                       .isEmpty()) continue;
 
                 if (acc.isAmountSet()) {
                     emptyAmountAccountBalance += acc.getAmount();
@@ -213,7 +226,8 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         getMenuInflater().inflate(R.menu.new_transaction, menu);
 
         if (BuildConfig.DEBUG) {
-            menu.findItem(R.id.action_simulate_crash).setVisible(true);
+            menu.findItem(R.id.action_simulate_crash)
+                .setVisible(true);
         }
 
         return true;
@@ -233,7 +247,8 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
         debug("visuals", "hiding progress");
 
         if (error == null) resetForm();
-        else Snackbar.make(list, error, BaseTransientBottomBar.LENGTH_LONG).show();
+        else Snackbar.make(list, error, BaseTransientBottomBar.LENGTH_LONG)
+                     .show();
 
         listAdapter.toggleAllEditing(true);
         viewModel.checkTransactionSubmittable(listAdapter);