]> git.ktnx.net Git - mobile-ledger.git/commitdiff
migrate backgroundTaskCount to an atomic counter and "running" boolean LiveData
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 16 Apr 2019 18:07:10 +0000 (21:07 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 16 Apr 2019 18:07:10 +0000 (21:07 +0300)
app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java
app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index 7a6f7138dc8e7904bfd1c2d74aa20a4e35f325e4..1fdedff0609f898f0ca0ab667cd6e23476993e40 100644 (file)
@@ -31,7 +31,7 @@ import java.util.ArrayList;
 public class CommitAccountsTask
         extends AsyncTask<CommitAccountsTaskParams, Void, ArrayList<LedgerAccount>> {
     protected ArrayList<LedgerAccount> doInBackground(CommitAccountsTaskParams... params) {
-        Data.backgroundTaskCount.incrementAndGet();
+        Data.backgroundTaskStarted();
         ArrayList<LedgerAccount> newList = new ArrayList<>();
         String profile = Data.profile.get().getUuid();
         try {
@@ -59,7 +59,7 @@ public class CommitAccountsTask
             }
         }
         finally {
-            Data.backgroundTaskCount.decrementAndGet();
+            Data.backgroundTaskFinished();
         }
 
         return newList;
index 41a58e980a1b09d817e85b92dcf55d17a73d1665..4e97bdd5826360b407bff2ce3f96858b665d822e 100644 (file)
@@ -36,7 +36,7 @@ public class RefreshDescriptionsTask extends AsyncTask<Void, Void, Void> {
         Log.d("descriptions", "Starting refresh");
         SQLiteDatabase db = MLDB.getDatabase();
 
-        Data.backgroundTaskCount.incrementAndGet();
+        Data.backgroundTaskStarted();
         try {
             db.beginTransaction();
             try {
@@ -64,7 +64,7 @@ public class RefreshDescriptionsTask extends AsyncTask<Void, Void, Void> {
             }
         }
         finally {
-            Data.backgroundTaskCount.decrementAndGet();
+            Data.backgroundTaskFinished();
             Log.d("descriptions", "Refresh done");
         }
 
index 0bf763a981656a3812941186d797d67474ddc6b1..3dd291ec29db9cbebeb9401f2416297d0d6a5045 100644 (file)
@@ -588,7 +588,7 @@ public class RetrieveTransactionsTask
     @Override
     protected String doInBackground(Void... params) {
         MobileLedgerProfile profile = Data.profile.get();
-        Data.backgroundTaskCount.incrementAndGet();
+        Data.backgroundTaskStarted();
         try {
             if (!retrieveAccountList(profile) || !retrieveTransactionList(profile))
                 return retrieveTransactionListLegacy(profile);
@@ -615,7 +615,7 @@ public class RetrieveTransactionsTask
             return "Operation cancelled";
         }
         finally {
-            Data.backgroundTaskCount.decrementAndGet();
+            Data.backgroundTaskFinished();
         }
     }
     private MainActivity getContext() {
index 65f272220557501c2a9dc37fd0b24f741e2e699a..743e8df2be1ce4fb76def73abde67f317d06beac 100644 (file)
@@ -31,7 +31,7 @@ import java.util.ArrayList;
 
 public class UpdateAccountsTask extends AsyncTask<Void, Void, ArrayList<LedgerAccount>> {
     protected ArrayList<LedgerAccount> doInBackground(Void... params) {
-        Data.backgroundTaskCount.incrementAndGet();
+        Data.backgroundTaskStarted();
         try {
             MobileLedgerProfile profile = Data.profile.get();
             String profileUUID = profile.getUuid();
@@ -57,7 +57,7 @@ public class UpdateAccountsTask extends AsyncTask<Void, Void, ArrayList<LedgerAc
         }
         finally {
             Log.d("UAT", "decrementing background task count");
-            Data.backgroundTaskCount.decrementAndGet();
+            Data.backgroundTaskFinished();
         }
     }
 }
index f60ba7e3c109e16c59b8eefd61771707c692be3d..1a158af1786d181017091d254b64b4353c73967a 100644 (file)
@@ -39,7 +39,7 @@ public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
         if (profile == null) return "Profile not configured";
 
         String profile_uuid = profile.getUuid();
-        Data.backgroundTaskCount.incrementAndGet();
+        Data.backgroundTaskStarted();
         try {
             ArrayList<TransactionListItem> newList = new ArrayList<>();
 
@@ -97,7 +97,7 @@ public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
             return String.format("Error parsing stored date '%s'", e.getMessage());
         }
         finally {
-            Data.backgroundTaskCount.decrementAndGet();
+            Data.backgroundTaskFinished();
         }
     }
 }
index 43c6e3bd6315738d132bdd5e9f19cb7b10012d66..f58b62529d1b4adead07c87e236b38cbd0f05fba 100644 (file)
@@ -19,28 +19,41 @@ package net.ktnx.mobileledger.model;
 
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.util.Log;
 
 import net.ktnx.mobileledger.utils.LockHolder;
 import net.ktnx.mobileledger.utils.MLDB;
-import net.ktnx.mobileledger.utils.ObservableAtomicInteger;
 import net.ktnx.mobileledger.utils.ObservableList;
 import net.ktnx.mobileledger.utils.ObservableValue;
 
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import androidx.lifecycle.MutableLiveData;
 
 public final class Data {
-    public static ObservableList<TransactionListItem> transactions = new ObservableList<>(new ArrayList<>());
+    public static ObservableList<TransactionListItem> transactions =
+            new ObservableList<>(new ArrayList<>());
     public static ObservableList<LedgerAccount> accounts = new ObservableList<>(new ArrayList<>());
-    public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0);
+    private static AtomicInteger backgroundTaskCount = new AtomicInteger(0);
+    public static MutableLiveData<Boolean> backgroundTasksRunning = new MutableLiveData<>(false);
     public static MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>();
     public static ObservableValue<MobileLedgerProfile> profile = new ObservableValue<>();
     public static ObservableList<MobileLedgerProfile> profiles =
             new ObservableList<>(new ArrayList<>());
     public static ObservableValue<Boolean> optShowOnlyStarred = new ObservableValue<>();
     public static MutableLiveData<String> accountFilter = new MutableLiveData<>();
+    public static void backgroundTaskStarted() {
+        int cnt = backgroundTaskCount.incrementAndGet();
+        Log.d("data", String.format("background task count is %d after incrementing", cnt));
+        backgroundTasksRunning.postValue(cnt > 0);
+    }
+    public static void backgroundTaskFinished() {
+        int cnt = backgroundTaskCount.decrementAndGet();
+        Log.d("data", String.format("background task count is %d after decrementing", cnt));
+        backgroundTasksRunning.postValue(cnt > 0);
+    }
     public static void setCurrentProfile(MobileLedgerProfile newProfile) {
         MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid());
         profile.set(newProfile);
index ab11d20cae01b12a1c2b9a497a0b0a44ac8d2ae9..093aff396d1a0fa51e232c9f219efed03704a917 100644 (file)
@@ -33,4 +33,9 @@ public class MobileLedgerListFragment extends Fragment {
     protected void themeChanged(Integer counter) {
         swiper.setColorSchemeColors(Colors.primary);
     }
+    public void onBackgroundTaskRunningChanged(Boolean isRunning) {
+        if (mActivity == null) return;
+        if (swiper == null) return;
+        swiper.setRefreshing(isRunning);
+    }
 }
index 94056ab80201c4412d7dea2d8f13677642b18771..19048c91b8d895fe3444d114f617b3d93d4c511c 100644 (file)
@@ -37,7 +37,6 @@ import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.Colors;
 
-import java.util.Observer;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -53,33 +52,13 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     public AccountSummaryAdapter modelAdapter;
     private Menu optMenu;
     private FloatingActionButton fab;
-    private Observer backgroundTaskCountObserver;
-    @Override
-    public void onDestroy() {
-        if (backgroundTaskCountObserver != null) {
-            Log.d("acc", "destroying background task count observer");
-            Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
-        }
-        super.onDestroy();
-    }
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         Log.d("flow", "AccountSummaryFragment.onCreate()");
         setHasOptionsMenu(true);
 
-        if (backgroundTaskCountObserver == null) {
-            Log.d("acc", "creating background task count observer");
-            Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = (o, arg) -> {
-                if (mActivity == null) return;
-                if (swiper == null) return;
-                mActivity.runOnUiThread(() -> {
-                    int cnt = Data.backgroundTaskCount.get();
-                    Log.d("acc", String.format("background task count changed to %d", cnt));
-                    swiper.setRefreshing(cnt > 0);
-                });
-            });
-        }
+        Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
     }
     public void onAttach(Context context) {
         super.onAttach(context);
index 088fd0bd94527f37107de03c1489e8a31c750e5c..d2f4f619b74c019f2d1977e97f33ccfb768aa6d9 100644 (file)
@@ -43,8 +43,6 @@ import net.ktnx.mobileledger.utils.MLDB;
 
 import org.jetbrains.annotations.NotNull;
 
-import java.util.Observer;
-
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.LinearLayoutManager;
@@ -56,28 +54,11 @@ public class TransactionListFragment extends MobileLedgerListFragment {
     private MenuItem menuTransactionListFilter;
     private View vAccountFilter;
     private AutoCompleteTextView accNameFilter;
-    private Observer backgroundTaskCountObserver;
-    @Override
-    public void onDestroy() {
-        if (backgroundTaskCountObserver != null) {
-            Log.d("rtl", "destroying background task count observer");
-            Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
-        }
-        super.onDestroy();
-    }
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
-        if (backgroundTaskCountObserver == null) {
-            Log.d("rtl", "creating background task count observer");
-            Data.backgroundTaskCount.addObserver(
-                    backgroundTaskCountObserver = (o, arg) -> mActivity.runOnUiThread(() -> {
-                        int cnt = Data.backgroundTaskCount.get();
-                        Log.d("trl", String.format("background task count changed to %d", cnt));
-                        swiper.setRefreshing(cnt > 0);
-                    }));
-        }
+        Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
     }
     @Override
     public void onAttach(@NotNull Context context) {