]> git.ktnx.net Git - mobile-ledger.git/commitdiff
fix saving template account removals
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 16 Feb 2021 20:07:39 +0000 (22:07 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:19:43 +0000 (07:19 +0000)
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

app/src/main/java/net/ktnx/mobileledger/dao/TemplateAccountDAO.java
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java

index 8a26bd793da0a3657d5b56c9e39f116ebf94f61e..44b1b201ede77c94aa1bc5dd71cd53e458a8151e 100644 (file)
@@ -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<TemplateAccount> 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);
 }
index 608bb6344cade07fab781509a1cdc37de1ade915..6090d12138cd94a85f0b4fe0e5320466da9474aa 100644 (file)
@@ -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