]> git.ktnx.net Git - mobile-ledger.git/commitdiff
convert new transaction item 'editable' flat to LiveData
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 10 Nov 2019 17:49:26 +0000 (19:49 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 10 Nov 2019 17:49:45 +0000 (19:49 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index d051fe006a085da605433fc6302ab1bd4d2bef6d..763c508f65beca634d59a43450abce928a09c4e1 100644 (file)
@@ -63,6 +63,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private Observer<String> hintObserver;
     private Observer<Integer> focusedAccountObserver;
     private Observer<Integer> accountCountObserver;
+    private Observer<Boolean> editableObserver;
     private boolean inUpdate = false;
     private boolean syncingData = false;
     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
@@ -148,6 +149,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 syncingData = false;
             }
         };
+        editableObserver = this::setEditable;
         focusedAccountObserver = index -> {
             if ((index != null) && index.equals(getAdapterPosition())) {
                 switch (item.getType()) {
@@ -194,6 +196,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             else tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
         };
     }
+    private void setEditable(Boolean editable) {
+        tvDate.setEnabled(editable);
+        tvDescription.setEnabled(editable);
+        tvAccount.setEnabled(editable);
+        tvAmount.setEnabled(editable);
+    }
     private void beginUpdates() {
         if (inUpdate) throw new RuntimeException("Already in update mode");
         inUpdate = true;
@@ -263,6 +271,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 this.item.stopObservingDate(dateObserver);
                 this.item.stopObservingDescription(descriptionObserver);
                 this.item.stopObservingAmountHint(hintObserver);
+                this.item.stopObservingEditableFlag(editableObserver);
                 this.item.getModel()
                          .stopObservingFocusedItem(focusedAccountObserver);
                 this.item.getModel()
@@ -278,6 +287,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     lHead.setVisibility(View.VISIBLE);
                     lAccount.setVisibility(View.GONE);
                     lPadding.setVisibility(View.GONE);
+                    setEditable(true);
                     break;
                 case transactionRow:
                     LedgerTransactionAccount acc = item.getAccount();
@@ -288,11 +298,13 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     lHead.setVisibility(View.GONE);
                     lAccount.setVisibility(View.VISIBLE);
                     lPadding.setVisibility(View.GONE);
+                    setEditable(true);
                     break;
                 case bottomFiller:
                     lHead.setVisibility(View.GONE);
                     lAccount.setVisibility(View.GONE);
                     lPadding.setVisibility(View.VISIBLE);
+                    setEditable(false);
                     break;
             }
 
@@ -303,6 +315,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 item.observeDate(activity, dateObserver);
                 item.observeDescription(activity, descriptionObserver);
                 item.observeAmountHint(activity, hintObserver);
+                item.observeEditableFlag(activity, editableObserver);
                 item.getModel()
                     .observeFocusedItem(activity, focusedAccountObserver);
                 item.getModel()
index f6ecb443f49fe775413d50b9df536fe0dedd352f..66b01276eb58094a9de66d9035ffae414532383e 100644 (file)
@@ -20,6 +20,7 @@ package net.ktnx.mobileledger.ui.activity;
 import androidx.annotation.NonNull;
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.Observer;
 import androidx.lifecycle.ViewModel;
 
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
@@ -223,32 +224,35 @@ public class NewTransactionModel extends ViewModel {
         private LedgerTransactionAccount account;
         private MutableLiveData<String> amountHint = new MutableLiveData<>();
         private NewTransactionModel model;
-        private boolean editable = true;
+        private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
         public Item(NewTransactionModel model) {
             this.model = model;
             type = ItemType.bottomFiller;
+            editable.setValue(false);
         }
         public Item(NewTransactionModel model, Date date, String description) {
             this.model = model;
             this.type = ItemType.generalData;
             this.date.setValue(date);
             this.description.setValue(description);
+            this.editable.setValue(true);
         }
         public Item(NewTransactionModel model, LedgerTransactionAccount account) {
             this.model = model;
             this.type = ItemType.transactionRow;
             this.account = account;
+            this.editable.setValue(true);
         }
         public NewTransactionModel getModel() {
             return model;
         }
         public boolean isEditable() {
             ensureType(ItemType.transactionRow);
-            return editable;
+            return this.editable.getValue();
         }
         public void setEditable(boolean editable) {
             ensureType(ItemType.transactionRow);
-            this.editable = editable;
+            this.editable.setValue(editable);
         }
         public String getAmountHint() {
             ensureType(ItemType.transactionRow);
@@ -376,5 +380,12 @@ public class NewTransactionModel extends ViewModel {
 
             return String.valueOf(myDay);
         }
+        public void observeEditableFlag(NewTransactionActivity activity,
+                                        Observer<Boolean> observer) {
+            editable.observe(activity, observer);
+        }
+        public void stopObservingEditableFlag(Observer<Boolean> observer) {
+            editable.removeObserver(observer);
+        }
     }
 }