From 467a7741917c1e4c4ba0d167f1e018080786711f Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 16 Feb 2021 22:07:39 +0200 Subject: [PATCH] fix saving template account removals simple idea. account positions are positive numbers. upon save, all positions are set to -1, then all accounts are saved with positions from 1 on, and finally all the accounts with positions still at -1 are deleted --- .../java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java | 7 +++++++ .../ui/templates/TemplateDetailsViewModel.java | 2 ++ 2 files changed, 9 insertions(+) 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 8a26bd79..44b1b201 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java @@ -17,6 +17,7 @@ package net.ktnx.mobileledger.dao; +import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; import androidx.room.Dao; import androidx.room.Delete; @@ -44,4 +45,10 @@ public interface TemplateAccountDAO { @Query("SELECT * FROM template_accounts WHERE id = :id") LiveData getPatternAccountById(Long id); + + @Query("UPDATE template_accounts set position=-1 WHERE template_id=:templateId") + void prepareForSave(@NonNull Long templateId); + + @Query("DELETE FROM template_accounts WHERE position=-1 AND template_id=:templateId") + void finishSave(@NonNull Long templateId); } 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 608bb634..6090d121 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 @@ -163,6 +163,7 @@ public class TemplateDetailsViewModel extends ViewModel { TemplateAccountDAO taDAO = DB.get() .getTemplateAccountDAO(); + taDAO.prepareForSave(mPatternId); for (int i = 1; i < list.size(); i++) { final TemplateDetailsItem.AccountRow accRowItem = list.get(i) .asAccountRowItem(); @@ -181,6 +182,7 @@ public class TemplateDetailsViewModel extends ViewModel { dbAccount.getId(), dbAccount.getAccountName(), dbAccount.getAccountComment(), dbAccount.getNegateAmount(), accRowItem)); } + taDAO.finishSave(mPatternId); }); } } \ No newline at end of file -- 2.39.5