]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
add app shortcuts (Android 7.1+) for adding new transaction for top profiles
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionActivity.java
index 6f0ea1a809646c7e5fde31cddd8d32c477786bf0..b4716075d1c6474905cf6ece759983f25521766a 100644 (file)
@@ -47,7 +47,7 @@ import com.google.android.material.snackbar.Snackbar;
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
-import net.ktnx.mobileledger.async.SaveTransactionTask;
+import net.ktnx.mobileledger.async.SendTransactionTask;
 import net.ktnx.mobileledger.async.TaskCallback;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
@@ -77,7 +77,7 @@ import androidx.fragment.app.DialogFragment;
 
 public class NewTransactionActivity extends ProfileThemedActivity
         implements TaskCallback, DescriptionSelectedCallback {
-    private static SaveTransactionTask saver;
+    private static SendTransactionTask saver;
     private TableLayout table;
     private ProgressBar progress;
     private FloatingActionButton fab;
@@ -93,7 +93,7 @@ public class NewTransactionActivity extends ProfileThemedActivity
         setContentView(R.layout.activity_new_transaction);
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
-        toolbar.setSubtitle(Data.profile.get().getName());
+        toolbar.setSubtitle(mProfile.getName());
 
         tvDate = findViewById(R.id.new_transaction_date);
         tvDate.setOnFocusChangeListener((v, hasFocus) -> {
@@ -122,7 +122,16 @@ public class NewTransactionActivity extends ProfileThemedActivity
 //            Log.d("swipe", "hooked to row "+i);
         }
     }
+    @Override
+    protected void initProfile() {
+        String profileUUID = getIntent().getStringExtra("profile_uuid");
 
+        if (profileUUID != null) {
+            mProfile = Data.getProfile(profileUUID);
+            if (mProfile == null) finish();
+        }
+        else super.initProfile();
+    }
     @Override
     public void finish() {
         super.finish();
@@ -149,7 +158,7 @@ public class NewTransactionActivity extends ProfileThemedActivity
         progress.setVisibility(View.VISIBLE);
         try {
 
-            saver = new SaveTransactionTask(this);
+            saver = new SendTransactionTask(this, mProfile);
 
             String dateString = tvDate.getText().toString();
             Date date;
@@ -158,16 +167,30 @@ public class NewTransactionActivity extends ProfileThemedActivity
             LedgerTransaction tr = new LedgerTransaction(date, tvDescription.getText().toString());
 
             TableLayout table = findViewById(R.id.new_transaction_accounts_table);
+            LedgerTransactionAccount emptyAmountAccount = null;
+            float emptyAmountAccountBalance = 0;
             for (int i = 0; i < table.getChildCount(); i++) {
                 TableRow row = (TableRow) table.getChildAt(i);
                 String acc = ((TextView) row.getChildAt(0)).getText().toString();
+                if (acc.isEmpty()) continue;
+
                 String amt = ((TextView) row.getChildAt(1)).getText().toString();
-                LedgerTransactionAccount item =
-                        amt.length() > 0 ? new LedgerTransactionAccount(acc, Float.parseFloat(amt))
-                                         : new LedgerTransactionAccount(acc);
+                LedgerTransactionAccount item;
+                if (amt.length() > 0) {
+                    final float amount = Float.parseFloat(amt);
+                    item = new LedgerTransactionAccount(acc, amount);
+                    emptyAmountAccountBalance += amount;
+                }
+                else {
+                    item = new LedgerTransactionAccount(acc);
+                    emptyAmountAccount = item;
+                }
 
                 tr.addAccount(item);
             }
+
+            if (emptyAmountAccount != null)
+                emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
             saver.execute(tr);
         }
         catch (ParseException e) {