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
package net.ktnx.mobileledger.dao;
+import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
@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);
}
TemplateAccountDAO taDAO = DB.get()
.getTemplateAccountDAO();
+ taDAO.prepareForSave(mPatternId);
for (int i = 1; i < list.size(); i++) {
final TemplateDetailsItem.AccountRow accRowItem = list.get(i)
.asAccountRowItem();
dbAccount.getId(), dbAccount.getAccountName(),
dbAccount.getAccountComment(), dbAccount.getNegateAmount(), accRowItem));
}
+ taDAO.finishSave(mPatternId);
});
}
}
\ No newline at end of file