+ @Override
+ public void descriptionSelected(String description) {
+ Log.d("descr selected", description);
+ if (!inputStateIsInitial()) return;
+
+ try (Cursor c = MLDB.getReadableDatabase().rawQuery(
+ "select profile, id from transactions where description=? order by date desc " +
+ "limit 1", new String[]{description}))
+ {
+ if (!c.moveToNext()) return;
+
+ String profileUUID = c.getString(0);
+ int transactionId = c.getInt(1);
+ List<MobileLedgerProfile> profiles = Data.profiles.getList();
+ MobileLedgerProfile profile = null;
+ for (int i = 0; i < profiles.size(); i++) {
+ MobileLedgerProfile p = profiles.get(i);
+ if (p.getUuid().equals(profileUUID)) {
+ profile = p;
+ break;
+ }
+ }
+ if (profile == null) throw new RuntimeException(String.format(
+ "Unable to find profile %s, which is supposed to contain " +
+ "transaction %d with description %s", profileUUID, transactionId, description));
+
+ LedgerTransaction tr = profile.loadTransaction(transactionId);
+ int i = 0;
+ table = findViewById(R.id.new_transaction_accounts_table);
+ ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
+ TableRow firstNegative = null;
+ int negativeCount = 0;
+ for (i = 0; i < accounts.size(); i++) {
+ LedgerTransactionAccount acc = accounts.get(i);
+ TableRow row = (TableRow) table.getChildAt(i);
+ if (row == null) row = doAddAccountRow(false);
+
+ ((TextView) row.getChildAt(0)).setText(acc.getAccountName());
+ ((TextView) row.getChildAt(1))
+ .setText(String.format(Locale.US, "%1.2f", acc.getAmount()));
+
+ if (acc.getAmount() < 0.005) {
+ if (firstNegative == null) firstNegative = row;
+ negativeCount++;
+ }
+ }
+
+ if (negativeCount == 1) {
+ ((TextView) firstNegative.getChildAt(1)).setText(null);
+ }
+
+ check_transaction_submittable();
+
+ EditText firstAmount = (EditText) ((TableRow) table.getChildAt(0)).getChildAt(1);
+ String amtString = String.valueOf(firstAmount.getText());
+ firstAmount.requestFocus();
+ firstAmount.setSelection(0, amtString.length());
+ }
+
+ }
+ private boolean inputStateIsInitial() {
+ table = findViewById(R.id.new_transaction_accounts_table);
+
+ if (table.getChildCount() != 2) return false;
+
+ for (int i = 0; i < 2; i++) {
+ TableRow row = (TableRow) table.getChildAt(i);
+ if (((TextView) row.getChildAt(0)).getText().length() > 0) return false;
+ if (((TextView) row.getChildAt(1)).getText().length() > 0) return false;
+ }
+
+ return true;
+ }