package net.ktnx.mobileledger.dao;
-import android.os.AsyncTask;
-
import androidx.annotation.NonNull;
import net.ktnx.mobileledger.utils.Misc;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
public abstract class BaseDAO<T> {
+ private final static Executor asyncRunner = Executors.newSingleThreadExecutor();
+ public static void runAsync(Runnable runnable) {
+ asyncRunner.execute(runnable);
+ }
abstract long insertSync(T item);
public void insert(T item) {
- AsyncTask.execute(() -> insertSync(item));
+ asyncRunner.execute(() -> insertSync(item));
}
public void insert(T item, @NonNull OnInsertedReceiver receiver) {
- AsyncTask.execute(() -> {
+ asyncRunner.execute(() -> {
long id = insertSync(item);
Misc.onMainThread(() -> receiver.onInsert(id));
});
abstract void updateSync(T item);
public void update(T item) {
- AsyncTask.execute(() -> updateSync(item));
+ asyncRunner.execute(() -> updateSync(item));
}
public void update(T item, @NonNull Runnable onDone) {
- AsyncTask.execute(() -> {
+ asyncRunner.execute(() -> {
updateSync(item);
Misc.onMainThread(onDone);
});
}
abstract void deleteSync(T item);
public void delete(T item) {
- AsyncTask.execute(() -> deleteSync(item));
+ asyncRunner.execute(() -> deleteSync(item));
}
public void delete(T item, @NonNull Runnable onDone) {
- AsyncTask.execute(() -> {
+ asyncRunner.execute(() -> {
deleteSync(item);
Misc.onMainThread(onDone);
});
package net.ktnx.mobileledger.dao;
-import android.os.AsyncTask;
-
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
return insertSync(item);
}
public void insertLast(Profile item, OnInsertedReceiver onInsertedReceiver) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
long id = insertLastSync(item);
if (onInsertedReceiver != null)
onInsertedReceiver.onInsert(id);
}
}
public void updateOrder(List<Profile> list, Runnable onDone) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
updateOrderSync(list);
if (onDone != null)
onDone.run();
package net.ktnx.mobileledger.dao;
-import android.os.AsyncTask;
-
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
public abstract long insertSync(TemplateHeader item);
public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
insertSync(item);
if (callback != null)
Misc.onMainThread(callback);
public abstract void deleteSync(TemplateHeader item);
public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
deleteSync(item);
Misc.onMainThread(callback);
});
});
}
public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
insertSync(item);
if (callback != null)
Misc.onMainThread(callback);
}
public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable
AsyncResultCallback<TemplateWithAccounts> callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
TemplateWithAccounts src = getTemplateWithAccountsSync(id);
TemplateWithAccounts dup = src.createDuplicate();
dup.header.setName(dup.header.getName());
package net.ktnx.mobileledger.dao;
-import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
}
}
public void storeLast(TransactionWithAccounts rec) {
- AsyncTask.execute(() -> appendSync(rec));
+ BaseDAO.runAsync(() -> appendSync(rec));
}
@androidx.room.Transaction
public void appendSync(TransactionWithAccounts rec) {
package net.ktnx.mobileledger.db;
-import android.os.AsyncTask;
-
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.ColumnInfo;
import androidx.room.Transaction;
import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.dao.OptionDAO;
import net.ktnx.mobileledger.dao.TransactionDAO;
import net.ktnx.mobileledger.utils.Misc;
trnDao.deleteSync(trnDao.getAllForProfileUnorderedSync(id));
}
public void wipeAllData() {
- AsyncTask.execute(this::wipeAllDataSync);
+ BaseDAO.runAsync(this::wipeAllDataSync);
}
}
package net.ktnx.mobileledger.ui.account_summary;
import android.content.res.Resources;
-import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.databinding.AccountListRowBinding;
import net.ktnx.mobileledger.databinding.AccountListSummaryRowBinding;
import net.ktnx.mobileledger.db.Account;
return;
debug("accounts", "Account expander clicked");
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
Account dbo = account.toDBO();
dbo.setExpanded(!dbo.isExpanded());
Logger.debug("accounts",
b.accountRowAmountsExpanderContainer.setVisibility(View.VISIBLE);
}
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
Account dbo = account.toDBO();
DB.get()
.getAccountDAO()
package net.ktnx.mobileledger.ui.activity;
import android.annotation.SuppressLint;
-import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.Nullable;
import net.ktnx.mobileledger.App;
+import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.dao.ProfileDAO;
import net.ktnx.mobileledger.db.DB;
import net.ktnx.mobileledger.db.Profile;
initProfile(profileId);
}
protected void initProfile(long profileId) {
- AsyncTask.execute(() -> initProfileAsync(profileId));
+ BaseDAO.runAsync(() -> initProfileSync(profileId));
}
private void initProfileAsync(long profileId) {
ProfileDAO dao = DB.get()
import android.content.Context;
import android.content.Intent;
import android.database.AbstractCursor;
-import android.os.AsyncTask;
import android.os.Bundle;
import android.os.ParcelFormatException;
import android.util.TypedValue;
import net.ktnx.mobileledger.async.GeneralBackgroundTasks;
import net.ktnx.mobileledger.async.SendTransactionTask;
import net.ktnx.mobileledger.async.TaskCallback;
+import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.dao.TransactionDAO;
import net.ktnx.mobileledger.databinding.ActivityNewTransactionBinding;
import net.ktnx.mobileledger.db.DB;
else {
navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
- AsyncTask.execute(() -> commitToDb((LedgerTransaction) arg));
+ BaseDAO.runAsync(() -> commitToDb((LedgerTransaction) arg));
}
}
public void commitToDb(LedgerTransaction tr) {
if (!model.accountListIsEmpty())
return;
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
String accFilter = mProfile.getPreferredAccountsFilter();
TransactionDAO trDao = DB.get()
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Typeface;
-import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import net.ktnx.mobileledger.BuildConfig;
import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.dao.ProfileDAO;
import net.ktnx.mobileledger.databinding.ProfileDetailBinding;
import net.ktnx.mobileledger.db.DB;
dao.getById(profileId)
.observe(getViewLifecycleOwner(), profile -> {
if (profile != null)
- AsyncTask.execute(() -> {
- dao.deleteSync(profile);
- dao.updateOrderSync(dao.getAllOrderedSync());
- });
+ BaseDAO.runAsync(() -> DB.get()
+ .runInTransaction(() -> {
+ dao.deleteSync(profile);
+ dao.updateOrderSync(dao.getAllOrderedSync());
+ }));
});
final FragmentActivity activity = getActivity();
package net.ktnx.mobileledger.ui.templates;
-import android.os.AsyncTask;
-
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import net.ktnx.mobileledger.BuildConfig;
+import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.dao.TemplateAccountDAO;
import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
import net.ktnx.mobileledger.db.DB;
Logger.debug("flow", "PatternDetailsViewModel.onSavePattern(); model=" + this);
final List<TemplateDetailsItem> list = Objects.requireNonNull(items.getValue());
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
boolean newPattern = mPatternId == null || mPatternId <= 0;
TemplateDetailsItem.Header modelHeader = list.get(0)