]> git.ktnx.net Git - mobile-ledger.git/commitdiff
async template DB manipulation routines
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 5 Feb 2021 06:16:56 +0000 (08:16 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 5 Feb 2021 13:16:26 +0000 (13:16 +0000)
app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java
app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java

index 4fd1d2045b24dfe7800806c1e128be7aaabbffba..8a26bd793da0a3657d5b56c9e39f116ebf94f61e 100644 (file)
@@ -31,22 +31,17 @@ import java.util.List;
 @Dao
 public interface TemplateAccountDAO {
     @Insert
-    Long insert(TemplateAccount item);
+    Long insertSync(TemplateAccount item);
 
     @Update
-    void update(TemplateAccount... items);
+    void updateSync(TemplateAccount... items);
 
     @Delete
-    void delete(TemplateAccount item);
+    void deleteSync(TemplateAccount item);
 
     @Query("SELECT * FROM template_accounts WHERE template_id=:template_id")
     LiveData<List<TemplateAccount>> getTemplateAccounts(Long template_id);
 
     @Query("SELECT * FROM template_accounts WHERE id = :id")
     LiveData<TemplateAccount> getPatternAccountById(Long id);
-
-//    not useful for now
-//    @Transaction
-//    @Query("SELECT * FROM patterns")
-//    List<PatternWithAccounts> getPatternsWithAccounts();
 }
index faef98fb95f65655be51eba04c1b5e2b0f68dcba..4ffd08b44f556b2e0376a4ba9cfd944f02c99f70 100644 (file)
 
 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;
 import androidx.lifecycle.LiveData;
+import androidx.lifecycle.Observer;
 import androidx.room.Dao;
 import androidx.room.Delete;
 import androidx.room.Insert;
@@ -25,29 +32,97 @@ import androidx.room.Query;
 import androidx.room.Transaction;
 import androidx.room.Update;
 
+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 java.util.List;
 
 @Dao
-public interface TemplateHeaderDAO {
+public abstract class TemplateHeaderDAO {
     @Insert()
-    long insert(TemplateHeader item);
+    public abstract long insertSync(TemplateHeader item);
+
+    public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
+        AsyncTask.execute(() -> {
+            insertSync(item);
+            if (callback != null) {
+                new Handler(Looper.getMainLooper()).post(callback);
+            }
+        });
+    }
 
     @Update
-    void update(TemplateHeader... items);
+    public abstract void updateSync(TemplateHeader... items);
 
     @Delete
-    void delete(TemplateHeader item);
+    public abstract void deleteSync(TemplateHeader item);
+
+    public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
+        AsyncTask.execute(() -> {
+            deleteSync(item);
+            new Handler(Looper.getMainLooper()).post(callback);
+        });
+    }
 
     @Query("SELECT * FROM templates ORDER BY UPPER(name)")
-    LiveData<List<TemplateHeader>> getTemplates();
+    public abstract LiveData<List<TemplateHeader>> getTemplates();
 
     @Query("SELECT * FROM templates WHERE id = :id")
-    LiveData<TemplateHeader> getTemplate(Long id);
+    public abstract LiveData<TemplateHeader> getTemplate(Long id);
+
+    public void getTemplateAsync(@NonNull Long id,
+                                 @NonNull AsyncResultCallback<TemplateHeader> callback) {
+        LiveData<TemplateHeader> resultReceiver = getTemplate(id);
+        resultReceiver.observeForever(new Observer<TemplateHeader>() {
+            @Override
+            public void onChanged(TemplateHeader h) {
+                if (h == null)
+                    return;
+
+                resultReceiver.removeObserver(this);
+                callback.onResult(h);
+            }
+        });
+    }
 
     @Transaction
     @Query("SELECT * FROM templates WHERE id = :id")
-    LiveData<TemplateWithAccounts> getTemplateWithAccounts(Long id);
+    public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(Long id);
+
+    @Transaction
+    public void insertSync(TemplateWithAccounts templateWithAccounts) {
+        long template_id = insertSync(templateWithAccounts.header);
+        for (TemplateAccount acc : templateWithAccounts.accounts) {
+            acc.setTemplateId(template_id);
+            DB.get()
+              .getTemplateAccountDAO()
+              .insertSync(acc);
+        }
+    }
+
+    public void getTemplateWitAccountsAsync(@NonNull Long id, @NonNull
+            AsyncResultCallback<TemplateWithAccounts> callback) {
+        LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
+        resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
+            @Override
+            public void onChanged(TemplateWithAccounts result) {
+                if (result == null)
+                    return;
+
+                resultReceiver.removeObserver(this);
+                callback.onResult(result);
+            }
+        });
+    }
+    public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
+        AsyncTask.execute(() -> {
+            insertSync(item);
+            if (callback != null) {
+                new Handler(Looper.getMainLooper()).post(callback);
+            }
+        });
+    }
+
 }
index 8a5300ca461df97ffd885a4e0241e600ec329db0..a5f5e0f9bac7e59e8967af0ca791965feb5c0f54 100644 (file)
@@ -144,10 +144,10 @@ public class TemplateDetailsViewModel extends ViewModel {
             TemplateHeader dbHeader = modelHeader.toDBO();
             if (newPattern) {
                 dbHeader.setId(null);
-                dbHeader.setId(mPatternId = headerDAO.insert(dbHeader));
+                dbHeader.setId(mPatternId = headerDAO.insertSync(dbHeader));
             }
             else
-                headerDAO.update(dbHeader);
+                headerDAO.updateSync(dbHeader);
 
             Logger.debug("pattern-db",
                     String.format(Locale.US, "Stored pattern header %d, item=%s", dbHeader.getId(),
@@ -164,10 +164,10 @@ public class TemplateDetailsViewModel extends ViewModel {
                 dbAccount.setPosition(i);
                 if (newPattern) {
                     dbAccount.setId(null);
-                    dbAccount.setId(taDAO.insert(dbAccount));
+                    dbAccount.setId(taDAO.insertSync(dbAccount));
                 }
                 else
-                    taDAO.update(dbAccount);
+                    taDAO.updateSync(dbAccount);
 
                 Logger.debug("pattern-db", String.format(Locale.US,
                         "Stored pattern account %d, account=%s, comment=%s, neg=%s, item=%s",