From: Damyan Ivanov Date: Thu, 22 Apr 2021 04:19:42 +0000 (+0000) Subject: BaseDAO: async operation variants without callbacks X-Git-Tag: v0.18.0~58 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=bd7fad9a3e9436efc7cfe505504da0c86eab72e5;p=mobile-ledger.git BaseDAO: async operation variants without callbacks --- 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())); }