]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
provide a common routine for running something on the main thread
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TemplateHeaderDAO.java
index 4ffd08b44f556b2e0376a4ba9cfd944f02c99f70..f72104b31514c5a4fbbfac813662c3588cb285f2 100644 (file)
@@ -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,11 +60,11 @@ 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);
         });
     }
 
-    @Query("SELECT * FROM templates ORDER BY UPPER(name)")
+    @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
     public abstract LiveData<List<TemplateHeader>> getTemplates();
 
     @Query("SELECT * FROM templates WHERE id = :id")
@@ -89,7 +87,11 @@ public abstract class TemplateHeaderDAO {
 
     @Transaction
     @Query("SELECT * FROM templates WHERE id = :id")
-    public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(Long id);
+    public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
+
+    @Transaction
+    @Query("SELECT * FROM templates WHERE id = :id")
+    public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
 
     @Transaction
     public void insertSync(TemplateWithAccounts templateWithAccounts) {
@@ -102,7 +104,7 @@ public abstract class TemplateHeaderDAO {
         }
     }
 
-    public void getTemplateWitAccountsAsync(@NonNull Long id, @NonNull
+    public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
             AsyncResultCallback<TemplateWithAccounts> callback) {
         LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
         resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
@@ -119,9 +121,25 @@ 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
+            AsyncResultCallback<TemplateWithAccounts> callback) {
+        AsyncTask.execute(() -> {
+            TemplateWithAccounts src = getTemplateWithAccountsSync(id);
+            TemplateWithAccounts dup = src.createDuplicate();
+            dup.header.setName(dup.header.getName());
+            dup.header.setId(insertSync(dup.header));
+            TemplateAccountDAO accDao = DB.get()
+                                          .getTemplateAccountDAO();
+            for (TemplateAccount dupAcc : dup.accounts) {
+                dupAcc.setTemplateId(dup.header.getId());
+                dupAcc.setId(accDao.insertSync(dupAcc));
             }
+            if (callback != null)
+                Misc.onMainThread(() -> callback.onResult(dup));
         });
     }