]> git.ktnx.net Git - mobile-ledger.git/commitdiff
add progress while retrieving previous transaction
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 3 May 2020 08:12:09 +0000 (11:12 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 3 May 2020 08:12:09 +0000 (11:12 +0300)
the search for the right transaction may take some time

app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
app/src/main/res/layout/fragment_new_transaction.xml
app/src/main/res/layout/new_transaction_row.xml

index ab2f40325bbf9788098598aff998d2dbaded335f..b63ce8e9adeab74c7fcc1a5c814e1983ec3babfd 100644 (file)
@@ -30,7 +30,7 @@ import android.view.inputmethod.EditorInfo;
 import android.widget.AutoCompleteTextView;
 import android.widget.EditText;
 import android.widget.FrameLayout;
-import android.widget.LinearLayout;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
@@ -75,7 +75,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private AutoCompleteTextView tvAccount;
     private TextView tvComment;
     private EditText tvAmount;
-    private LinearLayout lHead;
+    private ViewGroup lHead;
     private ViewGroup lAccount;
     private FrameLayout lPadding;
     private MobileLedgerProfile mProfile;
@@ -92,6 +92,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private Observer<Currency> currencyObserver;
     private Observer<Boolean> showCurrencyObserver;
     private Observer<String> commentObserver;
+    private Observer<Boolean> busyFlagObserver;
     private boolean inUpdate = false;
     private boolean syncingData = false;
     private View commentButton;
@@ -110,6 +111,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         lAccount = itemView.findViewById(R.id.ntr_account);
         lPadding = itemView.findViewById(R.id.ntr_padding);
         View commentLayout = itemView.findViewById(R.id.comment_layout);
+        ProgressBar p = itemView.findViewById(R.id.progressBar);
 
         tvDescription.setNextFocusForwardId(View.NO_ID);
         tvAccount.setNextFocusForwardId(View.NO_ID);
@@ -387,6 +389,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     ((focusedView != tvComment) && Misc.isEmptyOrNull(comment)) ? View.INVISIBLE
                                                                                 : View.VISIBLE);
         };
+
+        busyFlagObserver = isBusy -> {p.setVisibility(isBusy ? View.VISIBLE : View.INVISIBLE);};
     }
     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
         ConstraintLayout.LayoutParams amountLP =
@@ -583,6 +587,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 this.item.stopObservingCurrency(currencyObserver);
                 this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
                 this.item.stopObservingComment(commentObserver);
+                this.item.getModel().stopObservingBusyFlag(busyFlagObserver);
 
                 this.item = null;
             }
@@ -635,6 +640,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     case generalData:
                         item.observeDate(activity, dateObserver);
                         item.observeDescription(activity, descriptionObserver);
+                        item.getModel().observeBusyFlag(activity, busyFlagObserver);
                         break;
                     case transactionRow:
                         item.observeAmountHint(activity, hintObserver);
index 41f2af4034bd4caaed178fff69a8516821dea654..00e44508a33d4ac0856aa39b3c2485c8dbcaf252 100644 (file)
@@ -37,6 +37,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.Locale;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -56,6 +57,11 @@ public class NewTransactionModel extends ViewModel {
     private boolean observingDataProfile;
     private Observer<MobileLedgerProfile> profileObserver =
             profile -> showCurrency.postValue(profile.getShowCommodityByDefault());
+    private final AtomicInteger busyCounter = new AtomicInteger(0);
+    private final MutableLiveData<Boolean> busyFlag = new MutableLiveData<>(false);
+    void observeBusyFlag(LifecycleOwner owner, Observer<? super Boolean> observer) {
+        busyFlag.observe(owner, observer);
+    }
     void observeDataProfile(LifecycleOwner activity) {
         if (!observingDataProfile)
             Data.profile.observe(activity, profileObserver);
@@ -187,6 +193,17 @@ public class NewTransactionModel extends ViewModel {
     void toggleCurrencyVisible() {
         showCurrency.setValue(!showCurrency.getValue());
     }
+    void stopObservingBusyFlag(Observer<Boolean> observer) {
+        busyFlag.removeObserver(observer);
+    }
+    void incrementBusyCounter() {
+        int newValue = busyCounter.incrementAndGet();
+        if (newValue == 1) busyFlag.postValue(true);
+    }
+    void decrementBusyCounter() {
+        int newValue = busyCounter.decrementAndGet();
+        if (newValue == 0) busyFlag.postValue(false);
+    }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     enum FocusedElement {Account, Comment, Amount}
index a2bdb7ebdbb4fbb65f424406a6752df648bc7b5c..5981f8e7fec85a895a0e45093364913a320204a7 100644 (file)
   -->
 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
-    <androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
+    <androidx.constraintlayout.widget.ConstraintLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="net.ktnx.mobileledger.ui.activity.NewTransactionActivity">
index c40970883330d6fa173cd79e8acb85755ff4fcf7..782955db4da91397706691963b5337f497667cd0 100644 (file)
     android:animateLayoutChanges="false"
     android:orientation="vertical">
 
-    <LinearLayout
+    <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/ntr_data"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content">
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
 
         <EditText
             android:id="@+id/new_transaction_date"
@@ -45,7 +46,6 @@
             android:textAlignment="gravity"
             android:textCursorDrawable="@android:color/transparent"
             app:layout_constrainedHeight="true"
-            app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintHorizontal_weight="8"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent"
@@ -53,7 +53,7 @@
 
         <net.ktnx.mobileledger.ui.AutoCompleteTextViewWithClear
             android:id="@+id/new_transaction_description"
-            android:layout_width="match_parent"
+            android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom"
             android:layout_marginStart="8dp"
             android:nextFocusUp="@+id/new_transaction_date"
             android:selectAllOnFocus="false"
             android:singleLine="true"
-            app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintHorizontal_weight="30"
-            app:layout_constraintStart_toEndOf="@+id/new_transaction_date"
+            app:layout_constraintStart_toEndOf="@id/new_transaction_date"
             app:layout_constraintTop_toTopOf="parent" />
-    </LinearLayout>
+
+        <ProgressBar
+            android:id="@+id/progressBar"
+            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:indeterminate="true"
+            android:indeterminateBehavior="cycle"
+            android:orientation="horizontal"
+            android:visibility="invisible"
+            app:layout_constraintTop_toBottomOf="@id/new_transaction_date"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintEnd_toEndOf="parent
+"/>
+    </androidx.constraintlayout.widget.ConstraintLayout>
 
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/ntr_account"