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.
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 android.os.AsyncTask;
21 import android.os.Handler;
22 import android.os.Looper;
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.lifecycle.LiveData;
27 import androidx.lifecycle.Observer;
28 import androidx.room.Dao;
29 import androidx.room.Delete;
30 import androidx.room.Insert;
31 import androidx.room.Query;
32 import androidx.room.Transaction;
33 import androidx.room.Update;
35 import net.ktnx.mobileledger.db.DB;
36 import net.ktnx.mobileledger.db.TemplateAccount;
37 import net.ktnx.mobileledger.db.TemplateHeader;
38 import net.ktnx.mobileledger.db.TemplateWithAccounts;
40 import java.util.List;
43 public abstract class TemplateHeaderDAO {
45 public abstract long insertSync(TemplateHeader item);
47 public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
48 AsyncTask.execute(() -> {
50 if (callback != null) {
51 new Handler(Looper.getMainLooper()).post(callback);
57 public abstract void updateSync(TemplateHeader... items);
60 public abstract void deleteSync(TemplateHeader item);
62 public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
63 AsyncTask.execute(() -> {
65 new Handler(Looper.getMainLooper()).post(callback);
69 @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
70 public abstract LiveData<List<TemplateHeader>> getTemplates();
72 @Query("SELECT * FROM templates WHERE id = :id")
73 public abstract LiveData<TemplateHeader> getTemplate(Long id);
75 public void getTemplateAsync(@NonNull Long id,
76 @NonNull AsyncResultCallback<TemplateHeader> callback) {
77 LiveData<TemplateHeader> resultReceiver = getTemplate(id);
78 resultReceiver.observeForever(new Observer<TemplateHeader>() {
80 public void onChanged(TemplateHeader h) {
84 resultReceiver.removeObserver(this);
91 @Query("SELECT * FROM templates WHERE id = :id")
92 public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
95 @Query("SELECT * FROM templates WHERE id = :id")
96 public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
99 public void insertSync(TemplateWithAccounts templateWithAccounts) {
100 long template_id = insertSync(templateWithAccounts.header);
101 for (TemplateAccount acc : templateWithAccounts.accounts) {
102 acc.setTemplateId(template_id);
104 .getTemplateAccountDAO()
109 public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
110 AsyncResultCallback<TemplateWithAccounts> callback) {
111 LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
112 resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
114 public void onChanged(TemplateWithAccounts result) {
118 resultReceiver.removeObserver(this);
119 callback.onResult(result);
123 public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
124 AsyncTask.execute(() -> {
126 if (callback != null) {
127 new Handler(Looper.getMainLooper()).post(callback);
131 public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable
132 AsyncResultCallback<TemplateWithAccounts> callback) {
133 AsyncTask.execute(() -> {
134 TemplateWithAccounts src = getTemplateWithAccountsSync(id);
135 TemplateWithAccounts dup = src.createDuplicate();
136 dup.header.setName(dup.header.getName());
137 dup.header.setId(insertSync(dup.header));
138 TemplateAccountDAO accDao = DB.get()
139 .getTemplateAccountDAO();
140 for (TemplateAccount dupAcc : dup.accounts) {
141 dupAcc.setTemplateId(dup.header.getId());
142 dupAcc.setId(accDao.insertSync(dupAcc));
144 if (callback != null)
145 new Handler(Looper.getMainLooper()).post(() -> callback.onResult(dup));