From: Damyan Ivanov Date: Tue, 16 Feb 2021 20:07:39 +0000 (+0200) Subject: fix saving template account removals X-Git-Tag: v0.17.0~116 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=467a7741917c1e4c4ba0d167f1e018080786711f;p=mobile-ledger.git 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 --- 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