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 {
}
}
finally {
- Data.backgroundTaskCount.decrementAndGet();
+ Data.backgroundTaskFinished();
}
return newList;
Log.d("descriptions", "Starting refresh");
SQLiteDatabase db = MLDB.getDatabase();
- Data.backgroundTaskCount.incrementAndGet();
+ Data.backgroundTaskStarted();
try {
db.beginTransaction();
try {
}
}
finally {
- Data.backgroundTaskCount.decrementAndGet();
+ Data.backgroundTaskFinished();
Log.d("descriptions", "Refresh done");
}
@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);
return "Operation cancelled";
}
finally {
- Data.backgroundTaskCount.decrementAndGet();
+ Data.backgroundTaskFinished();
}
}
private MainActivity getContext() {
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();
}
finally {
Log.d("UAT", "decrementing background task count");
- Data.backgroundTaskCount.decrementAndGet();
+ Data.backgroundTaskFinished();
}
}
}
if (profile == null) return "Profile not configured";
String profile_uuid = profile.getUuid();
- Data.backgroundTaskCount.incrementAndGet();
+ Data.backgroundTaskStarted();
try {
ArrayList<TransactionListItem> newList = new ArrayList<>();
return String.format("Error parsing stored date '%s'", e.getMessage());
}
finally {
- Data.backgroundTaskCount.decrementAndGet();
+ Data.backgroundTaskFinished();
}
}
}
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);
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);
+ }
}
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;
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);
import org.jetbrains.annotations.NotNull;
-import java.util.Observer;
-
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
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) {