]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TemplateHeaderDAO.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.dao;
19
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;
30
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;
36
37 import java.util.List;
38
39 @Dao
40 public abstract class TemplateHeaderDAO {
41     @Insert()
42     public abstract long insertSync(TemplateHeader item);
43
44     public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
45         BaseDAO.runAsync(() -> {
46             insertSync(item);
47             if (callback != null)
48                 Misc.onMainThread(callback);
49         });
50     }
51
52     @Update
53     public abstract void updateSync(TemplateHeader... items);
54
55     @Delete
56     public abstract void deleteSync(TemplateHeader item);
57
58     public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
59         BaseDAO.runAsync(() -> {
60             deleteSync(item);
61             Misc.onMainThread(callback);
62         });
63     }
64
65     @Query("DELETE FROM templates")
66     public abstract void deleteAllSync();
67
68     @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
69     public abstract LiveData<List<TemplateHeader>> getTemplates();
70
71     @Query("SELECT * FROM templates WHERE id = :id")
72     public abstract LiveData<TemplateHeader> getTemplate(Long id);
73
74     @Query("SELECT * FROM templates WHERE id = :id")
75     public abstract TemplateHeader getTemplateSync(Long id);
76
77     public void getTemplateAsync(@NonNull Long id,
78                                  @NonNull AsyncResultCallback<TemplateHeader> callback) {
79         LiveData<TemplateHeader> resultReceiver = getTemplate(id);
80         resultReceiver.observeForever(new Observer<TemplateHeader>() {
81             @Override
82             public void onChanged(TemplateHeader h) {
83                 if (h == null)
84                     return;
85
86                 resultReceiver.removeObserver(this);
87                 callback.onResult(h);
88             }
89         });
90     }
91
92     @Transaction
93     @Query("SELECT * FROM templates WHERE id = :id")
94     public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
95
96     @Transaction
97     @Query("SELECT * FROM templates WHERE id = :id")
98     public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
99
100     @Transaction
101     @Query("SELECT * FROM templates WHERE uuid = :uuid")
102     public abstract TemplateWithAccounts getTemplateWithAccountsByUuidSync(String uuid);
103
104     @Transaction
105     @Query("SELECT * FROM templates")
106     public abstract List<TemplateWithAccounts> getAllTemplatesWithAccountsSync();
107
108     @Transaction
109     public void insertSync(TemplateWithAccounts templateWithAccounts) {
110         long template_id = insertSync(templateWithAccounts.header);
111         for (TemplateAccount acc : templateWithAccounts.accounts) {
112             acc.setTemplateId(template_id);
113             DB.get()
114               .getTemplateAccountDAO()
115               .insertSync(acc);
116         }
117     }
118
119     public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
120             AsyncResultCallback<TemplateWithAccounts> callback) {
121         LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
122         resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
123             @Override
124             public void onChanged(TemplateWithAccounts result) {
125                 if (result == null)
126                     return;
127
128                 resultReceiver.removeObserver(this);
129                 callback.onResult(result);
130             }
131         });
132     }
133     public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
134         BaseDAO.runAsync(() -> {
135             insertSync(item);
136             if (callback != null)
137                 Misc.onMainThread(callback);
138         });
139     }
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));
152             }
153             if (callback != null)
154                 Misc.onMainThread(() -> callback.onResult(dup));
155         });
156     }
157
158 }