]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
provide a common routine for running something on the main thread
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TemplateHeaderDAO.java
1 /*
2  * Copyright © 2021 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 android.os.AsyncTask;
21
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import androidx.lifecycle.LiveData;
25 import androidx.lifecycle.Observer;
26 import androidx.room.Dao;
27 import androidx.room.Delete;
28 import androidx.room.Insert;
29 import androidx.room.Query;
30 import androidx.room.Transaction;
31 import androidx.room.Update;
32
33 import net.ktnx.mobileledger.db.DB;
34 import net.ktnx.mobileledger.db.TemplateAccount;
35 import net.ktnx.mobileledger.db.TemplateHeader;
36 import net.ktnx.mobileledger.db.TemplateWithAccounts;
37 import net.ktnx.mobileledger.utils.Misc;
38
39 import java.util.List;
40
41 @Dao
42 public abstract class TemplateHeaderDAO {
43     @Insert()
44     public abstract long insertSync(TemplateHeader item);
45
46     public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
47         AsyncTask.execute(() -> {
48             insertSync(item);
49             if (callback != null)
50                 Misc.onMainThread(callback);
51         });
52     }
53
54     @Update
55     public abstract void updateSync(TemplateHeader... items);
56
57     @Delete
58     public abstract void deleteSync(TemplateHeader item);
59
60     public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
61         AsyncTask.execute(() -> {
62             deleteSync(item);
63             Misc.onMainThread(callback);
64         });
65     }
66
67     @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
68     public abstract LiveData<List<TemplateHeader>> getTemplates();
69
70     @Query("SELECT * FROM templates WHERE id = :id")
71     public abstract LiveData<TemplateHeader> getTemplate(Long id);
72
73     public void getTemplateAsync(@NonNull Long id,
74                                  @NonNull AsyncResultCallback<TemplateHeader> callback) {
75         LiveData<TemplateHeader> resultReceiver = getTemplate(id);
76         resultReceiver.observeForever(new Observer<TemplateHeader>() {
77             @Override
78             public void onChanged(TemplateHeader h) {
79                 if (h == null)
80                     return;
81
82                 resultReceiver.removeObserver(this);
83                 callback.onResult(h);
84             }
85         });
86     }
87
88     @Transaction
89     @Query("SELECT * FROM templates WHERE id = :id")
90     public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
91
92     @Transaction
93     @Query("SELECT * FROM templates WHERE id = :id")
94     public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
95
96     @Transaction
97     public void insertSync(TemplateWithAccounts templateWithAccounts) {
98         long template_id = insertSync(templateWithAccounts.header);
99         for (TemplateAccount acc : templateWithAccounts.accounts) {
100             acc.setTemplateId(template_id);
101             DB.get()
102               .getTemplateAccountDAO()
103               .insertSync(acc);
104         }
105     }
106
107     public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
108             AsyncResultCallback<TemplateWithAccounts> callback) {
109         LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
110         resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
111             @Override
112             public void onChanged(TemplateWithAccounts result) {
113                 if (result == null)
114                     return;
115
116                 resultReceiver.removeObserver(this);
117                 callback.onResult(result);
118             }
119         });
120     }
121     public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
122         AsyncTask.execute(() -> {
123             insertSync(item);
124             if (callback != null)
125                 Misc.onMainThread(callback);
126         });
127     }
128     public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable
129             AsyncResultCallback<TemplateWithAccounts> callback) {
130         AsyncTask.execute(() -> {
131             TemplateWithAccounts src = getTemplateWithAccountsSync(id);
132             TemplateWithAccounts dup = src.createDuplicate();
133             dup.header.setName(dup.header.getName());
134             dup.header.setId(insertSync(dup.header));
135             TemplateAccountDAO accDao = DB.get()
136                                           .getTemplateAccountDAO();
137             for (TemplateAccount dupAcc : dup.accounts) {
138                 dupAcc.setTemplateId(dup.header.getId());
139                 dupAcc.setId(accDao.insertSync(dupAcc));
140             }
141             if (callback != null)
142                 Misc.onMainThread(() -> callback.onResult(dup));
143         });
144     }
145
146 }