]> git.ktnx.net Git - mobile-ledger.git/commitdiff
add commodity support to template editor
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 6 May 2021 19:42:14 +0000 (22:42 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 6 May 2021 19:42:14 +0000 (22:42 +0300)
app/src/main/java/net/ktnx/mobileledger/model/TemplateDetailsItem.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsAdapter.java
app/src/main/res/layout/template_details_account.xml
app/src/main/res/values-bg/strings.xml
app/src/main/res/values/strings.xml

index 67d6a4f374b5b05bcd9d01116e94bbe9e5afbf8a..9a8f04cebf1a4ea9eee17039d12411464bfcc8e1 100644 (file)
@@ -384,6 +384,7 @@ abstract public class TemplateDetailsItem {
         public boolean hasLiteralAccountComment() {
             return accountComment.hasLiteralValue();
         }
+        public boolean hasLiteralCurrency() { return currency.hasLiteralValue(); }
         public boolean equalContents(AccountRow o) {
             if (position != o.position) {
                 Logger.debug("cmpAcc",
@@ -398,6 +399,9 @@ abstract public class TemplateDetailsItem {
         public void switchToLiteralAmount() {
             amount.switchToLiteral();
         }
+        public void switchToLiteralCurrency() {
+            currency.switchToLiteral();
+        }
         public void switchToLiteralAccountName() {
             accountName.switchToLiteral();
         }
@@ -426,6 +430,14 @@ abstract public class TemplateDetailsItem {
                 result.setNegateAmount(negateAmount ? true : null);
             }
 
+            if (currency.hasLiteralValue()) {
+                net.ktnx.mobileledger.db.Currency c = currency.getValue();
+                result.setCurrency((c == null) ? null : c.getId());
+            }
+            else {
+                result.setCurrencyMatchGroup(currency.getMatchGroup());
+            }
+
             return result;
         }
         public boolean isEmpty() {
index eae2ba33a56cdddfe5b9d6ef3cd6657b739493e0..ccf1b813fed8b58cd2fcf7f5fb2f56b1348de043 100644 (file)
@@ -30,6 +30,7 @@ import android.widget.TextView;
 
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.AppCompatActivity;
+import androidx.lifecycle.LifecycleOwner;
 import androidx.recyclerview.widget.AsyncListDiffer;
 import androidx.recyclerview.widget.DiffUtil;
 import androidx.recyclerview.widget.ItemTouchHelper;
@@ -40,8 +41,10 @@ import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.databinding.TemplateDetailsAccountBinding;
 import net.ktnx.mobileledger.databinding.TemplateDetailsHeaderBinding;
 import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
+import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.TemplateDetailsItem;
+import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
 import net.ktnx.mobileledger.ui.HelpDialog;
 import net.ktnx.mobileledger.ui.QR;
 import net.ktnx.mobileledger.ui.TemplateDetailSourceSelectorFragment;
@@ -277,7 +280,7 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter
 
     private enum HeaderDetail {DESCRIPTION, COMMENT, DATE_YEAR, DATE_MONTH, DATE_DAY}
 
-    private enum AccDetail {ACCOUNT, COMMENT, AMOUNT}
+    private enum AccDetail {ACCOUNT, COMMENT, AMOUNT, CURRENCY}
 
     public abstract static class ViewHolder extends RecyclerView.ViewHolder {
         ViewHolder(@NonNull View itemView) {
@@ -818,7 +821,22 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter
                     b.templateDetailsNegateAmountText.setVisibility(View.VISIBLE);
                 }
 
-                // TODO: currency
+                if (accRow.hasLiteralCurrency()) {
+                    b.templateDetailsAccountCurrencySource.setText(
+                            R.string.template_details_source_literal);
+                    net.ktnx.mobileledger.db.Currency c = accRow.getCurrency();
+                    if (c == null)
+                        b.templateDetailsAccountCurrency.setText(R.string.btn_no_currency);
+                    else
+                        b.templateDetailsAccountCurrency.setText(c.getName());
+                    b.templateDetailsAccountCurrency.setVisibility(View.VISIBLE);
+                }
+                else {
+                    b.templateDetailsAccountCurrencySource.setText(
+                            String.format(Locale.US, groupNoText, accRow.getCurrencyMatchGroup(),
+                                    getMatchGroupText(accRow.getCurrencyMatchGroup())));
+                    b.templateDetailsAccountCurrency.setVisibility(View.GONE);
+                }
 
                 b.templateAccountNameSourceLabel.setOnClickListener(
                         v -> selectAccountRowDetailSource(v, AccDetail.ACCOUNT));
@@ -832,6 +850,32 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter
                         v -> selectAccountRowDetailSource(v, AccDetail.AMOUNT));
                 b.templateDetailsAccountAmountSource.setOnClickListener(
                         v -> selectAccountRowDetailSource(v, AccDetail.AMOUNT));
+                b.templateDetailsAccountCurrencySource.setOnClickListener(
+                        v -> selectAccountRowDetailSource(v, AccDetail.CURRENCY));
+                b.templateAccountCurrencySourceLabel.setOnClickListener(
+                        v -> selectAccountRowDetailSource(v, AccDetail.CURRENCY));
+                if (accRow.hasLiteralCurrency())
+                    b.templateDetailsAccountCurrency.setOnClickListener(v -> {
+                        CurrencySelectorFragment cpf = CurrencySelectorFragment.newInstance(
+                                CurrencySelectorFragment.DEFAULT_COLUMN_COUNT, false);
+                        cpf.setOnCurrencySelectedListener(text -> {
+                            if (text == null) {
+                                b.templateDetailsAccountCurrency.setText(R.string.btn_no_currency);
+                                accRow.setCurrency(null);
+                            }
+                            else {
+                                b.templateDetailsAccountCurrency.setText(text);
+                                DB.get()
+                                  .getCurrencyDAO()
+                                  .getByName(text)
+                                  .observe((LifecycleOwner) b.getRoot()
+                                                             .getContext(), accRow::setCurrency);
+                            }
+                        });
+                        cpf.show(
+                                ((TemplatesActivity) b.templateDetailsAccountCurrency.getContext()).getSupportFragmentManager(),
+                                "currency-selector");
+                    });
             }
             finally {
                 enableUpdatePropagation();
@@ -861,6 +905,9 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter
                         case AMOUNT:
                             accRow.switchToLiteralAmount();
                             break;
+                        case CURRENCY:
+                            accRow.switchToLiteralCurrency();
+                            break;
                         default:
                             throw new IllegalStateException("Unexpected detail " + detail);
                     }
@@ -876,6 +923,9 @@ class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter
                         case AMOUNT:
                             accRow.setAmountMatchGroup(group);
                             break;
+                        case CURRENCY:
+                            accRow.setCurrencyMatchGroup(group);
+                            break;
                         default:
                             throw new IllegalStateException("Unexpected detail " + detail);
                     }
index 10ff6f70595591ef86ae77c228c186a7e443d11f..e09c6b014f279f0e03c6b150c08adcf9d5e88d34 100644 (file)
             android:selectAllOnFocus="true"
             />
     </com.google.android.material.textfield.TextInputLayout>
+
     <TextView
         android:id="@+id/template_details_negate_amount_label"
         android:layout_width="0dp"
         app:layout_constraintTop_toTopOf="@id/template_details_negate_amount_label"
         />
 
+    <TextView
+        android:id="@+id/template_account_currency_source_label"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_marginTop="@dimen/text_margin"
+        android:text="@string/account_currency_source_label"
+        android:textAppearance="?attr/textAppearanceListItem"
+        app:layout_constraintBottom_toTopOf="@+id/template_details_account_currency_source"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/negate_amount_switch"
+        />
+    <TextView
+        android:id="@+id/template_details_account_currency_source"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:textAppearance="?attr/textAppearanceListItemSecondary"
+        app:layout_constraintBottom_toTopOf="@+id/template_details_account_currency"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/template_account_currency_source_label"
+        />
+    <TextView
+        android:id="@+id/template_details_account_currency"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:layout_marginVertical="@dimen/half_text_margin"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/template_details_account_currency_source"
+        />
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
index af958a08d7b24d4f97604734cd982ba65cb08ecf..9b49a7fe13538ea66e3d354ed386a5eb28ad97c4 100644 (file)
     <string name="template_list_help_title">Макети</string>
     <string name="template_details_template_params_label">Параметри на макета</string>
     <string name="template_params_help_description">Помощна информация за пааметрите на макета</string>
+    <string name="account_currency_source_label">Източник на валутата</string>
 </resources>
index 4e928c7fd4de2b1efd00dbef1553470427f4ce41..49b483a019041d00a478e6a9a7d061fd096c8bc1 100644 (file)
     <string name="template_list_help_title">Templates</string>
     <string name="template_details_template_params_label">Template parameters</string>
     <string name="template_params_help_description">Show help on template parameters</string>
+    <string name="account_currency_source_label">Commodity source</string>
 </resources>