2 * Copyright © 2022 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.dao;
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.Observer;
24 import androidx.room.Dao;
25 import androidx.room.Delete;
26 import androidx.room.Insert;
27 import androidx.room.Query;
28 import androidx.room.Transaction;
29 import androidx.room.Update;
31 import net.ktnx.mobileledger.db.DB;
32 import net.ktnx.mobileledger.db.TemplateAccount;
33 import net.ktnx.mobileledger.db.TemplateHeader;
34 import net.ktnx.mobileledger.db.TemplateWithAccounts;
35 import net.ktnx.mobileledger.utils.Misc;
37 import java.util.List;
40 public abstract class TemplateHeaderDAO {
42 public abstract long insertSync(TemplateHeader item);
44 public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
45 BaseDAO.runAsync(() -> {
48 Misc.onMainThread(callback);
53 public abstract void updateSync(TemplateHeader... items);
56 public abstract void deleteSync(TemplateHeader item);
58 public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
59 BaseDAO.runAsync(() -> {
61 Misc.onMainThread(callback);
65 @Query("DELETE FROM templates")
66 public abstract void deleteAllSync();
68 @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
69 public abstract LiveData<List<TemplateHeader>> getTemplates();
71 @Query("SELECT * FROM templates WHERE id = :id")
72 public abstract LiveData<TemplateHeader> getTemplate(Long id);
74 @Query("SELECT * FROM templates WHERE id = :id")
75 public abstract TemplateHeader getTemplateSync(Long id);
77 public void getTemplateAsync(@NonNull Long id,
78 @NonNull AsyncResultCallback<TemplateHeader> callback) {
79 LiveData<TemplateHeader> resultReceiver = getTemplate(id);
80 resultReceiver.observeForever(new Observer<TemplateHeader>() {
82 public void onChanged(TemplateHeader h) {
86 resultReceiver.removeObserver(this);
93 @Query("SELECT * FROM templates WHERE id = :id")
94 public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
97 @Query("SELECT * FROM templates WHERE id = :id")
98 public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
101 @Query("SELECT * FROM templates WHERE uuid = :uuid")
102 public abstract TemplateWithAccounts getTemplateWithAccountsByUuidSync(String uuid);
105 @Query("SELECT * FROM templates")
106 public abstract List<TemplateWithAccounts> getAllTemplatesWithAccountsSync();
109 public void insertSync(TemplateWithAccounts templateWithAccounts) {
110 long template_id = insertSync(templateWithAccounts.header);
111 for (TemplateAccount acc : templateWithAccounts.accounts) {
112 acc.setTemplateId(template_id);
114 .getTemplateAccountDAO()
119 public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
120 AsyncResultCallback<TemplateWithAccounts> callback) {
121 LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
122 resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
124 public void onChanged(TemplateWithAccounts result) {
128 resultReceiver.removeObserver(this);
129 callback.onResult(result);
133 public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
134 BaseDAO.runAsync(() -> {
136 if (callback != null)
137 Misc.onMainThread(callback);
140 public void duplicateTemplateWithAccounts(@NonNull Long id, @Nullable
141 AsyncResultCallback<TemplateWithAccounts> callback) {
142 BaseDAO.runAsync(() -> {
143 TemplateWithAccounts src = getTemplateWithAccountsSync(id);
144 TemplateWithAccounts dup = src.createDuplicate();
145 dup.header.setName(dup.header.getName());
146 dup.header.setId(insertSync(dup.header));
147 TemplateAccountDAO accDao = DB.get()
148 .getTemplateAccountDAO();
149 for (TemplateAccount dupAcc : dup.accounts) {
150 dupAcc.setTemplateId(dup.header.getId());
151 dupAcc.setId(accDao.insertSync(dupAcc));
153 if (callback != null)
154 Misc.onMainThread(() -> callback.onResult(dup));