From bd7fad9a3e9436efc7cfe505504da0c86eab72e5 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 22 Apr 2021 04:19:42 +0000 Subject: [PATCH] BaseDAO: async operation variants without callbacks --- .../net/ktnx/mobileledger/dao/BaseDAO.java | 28 +++++++++++-------- .../ui/profiles/ProfileDetailFragment.java | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java index b68ea515..b6b23d18 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java @@ -21,32 +21,38 @@ import android.os.AsyncTask; import android.os.Handler; import android.os.Looper; -import androidx.annotation.Nullable; +import androidx.annotation.NonNull; -abstract class BaseDAO { +public abstract class BaseDAO { abstract long insertSync(T item); - public void insert(T item, @Nullable OnInsertedReceiver receiver) { + public void insert(T item) { + AsyncTask.execute(() -> insertSync(item)); + } + public void insert(T item, @NonNull OnInsertedReceiver receiver) { AsyncTask.execute(() -> { long id = insertSync(item); - if (receiver != null) - new Handler(Looper.getMainLooper()).post(() -> receiver.onInsert(id)); + new Handler(Looper.getMainLooper()).post(() -> receiver.onInsert(id)); }); } abstract void updateSync(T item); - public void update(T item, @Nullable Runnable onDone) { + public void update(T item) { + AsyncTask.execute(() -> updateSync(item)); + } + public void update(T item, @NonNull Runnable onDone) { AsyncTask.execute(() -> { updateSync(item); - if (onDone != null) - new Handler(Looper.getMainLooper()).post(onDone); + new Handler(Looper.getMainLooper()).post(onDone); }); } abstract void deleteSync(T item); - public void delete(T item, @Nullable Runnable onDone) { + public void delete(T item) { + AsyncTask.execute(() -> deleteSync(item)); + } + public void delete(T item, @NonNull Runnable onDone) { AsyncTask.execute(() -> { deleteSync(item); - if (onDone != null) - new Handler(Looper.getMainLooper()).post(onDone); + new Handler(Looper.getMainLooper()).post(onDone); }); } interface OnInsertedReceiver { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 9b0b43a1..547981e1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -399,7 +399,7 @@ public class ProfileDetailFragment extends Fragment { Profile profile = new Profile(); model.updateProfile(profile); if (profile.getId() > 0) { - dao.update(profile, null); + dao.update(profile); debug("profiles", "profile stored in DB"); // debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue())); } -- 2.39.5