From 70efe6225e8cd946b8bf33c795ad78952a81e344 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 5 Feb 2021 08:16:56 +0200 Subject: [PATCH] async template DB manipulation routines --- .../mobileledger/dao/TemplateAccountDAO.java | 11 +-- .../mobileledger/dao/TemplateHeaderDAO.java | 89 +++++++++++++++++-- .../templates/TemplateDetailsViewModel.java | 8 +- 3 files changed, 89 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java index 4fd1d204..8a26bd79 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java @@ -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> getTemplateAccounts(Long template_id); @Query("SELECT * FROM template_accounts WHERE id = :id") LiveData getPatternAccountById(Long id); - -// not useful for now -// @Transaction -// @Query("SELECT * FROM patterns") -// List getPatternsWithAccounts(); } 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 faef98fb..4ffd08b4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java @@ -17,7 +17,14 @@ 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> getTemplates(); + public abstract LiveData> getTemplates(); @Query("SELECT * FROM templates WHERE id = :id") - LiveData getTemplate(Long id); + public abstract LiveData getTemplate(Long id); + + public void getTemplateAsync(@NonNull Long id, + @NonNull AsyncResultCallback callback) { + LiveData resultReceiver = getTemplate(id); + resultReceiver.observeForever(new Observer() { + @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 getTemplateWithAccounts(Long id); + public abstract LiveData 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 callback) { + LiveData resultReceiver = getTemplateWithAccounts(id); + resultReceiver.observeForever(new Observer() { + @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); + } + }); + } + } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java index 8a5300ca..a5f5e0f9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java @@ -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", -- 2.39.5