From 40d4f7ebb9085a58133cf47eacf989ed35fa0b4e Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 24 Apr 2021 17:08:38 +0000 Subject: [PATCH] provide a common routine for running something on the main thread --- .../java/net/ktnx/mobileledger/dao/BaseDAO.java | 10 +++++----- .../mobileledger/dao/TemplateHeaderDAO.java | 17 +++++++---------- .../account_summary/AccountSummaryAdapter.java | 4 +--- .../ui/new_transaction/NewTransactionModel.java | 6 ++---- .../java/net/ktnx/mobileledger/utils/Misc.java | 5 +++++ 5 files changed, 20 insertions(+), 22 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 b6b23d18..b2d938c5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java @@ -18,11 +18,11 @@ package net.ktnx.mobileledger.dao; import android.os.AsyncTask; -import android.os.Handler; -import android.os.Looper; import androidx.annotation.NonNull; +import net.ktnx.mobileledger.utils.Misc; + public abstract class BaseDAO { abstract long insertSync(T item); public void insert(T item) { @@ -31,7 +31,7 @@ public abstract class BaseDAO { public void insert(T item, @NonNull OnInsertedReceiver receiver) { AsyncTask.execute(() -> { long id = insertSync(item); - new Handler(Looper.getMainLooper()).post(() -> receiver.onInsert(id)); + Misc.onMainThread(() -> receiver.onInsert(id)); }); } @@ -42,7 +42,7 @@ public abstract class BaseDAO { public void update(T item, @NonNull Runnable onDone) { AsyncTask.execute(() -> { updateSync(item); - new Handler(Looper.getMainLooper()).post(onDone); + Misc.onMainThread(onDone); }); } abstract void deleteSync(T item); @@ -52,7 +52,7 @@ public abstract class BaseDAO { public void delete(T item, @NonNull Runnable onDone) { AsyncTask.execute(() -> { deleteSync(item); - new Handler(Looper.getMainLooper()).post(onDone); + Misc.onMainThread(onDone); }); } interface OnInsertedReceiver { diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java index b14502c7..f72104b3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java @@ -18,8 +18,6 @@ package net.ktnx.mobileledger.dao; import android.os.AsyncTask; -import android.os.Handler; -import android.os.Looper; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -36,6 +34,7 @@ import net.ktnx.mobileledger.db.DB; import net.ktnx.mobileledger.db.TemplateAccount; import net.ktnx.mobileledger.db.TemplateHeader; import net.ktnx.mobileledger.db.TemplateWithAccounts; +import net.ktnx.mobileledger.utils.Misc; import java.util.List; @@ -47,9 +46,8 @@ public abstract class TemplateHeaderDAO { public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) { AsyncTask.execute(() -> { insertSync(item); - if (callback != null) { - new Handler(Looper.getMainLooper()).post(callback); - } + if (callback != null) + Misc.onMainThread(callback); }); } @@ -62,7 +60,7 @@ public abstract class TemplateHeaderDAO { public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) { AsyncTask.execute(() -> { deleteSync(item); - new Handler(Looper.getMainLooper()).post(callback); + Misc.onMainThread(callback); }); } @@ -123,9 +121,8 @@ public abstract class TemplateHeaderDAO { public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) { AsyncTask.execute(() -> { insertSync(item); - if (callback != null) { - new Handler(Looper.getMainLooper()).post(callback); - } + if (callback != null) + Misc.onMainThread(callback); }); } public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable @@ -142,7 +139,7 @@ public abstract class TemplateHeaderDAO { dupAcc.setId(accDao.insertSync(dupAcc)); } if (callback != null) - new Handler(Looper.getMainLooper()).post(() -> callback.onResult(dup)); + Misc.onMainThread(() -> callback.onResult(dup)); }); } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java index 3a613075..6b8d0930 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java @@ -19,8 +19,6 @@ package net.ktnx.mobileledger.ui.account_summary; import android.content.res.Resources; import android.os.AsyncTask; -import android.os.Handler; -import android.os.Looper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -162,7 +160,7 @@ public class AccountSummaryAdapter extends RecyclerView.Adapter newList) { - new Handler(Looper.getMainLooper()).post(() -> listDiffer.submitList(newList)); + Misc.onMainThread(() -> listDiffer.submitList(newList)); } static class Change { static final int NAME = 1; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java index 7ff061a6..ad9c75cd 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java @@ -18,8 +18,6 @@ package net.ktnx.mobileledger.ui.new_transaction; import android.annotation.SuppressLint; -import android.os.Handler; -import android.os.Looper; import android.text.TextUtils; import androidx.annotation.NonNull; @@ -301,7 +299,7 @@ public class NewTransactionModel extends ViewModel { newItems.add(accRow); } - new Handler(Looper.getMainLooper()).post(() -> replaceItems(newItems)); + Misc.onMainThread(() -> replaceItems(newItems)); }); } private int extractIntFromMatches(MatchResult m, Integer group, Integer literal) { @@ -523,7 +521,7 @@ public class NewTransactionModel extends ViewModel { moveItemLast(newList, singlePositiveIndex); } - new Handler(Looper.getMainLooper()).post(() -> { + Misc.onMainThread(() -> { setItems(newList); noteFocusChanged(1, FocusedElement.Amount); }); diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java b/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java index 197fe6d0..349a04a5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java @@ -19,6 +19,8 @@ package net.ktnx.mobileledger.utils; import android.app.Activity; import android.content.res.Configuration; +import android.os.Handler; +import android.os.Looper; import android.text.Editable; import android.view.WindowManager; @@ -102,4 +104,7 @@ public class Misc { return a.equals(b); } + public static void onMainThread(Runnable r) { + new Handler(Looper.getMainLooper()).post(r); + } } -- 2.39.5