From: Damyan Ivanov Date: Fri, 21 Dec 2018 17:03:30 +0000 (+0000) Subject: replace transaction accounts iterator with a getter X-Git-Tag: v0.3~193 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=95cf1db4897d97d1be0ec8e5a7c5d11de26efa85;p=mobile-ledger.git replace transaction accounts iterator with a getter --- diff --git a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java index 1ec01132..57de4c0c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java @@ -57,8 +57,7 @@ class TransactionListAdapter .setText(String.format("%s\n%s", tr.getDescription(), tr.getDate())); TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts); tbl.removeAllViews(); - for (Iterator it = tr.getAccountsIterator(); it.hasNext(); ) { - LedgerTransactionAccount acc = it.next(); + for (LedgerTransactionAccount acc : tr.getAccounts()) { TableRow row = new TableRow(holder.row.getContext()); TextView child = new TextView(ctx); child.setText(acc.getShortAccountName()); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SaveTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SaveTransactionTask.java index 5bca52ff..9cd669e2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SaveTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SaveTransactionTask.java @@ -32,7 +32,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; @@ -73,15 +72,11 @@ public class SaveTransactionTask extends AsyncTask items = ltr.getAccountsIterator(); - while (items.hasNext()) { - LedgerTransactionAccount item = items.next(); - params.add_pair("account", item.getAccountName()); - if (item.isAmountSet()) - params.add_pair("amount", String.format(Locale.US, "%1.2f", item.getAmount())); - else params.add_pair("amount", ""); - } + for (LedgerTransactionAccount acc : ltr.getAccounts()) { + params.add_pair("account", acc.getAccountName()); + if (acc.isAmountSet()) + params.add_pair("amount", String.format(Locale.US, "%1.2f", acc.getAmount())); + else params.add_pair("amount", ""); } String body = params.toString(); diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java index 3919ad14..40d1f5fe 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java @@ -27,7 +27,6 @@ import java.nio.charset.Charset; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Comparator; -import java.util.Iterator; public class LedgerTransaction { private static final String DIGEST_TYPE = "SHA-256"; @@ -45,17 +44,20 @@ public class LedgerTransaction { private Integer id; private String date; private String description; - private ArrayList items; - private String dataHash; - private boolean dataLoaded; + private ArrayList accounts; public LedgerTransaction(Integer id, String date, String description) { this.id = id; this.date = date; this.description = description; - this.items = new ArrayList<>(); + this.accounts = new ArrayList<>(); this.dataHash = null; dataLoaded = false; } + private String dataHash; + private boolean dataLoaded; + public ArrayList getAccounts() { + return accounts; + } public LedgerTransaction(String date, String description) { this(null, date, description); } @@ -63,7 +65,7 @@ public class LedgerTransaction { this(id, null, null); } public void addAccount(LedgerTransactionAccount item) { - items.add(item); + accounts.add(item); dataHash = null; } public String getDate() { @@ -80,20 +82,6 @@ public class LedgerTransaction { this.description = description; dataHash = null; } - public Iterator getAccountsIterator() { - return new Iterator() { - private int pointer = 0; - @Override - public boolean hasNext() { - return pointer < items.size(); - } - - @Override - public LedgerTransactionAccount next() { - return hasNext() ? items.get(pointer++) : null; - } - }; - } public int getId() { return id; } @@ -102,7 +90,7 @@ public class LedgerTransaction { db.execSQL("INSERT INTO transactions(id, date, description, data_hash) values(?,?,?,?)", new Object[]{id, date, description, dataHash}); - for (LedgerTransactionAccount item : items) { + for (LedgerTransactionAccount item : accounts) { db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, " + "currency) values(?, ?, ?, ?)", new Object[]{id, item.getAccountName(), item.getAmount(), item.getCurrency()}); @@ -117,7 +105,7 @@ public class LedgerTransaction { data.append('\0'); data.append(getDescription()); data.append('\0'); - for (LedgerTransactionAccount item : items) { + for (LedgerTransactionAccount item : accounts) { data.append(item.getAccountName()); data.append('\0'); data.append(item.getCurrency()); diff --git a/app/src/main/res/layout/transaction_list_fragment.xml b/app/src/main/res/layout/transaction_list_fragment.xml index 19bb72fa..c871f2ad 100644 --- a/app/src/main/res/layout/transaction_list_fragment.xml +++ b/app/src/main/res/layout/transaction_list_fragment.xml @@ -47,7 +47,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" - android:text="TextView" /> + android:text="\?" + tools:ignore="HardcodedText" /> . --> - + android:paddingEnd="8dp"> + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginStart="8dp" + android:layout_weight="5" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintStart_toEndOf="@id/transaction_row_description"> @@ -69,4 +76,4 @@ tools:ignore="HardcodedText" /> - \ No newline at end of file + \ No newline at end of file