]> git.ktnx.net Git - mobile-ledger.git/commitdiff
add UI and API support for sending posting (transaction account) comments
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 Jan 2020 15:37:30 +0000 (17:37 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 Jan 2020 15:37:30 +0000 (17:37 +0200)
app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedPosting.java
app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedPosting.java
app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
app/src/main/res/drawable-anydpi-v21/ic_comment_black_24dp.xml [new file with mode: 0644]
app/src/main/res/layout/new_transaction_row.xml
app/src/main/res/values/strings.xml

index 21cc6dbff22130980ca02e624d2c01ef0d13efbb..f1a6e1ff9975c214f6b0519f86f58f9fe12d2e31 100644 (file)
@@ -42,6 +42,12 @@ public class ParsedPosting {
     public static ParsedPosting fromLedgerAccount(LedgerTransactionAccount acc) {
         ParsedPosting result = new ParsedPosting();
         result.setPaccount(acc.getAccountName());
+
+        String comment = acc.getComment();
+        if (comment == null)
+            comment = "";
+        result.setPcomment(comment);
+
         ArrayList<ParsedAmount> amounts = new ArrayList<>();
         ParsedAmount amt = new ParsedAmount();
         amt.setAcommodity((acc.getCurrency() == null) ? "" : acc.getCurrency());
index 7c20a7ba87a62ab669557978d37b50a236da6600..0789f0d9fbd8e43fea1c6afec1a1d9699e507f70 100644 (file)
@@ -113,6 +113,12 @@ public class ParsedPosting {
     public static ParsedPosting fromLedgerAccount(LedgerTransactionAccount acc) {
         ParsedPosting result = new ParsedPosting();
         result.setPaccount(acc.getAccountName());
+
+        String comment = acc.getComment();
+        if (comment == null)
+            comment = "";
+        result.setPcomment(comment);
+
         ArrayList<ParsedAmount> amounts = new ArrayList<>();
         ParsedAmount amt = new ParsedAmount();
         amt.setAcommodity((acc.getCurrency() == null) ? "" : acc.getCurrency());
index 89d6a83a5d3c935ce98b0dcaa65c8d82fa0b6410..4a83b185a00ac7da9046244fc1efd7b26762753b 100644 (file)
@@ -25,7 +25,7 @@ public class LedgerTransactionAccount {
     private float amount;
     private boolean amountSet = false;
     private String currency;
-
+    private String comment;
     public LedgerTransactionAccount(String accountName, float amount) {
         this(accountName, amount, null);
     }
@@ -35,18 +35,23 @@ public class LedgerTransactionAccount {
         this.amountSet = true;
         this.currency = currency;
     }
-
     public LedgerTransactionAccount(String accountName) {
         this.accountName = accountName;
     }
     public LedgerTransactionAccount(LedgerTransactionAccount origin) {
         // copy constructor
         setAccountName(origin.getAccountName());
+        setComment(origin.getComment());
         if (origin.isAmountSet())
             setAmount(origin.getAmount());
         currency = origin.getCurrency();
     }
-
+    public String getComment() {
+        return comment;
+    }
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
     public String getAccountName() {
         return accountName;
     }
index e4d1d503d4ebc2ceb43d7a7fcdafb374b75495cf..6988fe4f4ba4b0b17e0584d4e9491f03a1807fd9 100644 (file)
@@ -51,6 +51,8 @@ import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.Locale;
 
+import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType;
+
 class NewTransactionItemHolder extends RecyclerView.ViewHolder
         implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
     private final String decimalSeparator;
@@ -59,6 +61,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private TextView tvDate;
     private AutoCompleteTextView tvDescription;
     private AutoCompleteTextView tvAccount;
+    private TextView tvComment;
     private TextView tvAmount;
     private LinearLayout lHead;
     private ViewGroup lAccount;
@@ -76,6 +79,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
         super(itemView);
         tvAccount = itemView.findViewById(R.id.account_row_acc_name);
+        tvComment = itemView.findViewById(R.id.comment);
         tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
         tvDate = itemView.findViewById(R.id.new_transaction_date);
         tvDescription = itemView.findViewById(R.id.new_transaction_description);
@@ -100,11 +104,16 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 try {
                     final int pos = getAdapterPosition();
                     adapter.updateFocusedItem(pos);
-                    if (v instanceof AutoCompleteTextViewWithClear) {
-                        adapter.noteFocusIsOnAccount(pos);
-                    }
-                    else {
-                        adapter.noteFocusIsOnAmount(pos);
+                    switch (v.getId()) {
+                        case R.id.account_row_acc_name:
+                            adapter.noteFocusIsOnAccount(pos);
+                            break;
+                        case R.id.account_row_acc_amounts:
+                            adapter.noteFocusIsOnAmount(pos);
+                            break;
+                        case R.id.comment:
+                            adapter.noteFocusIsOnComment(pos);
+                            break;
                     }
                 }
                 finally {
@@ -180,6 +189,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         };
         tvDescription.addTextChangedListener(tw);
         tvAccount.addTextChangedListener(tw);
+        tvComment.addTextChangedListener(tw);
         tvAmount.addTextChangedListener(amountWatcher);
 
         // FIXME: react on locale changes
@@ -242,15 +252,20 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     case transactionRow:
                         // do nothing if a row element already has the focus
                         if (!itemView.hasFocus()) {
-                            if (item.focusIsOnAmount()) {
-                                tvAmount.requestFocus();
-                            }
-                            else {
-                                focused = tvAccount.requestFocus();
-                                tvAccount.dismissDropDown();
-                                if (focused)
-                                    Misc.showSoftKeyboard(
-                                            (NewTransactionActivity) tvAccount.getContext());
+                            switch (item.getFocusedElement()) {
+                                case Amount:
+                                    tvAmount.requestFocus();
+                                    break;
+                                case Comment:
+                                    tvComment.requestFocus();
+                                    break;
+                                case Account:
+                                    focused = tvAccount.requestFocus();
+                                    tvAccount.dismissDropDown();
+                                    if (focused)
+                                        Misc.showSoftKeyboard(
+                                                (NewTransactionActivity) tvAccount.getContext());
+                                    break;
                             }
                         }
 
@@ -266,7 +281,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                             adapterPosition, layoutPosition, item.getType()
                                                                  .toString()
                                                                  .concat(item.getType() ==
-                                                                         NewTransactionModel.ItemType.transactionRow
+                                                                         ItemType.transactionRow
                                                                          ? String.format(Locale.US,
                                                                          "'%s'=%s",
                                                                          item.getAccount()
@@ -323,29 +338,28 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     item.setDescription(String.valueOf(tvDescription.getText()));
                     break;
                 case transactionRow:
-                    item.getAccount()
-                        .setAccountName(String.valueOf(tvAccount.getText()));
+                    final LedgerTransactionAccount account = item.getAccount();
+                    account.setAccountName(String.valueOf(tvAccount.getText()));
+
+                    account.setComment(String.valueOf(tvComment.getText()));
 
                     // TODO: handle multiple amounts
                     String amount = String.valueOf(tvAmount.getText());
                     amount = amount.trim();
 
                     if (amount.isEmpty()) {
-                        item.getAccount()
-                            .resetAmount();
+                        account.resetAmount();
                     }
                     else {
                         try {
                             amount = amount.replace(decimalSeparator, decimalDot);
-                            item.getAccount()
-                                .setAmount(Float.parseFloat(amount));
+                            account.setAmount(Float.parseFloat(amount));
                         }
                         catch (NumberFormatException e) {
                             Logger.debug("new-trans", String.format(
                                     "assuming amount is not set due to number format exception. " +
                                     "input was '%s'", amount));
-                            item.getAccount()
-                                .resetAmount();
+                            account.resetAmount();
                         }
                     }
 
@@ -399,6 +413,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 case transactionRow:
                     LedgerTransactionAccount acc = item.getAccount();
                     tvAccount.setText(acc.getAccountName());
+                    tvComment.setText(acc.getComment());
                     if (acc.isAmountSet()) {
                         tvAmount.setText(String.format("%1.2f", acc.getAmount()));
                     }
index 88f99a20fe330a71f38027171879acd4ffaee494..2cf972f89ed597df5bba2bce282381f4976b1bfb 100644 (file)
@@ -287,9 +287,12 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         model.updateFocusedItem(position);
     }
     public void noteFocusIsOnAccount(int position) {
-        model.noteFocusIsOnAccount(position);
+        model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Account);
     }
     public void noteFocusIsOnAmount(int position) {
-        model.noteFocusIsOnAmount(position);
+        model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Amount);
+    }
+    public void noteFocusIsOnComment(int position) {
+        model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
     }
 }
index 0801eb2ecbb4769ac5e740395d823a460ad84d04..bd2c640fd0ddfaa7b477f1287e6875c9f306b3e1 100644 (file)
@@ -265,8 +265,9 @@ public class NewTransactionModel extends ViewModel {
                 for (int i = 0; i < items.size(); i++) {
                     Item item = items.get(i);
                     LedgerTransactionAccount acc = item.getAccount();
-                    debug("submittable", String.format("Item %2d: [%4.2f] %s", i,
-                            acc.isAmountSet() ? acc.getAmount() : 0, acc.getAccountName()));
+                    debug("submittable", String.format("Item %2d: [%4.2f] %s (%s)", i,
+                            acc.isAmountSet() ? acc.getAmount() : 0, acc.getAccountName(),
+                            acc.getComment()));
                 }
             }
         }
@@ -293,11 +294,8 @@ public class NewTransactionModel extends ViewModel {
     public void updateFocusedItem(int position) {
         focusedItem.setValue(position);
     }
-    public void noteFocusIsOnAccount(int position) {
-        getItem(position).setFocusIsOnAmount(false);
-    }
-    public void noteFocusIsOnAmount(int position) {
-        getItem(position).setFocusIsOnAmount(true);
+    public void noteFocusChanged(int position, FocusedElement element) {
+        getItem(position).setFocusedElement(element);
     }
     public void swapItems(int one, int two) {
         Collections.swap(items, one-1, two-1);
@@ -306,7 +304,9 @@ public class NewTransactionModel extends ViewModel {
 
     //==========================================================================================
 
-    class Item extends Object {
+    enum FocusedElement {Account, Comment, Amount}
+
+    class Item {
         private ItemType type;
         private MutableLiveData<Date> date = new MutableLiveData<>();
         private MutableLiveData<String> description = new MutableLiveData<>();
@@ -314,7 +314,7 @@ public class NewTransactionModel extends ViewModel {
         private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
         private NewTransactionModel model;
         private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
-        private boolean focusIsOnAmount = false;
+        private FocusedElement focusedElement = FocusedElement.Account;
         public Item(NewTransactionModel model) {
             this.model = model;
             type = ItemType.bottomFiller;
@@ -333,8 +333,11 @@ public class NewTransactionModel extends ViewModel {
             this.account = account;
             this.editable.setValue(true);
         }
-        public boolean focusIsOnAmount() {
-            return focusIsOnAmount;
+        public FocusedElement getFocusedElement() {
+            return focusedElement;
+        }
+        public void setFocusedElement(FocusedElement focusedElement) {
+            this.focusedElement = focusedElement;
         }
         public NewTransactionModel getModel() {
             return model;
@@ -503,8 +506,5 @@ public class NewTransactionModel extends ViewModel {
         public void stopObservingEditableFlag(Observer<Boolean> observer) {
             editable.removeObserver(observer);
         }
-        public void setFocusIsOnAmount(boolean flag) {
-            focusIsOnAmount = flag;
-        }
     }
 }
diff --git a/app/src/main/res/drawable-anydpi-v21/ic_comment_black_24dp.xml b/app/src/main/res/drawable-anydpi-v21/ic_comment_black_24dp.xml
new file mode 100644 (file)
index 0000000..60588c1
--- /dev/null
@@ -0,0 +1,29 @@
+<!--
+  ~ Copyright Google Inc.
+  ~
+  ~ Licensed under the Apache License, version 2.0 ("the License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the license at:
+  ~
+  ~ https://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distribution under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  ~
+  ~ Modified/adapted by Damyan Ivanov for MoLe
+  -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:autoMirrored="true"
+    android:tint="?colorPrimary"
+    android:viewportWidth="24.0"
+    android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M21.99,4c0,-1.1 -0.89,-2 -1.99,-2L4,2c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h14l4,4 -0.01,-18zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z" />
+</vector>
index 9210b2980dcc795500beafea1558af791833728a..508eea9892b1529a7ae7e58d6625689d1bd2e66a 100644 (file)
@@ -78,8 +78,7 @@
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/ntr_account"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:orientation="horizontal">
+        android:layout_height="wrap_content">
 
         <net.ktnx.mobileledger.ui.AutoCompleteTextViewWithClear
             android:id="@+id/account_row_acc_name"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent" />
 
-        <EditText
-            android:id="@+id/account_row_acc_amounts"
+        <TextView
+            android:id="@+id/comment_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_gravity="bottom|end"
-            android:layout_weight="0"
-            android:width="0dp"
-            android:foregroundGravity="bottom"
-            android:gravity="bottom|end"
-            android:hint="@string/zero_amount"
+            android:alpha="0.5"
+            android:drawableStart="@drawable/ic_comment_black_24dp"
+            app:layout_constraintBottom_toBottomOf="@+id/comment"
+            app:layout_constraintStart_toStartOf="@+id/ntr_account"
+            app:layout_constraintTop_toBottomOf="@+id/account_row_acc_name" />
+
+        <EditText
+            android:id="@+id/comment"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:hint="@string/transaction_account_comment_hint"
             android:imeOptions="actionNext"
-            android:inputType="numberSigned|numberDecimal|number"
-            android:minWidth="80sp"
-            android:selectAllOnFocus="true"
-            android:textAlignment="viewEnd"
-            app:layout_constraintEnd_toEndOf="parent"
+            android:inputType="text"
+            app:layout_constraintEnd_toEndOf="@+id/ntr_account"
+            app:layout_constraintStart_toEndOf="@id/comment_button"
             app:layout_constraintTop_toBottomOf="@id/account_row_acc_name" />
 
+        <LinearLayout
+            android:id="@+id/amount_layout"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintTop_toBottomOf="@id/comment">
+
+            <EditText
+                android:id="@+id/account_row_acc_amounts"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_gravity="bottom|end"
+                android:layout_weight="0"
+                android:width="0dp"
+                android:foregroundGravity="bottom"
+                android:gravity="bottom|end"
+                android:hint="@string/zero_amount"
+                android:imeOptions="actionNext"
+                android:inputType="numberSigned|numberDecimal|number"
+                android:minWidth="80sp"
+                android:selectAllOnFocus="true"
+                android:textAlignment="viewEnd" />
+        </LinearLayout>
+
     </androidx.constraintlayout.widget.ConstraintLayout>
 
     <FrameLayout
index 968b680b2b8d60d11f911d8c961a58dd32e5a3ac..cd216197974e226ecdf803b0cffa86a62c5f0113 100644 (file)
     <string name="api_post_1_14">Version 1.15 and above</string>
     <string name="api_auto">Detect automaticaly</string>
     <string name="profile_api_version_title">Backend server version</string>
+    <string name="transaction_account_comment_hint">comment</string>
 </resources>