From 6993727d9b09a792f28e750e9a3aa7eb84e66d05 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 16 Apr 2019 21:07:10 +0300 Subject: [PATCH] migrate backgroundTaskCount to an atomic counter and "running" boolean LiveData --- .../async/CommitAccountsTask.java | 4 ++-- .../async/RefreshDescriptionsTask.java | 4 ++-- .../async/RetrieveTransactionsTask.java | 4 ++-- .../async/UpdateAccountsTask.java | 4 ++-- .../async/UpdateTransactionsTask.java | 4 ++-- .../net/ktnx/mobileledger/model/Data.java | 19 ++++++++++++--- .../ui/MobileLedgerListFragment.java | 5 ++++ .../AccountSummaryFragment.java | 23 +------------------ .../TransactionListFragment.java | 21 +---------------- 9 files changed, 33 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java index 7a6f7138..1fdedff0 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java @@ -31,7 +31,7 @@ import java.util.ArrayList; public class CommitAccountsTask extends AsyncTask> { protected ArrayList doInBackground(CommitAccountsTaskParams... params) { - Data.backgroundTaskCount.incrementAndGet(); + Data.backgroundTaskStarted(); ArrayList 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; diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java index 41a58e98..4e97bdd5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java @@ -36,7 +36,7 @@ public class RefreshDescriptionsTask extends AsyncTask { 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 { } } finally { - Data.backgroundTaskCount.decrementAndGet(); + Data.backgroundTaskFinished(); Log.d("descriptions", "Refresh done"); } diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index 0bf763a9..3dd291ec 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -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() { diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java index 65f27222..743e8df2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java @@ -31,7 +31,7 @@ import java.util.ArrayList; public class UpdateAccountsTask extends AsyncTask> { protected ArrayList 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 { if (profile == null) return "Profile not configured"; String profile_uuid = profile.getUuid(); - Data.backgroundTaskCount.incrementAndGet(); + Data.backgroundTaskStarted(); try { ArrayList newList = new ArrayList<>(); @@ -97,7 +97,7 @@ public class UpdateTransactionsTask extends AsyncTask { return String.format("Error parsing stored date '%s'", e.getMessage()); } finally { - Data.backgroundTaskCount.decrementAndGet(); + Data.backgroundTaskFinished(); } } } diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 43c6e3bd..f58b6252 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -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 transactions = new ObservableList<>(new ArrayList<>()); + public static ObservableList transactions = + new ObservableList<>(new ArrayList<>()); public static ObservableList accounts = new ObservableList<>(new ArrayList<>()); - public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0); + private static AtomicInteger backgroundTaskCount = new AtomicInteger(0); + public static MutableLiveData backgroundTasksRunning = new MutableLiveData<>(false); public static MutableLiveData lastUpdateDate = new MutableLiveData<>(); public static ObservableValue profile = new ObservableValue<>(); public static ObservableList profiles = new ObservableList<>(new ArrayList<>()); public static ObservableValue optShowOnlyStarred = new ObservableValue<>(); public static MutableLiveData 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); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java index ab11d20c..093aff39 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java @@ -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); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index 94056ab8..19048c91 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -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); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 088fd0bd..d2f4f619 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -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) { -- 2.39.5